Search in sources :

Example 1 with Tr

use of com.hp.gagawa.java.elements.Tr 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 Tr

use of com.hp.gagawa.java.elements.Tr 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 3 with Tr

use of com.hp.gagawa.java.elements.Tr in project ANNIS by korpling.

the class CorefVisualizer method addEmptyLines.

private void addEmptyLines(Table parentTable, int numberOfLines) {
    for (int i = 0; i < numberOfLines; i++) {
        Tr trSpace = new Tr();
        parentTable.appendChild(trSpace);
        Td tdSpace = new Td();
        trSpace.appendChild(tdSpace);
        trSpace.setCSSClass("line");
    }
}
Also used : Td(com.hp.gagawa.java.elements.Td) Tr(com.hp.gagawa.java.elements.Tr)

Aggregations

Td (com.hp.gagawa.java.elements.Td)3 Tr (com.hp.gagawa.java.elements.Tr)3 Node (com.hp.gagawa.java.Node)2 Table (com.hp.gagawa.java.elements.Table)2 Text (com.hp.gagawa.java.elements.Text)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 SStructuredNode (org.corpus_tools.salt.common.SStructuredNode)2 SToken (org.corpus_tools.salt.common.SToken)2 SNode (org.corpus_tools.salt.core.SNode)2 MatchedNodeColors (annis.libgui.MatchedNodeColors)1 Body (com.hp.gagawa.java.elements.Body)1 Doctype (com.hp.gagawa.java.elements.Doctype)1 Head (com.hp.gagawa.java.elements.Head)1 Html (com.hp.gagawa.java.elements.Html)1 Link (com.hp.gagawa.java.elements.Link)1 Script (com.hp.gagawa.java.elements.Script)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 SDocument (org.corpus_tools.salt.common.SDocument)1