Search in sources :

Example 1 with AnnisResult

use of annis.service.ifaces.AnnisResult in project ANNIS by korpling.

the class GeneralTextExporter method convertText.

public void convertText(AnnisResultSet queryResult, List<String> keys, Map<String, String> args, Writer out, int offset) throws IOException {
    Map<String, Map<String, Annotation>> metadataCache = new HashMap<>();
    List<String> metaKeys = new LinkedList<>();
    if (args.containsKey("metakeys")) {
        Iterable<String> it = Splitter.on(",").trimResults().split(args.get("metakeys"));
        for (String s : it) {
            metaKeys.add(s);
        }
    }
    int counter = 0;
    for (AnnisResult annisResult : queryResult) {
        Set<Long> matchedNodeIds = annisResult.getGraph().getMatchedNodeIds();
        counter++;
        out.append((counter + offset) + ". ");
        List<AnnisNode> tok = annisResult.getGraph().getTokens();
        for (AnnisNode annisNode : tok) {
            Long tokID = annisNode.getId();
            if (matchedNodeIds.contains(tokID)) {
                out.append("[");
                out.append(annisNode.getSpannedText());
                out.append("]");
            } else {
                out.append(annisNode.getSpannedText());
            }
            // for (Annotation annotation : annisNode.getNodeAnnotations()){
            // out.append("/"+annotation.getValue());
            // }
            out.append(" ");
        }
        out.append("\n");
        if (!metaKeys.isEmpty()) {
            String[] path = annisResult.getPath();
            appendMetaData(out, metaKeys, path[path.length - 1], annisResult.getDocumentName(), metadataCache);
        }
        out.append("\n");
    }
}
Also used : HashMap(java.util.HashMap) AnnisResult(annis.service.ifaces.AnnisResult) LinkedList(java.util.LinkedList) AnnisNode(annis.model.AnnisNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AnnisResult

use of annis.service.ifaces.AnnisResult in project ANNIS by korpling.

the class GridExporter method convertText.

@Override
public void convertText(AnnisResultSet queryResult, List<String> keys, Map<String, String> args, Writer out, int offset) throws IOException {
    Map<String, Map<String, Annotation>> metadataCache = new HashMap<>();
    boolean showNumbers = true;
    if (args.containsKey("numbers")) {
        String arg = args.get("numbers");
        if (arg.equalsIgnoreCase("false") || arg.equalsIgnoreCase("0") || arg.equalsIgnoreCase("off")) {
            showNumbers = false;
        }
    }
    List<String> metaKeys = new LinkedList<>();
    if (args.containsKey("metakeys")) {
        Iterable<String> it = Splitter.on(",").trimResults().split(args.get("metakeys"));
        for (String s : it) {
            metaKeys.add(s);
        }
    }
    int counter = 0;
    for (AnnisResult annisResult : queryResult) {
        HashMap<String, TreeMap<Long, Span>> annos = new HashMap<>();
        counter++;
        out.append((counter + offset) + ".");
        long tokenOffset = annisResult.getGraph().getTokens().get(0).getTokenIndex() - 1;
        for (AnnisNode resolveNode : annisResult.getGraph().getNodes()) {
            for (Annotation resolveAnnotation : resolveNode.getNodeAnnotations()) {
                String k = resolveAnnotation.getName();
                if (annos.get(k) == null) {
                    annos.put(k, new TreeMap<Long, Span>());
                }
                // create a separate span for every annotation
                annos.get(k).put(resolveNode.getLeftToken(), new Span(resolveNode.getLeftToken(), resolveNode.getRightToken(), resolveAnnotation.getValue()));
            }
        }
        for (String k : keys) {
            if ("tok".equals(k)) {
                out.append("\t" + k + "\t ");
                for (AnnisNode annisNode : annisResult.getGraph().getTokens()) {
                    out.append(annisNode.getSpannedText() + " ");
                }
                out.append("\n");
            } else {
                if (annos.get(k) != null) {
                    out.append("\t" + k + "\t ");
                    for (Span s : annos.get(k).values()) {
                        out.append(s.getValue());
                        if (showNumbers) {
                            long leftIndex = Math.max(1, s.getStart() - tokenOffset);
                            long rightIndex = s.getEnd() - tokenOffset;
                            out.append("[" + leftIndex + "-" + rightIndex + "]");
                        }
                        out.append(" ");
                    }
                    out.append("\n");
                }
            }
        }
        if (!metaKeys.isEmpty()) {
            String[] path = annisResult.getPath();
            super.appendMetaData(out, metaKeys, path[path.length - 1], annisResult.getDocumentName(), metadataCache);
        }
        out.append("\n\n");
    }
}
Also used : HashMap(java.util.HashMap) AnnisResult(annis.service.ifaces.AnnisResult) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) Annotation(annis.model.Annotation) AnnisNode(annis.model.AnnisNode) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 3 with AnnisResult

use of annis.service.ifaces.AnnisResult in project ANNIS by korpling.

the class TestAnnisResultImpl method getAnnotationLevelSet.

