Search in sources :

Example 6 with SAnnotation

use of org.corpus_tools.salt.core.SAnnotation in project ANNIS by korpling.

the class PDFPageHelper method getPageFromAnnotation.

/**
 * Returns the value of page annotiation for a node. It takes the visualizer
 * mappings into account. If no mapping is used, this definition is used: {@link
 * #PAGE_NUMBER_ANNOATATION_NAME}
 */
public String getPageFromAnnotation(SNode node) {
    if (node != null && node.getAnnotations() != null) {
        Set<SLayer> layers = node.getLayers();
        String nodeNamespace = null;
        if (layers != null) {
            for (SLayer l : layers) {
                nodeNamespace = l.getName();
            }
            for (SAnnotation anno : node.getAnnotations()) {
                if ((nodeNamespace == null || input.getNamespace() == null) && getPDFPageAnnotationName().equals(anno.getName())) {
                    return anno.getValue_STEXT();
                } else if (nodeNamespace.equals(input.getNamespace()) && getPDFPageAnnotationName().equals(anno.getName())) {
                    return anno.getValue_STEXT();
                }
            }
        }
    }
    return null;
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer) SAnnotation(org.corpus_tools.salt.core.SAnnotation)

Example 7 with SAnnotation

use of org.corpus_tools.salt.core.SAnnotation in project ANNIS by korpling.

the class CSVMultiTokExporter method createAdjacencyMatrix.

/**
 * Takes a match and stores annotation names to construct the header in
 * {@link #outputText(de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDocumentGraph, boolean, int, java.io.Writer) }
 *
 * @param graph
 * @param args
 * @param matchNumber
 * @param nodeCount
 *
 * @throws java.io.IOException
 */
@Override
public void createAdjacencyMatrix(SDocumentGraph graph, Map<String, String> args, int matchNumber, int nodeCount) throws IOException, IllegalArgumentException {
    // first match
    if (matchNumber == 0) {
        // get list of metakeys to export
        metakeys = new HashSet<>();
        if (args.containsKey("metakeys")) {
            metakeys.addAll(Arrays.asList(args.get("metakeys").split(",")));
        }
        // initialize list of annotations for the matched nodes
        annotationsForMatchedNodes = new TreeMap<>();
    }
    for (SNode node : this.getMatchedNodes(graph)) {
        int node_id = node.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_MATCHEDNODE).getValue_SNUMERIC().intValue();
        if (!annotationsForMatchedNodes.containsKey(node_id))
            annotationsForMatchedNodes.put(node_id, new TreeSet<String>());
        List<SAnnotation> annots = new ArrayList<>(node.getAnnotations());
        Set<String> annoNames = annotationsForMatchedNodes.get(node_id);
        for (SAnnotation annot : annots) {
            annoNames.add(annot.getNamespace() + "::" + annot.getName());
        }
    }
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SAnnotation(org.corpus_tools.salt.core.SAnnotation) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList)

Example 8 with SAnnotation

use of org.corpus_tools.salt.core.SAnnotation in project ANNIS by korpling.

the class TimelineReconstructor method convertSpanToToken.

