use of org.corpus_tools.salt.common.SOrderRelation 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.common.SOrderRelation in project ANNIS by korpling.
the class SaltAnnotateExtractor method addOrderingRelations.
private void addOrderingRelations(SDocumentGraph graph, TreeMap<String, TreeMap<Long, String>> nodeBySegmentationPath) {
AtomicInteger numberOfSOrderRels = new AtomicInteger();
for (Map.Entry<String, TreeMap<Long, String>> e : nodeBySegmentationPath.entrySet()) {
String segName = e.getKey();
TreeMap<Long, String> nodeBySegIndex = e.getValue();
// mark the first node in the chain
if (!nodeBySegIndex.isEmpty()) {
String idOfFirstNode = nodeBySegIndex.firstEntry().getValue();
SNode firstNodeInSegChain = graph.getNode(idOfFirstNode);
if (firstNodeInSegChain != null) {
SFeature featFistSegInChain = SaltFactory.createSFeature();
featFistSegInChain.setNamespace(ANNIS_NS);
featFistSegInChain.setName(FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
featFistSegInChain.setValue(segName);
firstNodeInSegChain.addFeature(featFistSegInChain);
}
}
SStructuredNode lastNode = null;
for (String nodeID : nodeBySegIndex.values()) {
SNode nodeById = graph.getNode(nodeID);
if (nodeById instanceof SStructuredNode) {
SStructuredNode n = (SStructuredNode) nodeById;
if (lastNode != null) {
SOrderRelation orderRel = SaltFactory.createSOrderRelation();
orderRel.setSource(lastNode);
orderRel.setTarget(n);
orderRel.setType(segName);
orderRel.setName("sOrderRel" + numberOfSOrderRels.getAndIncrement());
graph.addRelation(orderRel);
}
lastNode = n;
}
}
}
}
use of org.corpus_tools.salt.common.SOrderRelation in project ANNIS by korpling.
the class TimelineReconstructor method createTokenFromSOrder.
private void createTokenFromSOrder() {
nodesToDelete.add(graph.getTextualDSs().get(0));
Map<String, SSpan> rootNodes = new HashMap<>();
// also add nodes that are are marked as start by ANNIS even if they don't have an outgoing order rel
for (SSpan n : graph.getSpans()) {
SFeature feat = n.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
if (feat != null && feat.getValue_STEXT() != null) {
rootNodes.put(feat.getValue_STEXT(), n);
} else {
// check if there is no incoming SOrderRelation but an outgoing
boolean isRoot = true;
for (SRelation<?, ?> inRel : n.getInRelations()) {
if (inRel instanceof SOrderRelation) {
isRoot = false;
break;
}
}
if (isRoot) {
for (SRelation<?, ?> outRel : n.getOutRelations()) {
if (outRel instanceof SOrderRelation) {
rootNodes.put(((SOrderRelation) outRel).getType(), n);
break;
}
}
}
}
}
// convert all root nodes to spans
for (Map.Entry<String, SSpan> rootEntry : rootNodes.entrySet()) {
SNode root = rootEntry.getValue();
String orderName = rootEntry.getKey();
convertSpanToToken((SSpan) root, orderName);
}
// traverse through all SOrderRelations in order
graph.traverse(new LinkedList<SNode>(rootNodes.values()), GRAPH_TRAVERSE_TYPE.TOP_DOWN_DEPTH_FIRST, "TimeReconstructSOrderRelations", new GraphTraverseHandler() {
@Override
public void nodeReached(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation relation, SNode fromNode, long order) {
if (relation instanceof SOrderRelation && currNode instanceof SSpan) {
String orderName = ((SOrderRelation) relation).getType();
if (fromNode != null) {
// add a space to the text
StringBuilder t = textDataByName.get(orderName);
if (t != null) {
t.append(" ");
}
}
convertSpanToToken((SSpan) currNode, orderName);
}
}
@Override
public void nodeLeft(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation relation, SNode fromNode, long order) {
}
@Override
public boolean checkConstraint(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SRelation relation, SNode currNode, long order) {
if (relation == null || relation instanceof SOrderRelation) {
return true;
} else {
return false;
}
}
});
// update the text of the TextualDSs
for (Map.Entry<String, StringBuilder> textDataEntry : textDataByName.entrySet()) {
STextualDS textDS = textsByName.get(textDataEntry.getKey());
if (textDS != null) {
textDS.setText(textDataEntry.getValue().toString());
}
}
}
Aggregations