Search in sources :

Example 21 with Annotation

use of annis.model.Annotation in project ANNIS by korpling.

the class TestAnnisResultImpl method getAnnotationLevelSet.

// return annotation names of non-tokens
@Test
public void getAnnotationLevelSet() {
    // add annotation data to tokens and nodes
    token3.addNodeAnnotation(new Annotation(NAMESPACE, NAME1));
    node5.addNodeAnnotation(new Annotation(NAMESPACE, NAME2));
    // wrap and test: only the annotation for node5 is returned
    AnnisResult annisResult = new AnnisResultImpl(graph);
    Set<String> annotations = annisResult.getAnnotationLevelSet();
    assertThat(annotations, size(1));
    assertThat(annotations, hasItems(AnnisNode.qName(NAMESPACE, NAME2)));
}
Also used : AnnisResult(annis.service.ifaces.AnnisResult) Annotation(annis.model.Annotation) Test(org.junit.Test)

Example 22 with Annotation

use of annis.model.Annotation in project ANNIS by korpling.

the class TestAnnisResultImpl method getTokenAnnotationLevelSet.

// return annotation names of tokens
@Test
public void getTokenAnnotationLevelSet() {
    // add annotation data to tokens and nodes
    token3.addNodeAnnotation(new Annotation(NAMESPACE, NAME1));
    node5.addNodeAnnotation(new Annotation(NAMESPACE, NAME2));
    // wrap and test: only the annotation for token3 is returned
    AnnisResult annisResult = new AnnisResultImpl(graph);
    Set<String> tokenAnnotations = annisResult.getTokenAnnotationLevelSet();
    assertThat(tokenAnnotations, size(1));
    assertThat(tokenAnnotations, hasItems(AnnisNode.qName(NAMESPACE, NAME1)));
}
Also used : AnnisResult(annis.service.ifaces.AnnisResult) Annotation(annis.model.Annotation) Test(org.junit.Test)

Example 23 with Annotation

use of annis.model.Annotation in project ANNIS by korpling.

the class AomAnnotateSqlGeneratorTest method shouldMapAnnotation.

@Test
public void shouldMapAnnotation() throws SQLException {
    // given
    String table = uniqueString();
    stubDefaultAnnotation(table);
    // when
    Annotation actual = generator.mapAnnotation(resultSet, tableAccessStrategy, table);
    // then
    Annotation expected = new Annotation(NAMESPACE, NAME, VALUE);
    assertThat(actual, is(expected));
}
Also used : TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Annotation(annis.model.Annotation) Test(org.junit.Test)

Example 24 with Annotation

use of annis.model.Annotation in project ANNIS by korpling.

the class TextColumnExporter method outputText.

/**
 * Writes the specified record (if applicable, as multiple result lines) from query result set to the output file.
 *
 * @param graph the org.corpus_tools.salt.common.SDocumentGraph representation of a specified record
 * @param alignmc a boolean, which indicates, whether the data should be aligned by match numbers or not
 * @param recordNumber the number of record within  the record set
 * @param out the specified Writer
 *
 * @throws IOException, if an I/O error occurs
 */
