use of org.corpus_tools.salt.core.SNode in project ANNIS by korpling.
the class CSVMultiTokExporter method createAdjacencyMatrix.
/**
* Takes a match and stores annotation names to construct the header in
* {@link #outputText(de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDocumentGraph, boolean, int, java.io.Writer) }
*
* @param graph
* @param args
* @param matchNumber
* @param nodeCount
*
* @throws java.io.IOException
*/
@Override
public void createAdjacencyMatrix(SDocumentGraph graph, Map<String, String> args, int matchNumber, int nodeCount) throws IOException, IllegalArgumentException {
// first match
if (matchNumber == 0) {
// get list of metakeys to export
metakeys = new HashSet<>();
if (args.containsKey("metakeys")) {
metakeys.addAll(Arrays.asList(args.get("metakeys").split(",")));
}
// initialize list of annotations for the matched nodes
annotationsForMatchedNodes = new TreeMap<>();
}
for (SNode node : this.getMatchedNodes(graph)) {
int node_id = node.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_MATCHEDNODE).getValue_SNUMERIC().intValue();
if (!annotationsForMatchedNodes.containsKey(node_id))
annotationsForMatchedNodes.put(node_id, new TreeSet<String>());
List<SAnnotation> annots = new ArrayList<>(node.getAnnotations());
Set<String> annoNames = annotationsForMatchedNodes.get(node_id);
for (SAnnotation annot : annots) {
annoNames.add(annot.getNamespace() + "::" + annot.getName());
}
}
}
use of org.corpus_tools.salt.core.SNode 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.SNode 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;
}
use of org.corpus_tools.salt.core.SNode in project ANNIS by korpling.
the class TimelineReconstructor method cleanup.
private void cleanup() {
for (SNode node : nodesToDelete) {
graph.removeNode(node);
}
// update the feature matchedIDs
SFeature matchids = graph.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_MATCHEDIDS);
if (matchids != null) {
String[] ids = matchids.getValue_STEXT().split(",");
for (int i = 0; i < ids.length; i++) {
if (this.oldID2newID.containsKey(ids[i]))
ids[i] = this.oldID2newID.get(ids[i]);
}
matchids.setValue(Joiner.on(',').join(ids));
}
}
use of org.corpus_tools.salt.core.SNode in project ANNIS by korpling.
the class SingleResultPanel method initVisualizer.
private void initVisualizer() {
try {
ResolverEntry[] entries = resolverProvider == null ? new ResolverEntry[0] : resolverProvider.getResolverEntries(result);
visualizers = new LinkedList<>();
resolverEntries = new LinkedList<>();
List<VisualizerPanel> openVisualizers = new LinkedList<>();
List<SNode> segNodes = CommonHelper.getSortedSegmentationNodes(segmentationName, result.getDocumentGraph());
Map<String, Long> markedAndCovered = Helper.calculateMarkedAndCoveredIDs(result, segNodes, segmentationName);
Helper.calulcateColorsForMarkedAndCovered(result, markedAndCovered, markedCoveredMap);
String resultID = "" + new Random().nextInt(Integer.MAX_VALUE);
for (int i = 0; i < entries.length; i++) {
String htmlID = "resolver-" + resultNumber + "_" + i;
VisualizerPanel p = new VisualizerPanel(entries[i], result, match, visibleTokenAnnos, markedAndCovered, markedCoveredMap, markedExactMap, htmlID, resultID, this, segmentationName, ps, instanceConfig);
visualizers.add(p);
resolverEntries.add(entries[i]);
Properties mappings = entries[i].getMappings();
// resovler entry is used, for determine the visibility status
if (visualizerState.containsKey(entries[i].getId())) {
if (visualizerState.get(entries[i].getId())) {
openVisualizers.add(p);
}
} else {
if (Boolean.parseBoolean(mappings.getProperty(INITIAL_OPEN, "false"))) {
openVisualizers.add(p);
}
}
}
// attach visualizer
for (VisualizerPanel p : visualizers) {
addComponent(p);
}
for (VisualizerPanel p : openVisualizers) {
p.toggleVisualizer(true, null);
}
} catch (RuntimeException ex) {
log.error("problems with initializing Visualizer Panel", ex);
} catch (Exception ex) {
log.error("problems with initializing Visualizer Panel", ex);
}
}
Aggregations