Search in sources :

Example 1 with SDominanceRelation

use of org.corpus_tools.salt.common.SDominanceRelation in project ANNIS by korpling.

the class SaltAnnotateExtractor method createNewRelation.

private SRelation createNewRelation(SDocumentGraph graph, SStructuredNode sourceNode, SNode targetNode, String relationName, String type, long componentID, SLayer layer, long pre, FastInverseMap<Long, SNode> nodeByRankID, AtomicInteger numberOfRelations) {
    SRelation rel = null;
    if (null != type) // create new relation
    {
        switch(type) {
            case "d":
                SDominanceRelation domrel = SaltFactory.createSDominanceRelation();
                // always set a name by ourself since the SDocumentGraph#basicAddRelation()
                // functions otherwise real slow
                domrel.setName("sDomRel" + numberOfRelations.incrementAndGet());
                rel = domrel;
                if (sourceNode != null && !(sourceNode instanceof SStructure)) {
                    log.debug("Mismatched source type: should be SStructure");
                    SNode oldNode = sourceNode;
                    sourceNode = recreateNode(SStructure.class, sourceNode);
                    updateMapAfterRecreatingNode(oldNode, sourceNode, nodeByRankID);
                }
                if (relationName == null || relationName.isEmpty()) {
                    // layer but has a non-empty relation name
                    if (handleArtificialDominanceRelation(graph, sourceNode, targetNode, rel, layer, componentID, pre)) {
                        // don't include this relation
                        rel = null;
                    }
                }
                break;
            case "c":
                SSpanningRelation spanrel = SaltFactory.createSSpanningRelation();
                // always set a name by ourself since the SDocumentGraph#basicAddRelation()
                // functions is real slow otherwise
                spanrel.setName("sSpanRel" + numberOfRelations.incrementAndGet());
                rel = spanrel;
                sourceNode = testAndFixNonSpan(sourceNode, nodeByRankID);
                break;
            case "p":
                SPointingRelation pointingrel = SaltFactory.createSPointingRelation();
                pointingrel.setName("sPointingRel" + numberOfRelations.incrementAndGet());
                rel = pointingrel;
                break;
            default:
                throw new IllegalArgumentException("Invalid type " + type + " for new Relation");
        }
        try {
            if (rel != null) {
                rel.setType(relationName);
                RelannisEdgeFeature featRelation = new RelannisEdgeFeature();
                featRelation.setPre(pre);
                featRelation.setComponentID(componentID);
                SFeature sfeatRelation = SaltFactory.createSFeature();
                sfeatRelation.setNamespace(ANNIS_NS);
                sfeatRelation.setName(FEAT_RELANNIS_EDGE);
                sfeatRelation.setValue(featRelation);
                rel.addFeature(sfeatRelation);
                rel.setSource(sourceNode);
                if ("c".equals(type) && !(targetNode instanceof SToken)) {
                    log.warn("invalid relation detected: target node ({}) " + "of a coverage relation (from: {}, internal id {}) was not a token", new Object[] { targetNode.getName(), sourceNode == null ? "null" : sourceNode.getName(), "" + pre });
                } else {
                    rel.setTarget(targetNode);
                    graph.addRelation(rel);
                    layer.addRelation(rel);
                }
            }
        } catch (SaltException ex) {
            log.warn("invalid relation detected", ex);
        }
    }
    return rel;
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SNode(org.corpus_tools.salt.core.SNode) SaltException(org.corpus_tools.salt.exceptions.SaltException) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) RelannisEdgeFeature(annis.model.RelannisEdgeFeature) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) SStructure(org.corpus_tools.salt.common.SStructure) SFeature(org.corpus_tools.salt.core.SFeature)

Example 2 with SDominanceRelation

use of org.corpus_tools.salt.common.SDominanceRelation 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 3 with SDominanceRelation

use of org.corpus_tools.salt.common.SDominanceRelation in project ANNIS by korpling.

the class SaltAnnotateExtractorTest method testRelationType.