@Override
public void outputText(SDocumentGraph graph, boolean alignmc, int recordNumber, Writer out) throws IOException {
    String currSpeakerName = "";
    String prevSpeakerName = "";
    if (graph != null) {
        List<SToken> orderedToken = graph.getSortedTokenByText();
        if (orderedToken != null) {
            // iterate over token
            ListIterator<SToken> it = orderedToken.listIterator();
            long lastTokenWasMatched = -1;
            boolean noPreviousTokenInLine = false;
            // if match number == 0, reset global variables and output warning, if necessary
            if (recordNumber == 0) {
                isFirstSpeakerWithMatch = true;
                counterGlobal = 0;
                // create warning message
                String numbersString = "";
                String warnMessage = "";
                StringBuilder sb = new StringBuilder();
                List<Integer> copyOfFilterNumbersSetByUser = new ArrayList<Integer>();
                for (Long filterNumber : filterNumbersSetByUser) {
                    copyOfFilterNumbersSetByUser.add(Integer.parseInt(String.valueOf(filterNumber)));
                }
                for (Integer matchNumberGlobal : matchNumbersGlobal) {
                    copyOfFilterNumbersSetByUser.remove(matchNumberGlobal);
                }
                Collections.sort(copyOfFilterNumbersSetByUser);
                if (!copyOfFilterNumbersSetByUser.isEmpty()) {
                    for (Integer filterNumber : copyOfFilterNumbersSetByUser) {
                        sb.append(filterNumber + ", ");
                    }
                    if (copyOfFilterNumbersSetByUser.size() == 1) {
                        numbersString = "number";
                    } else {
                        numbersString = "numbers";
                    }
                    warnMessage = "1. Filter " + numbersString + " " + sb.toString().substring(0, sb.lastIndexOf(",")) + " couldn't be represented.";
                }
                if (alignmc && !dataIsAlignable) {
                    if (!warnMessage.isEmpty()) {
                        warnMessage += (NEWLINE + NEWLINE + "2. ");
                    } else {
                        warnMessage += "1. ";
                    }
                    warnMessage += "You have tried to align matches by node number via check box." + "Unfortunately this option is not applicable for this data set, " + "so the data couldn't be aligned.";
                }
                if (!warnMessage.isEmpty()) {
                    String warnCaption = "Some export options couldn't be realized.";
                    Notification warn = new Notification(warnCaption, warnMessage, Notification.Type.WARNING_MESSAGE);
                    warn.setDelayMsec(20000);
                    warn.show(Page.getCurrent());
                }
            }
            // global variables reset; warning issued
            int matchesWrittenForSpeaker = 0;
            while (it.hasNext()) {
                SToken tok = it.next();
                counterGlobal++;
                // get current speaker name
                String name;
                if ((name = CommonHelper.getTextualDSForNode(tok, graph).getName()) == null) {
                    name = "";
                }
                currSpeakerName = (recordNumber + 1) + "_" + name;
                // if speaker has no matches, skip token
                if (speakerHasMatches.get(currSpeakerName) == false) {
                    prevSpeakerName = currSpeakerName;
                // continue;
                } else // if speaker has matches
                {
                    // if the current speaker is new, write header and append his name
                    if (!currSpeakerName.equals(prevSpeakerName)) {
                        // reset the counter of matches, which were written for this speaker
                        matchesWrittenForSpeaker = 0;
                        if (isFirstSpeakerWithMatch) {
                            out.append("match_number" + TAB_MARK);
                            out.append("speaker" + TAB_MARK);
                            // write header for meta data columns
                            if (!listOfMetakeys.isEmpty()) {
                                for (String metakey : listOfMetakeys) {
                                    out.append(metakey + TAB_MARK);
                                }
                            }
                            out.append("left_context" + TAB_MARK);
                            String prefixAlignmc = "match_";
                            String prefix = "match_column";
                            String middle_context = "middle_context_";
                            if (alignmc && dataIsAlignable) {
                                for (int i = 0; i < orderedMatchNumbersGlobal.size(); i++) {
                                    out.append(prefixAlignmc + orderedMatchNumbersGlobal.get(i) + TAB_MARK);
                                    if (i < orderedMatchNumbersGlobal.size() - 1) {
                                        out.append(middle_context + (i + 1) + TAB_MARK);
                                    }
                                }
                            } else {
                                for (int i = 0; i < maxMatchesPerLine; i++) {
                                    out.append(prefix + TAB_MARK);
                                    if (i < (maxMatchesPerLine - 1)) {
                                        out.append(middle_context + (i + 1) + TAB_MARK);
                                    }
                                }
                            }
                            out.append("right_context");
                            out.append(NEWLINE);
                            isFirstSpeakerWithMatch = false;
                        } else {
                            out.append(NEWLINE);
                        }
                        out.append(String.valueOf(recordNumber + 1) + TAB_MARK);
                        String trimmedName = "";
                        if (currSpeakerName.indexOf("_") < currSpeakerName.length()) {
                            trimmedName = currSpeakerName.substring(currSpeakerName.indexOf("_") + 1);
                        }
                        out.append(trimmedName + TAB_MARK);
                        // write meta data
                        if (!listOfMetakeys.isEmpty()) {
                            // get metadata
                            String docName = graph.getDocument().getName();
                            List<String> corpusPath = CommonHelper.getCorpusPath(graph.getDocument().getGraph(), graph.getDocument());
                            String corpusName = corpusPath.get(corpusPath.size() - 1);
                            corpusName = urlPathEscape.escape(corpusName);
                            List<Annotation> metadata = Helper.getMetaData(corpusName, docName);
                            Map<String, String> annosWithoutNamespace = new HashMap<String, String>();
                            Map<String, Map<String, String>> annosWithNamespace = new HashMap<String, Map<String, String>>();
                            // put metadata annotations into hash maps for better access
                            for (Annotation metaAnno : metadata) {
                                String ns;
                                Map<String, String> data = new HashMap<String, String>();
                                data.put(metaAnno.getName(), metaAnno.getValue());
                                // a namespace is present
                                if ((ns = metaAnno.getNamespace()) != null && !ns.isEmpty()) {
                                    Map<String, String> nsMetadata = new HashMap<String, String>();
                                    if (annosWithNamespace.get(ns) != null) {
                                        nsMetadata = annosWithNamespace.get(ns);
                                    }
                                    nsMetadata.putAll(data);
                                    annosWithNamespace.put(ns, nsMetadata);
                                } else {
                                    annosWithoutNamespace.putAll(data);
                                }
                            }
                            for (String metakey : listOfMetakeys) {
                                String metaValue = "";
                                // try to get meta value specific for current speaker
                                if (!trimmedName.isEmpty() && annosWithNamespace.containsKey(trimmedName)) {
                                    Map<String, String> speakerAnnos = annosWithNamespace.get(trimmedName);
                                    if (speakerAnnos.containsKey(metakey)) {
                                        metaValue = speakerAnnos.get(metakey).trim();
                                    }
                                }
                                // try to get meta value, if metaValue is not set
                                if (metaValue.isEmpty() && annosWithoutNamespace.containsKey(metakey)) {
                                    metaValue = annosWithoutNamespace.get(metakey).trim();
                                }
                                out.append(metaValue + TAB_MARK);
                            }
                        }
                        // metadata written
                        lastTokenWasMatched = -1;
                        noPreviousTokenInLine = true;
                    }
                    // header, speaker name and metadata ready
                    // default to space as separator
                    String separator = SPACE;
                    List<SNode> root = new LinkedList<>();
                    root.add(tok);
                    Long matchedNode;
                    // token matched
                    if ((matchedNode = tokenToMatchNumber.get(counterGlobal)) != null) {
                        // is dominated by a (new) matched node, thus use tab to separate the non-matches from the matches
                        if (lastTokenWasMatched < 0) {
                            if (alignmc && dataIsAlignable) {
                                int orderInList = orderedMatchNumbersGlobal.indexOf(matchedNode);
                                if (orderInList >= matchesWrittenForSpeaker) {
                                    int diff = orderInList - matchesWrittenForSpeaker;
                                    matchesWrittenForSpeaker++;
                                    StringBuilder sb = new StringBuilder(TAB_MARK);
                                    for (int i = 0; i < diff; i++) {
                                        sb.append(TAB_MARK + TAB_MARK);
                                        matchesWrittenForSpeaker++;
                                    }
                                    separator = sb.toString();
                                }
                            } else {
                                separator = TAB_MARK;
                            }
                        } else if (lastTokenWasMatched != matchedNode) {
                            // always leave an empty column between two matches, even if there is no actual context
                            if (alignmc && dataIsAlignable) {
                                int orderInList = orderedMatchNumbersGlobal.indexOf(matchedNode);
                                if (orderInList >= matchesWrittenForSpeaker) {
                                    int diff = orderInList - matchesWrittenForSpeaker;
                                    matchesWrittenForSpeaker++;
                                    StringBuilder sb = new StringBuilder(TAB_MARK + TAB_MARK);
                                    for (int i = 0; i < diff; i++) {
                                        sb.append(TAB_MARK + TAB_MARK);
                                        matchesWrittenForSpeaker++;
                                    }
                                    separator = sb.toString();
                                }
                            } else {
                                separator = TAB_MARK + TAB_MARK;
                            }
                        }
                        lastTokenWasMatched = matchedNode;
                    } else // token not matched, but last token matched
                    if (lastTokenWasMatched >= 0) {
                        // handle crossing edges
                        if (!tokenToMatchNumber.containsKey(counterGlobal) && tokenToMatchNumber.containsKey(counterGlobal - 1) && tokenToMatchNumber.containsKey(counterGlobal + 1)) {
                            if (Objects.equals(tokenToMatchNumber.get(counterGlobal - 1), tokenToMatchNumber.get(counterGlobal + 1))) {
                                separator = SPACE;
                                lastTokenWasMatched = tokenToMatchNumber.get(counterGlobal + 1);
                            } else {
                                separator = TAB_MARK;
                                lastTokenWasMatched = -1;
                            }
                        } else // mark the end of a match with the tab
                        {
                            separator = TAB_MARK;
                            lastTokenWasMatched = -1;
                        }
                    }
                    // if tok is the first token in the line and not matched, set separator to empty string
                    if (noPreviousTokenInLine && separator.equals(SPACE)) {
                        separator = "";
                    }
                    out.append(separator);
                    // append the current token
                    out.append(graph.getText(tok));
                    noPreviousTokenInLine = false;
                    prevSpeakerName = currSpeakerName;
                }
            }
        }
    }
}
Also used : SNode(org.corpus_tools.salt.core.SNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Notification(com.vaadin.ui.Notification) Annotation(annis.model.Annotation) LinkedList(java.util.LinkedList) SToken(org.corpus_tools.salt.common.SToken) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with Annotation

use of annis.model.Annotation in project ANNIS by korpling.

the class TokenExporter method convertText.

@Override
public void convertText(AnnisResultSet queryResult, List<String> keys, Map<String, String> args, Writer out, int offset) throws IOException {
    Map<String, Map<String, Annotation>> metadataCache = new HashMap<>();
    List<String> metaKeys = new LinkedList<>();
    if (args.containsKey("metakeys")) {
        Iterable<String> it = Splitter.on(",").trimResults().split(args.get("metakeys"));
        for (String s : it) {
            metaKeys.add(s);
        }
    }
    int counter = 0;
    for (AnnisResult annisResult : queryResult) {
        Set<Long> matchedNodeIds = annisResult.getGraph().getMatchedNodeIds();
        counter++;
        out.append((counter + offset) + ". ");
        List<AnnisNode> tok = annisResult.getGraph().getTokens();
        for (AnnisNode annisNode : tok) {
            Long tokID = annisNode.getId();
            if (matchedNodeIds.contains(tokID)) {
                out.append("[");
                out.append(annisNode.getSpannedText());
                out.append("]");
            } else {
                out.append(annisNode.getSpannedText());
            }
            for (Annotation annotation : annisNode.getNodeAnnotations()) {
                out.append("/" + annotation.getValue());
            }
            out.append(" ");
        }
        out.append("\n");
        if (!metaKeys.isEmpty()) {
            String[] path = annisResult.getPath();
            super.appendMetaData(out, metaKeys, path[path.length - 1], annisResult.getDocumentName(), metadataCache);
        }
        out.append("\n");
    }
}
Also used : HashMap(java.util.HashMap) AnnisResult(annis.service.ifaces.AnnisResult) LinkedList(java.util.LinkedList) Annotation(annis.model.Annotation) AnnisNode(annis.model.AnnisNode) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Annotation (annis.model.Annotation)43 ArrayList (java.util.ArrayList)13 LinkedList (java.util.LinkedList)12 HashMap (java.util.HashMap)11 AnnisNode (annis.model.AnnisNode)10 AnnotatedSpan (annis.dao.objects.AnnotatedSpan)6 Test (org.junit.Test)6 AnnotatedMatch (annis.dao.objects.AnnotatedMatch)5 List (java.util.List)5 Map (java.util.Map)5 TreeMap (java.util.TreeMap)5 SAnnotation (org.corpus_tools.salt.core.SAnnotation)5 AnnisResult (annis.service.ifaces.AnnisResult)4 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)4 WebResource (com.sun.jersey.api.client.WebResource)4 Array (java.sql.Array)4 SNode (org.corpus_tools.salt.core.SNode)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 SToken (org.corpus_tools.salt.common.SToken)3 AnnotationGraph (annis.model.AnnotationGraph)2