private void convertSpanToToken(SStructuredNode span, String orderName) {
    final Set<String> validSpanAnnos = new HashSet<>(order2spanAnnos.get(orderName));
    if (!nodesToDelete.contains(span)) {
        nodesToDelete.add(span);
        if (textsByName.get(orderName) == null) {
            STextualDS newText = graph.createTextualDS("");
            newText.setName(orderName);
            textsByName.put(orderName, newText);
            textDataByName.put(orderName, new StringBuilder());
        }
        STextualDS textDS = textsByName.get(orderName);
        StringBuilder textData = textDataByName.get(orderName);
        TreeSet<Integer> coveredIdx = new TreeSet<>(spans2TimelinePos.get(span));
        if (!coveredIdx.isEmpty()) {
            SAnnotation textValueAnno = getTextValueAnno(orderName, span);
            if (textValueAnno != null) {
                String textValue = textValueAnno.getValue_STEXT();
                int startTextIdx = textData.length();
                textData.append(textValue);
                int endTextIdx = textData.length();
                SToken newToken = graph.createToken(textDS, startTextIdx, endTextIdx);
                // keep track of changed ids for matches
                if (this.matchIDs.contains(span.getId()))
                    this.oldID2newID.put(span.getId(), newToken.getId());
                // move all features to the new token
                if (span.getFeatures() != null) {
                    for (SFeature feat : span.getFeatures()) {
                        if (!"salt".equals(feat.getNamespace())) {
                            newToken.addFeature(feat);
                        }
                    }
                }
                // move all annotations to the new token
                if (span.getAnnotations() != null) {
                    for (SAnnotation annot : span.getAnnotations()) {
                        if (!"salt".equals(annot.getNamespace()) && !orderName.equals(annot.getName())) {
                            newToken.addAnnotation(annot);
                        }
                    }
                }
                STimelineRelation timeRel = SaltFactory.createSTimelineRelation();
                timeRel.setSource(newToken);
                timeRel.setTarget(graph.getTimeline());
                timeRel.setStart(coveredIdx.first());
                timeRel.setEnd(coveredIdx.last());
                graph.addRelation(timeRel);
                moveRelations(span, newToken, validSpanAnnos, orderName);
            }
        }
    }
}
Also used : SAnnotation(org.corpus_tools.salt.core.SAnnotation) SToken(org.corpus_tools.salt.common.SToken) TreeSet(java.util.TreeSet) STextualDS(org.corpus_tools.salt.common.STextualDS) STimelineRelation(org.corpus_tools.salt.common.STimelineRelation) HashSet(java.util.HashSet) SFeature(org.corpus_tools.salt.core.SFeature)

Example 9 with SAnnotation

use of org.corpus_tools.salt.core.SAnnotation in project ANNIS by korpling.

the class TimelineReconstructor method getTextValueAnno.

private SAnnotation getTextValueAnno(String orderName, SNode node) {
    SAnnotation result = null;
    Set<SAnnotation> annos = node.getAnnotations();
    if (annos != null) {
        for (SAnnotation a : annos) {
            if (a.getName().equals(orderName)) {
                result = a;
                break;
            }
        }
    }
    return result;
}
Also used : SAnnotation(org.corpus_tools.salt.core.SAnnotation)

Example 10 with SAnnotation

use of org.corpus_tools.salt.core.SAnnotation in project ANNIS by korpling.

the class EventExtractor method addAnnotationsForNode.

