Search in sources :

Example 16 with SFeature

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

the class Helper method calculateMarkedAndCoveredIDs.

public static Map<String, Long> calculateMarkedAndCoveredIDs(SDocument doc, List<SNode> segNodes, String segmentationName) {
    Map<String, Long> initialCovered = new HashMap<>();
    // add all covered nodes
    for (SNode n : doc.getDocumentGraph().getNodes()) {
        SFeature featMatched = n.getFeature(ANNIS_NS, FEAT_MATCHEDNODE);
        Long match = featMatched == null ? null : featMatched.getValue_SNUMERIC();
        if (match != null) {
            initialCovered.put(n.getId(), match);
        }
    }
    // calculate covered nodes
    CoveredMatchesCalculator cmc = new CoveredMatchesCalculator(doc.getDocumentGraph(), initialCovered);
    Map<String, Long> covered = cmc.getMatchedAndCovered();
    if (segmentationName != null) {
        // filter token
        Map<SToken, Long> coveredToken = new HashMap<>();
        for (Map.Entry<String, Long> e : covered.entrySet()) {
            SNode n = doc.getDocumentGraph().getNode(e.getKey());
            if (n instanceof SToken) {
                coveredToken.put((SToken) n, e.getValue());
            }
        }
        for (SNode segNode : segNodes) {
            RelannisNodeFeature featSegNode = (RelannisNodeFeature) segNode.getFeature(ANNIS_NS, FEAT_RELANNIS_NODE).getValue();
            if (!covered.containsKey(segNode.getId())) {
                long leftTok = featSegNode.getLeftToken();
                long rightTok = featSegNode.getRightToken();
                // check for each covered token if this segment is covering it
                for (Map.Entry<SToken, Long> e : coveredToken.entrySet()) {
                    RelannisNodeFeature featTok = (RelannisNodeFeature) e.getKey().getFeature(ANNIS_NS, FEAT_RELANNIS_NODE).getValue();
                    long entryTokenIndex = featTok.getTokenIndex();
                    if (entryTokenIndex <= rightTok && entryTokenIndex >= leftTok) {
                        // add this segmentation node to the covered set
                        covered.put(segNode.getId(), e.getValue());
                        break;
                    }
                }
            // end for each covered token
            }
        // end if not already contained
        }
    // end for each segmentation node
    }
    return covered;
}
Also used : RelannisNodeFeature(annis.model.RelannisNodeFeature) SNode(org.corpus_tools.salt.core.SNode) HashMap(java.util.HashMap) SToken(org.corpus_tools.salt.common.SToken) Map(java.util.Map) CorpusConfigMap(annis.service.objects.CorpusConfigMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) SFeature(org.corpus_tools.salt.core.SFeature)

Example 17 with SFeature

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

the class LegacyGraphConverter method convertToAnnotationGraph.

public static AnnotationGraph convertToAnnotationGraph(SDocument document) {
    SDocumentGraph docGraph = document.getDocumentGraph();
    SFeature featMatchedIDs = docGraph.getFeature(ANNIS_NS, FEAT_MATCHEDIDS);
    Match match = new Match();
    if (featMatchedIDs != null && featMatchedIDs.getValue_STEXT() != null) {
        match = Match.parseFromString(featMatchedIDs.getValue_STEXT(), ',');
    }
    // get matched node names by using the IDs
    List<Long> matchedNodeIDs = new ArrayList<>();
    for (URI u : match.getSaltIDs()) {
        SNode node = docGraph.getNode(u.toASCIIString());
        if (node == null) {
            // that's weird, fallback to the id
            log.warn("Could not get matched node from id {}", u.toASCIIString());
            matchedNodeIDs.add(-1l);
        } else {
            RelannisNodeFeature relANNISFeat = (RelannisNodeFeature) node.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE)).getValue();
            matchedNodeIDs.add(relANNISFeat.getInternalID());
        }
    }
    AnnotationGraph result = convertToAnnotationGraph(docGraph, matchedNodeIDs);
    return result;
}
Also used : AnnotationGraph(annis.model.AnnotationGraph) RelannisNodeFeature(annis.model.RelannisNodeFeature) SNode(org.corpus_tools.salt.core.SNode) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) ArrayList(java.util.ArrayList) URI(java.net.URI) SFeature(org.corpus_tools.salt.core.SFeature) Match(annis.service.objects.Match)

