Search in sources :

Example 16 with AnnisNode

use of annis.model.AnnisNode in project ANNIS by korpling.

the class AomAnnotateSqlGeneratorTest method shouldMapNode.

@Test
public void shouldMapNode() throws SQLException {
    // given
    stubNodeResultSet();
    // when
    AnnisNode actual = generator.mapNode(resultSet, tableAccessStrategy, null);
    // then
    AnnisNode expected = new AnnisNode(ID, CORPUS_REF, TEXT_REF, LEFT, RIGHT, NODE_NAMESPACE, NODE_NAME, TOKEN_INDEX, SPAN, LEFT_TOKEN, RIGHT_TOKEN);
    assertThat(actual, is(expected));
}
Also used : AnnisNode(annis.model.AnnisNode) Test(org.junit.Test)

Example 17 with AnnisNode

use of annis.model.AnnisNode 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)

Example 18 with AnnisNode

use of annis.model.AnnisNode in project ANNIS by korpling.

the class TokenExporter 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<>();
    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();
            super.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) Annotation(annis.model.Annotation) AnnisNode(annis.model.AnnisNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with AnnisNode

use of annis.model.AnnisNode in project ANNIS by korpling.

the class LegacyGraphConverter method convertToAnnotationGraph.

public static AnnotationGraph convertToAnnotationGraph(SDocumentGraph docGraph, List<Long> matchedNodeIDs) {
    Set<Long> matchSet = new HashSet<>(matchedNodeIDs);
    AnnotationGraph annoGraph = new AnnotationGraph();
    List<String> pathList = CommonHelper.getCorpusPath(docGraph.getDocument().getGraph(), docGraph.getDocument());
    annoGraph.setPath(pathList.toArray(new String[pathList.size()]));
    annoGraph.setDocumentName(docGraph.getDocument().getName());
    Map<SNode, AnnisNode> allNodes = new HashMap<>();
    for (SNode sNode : docGraph.getNodes()) {
        SFeature featNodeRaw = sNode.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE));
        if (featNodeRaw != null) {
            RelannisNodeFeature featNode = (RelannisNodeFeature) featNodeRaw.getValue();
            long internalID = featNode.getInternalID();
            AnnisNode aNode = new AnnisNode(internalID);
            for (SAnnotation sAnno : sNode.getAnnotations()) {
                aNode.addNodeAnnotation(new Annotation(sAnno.getNamespace(), sAnno.getName(), sAnno.getValue_STEXT()));
            }
            aNode.setName(sNode.getName());
            Set<SLayer> layers = sNode.getLayers();
            if (!layers.isEmpty()) {
                aNode.setNamespace(layers.iterator().next().getName());
            }
            RelannisNodeFeature feat = (RelannisNodeFeature) sNode.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE)).getValue();
            if (sNode instanceof SToken) {
                List<DataSourceSequence> seqList = docGraph.getOverlappedDataSourceSequence(sNode, SALT_TYPE.STEXT_OVERLAPPING_RELATION);
                if (seqList != null) {
                    DataSourceSequence seq = seqList.get(0);
                    Preconditions.checkNotNull(seq, "DataSourceSequence is null for token %s", sNode.getId());
                    SSequentialDS seqDS = seq.getDataSource();
                    Preconditions.checkNotNull(seqDS, "SSequentalDS is null for token %s", sNode.getId());
                    Preconditions.checkNotNull(seqDS.getData(), "SSequentalDS data is null for token %s", sNode.getId());
                    String seqDSData = (String) seqDS.getData();
                    Preconditions.checkNotNull(seqDSData, "casted SSequentalDS data is null for token %s", sNode.getId());
                    Preconditions.checkNotNull(seq.getStart(), "SSequentalDS start is null for token %s", sNode.getId());
                    Preconditions.checkNotNull(seq.getEnd(), "SSequentalDS end is null for supposed token %s", sNode.getId());
                    int start = seq.getStart().intValue();
                    int end = seq.getEnd().intValue();
                    Preconditions.checkState(start >= 0 && start <= end && end <= seqDSData.length(), "Illegal start or end of textual DS for token (start %s, end: %s)", sNode.getId(), start, end);
                    String spannedText = seqDSData.substring(start, end);
                    Preconditions.checkNotNull(spannedText, "spanned text is null for supposed token %s (start: %s, end: %s)", sNode.getId(), start, end);
                    aNode.setSpannedText(spannedText);
                    aNode.setToken(true);
                    aNode.setTokenIndex(feat.getTokenIndex());
                }
            } else {
                aNode.setToken(false);
                aNode.setTokenIndex(null);
            }
            aNode.setCorpus(feat.getCorpusRef());
            aNode.setTextId(feat.getTextRef());
            aNode.setLeft(feat.getLeft());
            aNode.setLeftToken(feat.getLeftToken());
            aNode.setRight(feat.getRight());
            aNode.setRightToken(feat.getRightToken());
            if (matchSet.contains(aNode.getId())) {
                aNode.setMatchedNodeInQuery((long) matchedNodeIDs.indexOf(aNode.getId()) + 1);
                annoGraph.getMatchedNodeIds().add(aNode.getId());
            } else {
                aNode.setMatchedNodeInQuery(null);
            }
            annoGraph.addNode(aNode);
            allNodes.put(sNode, aNode);
        }
    }
    for (SRelation rel : docGraph.getRelations()) {
        RelannisEdgeFeature featRelation = RelannisEdgeFeature.extract(rel);
        if (featRelation != null) {
            addRelation(rel, featRelation.getPre(), featRelation.getComponentID(), allNodes, annoGraph);
        }
    }
    // add relations with empty relation name for every dominance relation
    List<SDominanceRelation> dominanceRelations = new LinkedList<>(docGraph.getDominanceRelations());
    for (SDominanceRelation rel : dominanceRelations) {
        RelannisEdgeFeature featEdge = RelannisEdgeFeature.extract(rel);
        if (featEdge != null && featEdge.getArtificialDominanceComponent() != null && featEdge.getArtificialDominancePre() != null) {
            addRelation(SDominanceRelation.class, null, rel.getAnnotations(), rel.getSource(), rel.getTarget(), rel.getLayers(), featEdge.getArtificialDominancePre(), featEdge.getArtificialDominanceComponent(), allNodes, annoGraph);
        }
    }
    return annoGraph;
}
Also used : SLayer(org.corpus_tools.salt.core.SLayer) SNode(org.corpus_tools.salt.core.SNode) HashMap(java.util.HashMap) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) RelannisEdgeFeature(annis.model.RelannisEdgeFeature) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) HashSet(java.util.HashSet) RelannisNodeFeature(annis.model.RelannisNodeFeature) SAnnotation(org.corpus_tools.salt.core.SAnnotation) SSequentialDS(org.corpus_tools.salt.common.SSequentialDS) DataSourceSequence(org.corpus_tools.salt.util.DataSourceSequence) Annotation(annis.model.Annotation) SAnnotation(org.corpus_tools.salt.core.SAnnotation) LinkedList(java.util.LinkedList) AnnotationGraph(annis.model.AnnotationGraph) AnnisNode(annis.model.AnnisNode) SFeature(org.corpus_tools.salt.core.SFeature)

