Search in sources :

Example 6 with SSpan

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

the class SpanHTMLOutputter method outputHTML.

public void outputHTML(SNode node, String matchedQName, SortedMap<Long, List<OutputItem>> outputStartTags, SortedMap<Long, List<OutputItem>> outputEndTags, String tokenColor, int priority) {
    this.tokenColor = tokenColor;
    if (node instanceof SToken && "tok".equals(matchedQName)) {
        SToken tok = (SToken) node;
        outputToken(tok, outputStartTags, outputEndTags, priority);
    } else if (node instanceof SSpan || node instanceof SToken) {
        outputAnnotation(node, matchedQName, outputStartTags, outputEndTags, priority);
    } else {
        throw new IllegalArgumentException("node must be either a SSpan or SToken");
    }
}
Also used : SToken(org.corpus_tools.salt.common.SToken) SSpan(org.corpus_tools.salt.common.SSpan)

Example 7 with SSpan

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

the class SaltAnnotateExtractor method testAndFixNonSpan.

/**
 * Tests if the source node is not a span and fixes this if necessary
 *
 * @param sourceNode The source node to check.
 * @param nodeByRankID
 * @return Either the original span or a new created one
 */
private SSpan testAndFixNonSpan(SStructuredNode sourceNode, FastInverseMap<Long, SNode> nodeByRankID) {
    if (sourceNode != null && !(sourceNode instanceof SSpan)) {
        log.debug("Mismatched source type: should be SSpan");
        SNode oldNode = sourceNode;
        sourceNode = recreateNode(SSpan.class, sourceNode);
        updateMapAfterRecreatingNode(oldNode, sourceNode, nodeByRankID);
    }
    return (SSpan) sourceNode;
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SSpan(org.corpus_tools.salt.common.SSpan)

Example 8 with SSpan

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

the class SaltAnnotateExtractor method createMissingSpanningRelations.

/**
 * Use the left/right token index of the spans to create spanning relations
 * when this did not happen yet.
 *
 * @param graph
 * @param nodeByRankID
 * @param numberOfRelations
 */
private void createMissingSpanningRelations(SDocumentGraph graph, FastInverseMap<Long, SNode> nodeByRankID, TreeMap<Long, SToken> tokenByIndex, Map<String, ComponentEntry> componentForSpan, AtomicInteger numberOfRelations) {
    // add the missing spanning relations for each continuous span of the graph
    for (SSpan span : graph.getSpans()) {
        long pre = 1;
        RelannisNodeFeature featSpan = RelannisNodeFeature.extract(span);
        ComponentEntry spanComponent = componentForSpan.get(span.getId());
        if (spanComponent != null && featSpan != null && featSpan.getLeftToken() >= 0 && featSpan.getRightToken() >= 0) {
            for (long i = featSpan.getLeftToken(); i <= featSpan.getRightToken(); i++) {
                SToken tok = tokenByIndex.get(i);
                if (tok != null) {
                    boolean missing = true;
                    List<SRelation<SNode, SNode>> existingRelations = graph.getRelations(span.getId(), tok.getId());
                    if (existingRelations != null) {
                        for (Relation e : existingRelations) {
                            if (e instanceof SSpanningRelation) {
                                missing = false;
                                break;
                            }
                        }
                    }
                    if (missing) {
                        String type = "c";
                        SLayer layer = findOrAddSLayer(spanComponent.getNamespace(), graph);
                        createNewRelation(graph, span, tok, null, type, spanComponent.getId(), layer, pre++, nodeByRankID, numberOfRelations);
                    }
                }
            // end if token exists
            }
        // end for each covered token index
        }
    }
// end for each span
}
Also used : SToken(org.corpus_tools.salt.common.SToken) RelannisNodeFeature(annis.model.RelannisNodeFeature) SRelation(org.corpus_tools.salt.core.SRelation) SLayer(org.corpus_tools.salt.core.SLayer) SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) STextualRelation(org.corpus_tools.salt.common.STextualRelation) SRelation(org.corpus_tools.salt.core.SRelation) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) Relation(org.corpus_tools.salt.graph.Relation) SSpan(org.corpus_tools.salt.common.SSpan) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation)

Example 9 with SSpan

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

the class SaltAnnotateExtractor method extractData.