Example 18 with SFeature

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

the class LegacyGraphConverter method convertToAnnotationGraph.

public static AnnotationGraph convertToAnnotationGraph(SDocumentGraph docGraph, List<Long> matchedNodeIDs) {
    Set<Long> matchSet = new HashSet<>(matchedNodeIDs);
    AnnotationGraph annoGraph = new AnnotationGraph();
    List<String> pathList = CommonHelper.getCorpusPath(docGraph.getDocument().getGraph(), docGraph.getDocument());
    annoGraph.setPath(pathList.toArray(new String[pathList.size()]));
    annoGraph.setDocumentName(docGraph.getDocument().getName());
    Map<SNode, AnnisNode> allNodes = new HashMap<>();
    for (SNode sNode : docGraph.getNodes()) {
        SFeature featNodeRaw = sNode.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE));
        if (featNodeRaw != null) {
            RelannisNodeFeature featNode = (RelannisNodeFeature) featNodeRaw.getValue();
            long internalID = featNode.getInternalID();
            AnnisNode aNode = new AnnisNode(internalID);
            for (SAnnotation sAnno : sNode.getAnnotations()) {
                aNode.addNodeAnnotation(new Annotation(sAnno.getNamespace(), sAnno.getName(), sAnno.getValue_STEXT()));
            }
            aNode.setName(sNode.getName());
            Set<SLayer> layers = sNode.getLayers();
            if (!layers.isEmpty()) {
                aNode.setNamespace(layers.iterator().next().getName());
            }
            RelannisNodeFeature feat = (RelannisNodeFeature) sNode.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE)).getValue();
            if (sNode instanceof SToken) {
                List<DataSourceSequence> seqList = docGraph.getOverlappedDataSourceSequence(sNode, SALT_TYPE.STEXT_OVERLAPPING_RELATION);
                if (seqList != null) {
                    DataSourceSequence seq = seqList.get(0);
                    Preconditions.checkNotNull(seq, "DataSourceSequence is null for token %s", sNode.getId());
                    SSequentialDS seqDS = seq.getDataSource();
                    Preconditions.checkNotNull(seqDS, "SSequentalDS is null for token %s", sNode.getId());
                    Preconditions.checkNotNull(seqDS.getData(), "SSequentalDS data is null for token %s", sNode.getId());
                    String seqDSData = (String) seqDS.getData();
                    Preconditions.checkNotNull(seqDSData, "casted SSequentalDS data is null for token %s", sNode.getId());
                    Preconditions.checkNotNull(seq.getStart(), "SSequentalDS start is null for token %s", sNode.getId());
                    Preconditions.checkNotNull(seq.getEnd(), "SSequentalDS end is null for supposed token %s", sNode.getId());
                    int start = seq.getStart().intValue();
                    int end = seq.getEnd().intValue();
                    Preconditions.checkState(start >= 0 && start <= end && end <= seqDSData.length(), "Illegal start or end of textual DS for token (start %s, end: %s)", sNode.getId(), start, end);
                    String spannedText = seqDSData.substring(start, end);
                    Preconditions.checkNotNull(spannedText, "spanned text is null for supposed token %s (start: %s, end: %s)", sNode.getId(), start, end);
                    aNode.setSpannedText(spannedText);
                    aNode.setToken(true);
                    aNode.setTokenIndex(feat.getTokenIndex());
                }
            } else {
                aNode.setToken(false);
                aNode.setTokenIndex(null);
            }
            aNode.setCorpus(feat.getCorpusRef());
            aNode.setTextId(feat.getTextRef());
            aNode.setLeft(feat.getLeft());
            aNode.setLeftToken(feat.getLeftToken());
            aNode.setRight(feat.getRight());
            aNode.setRightToken(feat.getRightToken());
            if (matchSet.contains(aNode.getId())) {
                aNode.setMatchedNodeInQuery((long) matchedNodeIDs.indexOf(aNode.getId()) + 1);
                annoGraph.getMatchedNodeIds().add(aNode.getId());
            } else {
                aNode.setMatchedNodeInQuery(null);
            }
            annoGraph.addNode(aNode);
            allNodes.put(sNode, aNode);
        }
    }
    for (SRelation rel : docGraph.getRelations()) {
        RelannisEdgeFeature featRelation = RelannisEdgeFeature.extract(rel);
        if (featRelation != null) {
            addRelation(rel, featRelation.getPre(), featRelation.getComponentID(), allNodes, annoGraph);
        }
    }
    // add relations with empty relation name for every dominance relation
    List<SDominanceRelation> dominanceRelations = new LinkedList<>(docGraph.getDominanceRelations());
    for (SDominanceRelation rel : dominanceRelations) {
        RelannisEdgeFeature featEdge = RelannisEdgeFeature.extract(rel);
        if (featEdge != null && featEdge.getArtificialDominanceComponent() != null && featEdge.getArtificialDominancePre() != null) {
            addRelation(SDominanceRelation.class, null, rel.getAnnotations(), rel.getSource(), rel.getTarget(), rel.getLayers(), featEdge.getArtificialDominancePre(), featEdge.getArtificialDominanceComponent(), allNodes, annoGraph);
        }
    }
    return annoGraph;
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer) SNode(org.corpus_tools.salt.core.SNode) HashMap(java.util.HashMap) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) RelannisEdgeFeature(annis.model.RelannisEdgeFeature) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) HashSet(java.util.HashSet) RelannisNodeFeature(annis.model.RelannisNodeFeature) SAnnotation(org.corpus_tools.salt.core.SAnnotation) SSequentialDS(org.corpus_tools.salt.common.SSequentialDS) DataSourceSequence(org.corpus_tools.salt.util.DataSourceSequence) Annotation(annis.model.Annotation) SAnnotation(org.corpus_tools.salt.core.SAnnotation) LinkedList(java.util.LinkedList) AnnotationGraph(annis.model.AnnotationGraph) AnnisNode(annis.model.AnnisNode) SFeature(org.corpus_tools.salt.core.SFeature)

