Search in sources :

Example 1 with SStructuredNode

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

the class SaltAnnotateExtractor method recreateNode.

// non used functions, commmented out in order to avoid some findbugs warnings
// private void addStringSFeature(SNode node, String name,
// String value) throws SQLException
// {
// SFeature feat = SaltFactory.createSFeature();
// feat.setNamespace(ANNIS_NS);
// feat.setName(name);
// feat.setValue(value);
// node.addFeature(feat);
// }
// 
// private void addLongSFeature(SNode node, ResultSet resultSet, String name,
// String table, String tupleName) throws SQLException
// {
// addLongSFeature(node, name, longValue(resultSet, table, tupleName));
// }
// 
// private void addStringSFeature(SNode node, ResultSet resultSet, String name,
// String table, String tupleName) throws SQLException
// {
// addStringSFeature(node, name, stringValue(resultSet, table, tupleName));
// }
private SStructuredNode recreateNode(Class<? extends SStructuredNode> clazz, SStructuredNode oldNode) {
    if (oldNode.getClass() == clazz) {
        return oldNode;
    }
    SStructuredNode node = oldNode;
    if (clazz == SSpan.class) {
        node = SaltFactory.createSSpan();
    } else if (clazz == SStructure.class) {
        node = SaltFactory.createSStructure();
    } else {
        throw new UnsupportedOperationException("no node creation possible for class: " + clazz.getName());
    }
    moveNodeProperties(oldNode, node, oldNode.getGraph());
    return node;
}
Also used : SStructuredNode(org.corpus_tools.salt.common.SStructuredNode) SStructure(org.corpus_tools.salt.common.SStructure)

Example 2 with SStructuredNode

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

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

the class SaltAnnotateExtractor method createOrFindNewNode.

private SNode createOrFindNewNode(ResultSet resultSet, SDocumentGraph graph, TreeSet<Long> allTextIDs, TreeMap<Long, String> tokenTexts, TreeMap<Long, SToken> tokenByIndex, TreeMap<String, TreeMap<Long, String>> nodeBySegmentationPath, SolutionKey<?> key, FastInverseMap<Long, SNode> nodeByRankID) throws SQLException {
    String name = stringValue(resultSet, NODE_TABLE, "node_name");
    String saltID = stringValue(resultSet, NODE_TABLE, "salt_id");
    if (saltID == null) {
        // fallback to the name
        saltID = name;
    }
    long internalID = longValue(resultSet, "node", "id");
    String relationType = stringValue(resultSet, COMPONENT_TABLE, "type");
    long tokenIndex = longValue(resultSet, NODE_TABLE, "token_index");
    boolean isToken = !resultSet.wasNull();
    org.eclipse.emf.common.util.URI nodeURI = graph.getDocument().getPath();
    nodeURI = nodeURI.appendFragment(saltID);
    SStructuredNode node = (SStructuredNode) graph.getNode(nodeURI.toString());
    if (node == null) {
        // create new node
        if (isToken) {
            node = createSToken(tokenIndex, resultSet, tokenTexts, tokenByIndex);
        } else {
            node = createOtherSNode(resultSet);
        }
        node.setName(name);
        node.setId(nodeURI.toString());
        setFeaturesForNode(node, internalID, resultSet);
        Object nodeId = key.getNodeId(resultSet, outerQueryTableAccessStrategy);
        graph.addNode(node);
        Integer matchedNode = key.getMatchedNodeIndex(nodeId);
        if (matchedNode != null) {
            addLongSFeature(node, FEAT_MATCHEDNODE, matchedNode);
        }
        mapLayer(node, graph, resultSet);
        long textRef = longValue(resultSet, NODE_TABLE, "text_ref");
        allTextIDs.add(textRef);
    } else if ("c".equals(relationType) && isToken == false) {
        node = testAndFixNonSpan(node, nodeByRankID);
    }
    String nodeAnnoValue = stringValue(resultSet, NODE_ANNOTATION_TABLE, "value");
    String nodeAnnoNameSpace = stringValue(resultSet, NODE_ANNOTATION_TABLE, "namespace");
    String nodeAnnoName = stringValue(resultSet, NODE_ANNOTATION_TABLE, "name");
    if (!resultSet.wasNull()) {
        String fullName = (nodeAnnoNameSpace == null || nodeAnnoNameSpace.isEmpty() ? "" : (nodeAnnoNameSpace + "::")) + nodeAnnoName;
        SAnnotation anno = node.getAnnotation(fullName);
        if (anno == null) {
            anno = SaltFactory.createSAnnotation();
            anno.setNamespace(nodeAnnoNameSpace);
            anno.setName(nodeAnnoName);
            anno.setValue(nodeAnnoValue);
            node.addAnnotation(anno);
        }
    }
    // prepare SOrderingRelation if the node is part of a segmentation path
    String segName = stringValue(resultSet, "node", "seg_name");
    if (segName != null) {
        long left = longValue(resultSet, "node", "seg_index");
        // last ones
        if (!nodeBySegmentationPath.containsKey(segName)) {
            nodeBySegmentationPath.put(segName, new TreeMap<Long, String>());
        }
        nodeBySegmentationPath.get(segName).put(left, node.getId());
    }
    return node;
}
Also used : SStructuredNode(org.corpus_tools.salt.common.SStructuredNode) SAnnotation(org.corpus_tools.salt.core.SAnnotation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 4 with SStructuredNode

use of org.corpus_tools.salt.common.SStructuredNode 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)

Aggregations

SStructuredNode (org.corpus_tools.salt.common.SStructuredNode)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)1 SStructure (org.corpus_tools.salt.common.SStructure)1 SAnnotation (org.corpus_tools.salt.core.SAnnotation)1 SFeature (org.corpus_tools.salt.core.SFeature)1 SLayer (org.corpus_tools.salt.core.SLayer)1 SNode (org.corpus_tools.salt.core.SNode)1 SRelation (org.corpus_tools.salt.core.SRelation)1