Search in sources :

Example 1 with AnnotationGraph

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

the class LegacyGraphConverter method convertToResultSet.

public static AnnisResultSet convertToResultSet(SaltProject p) {
    List<AnnotationGraph> annotationGraphs = convertToAOM(p);
    AnnisResultSetImpl annisResultSet = new AnnisResultSetImpl();
    for (AnnotationGraph annotationGraph : annotationGraphs) {
        annisResultSet.add(new AnnisResultImpl(annotationGraph));
    }
    return annisResultSet;
}
Also used : AnnotationGraph(annis.model.AnnotationGraph) AnnisResultSetImpl(annis.service.objects.AnnisResultSetImpl) AnnisResultImpl(annis.service.objects.AnnisResultImpl)

Example 2 with AnnotationGraph

use of annis.model.AnnotationGraph 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 3 with AnnotationGraph

use of annis.model.AnnotationGraph 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 4 with AnnotationGraph

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

the class AnnisGraphTools method getSyntaxGraphs.

public List<DirectedGraph<AnnisNode, Edge>> getSyntaxGraphs() {
    AnnotationGraph ag = input.getResult().getGraph();
    String namespace = input.getMappings().getProperty("node_ns", input.getNamespace());
    String terminalName = input.getMappings().getProperty(TigerTreeVisualizer.TERMINAL_NAME_KEY);
    String terminalNamespace = input.getMappings().getProperty(TigerTreeVisualizer.TERMINAL_NS_KEY);
    List<DirectedGraph<AnnisNode, Edge>> resultGraphs = new ArrayList<>();
    List<AnnisNode> rootNodes = new LinkedList<>();
    for (AnnisNode n : ag.getNodes()) {
        if (isRootNode(n, namespace)) {
            rootNodes.add(n);
        }
    }
    // sort root nodes according to their left-most covered token
    HorizontalOrientation orientation = detectLayoutDirection(ag);
    if (orientation == HorizontalOrientation.LEFT_TO_RIGHT) {
        Collections.sort(rootNodes, new Comparator<AnnisNode>() {

            @Override
            public int compare(AnnisNode o1, AnnisNode o2) {
                return Long.compare(o1.getLeftToken(), o2.getLeftToken());
            }
        });
    } else if (orientation == HorizontalOrientation.RIGHT_TO_LEFT) {
        Collections.sort(rootNodes, new Comparator<AnnisNode>() {

            @Override
            public int compare(AnnisNode o1, AnnisNode o2) {
                return Long.compare(o2.getLeftToken(), o1.getLeftToken());
            }
        });
    }
    for (AnnisNode r : rootNodes) {
        resultGraphs.add(extractGraph(ag, r, terminalNamespace, terminalName));
    }
    return resultGraphs;
}
Also used : AnnotationGraph(annis.model.AnnotationGraph) DirectedGraph(edu.uci.ics.jung.graph.DirectedGraph) ArrayList(java.util.ArrayList) AnnisNode(annis.model.AnnisNode) LinkedList(java.util.LinkedList) Comparator(java.util.Comparator)

Example 5 with AnnotationGraph

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

the class LegacyGraphConverterTest method testConvertToAOM.

/**
 * Test of convertToAOM method, of class LegacyGraphConverter.
 */
