Search in sources :

Example 1 with SLayer

use of org.corpus_tools.salt.core.SLayer 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 2 with SLayer

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

the class PDFPageHelper method getPageFromAnnotation.

/**
 * Returns the value of page annotiation for a node. It takes the visualizer
 * mappings into account. If no mapping is used, this definition is used: {@link
 * #PAGE_NUMBER_ANNOATATION_NAME}
 */
public String getPageFromAnnotation(SNode node) {
    if (node != null && node.getAnnotations() != null) {
        Set<SLayer> layers = node.getLayers();
        String nodeNamespace = null;
        if (layers != null) {
            for (SLayer l : layers) {
                nodeNamespace = l.getName();
            }
            for (SAnnotation anno : node.getAnnotations()) {
                if ((nodeNamespace == null || input.getNamespace() == null) && getPDFPageAnnotationName().equals(anno.getName())) {
                    return anno.getValue_STEXT();
                } else if (nodeNamespace.equals(input.getNamespace()) && getPDFPageAnnotationName().equals(anno.getName())) {
                    return anno.getValue_STEXT();
                }
            }
        }
    }
    return null;
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer) SAnnotation(org.corpus_tools.salt.core.SAnnotation)

Example 3 with SLayer

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

the class ResolverProviderImpl method getResolverEntries.

@Override
public ResolverEntry[] getResolverEntries(SDocument doc) {
    HashSet<ResolverEntry> visSet = new HashSet<ResolverEntry>();
    // create a request for resolver entries
    HashSet<SingleResolverRequest> resolverRequests = new HashSet<SingleResolverRequest>();
    Set<String> nodeLayers = new HashSet<String>();
    for (SNode n : doc.getDocumentGraph().getNodes()) {
        for (SLayer layer : n.getLayers()) {
            nodeLayers.add(layer.getName());
        }
    }
    Set<String> edgeLayers = new HashSet<String>();
    for (SRelation<SNode, SNode> e : doc.getDocumentGraph().getRelations()) {
        for (SLayer layer : e.getLayers()) {
            try {
                edgeLayers.add(layer.getName());
            } catch (NullPointerException ex) {
                log.warn("NullPointerException when using Salt, was trying to get layer name", ex);
            }
        }
    }
    for (String ns : nodeLayers) {
        resolverRequests.add(new SingleResolverRequest(doc.getGraph().getRoots().get(0).getName(), ns, ResolverEntry.ElementType.node));
    }
    for (String ns : edgeLayers) {
        resolverRequests.add(new SingleResolverRequest(doc.getGraph().getRoots().get(0).getName(), ns, ResolverEntry.ElementType.edge));
    }
    // query with this resolver request and make sure it is unique
    if (cacheResolver.containsKey(resolverRequests)) {
        visSet.addAll(cacheResolver.get(resolverRequests));
    } else {
        List<ResolverEntry> resolverList = new LinkedList<ResolverEntry>();
        WebResource resResolver = Helper.getAnnisWebResource().path("query").path("resolver");
        for (SingleResolverRequest r : resolverRequests) {
            List<ResolverEntry> tmp;
            String corpusName = urlPathEscape.escape(r.getCorpusName());
            String namespace = r.getNamespace();
            String type = r.getType() == null ? null : r.getType().toString();
            if (corpusName != null && namespace != null && type != null) {
                WebResource res = resResolver.path(corpusName).path(namespace).path(type);
                try {
                    tmp = res.get(new ResolverEntryListType());
                    resolverList.addAll(tmp);
                } catch (UniformInterfaceException | ClientHandlerException ex) {
                    if (!AnnisBaseUI.handleCommonError(ex, "query resolver entries")) {
                        log.error("could not query resolver entries: " + res.toString(), ex);
                    }
                }
            }
        }
        visSet.addAll(resolverList);
        cacheResolver.put(resolverRequests, resolverList);
    }
    // sort everything
    ResolverEntry[] visArray = visSet.toArray(new ResolverEntry[visSet.size()]);
    Arrays.sort(visArray, new ResolverEntryComparator());
    return visArray;
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) SLayer(org.corpus_tools.salt.core.SLayer) SNode(org.corpus_tools.salt.core.SNode) SingleResolverRequest(annis.resolver.SingleResolverRequest) ResolverEntry(annis.resolver.ResolverEntry) WebResource(com.sun.jersey.api.client.WebResource) LinkedList(java.util.LinkedList) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) HashSet(java.util.HashSet)