@Test
public void testRelationType() throws SQLException {
    SaltProject project = instance.extractData(resultSetProviderSingleText.getResultSet());
    assertNotNull(project);
    SDocumentGraph g = project.getCorpusGraphs().get(0).getDocuments().get(0).getDocumentGraph();
    for (SRelation<? extends SNode, ? extends SNode> r : g.getRelations()) {
        if (!(r instanceof STextualRelation)) {
            assertEquals(1, r.getLayers().size());
            String layerName = r.getLayers().iterator().next().getName();
            if ("exmaralda".equals(layerName) || "urml".equals(layerName) || "mmax".equals(layerName)) {
                assertTrue("instance of SSpanningRelation", r instanceof SSpanningRelation);
            } else if ("dep".equals(layerName)) {
                assertTrue("instance of SPointingRelation", r instanceof SPointingRelation);
            } else if ("tiger".equals(layerName)) {
                assertTrue("instance of SDominanceRelation", r instanceof SDominanceRelation);
            }
        }
    }
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) STextualRelation(org.corpus_tools.salt.common.STextualRelation) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) SaltProject(org.corpus_tools.salt.common.SaltProject) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) Test(org.junit.Test)

Example 4 with SDominanceRelation

use of org.corpus_tools.salt.common.SDominanceRelation in project ANNIS by korpling.

the class TimelineReconstructor method moveRelations.

private void moveRelations(SStructuredNode oldSpan, SToken newToken, Set<String> validSpanAnnos, String orderName) {
    final List<SRelation> inRels = new LinkedList<>(oldSpan.getInRelations());
    final List<SRelation> outRels = new LinkedList<>(oldSpan.getOutRelations());
    final List<SToken> coveredByOldSpan = new LinkedList<>();
    for (SRelation rel : outRels) {
        if (rel instanceof SPointingRelation || rel instanceof SDominanceRelation) {
            rel.setSource(newToken);
        } else if (rel instanceof SSpanningRelation) {
            coveredByOldSpan.add(((SSpanningRelation) rel).getTarget());
        }
    }
    for (SRelation rel : inRels) {
        if (rel instanceof SPointingRelation || rel instanceof SDominanceRelation) {
            rel.setTarget(newToken);
        }
    }
    // find the connected spans and connect them with the new token instead
    for (SToken tok : coveredByOldSpan) {
        if (tok.getInRelations() != null) {
            for (SRelation<?, ?> rel : tok.getInRelations()) {
                if (rel instanceof SSpanningRelation) {
                    boolean valid = false;
                    SSpan spanToMap = ((SSpanningRelation) rel).getSource();
                    if (virtualTokenizationFromNamespace) {
                        for (SAnnotation anno : spanToMap.getAnnotations()) {
                            if (anno.getNamespace() != null && anno.getNamespace().equals(orderName)) {
                                valid = true;
                                break;
                            }
                        }
                    } else {
                        for (String validAnno : validSpanAnnos) {
                            if (spanToMap.getAnnotation(validAnno) != null) {
                                valid = true;
                                break;
                            }
                        }
                    }
                    if (valid) {
                        graph.createRelation(spanToMap, newToken, SALT_TYPE.SSPANNING_RELATION, null);
                    }
                }
            }
        }
    }
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SSpan(org.corpus_tools.salt.common.SSpan) SAnnotation(org.corpus_tools.salt.core.SAnnotation) LinkedList(java.util.LinkedList) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation)

Aggregations

SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)4 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)3 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)3 SToken (org.corpus_tools.salt.common.SToken)3 SRelation (org.corpus_tools.salt.core.SRelation)3 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)2 LinkedList (java.util.LinkedList)2 SAnnotation (org.corpus_tools.salt.core.SAnnotation)2 SFeature (org.corpus_tools.salt.core.SFeature)2 SNode (org.corpus_tools.salt.core.SNode)2 AnnisNode (annis.model.AnnisNode)1 Annotation (annis.model.Annotation)1 AnnotationGraph (annis.model.AnnotationGraph)1 RelannisNodeFeature (annis.model.RelannisNodeFeature)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)1 SSequentialDS (org.corpus_tools.salt.common.SSequentialDS)1 SSpan (org.corpus_tools.salt.common.SSpan)1 SStructure (org.corpus_tools.salt.common.SStructure)1