@Test
public void testConvertToAOM() throws SQLException {
    SaltAnnotateExtractor saltExtractor = new SaltAnnotateExtractor() {

        @Override
        protected SolutionKey<?> createSolutionKey() {
            PostgreSqlArraySolutionKey<Long> key = new PostgreSqlArraySolutionKey<>();
            key.setKeyColumnName("key");
            key.setIdColumnName("id");
            return key;
        }
    };
    CorpusPathExtractor corpusPathExtractor = new ArrayCorpusPathExtractor();
    saltExtractor.setCorpusPathExtractor(corpusPathExtractor);
    TestAnnotateSqlGenerator.setupOuterQueryFactsTableColumnAliases(saltExtractor);
    List<Match> matches = new ArrayList<>();
    matches.add(Match.parseFromString("salt:/pcc2/4282/#tok_155 tiger::pos::salt:/pcc2/4282#tok_156"));
    MatchGroup matchGroup = new MatchGroup(matches);
    SaltProject p = saltExtractor.extractData(new CsvResultSetProvider(annis.sqlgen.SaltAnnotateExtractorTest.class.getResourceAsStream("SampleAnnotateResult.csv")).getResultSet());
    SaltAnnotateExtractor.addMatchInformation(p, matchGroup);
    List<AnnotationGraph> expected = aomSqlGen.extractData(new CsvResultSetProvider(annis.sqlgen.SaltAnnotateExtractorTest.class.getResourceAsStream("SampleAnnotateResult.csv")).getResultSet());
    List<AnnotationGraph> result = LegacyGraphConverter.convertToAOM(p);
    assertEquals(expected.size(), result.size());
    Iterator<AnnotationGraph> itGraphExpected = expected.iterator();
    Iterator<AnnotationGraph> itGraphResult = result.iterator();
    while (itGraphExpected.hasNext() && itGraphResult.hasNext()) {
        AnnotationGraph graphExpected = itGraphExpected.next();
        AnnotationGraph graphResult = itGraphResult.next();
        List<AnnisNode> nodeListExpected = graphExpected.getNodes();
        List<AnnisNode> nodeListResult = graphResult.getNodes();
        assertEquals(nodeListExpected.size(), nodeListResult.size());
        Collections.sort(nodeListExpected, new Comparator<AnnisNode>() {

            @Override
            public int compare(AnnisNode arg0, AnnisNode arg1) {
                return Long.valueOf(arg0.getId()).compareTo(Long.valueOf(arg1.getId()));
            }
        });
        Collections.sort(nodeListResult, new Comparator<AnnisNode>() {

            @Override
            public int compare(AnnisNode arg0, AnnisNode arg1) {
                return Long.valueOf(arg0.getId()).compareTo(Long.valueOf(arg1.getId()));
            }
        });
        Iterator<AnnisNode> itNodeExpected = nodeListExpected.iterator();
        Iterator<AnnisNode> itNodeResult = nodeListResult.iterator();
        while (itNodeExpected.hasNext() && itNodeResult.hasNext()) {
            checkAnnisNodeEqual(itNodeExpected.next(), itNodeResult.next());
        }
    }
}
Also used : ArrayCorpusPathExtractor(annis.sqlgen.ArrayCorpusPathExtractor) CorpusPathExtractor(annis.sqlgen.CorpusPathExtractor) ArrayList(java.util.ArrayList) SaltProject(org.corpus_tools.salt.common.SaltProject) PostgreSqlArraySolutionKey(annis.sqlgen.PostgreSqlArraySolutionKey) SaltAnnotateExtractor(annis.sqlgen.SaltAnnotateExtractor) Match(annis.service.objects.Match) AnnotationGraph(annis.model.AnnotationGraph) ArrayCorpusPathExtractor(annis.sqlgen.ArrayCorpusPathExtractor) MatchGroup(annis.service.objects.MatchGroup) AnnisNode(annis.model.AnnisNode) CsvResultSetProvider(annis.test.CsvResultSetProvider) Test(org.junit.Test)

Aggregations

AnnotationGraph (annis.model.AnnotationGraph)6 AnnisNode (annis.model.AnnisNode)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 Annotation (annis.model.Annotation)2 RelannisNodeFeature (annis.model.RelannisNodeFeature)2 Match (annis.service.objects.Match)2 HashMap (java.util.HashMap)2 SFeature (org.corpus_tools.salt.core.SFeature)2 SNode (org.corpus_tools.salt.core.SNode)2 DocumentNameMapRow (annis.dao.DocumentNameMapRow)1 Edge (annis.model.Edge)1 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)1 AnnisResultImpl (annis.service.objects.AnnisResultImpl)1 AnnisResultSetImpl (annis.service.objects.AnnisResultSetImpl)1 MatchGroup (annis.service.objects.MatchGroup)1 ArrayCorpusPathExtractor (annis.sqlgen.ArrayCorpusPathExtractor)1 CorpusPathExtractor (annis.sqlgen.CorpusPathExtractor)1 PostgreSqlArraySolutionKey (annis.sqlgen.PostgreSqlArraySolutionKey)1 SaltAnnotateExtractor (annis.sqlgen.SaltAnnotateExtractor)1