Example 4 with SLayer

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

the class SaltAnnotateExtractor method handleArtificialDominanceRelation.

/**
 * In ANNIS there is a special combined dominance component which has an empty
 * name, but which should not directly be included in the Salt graph.
 *
 * This functions checks if a dominance relation with empty name has a
 * "mirror" relation which is inside the same layer and between the same nodes
 * but has an relation name. If yes the original dominance relation is an
 * artificial one. The function will return true in this case and update the
 * mirror relation to include information about the artificial dominance
 * relation.
 *
 * @param graph
 * @param rel
 * @parem layer
 * @param componentID
 * @param pre
 * @return True if the dominance relation was an artificial one
 */
private boolean handleArtificialDominanceRelation(SDocumentGraph graph, SNode source, SNode target, SRelation rel, SLayer layer, long componentID, long pre) {
    List<SRelation<SNode, SNode>> mirrorRelations = graph.getRelations(source.getId(), target.getId());
    if (mirrorRelations != null && mirrorRelations.size() > 0) {
        for (Relation mirror : mirrorRelations) {
            if (mirror != rel && mirror instanceof SRelation) {
                // check layer
                SRelation mirrorRel = (SRelation) mirror;
                Set<SLayer> mirrorLayers = mirrorRel.getLayers();
                if (mirrorLayers != null) {
                    for (SLayer mirrorLayer : mirrorLayers) {
                        if (mirrorLayer == layer) {
                            // adjust the feature of the mirror relation to include
                            // information about the artificial dominance relation
                            RelannisEdgeFeature mirrorFeat = RelannisEdgeFeature.extract(mirrorRel);
                            mirrorFeat.setArtificialDominanceComponent(componentID);
                            mirrorFeat.setArtificialDominancePre(pre);
                            mirrorRel.removeLabel(ANNIS_NS, FEAT_RELANNIS_EDGE);
                            mirrorRel.createFeature(ANNIS_NS, FEAT_RELANNIS_EDGE, mirrorFeat);
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : 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) RelannisEdgeFeature(annis.model.RelannisEdgeFeature)

Example 5 with SLayer

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

the class SaltAnnotateExtractor method mapLayer.

private void mapLayer(SStructuredNode node, SDocumentGraph graph, ResultSet resultSet) throws SQLException {
    String namespace = stringValue(resultSet, NODE_TABLE, "namespace");
    List<SLayer> layerList = graph.getLayerByName(namespace);
    SLayer layer = (layerList != null && layerList.size() > 0) ? layerList.get(0) : null;
    if (layer == null) {
        layer = SaltFactory.createSLayer();
        layer.setName(namespace);
        graph.addLayer(layer);
    }
    node.addLayer(layer);
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer)

Aggregations

SLayer (org.corpus_tools.salt.core.SLayer)11 SRelation (org.corpus_tools.salt.core.SRelation)6 LinkedList (java.util.LinkedList)4 SAnnotation (org.corpus_tools.salt.core.SAnnotation)4 SNode (org.corpus_tools.salt.core.SNode)4 RelannisNodeFeature (annis.model.RelannisNodeFeature)3 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)3 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)3 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)2 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)2 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)2 STextualRelation (org.corpus_tools.salt.common.STextualRelation)2 SToken (org.corpus_tools.salt.common.SToken)2 SFeature (org.corpus_tools.salt.core.SFeature)2 Relation (org.corpus_tools.salt.graph.Relation)2 AnnisNode (annis.model.AnnisNode)1 Annotation (annis.model.Annotation)1