Search in sources :

Example 26 with Annotation

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

the class DocBrowserTable method setDocNames.

/**
 * Updates the table with docnames and generate the additional columns defined
 * by the user.
 *
 * @param docs the list of documents, wrapped in the {@link Annotation} POJO
 */
void setDocNames(List<Annotation> docs) {
    container = new IndexedContainer();
    container.addContainerProperty(PROP_DOC_NAME, String.class, "n/a");
    MetaColumns metaCols = generateMetaColumns();
    for (MetaDataCol metaDatum : metaCols.visibleColumns) {
        container.addContainerProperty(metaDatum.getColName(), String.class, "n/a");
    }
    for (MetaDataCol metaDatum : metaCols.sortColumns) {
        container.addContainerProperty(metaDatum.getColName(), String.class, "n/a");
    }
    container.addContainerProperty("corpus path", String.class, "n/a");
    container.addContainerProperty("info", Button.class, null);
    container.addContainerProperty("visualizer", Panel.class, null);
    for (Annotation a : docs) {
        String doc = a.getName();
        // reverse path and delete the brackets and set a new separator:
        // corpus > ... > subcorpus > document
        List<String> pathList = a.getAnnotationPath();
        if (pathList == null) {
            pathList = new LinkedList<>();
        }
        Collections.reverse(pathList);
        String path = StringUtils.join(pathList, " > ");
        // use corpus path for row id, since it should be unique by annis db schema
        Item row = container.addItem(path);
        if (row != null) {
            row.getItemProperty(PROP_DOC_NAME).setValue(doc);
            // add the metadata columns.
            for (MetaDataCol metaDataCol : metaCols.visibleColumns) {
                String value = generateCell(a.getAnnotationPath(), metaDataCol);
                row.getItemProperty(metaDataCol.getColName()).setValue(value);
            }
            for (MetaDataCol metaDataCol : metaCols.sortColumns) {
                if (!metaCols.visibleColumns.contains(metaDataCol)) {
                    // corpusName() holds the corpus path
                    String value = generateCell(a.getAnnotationPath(), metaDataCol);
                    row.getItemProperty(metaDataCol.getColName()).setValue(value);
                }
            }
            row.getItemProperty("corpus path").setValue(path);
            row.getItemProperty("visualizer").setValue(generateVisualizerLinks(doc));
            row.getItemProperty("info").setValue(generateInfoButtonCell(doc));
        }
    }
    setContainerDataSource(container);
    Object[] metaDataColNames = new Object[metaCols.visibleColumns.size()];
    for (int i = 0; i < metaDataColNames.length; i++) {
        metaDataColNames[i] = metaCols.visibleColumns.get(i).getColName();
    }
    Object[] columnNames = ArrayUtils.addAll(ArrayUtils.addAll(new Object[] { "document name" }, metaDataColNames), new Object[] { "corpus path", "visualizer", "info" });
    setVisibleColumns(columnNames);
    for (Object colName : columnNames) {
        setColumnHeader((String) colName, (String) colName);
    }
    sortByMetaData(metaCols.sortColumns);
}
Also used : Item(com.vaadin.data.Item) IndexedContainer(com.vaadin.data.util.IndexedContainer) Annotation(annis.model.Annotation)

Example 27 with Annotation

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

the class CSVMultiTokExporter method outputText.

/**
 * Takes a match and outputs a csv-line
 *
 * @param graph
 * @param alignmc
 * @param matchNumber
 * @param out
 *
 * @throws java.io.IOException
 */
@Override
public void outputText(SDocumentGraph graph, boolean alignmc, int matchNumber, Writer out) throws IOException, IllegalArgumentException {
    // first match
    if (matchNumber == 0) {
        // output header
        List<String> headerLine = new ArrayList<>();
        for (Map.Entry<Integer, TreeSet<String>> match : annotationsForMatchedNodes.entrySet()) {
            int node_id = match.getKey();
            headerLine.add(String.valueOf(node_id) + "_id");
            headerLine.add(String.valueOf(node_id) + "_span");
            for (String annoName : match.getValue()) {
                headerLine.add(String.valueOf(node_id) + "_anno_" + annoName);
            }
        }
        for (String key : metakeys) {
            headerLine.add("meta_" + key);
        }
        out.append(StringUtils.join(headerLine, "\t"));
        out.append("\n");
    }
    // output nodes in the order of the matches
    SortedMap<Integer, String> contentLine = new TreeMap<>();
    for (SNode node : this.getMatchedNodes(graph)) {
        List<String> nodeLine = new ArrayList<>();
        // export id
        RelannisNodeFeature feats = RelannisNodeFeature.extract(node);
        nodeLine.add(String.valueOf(feats.getInternalID()));
        // export spanned text
        String span = graph.getText(node);
        if (span != null)
            nodeLine.add(graph.getText(node));
        else
            nodeLine.add("");
        // export annotations
        int node_id = node.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_MATCHEDNODE).getValue_SNUMERIC().intValue();
        for (String annoName : annotationsForMatchedNodes.get(node_id)) {
            SAnnotation anno = node.getAnnotation(annoName);
            if (anno != null) {
                nodeLine.add(anno.getValue_STEXT());
            } else
                nodeLine.add("'NULL'");
        }
        // add everything to line
        contentLine.put(node_id, StringUtils.join(nodeLine, "\t"));
    }
    out.append(StringUtils.join(contentLine.values(), "\t"));
    // TODO cache the metadata
    if (!metakeys.isEmpty()) {
        // TODO is this the best way to get the corpus name?
        String corpus_name = CommonHelper.getCorpusPath(java.net.URI.create(graph.getDocument().getId())).get(0);
        List<Annotation> asList = Helper.getMetaData(corpus_name, graph.getDocument().getName());
        for (Annotation anno : asList) {
            if (metakeys.contains(anno.getName()))
                out.append("\t" + anno.getValue());
        }
    }
    out.append("\n");
}
Also used : RelannisNodeFeature(annis.model.RelannisNodeFeature) SNode(org.corpus_tools.salt.core.SNode) SAnnotation(org.corpus_tools.salt.core.SAnnotation) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Annotation(annis.model.Annotation) SAnnotation(org.corpus_tools.salt.core.SAnnotation) TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 28 with Annotation

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

