Search in sources :

Example 1 with SNode

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

the class CorefVisualizer method writeOutput.

/**
 * writes Output for the CorefVisualizer
 * @param writer writer to write with
 */
@Override
public void writeOutput(VisualizerInput input, Writer w) {
    // root html element
    Html html = new Html();
    Head head = new Head();
    Body body = new Body();
    html.removeXmlns();
    html.appendChild(head);
    html.appendChild(body);
    try {
        LinkedList<String> fonts = new LinkedList<String>();
        if (input.getFont() != null) {
            Link linkFont = new Link();
            linkFont.setHref(input.getFont().getUrl());
            head.appendChild(linkFont);
            fonts.add(input.getFont().getName());
        }
        fonts.add("serif");
        Link linkJQueryUI = new Link();
        linkJQueryUI.setHref(input.getResourcePath("coref/jquery-ui-1.11.4.custom/jquery-ui.min.css"));
        linkJQueryUI.setRel("stylesheet");
        linkJQueryUI.setType("text/css");
        head.appendChild(linkJQueryUI);
        Link linkJQueryUIStructure = new Link();
        linkJQueryUIStructure.setHref(input.getResourcePath("coref/jquery-ui-1.11.4.custom/jquery-ui.structure.min.css"));
        linkJQueryUIStructure.setRel("stylesheet");
        linkJQueryUIStructure.setType("text/css");
        head.appendChild(linkJQueryUIStructure);
        Script scriptJquery = new Script("text/javascript");
        scriptJquery.setSrc(input.getResourcePath("coref/jquery.js"));
        head.appendChild(scriptJquery);
        Script scriptUI = new Script("text/javascript");
        scriptUI.setSrc(input.getResourcePath("coref/jquery-ui-1.11.4.custom/jquery-ui.min.js"));
        head.appendChild(scriptUI);
        Link linkCoref = new Link();
        linkCoref.setHref(input.getResourcePath("coref/coref.css"));
        linkCoref.setRel("stylesheet");
        linkCoref.setType("text/css");
        head.appendChild(linkCoref);
        Script scriptCoref = new Script("text/javascript");
        scriptCoref.setSrc(input.getResourcePath("coref/CorefVisualizer.js"));
        head.appendChild(scriptCoref);
        body.setStyle("font-family: '" + StringUtils.join(fonts, "', '") + "';");
        // get Info
        globalIndex = 0;
        tokensOfNode = new HashMap<String, List<String>>();
        referentList = new LinkedList<TReferent>();
        komponent = new LinkedList<TComponent>();
        referentOfToken = new HashMap<String, HashMap<Long, Integer>>();
        componentOfToken = new HashMap<String, List<Long>>();
        componenttype = new LinkedList<TComponenttype>();
        SDocument saltDoc = input.getDocument();
        SDocumentGraph saltGraph = saltDoc.getDocumentGraph();
        if (saltGraph == null) {
            body.setText("An Error occured: Could not get Graph of Result (Graph == null).");
            return;
        }
        List<SRelation<SNode, SNode>> edgeList = saltGraph.getRelations();
        if (edgeList == null) {
            return;
        }
        for (SRelation rawRel : edgeList) {
            if (includeEdge(rawRel, input.getNamespace())) {
                SPointingRelation rel = (SPointingRelation) rawRel;
                String relType = componentNameForRelation(rel);
                visitedNodes = new LinkedList<String>();
                // got type for this?
                boolean gotIt = false;
                int componentnr;
                for (componentnr = 0; componentnr < componenttype.size(); componentnr++) {
                    if (componenttype.get(componentnr) != null && componenttype.get(componentnr).type != null && componenttype.get(componentnr).nodeList != null && componenttype.get(componentnr).type.equals(relType) && componenttype.get(componentnr).nodeList.contains(rel.getSource().getId())) {
                        gotIt = true;
                        break;
                    }
                }
                TComponent currentComponent;
                TComponenttype currentComponenttype;
                if (gotIt) {
                    currentComponent = komponent.get(componentnr);
                    currentComponenttype = componenttype.get(componentnr);
                } else {
                    currentComponenttype = new TComponenttype();
                    currentComponenttype.type = relType;
                    componenttype.add(currentComponenttype);
                    componentnr = komponent.size();
                    currentComponent = new TComponent();
                    currentComponent.type = relType;
                    currentComponent.tokenList = new LinkedList<String>();
                    komponent.add(currentComponent);
                    currentComponenttype.nodeList.add(rel.getSource().getId());
                }
                TReferent ref = new TReferent();
                ref.annotations = new HashSet<SerializableAnnotation>();
                for (SAnnotation anno : rel.getAnnotations()) {
                    ref.annotations.add(new SerializableAnnotation(anno));
                }
                ref.component = componentnr;
                referentList.add(ref);
                List<String> currentTokens = getAllTokens(rel.getSource(), componentNameForRelation(rel), currentComponenttype, componentnr, input.getNamespace());
                // neu
                setReferent(rel.getTarget(), globalIndex, 0);
                // neu
                setReferent(rel.getSource(), globalIndex, 1);
                for (String s : currentTokens) {
                    if (!currentComponent.tokenList.contains(s)) {
                        currentComponent.tokenList.add(s);
                    }
                }
                globalIndex++;
            }
        }
        colorlist = new HashMap<Integer, Integer>();
        // A list containing all the generated HTML elements, one list entry
        // for each text.
        List<List<Node>> nodesPerText = new LinkedList<List<Node>>();
        // write output for each text separatly
        List<STextualDS> texts = saltGraph.getTextualDSs();
        if (texts != null && !texts.isEmpty()) {
            for (STextualDS t : texts) {
                DataSourceSequence<Integer> sequence = new DataSourceSequence<>(t, 0, (t.getText() != null) ? t.getText().length() : 0);
                List<SToken> token = saltGraph.getSortedTokenByText(saltGraph.getTokensBySequence(sequence));
                if (token != null) {
                    boolean validText = true;
                    if (Boolean.parseBoolean(input.getMappings().getProperty("hide_empty", "false"))) {
                        validText = false;
                        // check if the text contains any matching annotations
                        for (SToken tok : token) {
                            /* 
                 * The token is only added to this map if an valid edge
                 * (according to the resolver trigger) conntected to 
                 * this token was found.
                 */
                            if (referentOfToken.get(tok.getId()) != null && !referentOfToken.get(tok.getId()).isEmpty()) {
                                validText = true;
                                break;
                            }
                        }
                    }
                    if (validText) {
                        List<Node> nodes = outputSingleText(token, input);
                        nodesPerText.add(nodes);
                    }
                }
            }
            // end for each STexutalDS
            /* 
         * Append the generated output to the body, wrap in table if necessary. 
         */
            // present all texts as columns side by side if using multiple texts
            Table tableTexts = new Table();
            Tr trTextRow = new Tr();
            trTextRow.setCSSClass("textRow");
            // only append wrapper table if we have multiple texts
            if (nodesPerText.size() > 1) {
                body.appendChild(tableTexts);
                tableTexts.appendChild(trTextRow);
            }
            for (List<Node> nodes : nodesPerText) {
                // multi-text mode?
                if (nodesPerText.size() > 1) {
                    Td tdSingleText = new Td();
                    trTextRow.appendChild(tdSingleText);
                    tdSingleText.setCSSClass("text");
                    tdSingleText.appendChild(nodes);
                } else {
                    body.appendChild(nodes);
                }
            }
        } else {
            Text errorTxt = new Text("Could not find any texts for the " + input.getNamespace() + " node namespace (layer).");
            body.appendChild(errorTxt);
        }
        // write HTML4 transitional doctype
        w.append(new Doctype(DocumentType.HTMLTransitional).write());
        // append the html tree
        w.append(html.write());
    } catch (IOException ex) {
        log.error(null, ex);
    }
}
Also used : HashMap(java.util.HashMap) SNode(org.corpus_tools.salt.core.SNode) SStructuredNode(org.corpus_tools.salt.common.SStructuredNode) Node(com.hp.gagawa.java.Node) SToken(org.corpus_tools.salt.common.SToken) Td(com.hp.gagawa.java.elements.Td) SRelation(org.corpus_tools.salt.core.SRelation) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) STextualDS(org.corpus_tools.salt.common.STextualDS) LinkedList(java.util.LinkedList) List(java.util.List) Body(com.hp.gagawa.java.elements.Body) Tr(com.hp.gagawa.java.elements.Tr) SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) Script(com.hp.gagawa.java.elements.Script) Head(com.hp.gagawa.java.elements.Head) Table(com.hp.gagawa.java.elements.Table) SAnnotation(org.corpus_tools.salt.core.SAnnotation) Html(com.hp.gagawa.java.elements.Html) SDocument(org.corpus_tools.salt.common.SDocument) Doctype(com.hp.gagawa.java.elements.Doctype) Text(com.hp.gagawa.java.elements.Text) IOException(java.io.IOException) DataSourceSequence(org.corpus_tools.salt.util.DataSourceSequence) LinkedList(java.util.LinkedList) Link(com.hp.gagawa.java.elements.Link)

