Search in sources :

Example 6 with SLayer

use of org.corpus_tools.salt.core.SLayer 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 7 with SLayer

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

the class SaltAnnotateExtractorTest method testLayerNames.

@Test
public void testLayerNames() throws SQLException {
    SaltProject project = instance.extractData(resultSetProviderSingleText.getResultSet());
    assertNotNull(project);
    SDocumentGraph g = project.getCorpusGraphs().get(0).getDocuments().get(0).getDocumentGraph();
    List<SLayer> layers = new ArrayList<>(g.getLayers());
    Collections.sort(layers, new NameComparator());
    assertEquals(6, layers.size());
    assertEquals("default_ns", layers.get(0).getName());
    assertEquals("dep", layers.get(1).getName());
    assertEquals("exmaralda", layers.get(2).getName());
    assertEquals("mmax", layers.get(3).getName());
    assertEquals("rst", layers.get(4).getName());
    assertEquals("tiger", layers.get(5).getName());
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) ArrayList(java.util.ArrayList) SaltProject(org.corpus_tools.salt.common.SaltProject) Test(org.junit.Test)

Example 8 with SLayer

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

the class SaltAnnotateExtractor method findOrAddSLayer.

/**
 * Retrieves an existing layer by it's name or creates and adds a new one if
 * not existing yet
 *
 * @param name
 * @param graph
 * @return Either the old or the newly created layer
 */
private SLayer findOrAddSLayer(String name, SDocumentGraph graph) {
    List<SLayer> layerList = graph.getLayerByName(name);
    SLayer layer = (layerList != null && layerList.size() > 0) ? layerList.get(0) : null;
    if (layer == null) {
        layer = SaltFactory.createSLayer();
        layer.setName(name);
        graph.addLayer(layer);
    }
    return layer;
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer)

Example 9 with SLayer

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

the class SaltAnnotateExtractor method createRelation.

private void createRelation(ResultSet resultSet, SDocumentGraph graph, FastInverseMap<Long, SNode> nodeByRankID, SNode targetNode, AtomicInteger numberOfRelations) throws SQLException {
    long parent = longValue(resultSet, RANK_TABLE, "parent");
    if (resultSet.wasNull()) {
        return;
    }
    long pre = longValue(resultSet, RANK_TABLE, "pre");
    long componentID = longValue(resultSet, RANK_TABLE, "component_id");
    String relationNamespace = stringValue(resultSet, COMPONENT_TABLE, "namespace");
    if (relationNamespace == null) {
        relationNamespace = "default_ns";
    }
    String relationName = stringValue(resultSet, COMPONENT_TABLE, "name");
    String type = stringValue(resultSet, COMPONENT_TABLE, "type");
    SStructuredNode sourceNode = (SStructuredNode) nodeByRankID.get(parent);
    if (sourceNode == null) {
        // the relation is not fully included in the result
        return;
    }
    SLayer layer = findOrAddSLayer(relationNamespace, graph);
    SRelation rel;
    if (!resultSet.wasNull()) {
        rel = findExistingRelation(graph, sourceNode, targetNode, relationName, layer);
        if (rel == null) {
            rel = createNewRelation(graph, sourceNode, targetNode, relationName, type, componentID, layer, pre, nodeByRankID, numberOfRelations);
        }
        // add relation annotations if relation was successfully created
        if (rel != null) {
            addRelationAnnotations(resultSet, rel);
        }
    }
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer) SRelation(org.corpus_tools.salt.core.SRelation) SStructuredNode(org.corpus_tools.salt.common.SStructuredNode)

Example 10 with SLayer

use of org.corpus_tools.salt.core.SLayer 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

SLayer (org.corpus_tools.salt.core.SLayer)11 SRelation (org.corpus_tools.salt.core.SRelation)6 LinkedList (java.util.LinkedList)4 SAnnotation (org.corpus_tools.salt.core.SAnnotation)4 SNode (org.corpus_tools.salt.core.SNode)4 RelannisNodeFeature (annis.model.RelannisNodeFeature)3 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)3 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)3 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)2 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)2 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)2 STextualRelation (org.corpus_tools.salt.common.STextualRelation)2 SToken (org.corpus_tools.salt.common.SToken)2 SFeature (org.corpus_tools.salt.core.SFeature)2 Relation (org.corpus_tools.salt.graph.Relation)2 AnnisNode (annis.model.AnnisNode)1 Annotation (annis.model.Annotation)1