@Override
public SaltProject extractData(ResultSet resultSet) throws SQLException, DataAccessException {
    SaltProject project = SaltFactory.createSaltProject();
    try {
        SCorpusGraph corpusGraph = null;
        SDocumentGraph graph = null;
        // fn: parent information (pre and component) id to node
        FastInverseMap<Long, SNode> nodeByRankID = new FastInverseMap<>();
        TreeSet<Long> allTextIDs = new TreeSet<>();
        TreeMap<Long, String> tokenTexts = new TreeMap<>();
        TreeMap<Long, SToken> tokenByIndex = new TreeMap<>();
        TreeMap<String, TreeMap<Long, String>> nodeBySegmentationPath = new TreeMap<>();
        Map<String, ComponentEntry> componentForSpan = new HashMap<>();
        // clear mapping functions for this graph
        // assumes that the result set is sorted by key, pre
        nodeByRankID.clear();
        SDocument document = null;
        AtomicInteger numberOfRelations = new AtomicInteger();
        int match_index = 0;
        SolutionKey<?> key = createSolutionKey();
        int counter = 0;
        while (resultSet.next()) {
            if (counter % 1000 == 0) {
                log.debug("handling resultset row {}", counter);
            }
            counter++;
            // List<String> annotationGraphKey =
            key.retrieveKey(resultSet);
            if (key.isNewKey()) {
                // create the text for the last graph
                if (graph != null && document != null) {
                    createMissingSpanningRelations(graph, nodeByRankID, tokenByIndex, componentForSpan, numberOfRelations);
                    createPrimaryTexts(graph, allTextIDs, tokenTexts, tokenByIndex);
                    addOrderingRelations(graph, nodeBySegmentationPath);
                }
                // new match, reset everything
                nodeByRankID.clear();
                tokenTexts.clear();
                tokenByIndex.clear();
                componentForSpan.clear();
                Integer matchstart = resultSet.getInt("matchstart");
                corpusGraph = SaltFactory.createSCorpusGraph();
                corpusGraph.setName("match_" + (match_index + matchstart));
                project.addCorpusGraph(corpusGraph);
                graph = SaltFactory.createSDocumentGraph();
                document = SaltFactory.createSDocument();
                document.setDocumentGraphLocation(org.eclipse.emf.common.util.URI.createFileURI(Files.createTempDir().getAbsolutePath()));
                List<String> path = corpusPathExtractor.extractCorpusPath(resultSet, "path");
                SCorpus toplevelCorpus = SaltFactory.createSCorpus();
                toplevelCorpus.setName(path.get(0));
                corpusGraph.addNode(toplevelCorpus);
                Validate.isTrue(path.size() >= 2, "Corpus path must be have at least two members (toplevel and document)");
                SCorpus corpus = toplevelCorpus;
                for (int i = 1; i < path.size() - 1; i++) {
                    SCorpus subcorpus = SaltFactory.createSCorpus();
                    subcorpus.setName(path.get(i));
                    corpusGraph.addSubCorpus(corpus, subcorpus);
                    corpus = subcorpus;
                }
                document.setName(path.get(path.size() - 1));
                document.setId("" + match_index);
                corpusGraph.addDocument(corpus, document);
                document.setDocumentGraph(graph);
                match_index++;
            }
            // end if new key
            // get node data
            SNode node = createOrFindNewNode(resultSet, graph, allTextIDs, tokenTexts, tokenByIndex, nodeBySegmentationPath, key, nodeByRankID);
            long rankID = longValue(resultSet, RANK_TABLE, "id");
            long componentID = longValue(resultSet, COMPONENT_TABLE, "id");
            if (!resultSet.wasNull()) {
                nodeByRankID.put(rankID, node);
                createRelation(resultSet, graph, nodeByRankID, node, numberOfRelations);
                if (node instanceof SSpan) {
                    componentForSpan.put(node.getId(), new ComponentEntry(componentID, 'c', stringValue(resultSet, COMPONENT_TABLE, "namespace"), stringValue(resultSet, COMPONENT_TABLE, "name")));
                }
            }
        }
        // the last match needs a primary text, too
        if (graph != null) {
            createMissingSpanningRelations(graph, nodeByRankID, tokenByIndex, componentForSpan, numberOfRelations);
            createPrimaryTexts(graph, allTextIDs, tokenTexts, tokenByIndex);
            addOrderingRelations(graph, nodeBySegmentationPath);
        }
    } catch (Exception ex) {
        log.error("could not map result set to SaltProject", ex);
    }
    return project;
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SSpan(org.corpus_tools.salt.common.SSpan) HashMap(java.util.HashMap) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) SToken(org.corpus_tools.salt.common.SToken) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) TreeSet(java.util.TreeSet) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject) TreeMap(java.util.TreeMap) SaltException(org.corpus_tools.salt.exceptions.SaltException) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SCorpus(org.corpus_tools.salt.common.SCorpus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 10 with SSpan

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

SSpan (org.corpus_tools.salt.common.SSpan)12 SToken (org.corpus_tools.salt.common.SToken)8 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)4 HashMap (java.util.HashMap)3 SAnnotation (org.corpus_tools.salt.core.SAnnotation)3 SNode (org.corpus_tools.salt.core.SNode)3 SRelation (org.corpus_tools.salt.core.SRelation)3 Row (annis.gui.widgets.grid.Row)2 RelannisNodeFeature (annis.model.RelannisNodeFeature)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)2 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)2 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)2 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)2 SFeature (org.corpus_tools.salt.core.SFeature)2