// return annotation names of non-tokens
@Test
public void getAnnotationLevelSet() {
    // add annotation data to tokens and nodes
    token3.addNodeAnnotation(new Annotation(NAMESPACE, NAME1));
    node5.addNodeAnnotation(new Annotation(NAMESPACE, NAME2));
    // wrap and test: only the annotation for node5 is returned
    AnnisResult annisResult = new AnnisResultImpl(graph);
    Set<String> annotations = annisResult.getAnnotationLevelSet();
    assertThat(annotations, size(1));
    assertThat(annotations, hasItems(AnnisNode.qName(NAMESPACE, NAME2)));
}
Also used : AnnisResult(annis.service.ifaces.AnnisResult) Annotation(annis.model.Annotation) Test(org.junit.Test)

Example 4 with AnnisResult

use of annis.service.ifaces.AnnisResult in project ANNIS by korpling.

the class TestAnnisResultImpl method getTokenAnnotationLevelSet.

// return annotation names of tokens
@Test
public void getTokenAnnotationLevelSet() {
    // add annotation data to tokens and nodes
    token3.addNodeAnnotation(new Annotation(NAMESPACE, NAME1));
    node5.addNodeAnnotation(new Annotation(NAMESPACE, NAME2));
    // wrap and test: only the annotation for token3 is returned
    AnnisResult annisResult = new AnnisResultImpl(graph);
    Set<String> tokenAnnotations = annisResult.getTokenAnnotationLevelSet();
    assertThat(tokenAnnotations, size(1));
    assertThat(tokenAnnotations, hasItems(AnnisNode.qName(NAMESPACE, NAME1)));
}
Also used : AnnisResult(annis.service.ifaces.AnnisResult) Annotation(annis.model.Annotation) Test(org.junit.Test)

Example 5 with AnnisResult

use of annis.service.ifaces.AnnisResult in project ANNIS by korpling.

the class TigerTreeVisualizer method writeOutput.

@Override
public void writeOutput(VisualizerInput input, OutputStream outstream) {
    AnnisResult result = input.getResult();
    graphtools = new AnnisGraphTools(input);
    List<AbstractImageGraphicsItem> layouts = new LinkedList<AbstractImageGraphicsItem>();
    double width = 0;
    double maxheight = 0;
    for (DirectedGraph<AnnisNode, Edge> g : graphtools.getSyntaxGraphs()) {
        if (g.getEdgeCount() > 0 && g.getVertexCount() > 0) {
            ConstituentLayouter<AbstractImageGraphicsItem> cl = new ConstituentLayouter<AbstractImageGraphicsItem>(g, getBackend(), labeler, styler, input, graphtools);
            AbstractImageGraphicsItem item = cl.createLayout(new LayoutOptions(VerticalOrientation.TOP_ROOT, AnnisGraphTools.detectLayoutDirection(result.getGraph())));
            Rectangle2D treeSize = item.getBounds();
            maxheight = Math.max(maxheight, treeSize.getHeight());
            width += treeSize.getWidth();
            layouts.add(item);
        }
    }
    BufferedImage image;
    if (width == 0 || maxheight == 0) {
        Notification.show("Can't generate tree visualization.", Notification.Type.WARNING_MESSAGE);
        image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    } else {
        image = new BufferedImage((int) (width + (layouts.size() - 1) * TREE_DISTANCE + 2 * SIDE_MARGIN), (int) (maxheight + 2 * TOP_MARGIN), BufferedImage.TYPE_INT_ARGB);
        Graphics2D canvas = createCanvas(image);
        double xOffset = SIDE_MARGIN;
        for (AbstractImageGraphicsItem item : layouts) {
            AffineTransform t = canvas.getTransform();
            Rectangle2D bounds = item.getBounds();
            canvas.translate(xOffset, TOP_MARGIN + maxheight - bounds.getHeight());
            renderTree(item, canvas);
            xOffset += bounds.getWidth() + TREE_DISTANCE;
            canvas.setTransform(t);
        }
    }
    try {
        ImageIO.write(image, "png", outstream);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AbstractImageGraphicsItem(annis.visualizers.component.tree.backends.staticimg.AbstractImageGraphicsItem) Rectangle2D(java.awt.geom.Rectangle2D) AnnisResult(annis.service.ifaces.AnnisResult) IOException(java.io.IOException) LinkedList(java.util.LinkedList) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) AnnisNode(annis.model.AnnisNode) AffineTransform(java.awt.geom.AffineTransform) Edge(annis.model.Edge)

Aggregations

AnnisResult (annis.service.ifaces.AnnisResult)10 Test (org.junit.Test)6 AnnisNode (annis.model.AnnisNode)4 Annotation (annis.model.Annotation)4 LinkedList (java.util.LinkedList)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Edge (annis.model.Edge)1 AnnisToken (annis.service.ifaces.AnnisToken)1 AbstractImageGraphicsItem (annis.visualizers.component.tree.backends.staticimg.AbstractImageGraphicsItem)1 Graphics2D (java.awt.Graphics2D)1 AffineTransform (java.awt.geom.AffineTransform)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1