Search in sources :

Example 1 with RelannisEdgeFeature

use of annis.model.RelannisEdgeFeature 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 2 with RelannisEdgeFeature

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

the class SaltAnnotateExtractor method createNewRelation.

private SRelation createNewRelation(SDocumentGraph graph, SStructuredNode sourceNode, SNode targetNode, String relationName, String type, long componentID, SLayer layer, long pre, FastInverseMap<Long, SNode> nodeByRankID, AtomicInteger numberOfRelations) {
    SRelation rel = null;
    if (null != type) // create new relation
    {
        switch(type) {
            case "d":
                SDominanceRelation domrel = SaltFactory.createSDominanceRelation();
                // always set a name by ourself since the SDocumentGraph#basicAddRelation()
                // functions otherwise real slow
                domrel.setName("sDomRel" + numberOfRelations.incrementAndGet());
                rel = domrel;
                if (sourceNode != null && !(sourceNode instanceof SStructure)) {
                    log.debug("Mismatched source type: should be SStructure");
                    SNode oldNode = sourceNode;
                    sourceNode = recreateNode(SStructure.class, sourceNode);
                    updateMapAfterRecreatingNode(oldNode, sourceNode, nodeByRankID);
                }
                if (relationName == null || relationName.isEmpty()) {
                    // layer but has a non-empty relation name
                    if (handleArtificialDominanceRelation(graph, sourceNode, targetNode, rel, layer, componentID, pre)) {
                        // don't include this relation
                        rel = null;
                    }
                }
                break;
            case "c":
                SSpanningRelation spanrel = SaltFactory.createSSpanningRelation();
                // always set a name by ourself since the SDocumentGraph#basicAddRelation()
                // functions is real slow otherwise
                spanrel.setName("sSpanRel" + numberOfRelations.incrementAndGet());
                rel = spanrel;
                sourceNode = testAndFixNonSpan(sourceNode, nodeByRankID);
                break;
            case "p":
                SPointingRelation pointingrel = SaltFactory.createSPointingRelation();
                pointingrel.setName("sPointingRel" + numberOfRelations.incrementAndGet());
                rel = pointingrel;
                break;
            default:
                throw new IllegalArgumentException("Invalid type " + type + " for new Relation");
        }
        try {
            if (rel != null) {
                rel.setType(relationName);
                RelannisEdgeFeature featRelation = new RelannisEdgeFeature();
                featRelation.setPre(pre);
                featRelation.setComponentID(componentID);
                SFeature sfeatRelation = SaltFactory.createSFeature();
                sfeatRelation.setNamespace(ANNIS_NS);
                sfeatRelation.setName(FEAT_RELANNIS_EDGE);
                sfeatRelation.setValue(featRelation);
                rel.addFeature(sfeatRelation);
                rel.setSource(sourceNode);
                if ("c".equals(type) && !(targetNode instanceof SToken)) {
                    log.warn("invalid relation detected: target node ({}) " + "of a coverage relation (from: {}, internal id {}) was not a token", new Object[] { targetNode.getName(), sourceNode == null ? "null" : sourceNode.getName(), "" + pre });
                } else {
                    rel.setTarget(targetNode);
                    graph.addRelation(rel);
                    layer.addRelation(rel);
                }
            }
        } catch (SaltException ex) {
            log.warn("invalid relation detected", ex);
        }
    }
    return rel;
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SNode(org.corpus_tools.salt.core.SNode) SaltException(org.corpus_tools.salt.exceptions.SaltException) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) RelannisEdgeFeature(annis.model.RelannisEdgeFeature) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) SStructure(org.corpus_tools.salt.common.SStructure) SFeature(org.corpus_tools.salt.core.SFeature)

Example 3 with RelannisEdgeFeature

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

RelannisEdgeFeature (annis.model.RelannisEdgeFeature)3 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)3 SRelation (org.corpus_tools.salt.core.SRelation)3 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)2 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)2 SToken (org.corpus_tools.salt.common.SToken)2 SFeature (org.corpus_tools.salt.core.SFeature)2 SLayer (org.corpus_tools.salt.core.SLayer)2 SNode (org.corpus_tools.salt.core.SNode)2 AnnisNode (annis.model.AnnisNode)1 Annotation (annis.model.Annotation)1 AnnotationGraph (annis.model.AnnotationGraph)1 RelannisNodeFeature (annis.model.RelannisNodeFeature)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)1 SSequentialDS (org.corpus_tools.salt.common.SSequentialDS)1 SStructure (org.corpus_tools.salt.common.SStructure)1 STextualRelation (org.corpus_tools.salt.common.STextualRelation)1