the class Helper method getMetaDataDoc.

/**
 * Retrieve the meta data for a given document of a corpus.
 *
 * @param toplevelCorpusName specifies the toplevel corpus
 * @param documentName specifies the document.
 * @return returns only the meta data for a single document.
 */
public static List<Annotation> getMetaDataDoc(String toplevelCorpusName, String documentName) {
    List<Annotation> result = new ArrayList<Annotation>();
    WebResource res = Helper.getAnnisWebResource();
    try {
        res = res.path("meta").path("doc").path(urlPathEscape.escape(toplevelCorpusName));
        res = res.path(urlPathEscape.escape(documentName));
        result = res.get(new GenericType<List<Annotation>>() {
        });
    } catch (UniformInterfaceException | ClientHandlerException ex) {
        log.error(null, ex);
        if (!AnnisBaseUI.handleCommonError(ex, "retrieve metadata")) {
            Notification.show("Remote exception: " + ex.getLocalizedMessage(), Notification.Type.WARNING_MESSAGE);
        }
    }
    return result;
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) GenericType(com.sun.jersey.api.client.GenericType) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) ArrayList(java.util.ArrayList) AsyncWebResource(com.sun.jersey.api.client.AsyncWebResource) WebResource(com.sun.jersey.api.client.WebResource) SAnnotation(org.corpus_tools.salt.core.SAnnotation) Annotation(annis.model.Annotation)

Example 29 with Annotation

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

the class LegacyGraphConverter method addRelation.

private static void addRelation(Class<? extends SRelation> clazz, String type, Collection<SAnnotation> annotations, SNode source, SNode target, Set<SLayer> relLayers, long pre, long componentID, Map<SNode, AnnisNode> allNodes, AnnotationGraph annoGraph) {
    Edge aEdge = new Edge();
    aEdge.setSource(allNodes.get(source));
    aEdge.setDestination(allNodes.get(target));
    aEdge.setEdgeType(EdgeType.UNKNOWN);
    aEdge.setPre(pre);
    aEdge.setComponentID(componentID);
    if (!relLayers.isEmpty()) {
        aEdge.setNamespace(relLayers.iterator().next().getName());
    }
    aEdge.setName(type);
    if (SDominanceRelation.class.isAssignableFrom(clazz)) {
        aEdge.setEdgeType(EdgeType.DOMINANCE);
    } else if (SPointingRelation.class.isAssignableFrom(clazz)) {
        aEdge.setEdgeType(EdgeType.POINTING_RELATION);
    } else if (SSpanningRelation.class.isAssignableFrom(clazz)) {
        aEdge.setEdgeType(EdgeType.COVERAGE);
    }
    for (SAnnotation sAnno : annotations) {
        aEdge.addAnnotation(new Annotation(sAnno.getNamespace(), sAnno.getName(), sAnno.getValue_STEXT()));
    }
    annoGraph.addEdge(aEdge);
    aEdge.getDestination().addIncomingEdge(aEdge);
    if (aEdge.getSource() != null) {
        aEdge.getSource().addOutgoingEdge(aEdge);
    }
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SAnnotation(org.corpus_tools.salt.core.SAnnotation) Edge(annis.model.Edge) Annotation(annis.model.Annotation) SAnnotation(org.corpus_tools.salt.core.SAnnotation)

Example 30 with Annotation

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

Aggregations

Annotation (annis.model.Annotation)43 ArrayList (java.util.ArrayList)13 LinkedList (java.util.LinkedList)12 HashMap (java.util.HashMap)11 AnnisNode (annis.model.AnnisNode)10 AnnotatedSpan (annis.dao.objects.AnnotatedSpan)6 Test (org.junit.Test)6 AnnotatedMatch (annis.dao.objects.AnnotatedMatch)5 List (java.util.List)5 Map (java.util.Map)5 TreeMap (java.util.TreeMap)5 SAnnotation (org.corpus_tools.salt.core.SAnnotation)5 AnnisResult (annis.service.ifaces.AnnisResult)4 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)4 WebResource (com.sun.jersey.api.client.WebResource)4 Array (java.sql.Array)4 SNode (org.corpus_tools.salt.core.SNode)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 SToken (org.corpus_tools.salt.common.SToken)3 AnnotationGraph (annis.model.AnnotationGraph)2