Search in sources :

Example 21 with SToken

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

the class CorefVisualizer method setReferent.

/**
 * adds a Referent for all Nodes dominated or covered by outgoing Edges of AnnisNode a
 * @param n the Node
 * @param index index of the Referent
 * @param value determines wheather the refered P-Edge is incoming (1) or outgoing (0)
 */
private void setReferent(SNode n, long index, int value) {
    if (n instanceof SToken) {
        SToken tok = (SToken) n;
        if (!referentOfToken.containsKey(tok.getId())) {
            HashMap<Long, Integer> newlist = new HashMap<Long, Integer>();
            // globalindex?
            newlist.put(index, value);
            referentOfToken.put(tok.getId(), newlist);
        } else {
            referentOfToken.get(tok.getId()).put(globalIndex, value);
        }
    } else {
        List<SRelation<SNode, SNode>> outEdges = n.getGraph().getOutRelations(n.getId());
        if (outEdges != null) {
            for (SRelation<? extends SNode, ? extends SNode> edge : outEdges) {
                if (!(edge instanceof SPointingRelation)) {
                    if (edge.getSource() != null && edge.getTarget() != null) {
                        setReferent(edge.getTarget(), index, value);
                    }
                }
            }
        }
    }
}
Also used : SToken(org.corpus_tools.salt.common.SToken) SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SRelation(org.corpus_tools.salt.core.SRelation) HashMap(java.util.HashMap)

Example 22 with SToken

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

the class CorefVisualizer method outputSingleText.