Example 19 with SFeature

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

the class SaltAnnotateExtractor method addOrderingRelations.

private void addOrderingRelations(SDocumentGraph graph, TreeMap<String, TreeMap<Long, String>> nodeBySegmentationPath) {
    AtomicInteger numberOfSOrderRels = new AtomicInteger();
    for (Map.Entry<String, TreeMap<Long, String>> e : nodeBySegmentationPath.entrySet()) {
        String segName = e.getKey();
        TreeMap<Long, String> nodeBySegIndex = e.getValue();
        // mark the first node in the chain
        if (!nodeBySegIndex.isEmpty()) {
            String idOfFirstNode = nodeBySegIndex.firstEntry().getValue();
            SNode firstNodeInSegChain = graph.getNode(idOfFirstNode);
            if (firstNodeInSegChain != null) {
                SFeature featFistSegInChain = SaltFactory.createSFeature();
                featFistSegInChain.setNamespace(ANNIS_NS);
                featFistSegInChain.setName(FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
                featFistSegInChain.setValue(segName);
                firstNodeInSegChain.addFeature(featFistSegInChain);
            }
        }
        SStructuredNode lastNode = null;
        for (String nodeID : nodeBySegIndex.values()) {
            SNode nodeById = graph.getNode(nodeID);
            if (nodeById instanceof SStructuredNode) {
                SStructuredNode n = (SStructuredNode) nodeById;
                if (lastNode != null) {
                    SOrderRelation orderRel = SaltFactory.createSOrderRelation();
                    orderRel.setSource(lastNode);
                    orderRel.setTarget(n);
                    orderRel.setType(segName);
                    orderRel.setName("sOrderRel" + numberOfSOrderRels.getAndIncrement());
                    graph.addRelation(orderRel);
                }
                lastNode = n;
            }
        }
    }
}
Also used : SStructuredNode(org.corpus_tools.salt.common.SStructuredNode) SNode(org.corpus_tools.salt.core.SNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) SFeature(org.corpus_tools.salt.core.SFeature)

Example 20 with SFeature

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

the class SaltAnnotateExtractor method moveNodeProperties.

private void moveNodeProperties(SStructuredNode oldNode, SStructuredNode newNode, SGraph graph) {
    Validate.notNull(oldNode);
    Validate.notNull(newNode);
    // step 1: collect every information that is need in a separate variable
    String id = oldNode.getId();
    String name = oldNode.getName();
    Set<SAnnotation> annotations = new LinkedHashSet<>(oldNode.getAnnotations());
    Set<SFeature> features = new LinkedHashSet<>(oldNode.getFeatures());
    Set<SProcessingAnnotation> processingAnnotations = new LinkedHashSet<>(oldNode.getProcessingAnnotations());
    Set<SMetaAnnotation> metaAnnotations = new LinkedHashSet<>(oldNode.getMetaAnnotations());
    Set<SLayer> nodeLayers = new LinkedHashSet<>(oldNode.getLayers());
    Multimap<SRelation, SLayer> layerOfRelation = ArrayListMultimap.create();
    List<SRelation<SNode, SNode>> inRelations = new LinkedList<>(graph.getInRelations(oldNode.getId()));
    List<SRelation<SNode, SNode>> outRelations = new LinkedList<>(graph.getOutRelations(oldNode.getId()));
    // step 2: remove the old node from everything it is connected to
    for (SRelation<SNode, SNode> rel : inRelations) {
        if (rel.getLayers() != null) {
            layerOfRelation.putAll(rel, rel.getLayers());
        }
        graph.removeRelation(rel);
    }
    for (SRelation<SNode, SNode> rel : outRelations) {
        if (rel.getLayers() != null) {
            layerOfRelation.putAll(rel, rel.getLayers());
        }
        graph.removeRelation(rel);
    }
    graph.removeNode(oldNode);
    // step 3: add the new node to everything it should be connected to
    newNode.setName(name);
    newNode.setId(id);
    graph.addNode(newNode);
    for (SAnnotation anno : annotations) {
        newNode.addAnnotation(anno);
    }
    for (SFeature feat : features) {
        // filter the features, do not include salt::SNAME
        if (!(SaltUtil.SALT_NAMESPACE.equals(feat.getNamespace()) && SaltUtil.FEAT_NAME.equals(feat.getName()))) {
            newNode.addFeature(feat);
        }
    }
    for (SProcessingAnnotation proc : processingAnnotations) {
        newNode.addProcessingAnnotation(proc);
    }
    for (SMetaAnnotation meta : metaAnnotations) {
        newNode.addMetaAnnotation(meta);
    }
    for (SLayer l : nodeLayers) {
        l.addNode(newNode);
    }
    for (SRelation rel : inRelations) {
        rel.setTarget(newNode);
        graph.addRelation(rel);
        if (layerOfRelation.containsKey(rel)) {
            for (SLayer l : layerOfRelation.get(rel)) {
                l.addRelation(rel);
            }
        }
    }
    for (SRelation rel : outRelations) {
        rel.setSource(newNode);
        graph.addRelation(rel);
        if (layerOfRelation.containsKey(rel)) {
            for (SLayer l : layerOfRelation.get(rel)) {
                l.addRelation(rel);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SLayer(org.corpus_tools.salt.core.SLayer) SNode(org.corpus_tools.salt.core.SNode) SAnnotation(org.corpus_tools.salt.core.SAnnotation) SProcessingAnnotation(org.corpus_tools.salt.core.SProcessingAnnotation) LinkedList(java.util.LinkedList) SRelation(org.corpus_tools.salt.core.SRelation) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) SFeature(org.corpus_tools.salt.core.SFeature)

Aggregations

SFeature (org.corpus_tools.salt.core.SFeature)24 SNode (org.corpus_tools.salt.core.SNode)12 RelannisNodeFeature (annis.model.RelannisNodeFeature)8 SToken (org.corpus_tools.salt.common.SToken)8 HashMap (java.util.HashMap)6 SRelation (org.corpus_tools.salt.core.SRelation)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 SAnnotation (org.corpus_tools.salt.core.SAnnotation)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 TreeMap (java.util.TreeMap)3 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)3 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)3 STextualDS (org.corpus_tools.salt.common.STextualDS)3 AnnotationGraph (annis.model.AnnotationGraph)2 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)2 LinkedHashSet (java.util.LinkedHashSet)2 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)2