Example 2 with SNode

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

the class VakyarthaDependencyTree method printHTMLOutput.

public void printHTMLOutput(VisualizerInput input, Writer writer, Map<SNode, Integer> selectedNodes) {
    SDocumentGraph sDocumentGraph = input.getSResult().getDocumentGraph();
    for (SNode n : sDocumentGraph.getNodes()) {
        if (selectNode(n)) {
            RelannisNodeFeature feat = (RelannisNodeFeature) n.getFeature(ANNIS_NS, FEAT_RELANNIS_NODE).getValue();
            int tokenIdx = feat != null ? (int) feat.getTokenIndex() : -1;
            selectedNodes.put(n, tokenIdx);
        }
    }
    Map<SNode, Integer> node2Int = new HashMap<SNode, Integer>();
    int count = 0;
    for (SNode tok : selectedNodes.keySet()) {
        node2Int.put(tok, count++);
    }
    try {
        println("<html>", writer);
        println("<head>", writer);
        LinkedList<String> fontsText = new LinkedList<String>();
        LinkedList<String> fontsDep = new LinkedList<String>();
        if (input.getFont() != null) {
            fontsText.add(input.getFont().getName());
            fontsDep.add(input.getFont().getName());
            println("<link href=\"" + input.getFont().getUrl() + "\" rel=\"stylesheet\" type=\"text/css\" >", writer);
        }
        fontsText.add("sans-serif");
        fontsDep.add("serif");
        println("<script type=\"text/javascript\" src=\"" + input.getResourcePath("vakyartha/jquery.js") + "\"></script>", writer);
        println("<script type=\"text/javascript\" src=\"" + input.getResourcePath("vakyartha/raphael-min.js") + "\"></script>", writer);
        println("<script type=\"text/javascript\" src=\"" + input.getResourcePath("vakyartha/vakyarthaDependency.js") + "\"></script>", writer);
        // output the data for the javascript
        println("<script type=\"text/javascript\">", writer);
        println("fcolors={};", writer);
        println("shownfeatures=[\"t\"];", writer);
        println("tokens=new Object();", writer);
        count = 0;
        for (SNode node : selectedNodes.keySet()) {
            JSONObject vakyarthaObject = new JSONObject();
            String completeAnnotation = getAnnotation(node);
            String annotationValue = completeAnnotation.replaceFirst(".*=", "");
            String text = getText(node, input);
            // decide, if the visualization is token based.
            if (mappings.containsKey(MAPPING_NODE_KEY)) {
                vakyarthaObject.put("t", annotationValue);
            } else {
                vakyarthaObject.put("t", text);
            }
            vakyarthaObject.put("annotation", annotationValue);
            vakyarthaObject.put("text", text);
            vakyarthaObject.put("tooltip", completeAnnotation);
            JSONObject govs = new JSONObject();
            List<SRelation<SNode, SNode>> sEdges = node.getGraph().getInRelations(node.getId());
            for (SRelation<? extends SNode, ? extends SNode> e : sEdges) {
                if (e instanceof SPointingRelation) {
                    SPointingRelation sRelation = (SPointingRelation) e;
                    boolean includeEdge = true;
                    // check layer
                    if (input.getNamespace() != null) {
                        // must be included in the layer in order to be included
                        includeEdge = false;
                        if (sRelation.getLayers() != null) {
                            for (SLayer layer : sRelation.getLayers()) {
                                if (input.getNamespace().equals(layer.getName())) {
                                    includeEdge = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (includeEdge) {
                        SNode source = (SNode) sRelation.getSource();
                        String label = "";
                        for (SAnnotation anno : sRelation.getAnnotations()) {
                            label = anno.getValue_STEXT();
                            break;
                        }
                        if (sRelation.getSource() != null && node2Int.containsKey(source)) {
                            govs.put(String.valueOf(node2Int.get(source)), label);
                        }
                    }
                }
            // end if pointing relation
            }
            vakyarthaObject.put("govs", govs);
            JSONObject attris = new JSONObject();
            JSONObject tAttris = new JSONObject();
            String tokenColor = "black";
            if (input.getMarkedAndCovered().containsKey(node)) {
                tokenColor = MatchedNodeColors.getHTMLColorByMatch(input.getMarkedAndCovered().get(node));
            }
            tAttris.put("fill", tokenColor);
            tAttris.put("font", "11px " + StringUtils.join(fontsText, ","));
            attris.put("t", tAttris);
            JSONObject depAttris = new JSONObject();
            depAttris.put("fill", "#999");
            depAttris.put("font-style", "italic");
            depAttris.put("font", "12px " + StringUtils.join(fontsDep, ","));
            attris.put("deptext", depAttris);
            vakyarthaObject.put("attris", attris);
            writer.append("tokens[").append("" + count++).append("]=");
            writer.append(vakyarthaObject.toString().replaceAll("\n", " "));
            writer.append(";\n");
        }
        println("</script>", writer);
        println("</head>", writer);
        println("<body id=\"holder\">", writer);
        // the div to render the javascript to
        // println(
        // "<div id=\"holder\"> </div>",
        // writer);
        println("</body>", writer);
        println("</html>", writer);
    } catch (JSONException ex) {
        log.error(null, ex);
    } catch (IOException ex) {
        log.error(null, ex);
    }
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) RelannisNodeFeature(annis.model.RelannisNodeFeature) SLayer(org.corpus_tools.salt.core.SLayer) SNode(org.corpus_tools.salt.core.SNode) HashMap(java.util.HashMap) SAnnotation(org.corpus_tools.salt.core.SAnnotation) JSONException(org.json.JSONException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) SRelation(org.corpus_tools.salt.core.SRelation) JSONObject(org.json.JSONObject) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph)

Example 3 with SNode

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

the class QueryDaoImpl method exportCorpus.

@Override
@Transactional(readOnly = true)
public void exportCorpus(String toplevelCorpus, File outputDirectory) {
    // check if the corpus really exists
    mapCorpusNameToId(toplevelCorpus);
    SaltProject corpusProject = SaltFactory.createSaltProject();
    SCorpusGraph corpusGraph = SaltFactory.createSCorpusGraph();
    corpusGraph.setSaltProject(corpusProject);
    SCorpus rootCorpus = corpusGraph.createCorpus(null, toplevelCorpus);
    // add all root metadata
    for (Annotation metaAnno : listCorpusAnnotations(toplevelCorpus)) {
        rootCorpus.createMetaAnnotation(metaAnno.getNamespace(), metaAnno.getName(), metaAnno.getValue());
    }
    File documentRootDir = new File(outputDirectory, toplevelCorpus);
    if (!outputDirectory.exists()) {
        if (!outputDirectory.mkdirs()) {
            log.warn("Could not create output directory \"{}\" for exporting the corpus", outputDirectory.getAbsolutePath());
        }
    }
    List<Annotation> docs = listDocuments(toplevelCorpus);
    int i = 1;
    for (Annotation docAnno : docs) {
        log.info("Loading document {} from database ({}/{})", docAnno.getName(), i, docs.size());
        SaltProject docProject = retrieveAnnotationGraph(toplevelCorpus, docAnno.getName(), null);
        if (docProject != null && docProject.getCorpusGraphs() != null && !docProject.getCorpusGraphs().isEmpty()) {
            List<Annotation> docMetaData = listCorpusAnnotations(toplevelCorpus, docAnno.getName(), true);
            SCorpusGraph docCorpusGraph = docProject.getCorpusGraphs().get(0);
            // TODO: we could re-use the actual corpus structure instead of just adding a flat list of documents
            if (docCorpusGraph.getDocuments() != null) {
                for (SDocument doc : docCorpusGraph.getDocuments()) {
                    log.info("Removing SFeatures from {} ({}/{})", docAnno.getName(), i, docs.size());
                    // remove all ANNIS specific features that require a special Java class
                    SDocumentGraph graph = doc.getDocumentGraph();
                    if (graph != null) {
                        if (graph.getNodes() != null) {
                            for (SNode n : graph.getNodes()) {
                                n.removeLabel(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_RELANNIS_NODE);
                            }
                        }
                        if (graph.getRelations() != null) {
                            for (SRelation e : graph.getRelations()) {
                                e.removeLabel(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_RELANNIS_EDGE);
                            }
                        }
                    }
                    log.info("Saving document {} ({}/{})", doc.getName(), i, docs.size());
                    SaltUtil.saveDocumentGraph(graph, URI.createFileURI(new File(documentRootDir, doc.getName() + "." + SaltUtil.FILE_ENDING_SALT_XML).getAbsolutePath()));
                    SDocument docCopy = corpusGraph.createDocument(rootCorpus, doc.getName());
                    log.info("Adding metadata to document {} ({}/{})", doc.getName(), i, docs.size());
                    for (Annotation metaAnno : docMetaData) {
                        docCopy.createMetaAnnotation(metaAnno.getNamespace(), metaAnno.getName(), metaAnno.getValue());
                    }
                }
            }
        }
        i++;
    }
    // end for each document
    // save the actual SaltProject
    log.info("Saving corpus structure");
    File projectFile = new File(outputDirectory, SaltUtil.FILE_SALT_PROJECT);
    SaltXML10Writer writer = new SaltXML10Writer(projectFile);
    writer.writeSaltProject(corpusProject);
}
Also used : SCorpus(org.corpus_tools.salt.common.SCorpus) SRelation(org.corpus_tools.salt.core.SRelation) SNode(org.corpus_tools.salt.core.SNode) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SDocument(org.corpus_tools.salt.common.SDocument) SaltXML10Writer(org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer) SaltProject(org.corpus_tools.salt.common.SaltProject) File(java.io.File) Annotation(annis.model.Annotation) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with SNode

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

the class Helper method addMatchToDocumentGraph.

public static void addMatchToDocumentGraph(Match match, SDocument document) {
    List<String> allUrisAsString = new LinkedList<>();
    long i = 1;
    for (URI u : match.getSaltIDs()) {
        allUrisAsString.add(u.toASCIIString());
        SNode matchedNode = document.getDocumentGraph().getNode(u.toASCIIString());
        // set the feature for this specific node
        if (matchedNode != null) {
            SFeature existing = matchedNode.getFeature(ANNIS_NS, FEAT_MATCHEDNODE);
            if (existing == null) {
                SFeature featMatchedNode = SaltFactory.createSFeature();
                featMatchedNode.setNamespace(ANNIS_NS);
                featMatchedNode.setName(FEAT_MATCHEDNODE);
                featMatchedNode.setValue(i);
                matchedNode.addFeature(featMatchedNode);
            }
        }
        i++;
    }
    SFeature featIDs = SaltFactory.createSFeature();
    featIDs.setNamespace(ANNIS_NS);
    featIDs.setName(FEAT_MATCHEDIDS);
    featIDs.setValue(Joiner.on(",").join(allUrisAsString));
    document.addFeature(featIDs);
    SFeature featAnnos = SaltFactory.createSFeature();
    featAnnos.setNamespace(ANNIS_NS);
    featAnnos.setName(FEAT_MATCHEDANNOS);
    featAnnos.setValue(Joiner.on(",").join(match.getAnnos()));
    document.addFeature(featAnnos);
}
Also used : SNode(org.corpus_tools.salt.core.SNode) URI(java.net.URI) LinkedList(java.util.LinkedList) SFeature(org.corpus_tools.salt.core.SFeature)

Example 5 with SNode

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

the class Helper method calculateColorsForMarkedExact.

public static Map<String, String> calculateColorsForMarkedExact(SDocument result) {
    Map<String, String> markedExactMap = new HashMap<>();
    if (result != null) {
        SDocumentGraph g = result.getDocumentGraph();
        if (g != null) {
            for (SNode n : result.getDocumentGraph().getNodes()) {
                SFeature featMatched = n.getFeature(ANNIS_NS, FEAT_MATCHEDNODE);
                Long matchNum = featMatched == null ? null : featMatched.getValue_SNUMERIC();
                if (matchNum != null) {
                    int color = Math.max(0, Math.min((int) matchNum.longValue() - 1, MatchedNodeColors.values().length - 1));
                    RelannisNodeFeature feat = RelannisNodeFeature.extract(n);
                    if (feat != null) {
                        markedExactMap.put("" + feat.getInternalID(), MatchedNodeColors.values()[color].name());
                    }
                }
            }
        }
    // end if g not null
    }
    // end if result not null
    return markedExactMap;
}
Also used : RelannisNodeFeature(annis.model.RelannisNodeFeature) SNode(org.corpus_tools.salt.core.SNode) HashMap(java.util.HashMap) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SFeature(org.corpus_tools.salt.core.SFeature)

Aggregations

SNode (org.corpus_tools.salt.core.SNode)37 SToken (org.corpus_tools.salt.common.SToken)15 SRelation (org.corpus_tools.salt.core.SRelation)14 ArrayList (java.util.ArrayList)13 LinkedList (java.util.LinkedList)12 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)12 SFeature (org.corpus_tools.salt.core.SFeature)12 RelannisNodeFeature (annis.model.RelannisNodeFeature)11 HashMap (java.util.HashMap)11 SAnnotation (org.corpus_tools.salt.core.SAnnotation)8 Map (java.util.Map)6 SDocument (org.corpus_tools.salt.common.SDocument)6 HashSet (java.util.HashSet)5 SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)5 Annotation (annis.model.Annotation)4 TreeMap (java.util.TreeMap)4 SCorpus (org.corpus_tools.salt.common.SCorpus)4 SaltProject (org.corpus_tools.salt.common.SaltProject)4 SLayer (org.corpus_tools.salt.core.SLayer)4 JSONException (org.json.JSONException)4