private List<Node> outputSingleText(List<SToken> token, VisualizerInput input) throws IOException {
    List<Node> result = new LinkedList<Node>();
    List<Long> prevpositions, listpositions;
    List<Long> finalpositions = null;
    int maxlinkcount = 0;
    String lastId, currentId = null;
    for (SToken tok : token) {
        prevpositions = finalpositions;
        if (prevpositions != null && prevpositions.size() < 1) {
            prevpositions = null;
        }
        lastId = currentId;
        currentId = tok.getId();
        listpositions = componentOfToken.get(currentId);
        List<Boolean> checklist = null;
        if (prevpositions == null && listpositions != null) {
            finalpositions = listpositions;
        } else if (listpositions == null) {
            finalpositions = new LinkedList<Long>();
        } else {
            checklist = new LinkedList<Boolean>();
            for (int i = 0; prevpositions != null && i < prevpositions.size(); i++) {
                if (listpositions.contains(prevpositions.get(i))) {
                    checklist.add(true);
                } else {
                    checklist.add(false);
                }
            }
            List<Long> remains = new LinkedList<Long>();
            for (int i = 0; i < listpositions.size(); i++) {
                if (prevpositions != null && !prevpositions.contains(listpositions.get(i))) {
                    remains.add(listpositions.get(i));
                }
            }
            int minsize = checklist.size() + remains.size();
            int number = 0;
            finalpositions = new LinkedList<Long>();
            for (int i = 0; i < minsize; i++) {
                if (prevpositions != null && checklist.size() > i && checklist.get(i).booleanValue()) {
                    finalpositions.add(prevpositions.get(i));
                } else {
                    if (remains.size() > number) {
                        Long ll = remains.get(number);
                        finalpositions.add(ll);
                        number++;
                        minsize--;
                    } else {
                        finalpositions.add(Long.MIN_VALUE);
                    }
                }
            }
        }
        String onclick = "";
        String style = "";
        if (input.getMarkedAndCovered().containsKey(tok)) {
            MatchedNodeColors[] vals = MatchedNodeColors.values();
            long match = Math.min(input.getMarkedAndCovered().get(tok) - 1, vals.length - 1);
            style += ("color: " + vals[(int) match].getHTMLColor() + ";");
        }
        boolean underline = false;
        if (!finalpositions.isEmpty()) {
            style += "cursor:pointer;";
            underline = true;
            onclick = "togglePRAuto(this);";
        }
        Table tableSingleTok = new Table();
        result.add(tableSingleTok);
        tableSingleTok.setCSSClass("token");
        int currentlinkcount = 0;
        if (underline) {
            boolean firstone = true;
            int index = -1;
            String tooltip;
            if (!finalpositions.isEmpty()) {
                for (Long currentPositionComponent : finalpositions) {
                    index++;
                    String left = "", right = "";
                    // == pir
                    TComponent currentWriteComponent = null;
                    String currentType = "";
                    if (!currentPositionComponent.equals(Long.MIN_VALUE) && komponent.size() > currentPositionComponent) {
                        currentWriteComponent = komponent.get((int) (long) currentPositionComponent);
                        List<String> pi = currentWriteComponent.tokenList;
                        List<String> preparedPi = new LinkedList<String>();
                        for (String s : pi) {
                            preparedPi.add(prepareID(s));
                        }
                        currentType = currentWriteComponent.type;
                        left = StringUtils.join(preparedPi, ",");
                        right = "" + currentPositionComponent + 1;
                    }
                    String annotations = getAnnotations(tok.getId(), currentPositionComponent);
                    if (firstone) {
                        firstone = false;
                        if (currentWriteComponent == null) {
                            String left2 = "", right2 = "";
                            long pr = 0;
                            // == pir
                            TComponent currentWriteComponent2;
                            String currentType2 = "";
                            String annotations2 = "";
                            for (Long currentPositionComponent2 : finalpositions) {
                                if (!currentPositionComponent2.equals(Long.MIN_VALUE) && komponent.size() > currentPositionComponent2) {
                                    currentWriteComponent2 = komponent.get((int) (long) currentPositionComponent2);
                                    List<String> pi2 = currentWriteComponent2.tokenList;
                                    // prepare each single ID
                                    List<String> preparedPi2 = new LinkedList<String>();
                                    for (String s : pi2) {
                                        preparedPi2.add(prepareID(s));
                                    }
                                    currentType2 = currentWriteComponent2.type;
                                    left2 = StringUtils.join(preparedPi2, ",");
                                    right2 = "" + currentPositionComponent2 + 1;
                                    annotations2 = getAnnotations(tok.getId(), currentPositionComponent2);
                                    pr = currentPositionComponent2;
                                    break;
                                }
                            }
                            tooltip = "&lt;b&gt;Component&lt;/b&gt;: " + (pr + 1) + ", &lt;b&gt;Type&lt;/b&gt;: " + currentType2 + annotations2;
                            Tr trTok = new Tr();
                            tableSingleTok.appendChild(trTok);
                            Td tdTok = new Td();
                            trTok.appendChild(tdTok);
                            tdTok.setId("tok_" + prepareID(tok.getId()));
                            tdTok.setTitle(tooltip);
                            tdTok.setStyle(style);
                            tdTok.setAttribute("onclick", onclick);
                            tdTok.setAttribute("annis:pr_left", left2);
                            tdTok.setAttribute("annis:pr_right", right2);
                            Text textTok = new Text("&nbsp;" + CommonHelper.getSpannedText(tok) + "&nbsp;");
                            tdTok.appendChild(textTok);
                        } else {
                            // easier
                            tooltip = "&lt;b&gt;Component&lt;/b&gt;: " + (currentPositionComponent + 1) + ", &lt;b&gt;Type&lt;/b&gt; " + currentType + annotations;
                            Tr trTok = new Tr();
                            tableSingleTok.appendChild(trTok);
                            Td tdTok = new Td();
                            trTok.appendChild(tdTok);
                            tdTok.setId("tok_" + prepareID(tok.getId()));
                            tdTok.setTitle(tooltip);
                            tdTok.setStyle(style);
                            tdTok.setAttribute("onclick", onclick);
                            tdTok.setAttribute("annis:pr_left", left);
                            tdTok.setAttribute("annis:pr_right", right);
                            Text textTok = new Text("&nbsp;" + CommonHelper.getSpannedText(tok) + "&nbsp;");
                            tdTok.appendChild(textTok);
                        }
                    }
                    currentlinkcount++;
                    // while we've got underlines
                    if (currentPositionComponent.equals(Long.MIN_VALUE)) {
                        Tr trBlank = new Tr();
                        tableSingleTok.appendChild(trBlank);
                        Td tdBlank = new Td();
                        trBlank.appendChild(tdBlank);
                        trBlank.setCSSClass("line");
                    } else {
                        int color;
                        if (colorlist.containsKey((int) (long) currentPositionComponent)) {
                            color = colorlist.get((int) (long) currentPositionComponent);
                        } else {
                            color = getNewColor((int) (long) currentPositionComponent);
                            colorlist.put((int) (long) currentPositionComponent, color);
                        }
                        if (color > 16777215) {
                            color = 16777215;
                        }
                        String addition = ";border-style: solid; border-width: 0px 0px 0px 2px; border-color: white ";
                        if (lastId != null && currentId != null && checklist != null && checklist.size() > index && checklist.get(index).booleanValue() == true) {
                            if (connectionOf(lastId, currentId, currentPositionComponent)) {
                                addition = "";
                            }
                        }
                        tooltip = "&lt;b&gt;Component&lt;/b&gt;: " + (currentPositionComponent + 1) + ", &lt;b&gt;Type&lt;/b&gt;: " + currentType + annotations;
                        Tr trLineContainer = new Tr();
                        tableSingleTok.appendChild(trLineContainer);
                        Td tdLineContainer = new Td();
                        trLineContainer.appendChild(tdLineContainer);
                        Table tableLineContainer = new Table();
                        tdLineContainer.appendChild(tableLineContainer);
                        trLineContainer.setCSSClass("line");
                        tableLineContainer.setCSSClass("linecontainer");
                        Tr trLine = new Tr();
                        tableLineContainer.appendChild(trLine);
                        Td tdLine = new Td();
                        trLine.appendChild(tdLine);
                        tdLine.setCSSClass("clickableline");
                        tdLine.setStyle("background-color: #" + Integer.toHexString(color) + "; " + style + addition);
                        tdLine.setAttribute("onclick", onclick);
                        tdLine.setAttribute("annis:pr_left", left);
                        tdLine.setAttribute("annis:pr_right", right);
                        tdLine.setTitle(tooltip);
                        Tr trSpace = new Tr();
                        tableLineContainer.appendChild(trSpace);
                        Td tdSpace = new Td();
                        trSpace.appendChild(tdSpace);
                        tdSpace.setCSSClass("space");
                    }
                }
            }
            if (currentlinkcount > maxlinkcount) {
                maxlinkcount = currentlinkcount;
            } else {
                if (currentlinkcount < maxlinkcount) {
                    addEmptyLines(tableSingleTok, maxlinkcount - currentlinkcount);
                }
            }
        } else {
            // print a token without lines
            Tr trTok = new Tr();
            tableSingleTok.appendChild(trTok);
            Td tdTok = new Td();
            trTok.appendChild(tdTok);
            tdTok.setId("tok_" + prepareID(tok.getId()));
            tdTok.setStyle(style);
            Text textTok = new Text("&nbsp;" + CommonHelper.getSpannedText(tok) + "&nbsp;");
            tdTok.appendChild(textTok);
            if (maxlinkcount > 0) {
                addEmptyLines(tableSingleTok, maxlinkcount);
            }
        }
    }
    return result;
}
Also used : Table(com.hp.gagawa.java.elements.Table) SNode(org.corpus_tools.salt.core.SNode) SStructuredNode(org.corpus_tools.salt.common.SStructuredNode) Node(com.hp.gagawa.java.Node) Text(com.hp.gagawa.java.elements.Text) LinkedList(java.util.LinkedList) SToken(org.corpus_tools.salt.common.SToken) Td(com.hp.gagawa.java.elements.Td) MatchedNodeColors(annis.libgui.MatchedNodeColors) LinkedList(java.util.LinkedList) List(java.util.List) Tr(com.hp.gagawa.java.elements.Tr)

Example 23 with SToken

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

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

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

Aggregations

SToken (org.corpus_tools.salt.common.SToken)30 SNode (org.corpus_tools.salt.core.SNode)16 SRelation (org.corpus_tools.salt.core.SRelation)12 HashMap (java.util.HashMap)9 SAnnotation (org.corpus_tools.salt.core.SAnnotation)9 ArrayList (java.util.ArrayList)8 LinkedList (java.util.LinkedList)8 SSpan (org.corpus_tools.salt.common.SSpan)8 SFeature (org.corpus_tools.salt.core.SFeature)8 RelannisNodeFeature (annis.model.RelannisNodeFeature)7 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)7 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)6 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)5 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 SDocument (org.corpus_tools.salt.common.SDocument)4 Annotation (annis.model.Annotation)3 HashSet (java.util.HashSet)3 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)3 STextualDS (org.corpus_tools.salt.common.STextualDS)3