use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class SaltAnnotateExtractor method findExistingRelation.
private SRelation findExistingRelation(SDocumentGraph graph, SNode sourceNode, SNode targetNode, String relationName, SLayer layer) {
SRelation rel = null;
List<SRelation<SNode, SNode>> existingRelations = graph.getRelations(sourceNode.getId(), targetNode.getId());
if (existingRelations != null) {
for (Relation e : existingRelations) {
// the same layer "edge_namespace")
if (e instanceof SRelation) {
SRelation existingRel = (SRelation) e;
boolean noType = existingRel.getType() == null;
if (((noType && relationName == null) || (!noType && existingRel.getType().equals(relationName))) && existingRel.getLayers().contains(layer)) {
rel = existingRel;
break;
}
}
}
}
return rel;
}
use of org.corpus_tools.salt.core.SRelation 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;
}
use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class CommonHelper method getSortedSegmentationNodes.
/**
* Calculates a {@link SOrderRelation} node chain of a {@link SDocumentGraph}.
*
* <p>
* If no segmentation name is set, a list of sorted {@link SToken} will be
* returned.<p>
*
* @param segName The segmentation name, for which the chain is computed.
* @param graph The salt document graph, which is traversed for the
* segmentation.
*
* @return Returns a List of {@link SNode}, which is sorted by the
* {@link SOrderRelation}.
*/
public static List<SNode> getSortedSegmentationNodes(String segName, SDocumentGraph graph) {
List<SNode> token = new ArrayList<SNode>();
if (segName == null) {
// if no segmentation is given just return the sorted token list
List<SToken> unsortedToken = graph.getSortedTokenByText();
if (unsortedToken != null) {
token.addAll(unsortedToken);
}
} else {
// get the very first node of the order relation chain
Set<SNode> startNodes = new LinkedHashSet<SNode>();
for (SNode n : graph.getNodes()) {
SFeature feat = n.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
if (feat != null && segName.equalsIgnoreCase(feat.getValue_STEXT())) {
startNodes.add(n);
}
}
Set<String> alreadyAdded = new HashSet<String>();
// add all nodes on the order relation chain beginning from the start node
for (SNode s : startNodes) {
SNode current = s;
while (current != null) {
token.add(current);
List<SRelation<SNode, SNode>> out = graph.getOutRelations(current.getId());
current = null;
if (out != null) {
for (SRelation<? extends SNode, ? extends SNode> e : out) {
if (e instanceof SOrderRelation) {
current = ((SOrderRelation) e).getTarget();
if (alreadyAdded.contains(current.getId())) {
// abort if cycle detected
current = null;
} else {
alreadyAdded.add(current.getId());
}
break;
}
}
}
}
}
}
return token;
}
use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class CommonHelper method getSpannedText.
/**
* Gets the spannend/covered text for a token. This will get all
* {@link STextualRelation} edges for a {@link SToken} from the
* {@link SDocumentGraph} and calculates the appropiate substring from the
* {@link STextualDS}.
*
* @param tok The {@link SToken} which is overlapping the text sequence.
* @return An empty {@link String} object, if there is no
* {@link STextualRelation}
*/
public static String getSpannedText(SToken tok) {
SGraph graph = tok.getGraph();
List<SRelation<SNode, SNode>> edges = graph.getOutRelations(tok.getId());
for (SRelation<? extends SNode, ? extends SNode> e : edges) {
if (e instanceof STextualRelation) {
STextualRelation textRel = (STextualRelation) e;
return textRel.getTarget().getText().substring(textRel.getStart(), textRel.getEnd());
}
}
return "";
}
use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class CommonHelper method getCorpusPath.
public static List<String> getCorpusPath(SCorpusGraph corpusGraph, SDocument doc) {
final List<String> result = new LinkedList<String>();
result.add(doc.getName());
SCorpus c = corpusGraph.getCorpus(doc);
List<SNode> cAsList = new ArrayList<>();
cAsList.add(c);
corpusGraph.traverse(cAsList, GRAPH_TRAVERSE_TYPE.BOTTOM_UP_DEPTH_FIRST, "getRootCorpora", new GraphTraverseHandler() {
@Override
public void nodeReached(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation edge, SNode fromNode, long order) {
result.add(currNode.getName());
}
@Override
public void nodeLeft(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation edge, SNode fromNode, long order) {
}
@Override
public boolean checkConstraint(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SRelation edge, SNode currNode, long order) {
return true;
}
});
return result;
}
Aggregations