private static void addAnnotationsForNode(SNode node, SDocumentGraph graph, long startTokenIndex, long endTokenIndex, PDFController pdfController, PDFPageHelper pageNumberHelper, AtomicInteger eventCounter, LinkedHashMap<String, ArrayList<Row>> rowsByAnnotation, boolean addMatch, Set<String> mediaLayer, boolean replaceValueWithMediaIcon) {
    List<String> matchedAnnos = new ArrayList<>();
    SFeature featMatchedAnnos = graph.getFeature(ANNIS_NS, FEAT_MATCHEDANNOS);
    if (featMatchedAnnos != null) {
        matchedAnnos = Splitter.on(',').trimResults().splitToList(featMatchedAnnos.getValue_STEXT());
    }
    // check if the span is a matched node
    SFeature featMatched = node.getFeature(ANNIS_NS, FEAT_MATCHEDNODE);
    Long matchRaw = featMatched == null ? null : featMatched.getValue_SNUMERIC();
    String matchedQualifiedAnnoName = "";
    if (matchRaw != null && matchRaw <= matchedAnnos.size()) {
        matchedQualifiedAnnoName = matchedAnnos.get((int) ((long) matchRaw) - 1);
    }
    // calculate the left and right values of a span
    // TODO: howto get these numbers with Salt?
    RelannisNodeFeature feat = (RelannisNodeFeature) node.getFeature(ANNIS_NS, FEAT_RELANNIS_NODE).getValue();
    long leftLong = feat.getLeftToken();
    long rightLong = feat.getRightToken();
    leftLong = clip(leftLong, startTokenIndex, endTokenIndex);
    rightLong = clip(rightLong, startTokenIndex, endTokenIndex);
    int left = (int) (leftLong - startTokenIndex);
    int right = (int) (rightLong - startTokenIndex);
    for (SAnnotation anno : node.getAnnotations()) {
        ArrayList<Row> rows = rowsByAnnotation.get(anno.getQName());
        if (rows == null) {
            // try again with only the name
            rows = rowsByAnnotation.get(anno.getName());
        }
        if (rows != null) {
            // only do something if the annotation was defined before
            // 1. give each annotation of each span an own row
            Row r = new Row();
            String id = "event_" + eventCounter.incrementAndGet();
            GridEvent event = new GridEvent(id, left, right, anno.getValue_STEXT());
            event.setTooltip(Helper.getQualifiedName(anno));
            if (addMatch && matchRaw != null) {
                long match = matchRaw;
                if (matchedQualifiedAnnoName.isEmpty()) {
                    // always set the match when there is no matched annotation at all
                    event.setMatch(match);
                } else // check if the annotation also matches
                if (matchedQualifiedAnnoName.equals(anno.getQName())) {
                    event.setMatch(match);
                }
            }
            if (node instanceof SSpan) {
                // calculate overlapped SToken
                List<? extends SRelation<? extends SNode, ? extends SNode>> outEdges = graph.getOutRelations(node.getId());
                if (outEdges != null) {
                    for (SRelation<? extends SNode, ? extends SNode> e : outEdges) {
                        if (e instanceof SSpanningRelation) {
                            SSpanningRelation spanRel = (SSpanningRelation) e;
                            SToken tok = spanRel.getTarget();
                            event.getCoveredIDs().add(tok.getId());
                            // get the STextualDS of this token and add it to the event
                            String textID = getTextID(tok, graph);
                            if (textID != null) {
                                event.setTextID(textID);
                            }
                        }
                    }
                }
            // end if span has out edges
            } else if (node instanceof SToken) {
                event.getCoveredIDs().add(node.getId());
                // get the STextualDS of this token and add it to the event
                String textID = getTextID((SToken) node, graph);
                if (textID != null) {
                    event.setTextID(textID);
                }
            }
            // try to get time annotations
            if (mediaLayer == null || mediaLayer.contains(anno.getQName())) {
                double[] startEndTime = TimeHelper.getOverlappedTime(node);
                if (startEndTime.length == 1) {
                    if (replaceValueWithMediaIcon) {
                        event.setValue(" ");
                        event.setTooltip("play excerpt " + event.getStartTime());
                    }
                    event.setStartTime(startEndTime[0]);
                } else if (startEndTime.length == 2) {
                    event.setStartTime(startEndTime[0]);
                    event.setEndTime(startEndTime[1]);
                    if (replaceValueWithMediaIcon) {
                        event.setValue(" ");
                        event.setTooltip("play excerpt " + event.getStartTime() + "-" + event.getEndTime());
                    }
                }
            }
            r.addEvent(event);
            rows.add(r);
            if (pdfController != null && pdfController.sizeOfRegisterdPDFViewer() > 0) {
                String page = pageNumberHelper.getPageFromAnnotation(node);
                if (page != null) {
                    event.setPage(page);
                }
            }
        }
    }
// end for each annotation of span
}
Also used : RelannisNodeFeature(annis.model.RelannisNodeFeature) GridEvent(annis.gui.widgets.grid.GridEvent) SSpan(org.corpus_tools.salt.common.SSpan) SAnnotation(org.corpus_tools.salt.core.SAnnotation) ArrayList(java.util.ArrayList) SToken(org.corpus_tools.salt.common.SToken) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) Row(annis.gui.widgets.grid.Row) SFeature(org.corpus_tools.salt.core.SFeature)

Aggregations

SAnnotation (org.corpus_tools.salt.core.SAnnotation)22 SToken (org.corpus_tools.salt.common.SToken)9 SNode (org.corpus_tools.salt.core.SNode)8 SRelation (org.corpus_tools.salt.core.SRelation)8 LinkedList (java.util.LinkedList)6 RelannisNodeFeature (annis.model.RelannisNodeFeature)5 HashMap (java.util.HashMap)4 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)4 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)4 SFeature (org.corpus_tools.salt.core.SFeature)4 SLayer (org.corpus_tools.salt.core.SLayer)4 Annotation (annis.model.Annotation)3 ArrayList (java.util.ArrayList)3 TreeSet (java.util.TreeSet)3 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)3 SSpan (org.corpus_tools.salt.common.SSpan)3 STextualDS (org.corpus_tools.salt.common.STextualDS)3 DataSourceSequence (org.corpus_tools.salt.util.DataSourceSequence)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2