Example 20 with AnnisNode

use of annis.model.AnnisNode in project ANNIS by korpling.

the class AnnisResultImpl method getTokenList.

public List<AnnisToken> getTokenList() {
    List<AnnisToken> result = new ArrayList<AnnisToken>();
    for (AnnisNode node : graph.getTokens()) {
        AnnisTokenImpl annisToken = new AnnisTokenImpl(node.getId(), node.getSpannedText(), node.getLeft(), node.getRight(), node.getTokenIndex(), node.getCorpus());
        for (Annotation annotation : node.getNodeAnnotations()) {
            annisToken.put(annotation.getQualifiedName(), annotation.getValue());
        }
        result.add(annisToken);
    }
    return result;
}
Also used : AnnisToken(annis.service.ifaces.AnnisToken) ArrayList(java.util.ArrayList) AnnisNode(annis.model.AnnisNode) Annotation(annis.model.Annotation)

Aggregations

AnnisNode (annis.model.AnnisNode)38 Annotation (annis.model.Annotation)10 Edge (annis.model.Edge)9 LinkedList (java.util.LinkedList)8 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Map (java.util.Map)5 AnnotationGraph (annis.model.AnnotationGraph)4 AnnisResult (annis.service.ifaces.AnnisResult)4 DetectHoles (annis.visualizers.iframe.partitur.DetectHoles)2 DocumentNameMapRow (annis.dao.DocumentNameMapRow)1 MatchedNodeColors (annis.libgui.MatchedNodeColors)1 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)1 RelannisNodeFeature (annis.model.RelannisNodeFeature)1 AnnisToken (annis.service.ifaces.AnnisToken)1 Match (annis.service.objects.Match)1 MatchGroup (annis.service.objects.MatchGroup)1 ArrayCorpusPathExtractor (annis.sqlgen.ArrayCorpusPathExtractor)1 CorpusPathExtractor (annis.sqlgen.CorpusPathExtractor)1