use of org.corpus_tools.salt.common.SSpan in project ANNIS by korpling.
the class SpanHTMLOutputter method outputHTML.
public void outputHTML(SNode node, String matchedQName, SortedMap<Long, List<OutputItem>> outputStartTags, SortedMap<Long, List<OutputItem>> outputEndTags, String tokenColor, int priority) {
this.tokenColor = tokenColor;
if (node instanceof SToken && "tok".equals(matchedQName)) {
SToken tok = (SToken) node;
outputToken(tok, outputStartTags, outputEndTags, priority);
} else if (node instanceof SSpan || node instanceof SToken) {
outputAnnotation(node, matchedQName, outputStartTags, outputEndTags, priority);
} else {
throw new IllegalArgumentException("node must be either a SSpan or SToken");
}
}
use of org.corpus_tools.salt.common.SSpan in project ANNIS by korpling.
the class SaltAnnotateExtractor method testAndFixNonSpan.
/**
* Tests if the source node is not a span and fixes this if necessary
*
* @param sourceNode The source node to check.
* @param nodeByRankID
* @return Either the original span or a new created one
*/
private SSpan testAndFixNonSpan(SStructuredNode sourceNode, FastInverseMap<Long, SNode> nodeByRankID) {
if (sourceNode != null && !(sourceNode instanceof SSpan)) {
log.debug("Mismatched source type: should be SSpan");
SNode oldNode = sourceNode;
sourceNode = recreateNode(SSpan.class, sourceNode);
updateMapAfterRecreatingNode(oldNode, sourceNode, nodeByRankID);
}
return (SSpan) sourceNode;
}
use of org.corpus_tools.salt.common.SSpan in project ANNIS by korpling.
the class SaltAnnotateExtractor method createMissingSpanningRelations.
/**
* Use the left/right token index of the spans to create spanning relations
* when this did not happen yet.
*
* @param graph
* @param nodeByRankID
* @param numberOfRelations
*/
private void createMissingSpanningRelations(SDocumentGraph graph, FastInverseMap<Long, SNode> nodeByRankID, TreeMap<Long, SToken> tokenByIndex, Map<String, ComponentEntry> componentForSpan, AtomicInteger numberOfRelations) {
// add the missing spanning relations for each continuous span of the graph
for (SSpan span : graph.getSpans()) {
long pre = 1;
RelannisNodeFeature featSpan = RelannisNodeFeature.extract(span);
ComponentEntry spanComponent = componentForSpan.get(span.getId());
if (spanComponent != null && featSpan != null && featSpan.getLeftToken() >= 0 && featSpan.getRightToken() >= 0) {
for (long i = featSpan.getLeftToken(); i <= featSpan.getRightToken(); i++) {
SToken tok = tokenByIndex.get(i);
if (tok != null) {
boolean missing = true;
List<SRelation<SNode, SNode>> existingRelations = graph.getRelations(span.getId(), tok.getId());
if (existingRelations != null) {
for (Relation e : existingRelations) {
if (e instanceof SSpanningRelation) {
missing = false;
break;
}
}
}
if (missing) {
String type = "c";
SLayer layer = findOrAddSLayer(spanComponent.getNamespace(), graph);
createNewRelation(graph, span, tok, null, type, spanComponent.getId(), layer, pre++, nodeByRankID, numberOfRelations);
}
}
// end if token exists
}
// end for each covered token index
}
}
// end for each span
}
use of org.corpus_tools.salt.common.SSpan in project ANNIS by korpling.
the class SaltAnnotateExtractor method extractData.
@Override
public SaltProject extractData(ResultSet resultSet) throws SQLException, DataAccessException {
SaltProject project = SaltFactory.createSaltProject();
try {
SCorpusGraph corpusGraph = null;
SDocumentGraph graph = null;
// fn: parent information (pre and component) id to node
FastInverseMap<Long, SNode> nodeByRankID = new FastInverseMap<>();
TreeSet<Long> allTextIDs = new TreeSet<>();
TreeMap<Long, String> tokenTexts = new TreeMap<>();
TreeMap<Long, SToken> tokenByIndex = new TreeMap<>();
TreeMap<String, TreeMap<Long, String>> nodeBySegmentationPath = new TreeMap<>();
Map<String, ComponentEntry> componentForSpan = new HashMap<>();
// clear mapping functions for this graph
// assumes that the result set is sorted by key, pre
nodeByRankID.clear();
SDocument document = null;
AtomicInteger numberOfRelations = new AtomicInteger();
int match_index = 0;
SolutionKey<?> key = createSolutionKey();
int counter = 0;
while (resultSet.next()) {
if (counter % 1000 == 0) {
log.debug("handling resultset row {}", counter);
}
counter++;
// List<String> annotationGraphKey =
key.retrieveKey(resultSet);
if (key.isNewKey()) {
// create the text for the last graph
if (graph != null && document != null) {
createMissingSpanningRelations(graph, nodeByRankID, tokenByIndex, componentForSpan, numberOfRelations);
createPrimaryTexts(graph, allTextIDs, tokenTexts, tokenByIndex);
addOrderingRelations(graph, nodeBySegmentationPath);
}
// new match, reset everything
nodeByRankID.clear();
tokenTexts.clear();
tokenByIndex.clear();
componentForSpan.clear();
Integer matchstart = resultSet.getInt("matchstart");
corpusGraph = SaltFactory.createSCorpusGraph();
corpusGraph.setName("match_" + (match_index + matchstart));
project.addCorpusGraph(corpusGraph);
graph = SaltFactory.createSDocumentGraph();
document = SaltFactory.createSDocument();
document.setDocumentGraphLocation(org.eclipse.emf.common.util.URI.createFileURI(Files.createTempDir().getAbsolutePath()));
List<String> path = corpusPathExtractor.extractCorpusPath(resultSet, "path");
SCorpus toplevelCorpus = SaltFactory.createSCorpus();
toplevelCorpus.setName(path.get(0));
corpusGraph.addNode(toplevelCorpus);
Validate.isTrue(path.size() >= 2, "Corpus path must be have at least two members (toplevel and document)");
SCorpus corpus = toplevelCorpus;
for (int i = 1; i < path.size() - 1; i++) {
SCorpus subcorpus = SaltFactory.createSCorpus();
subcorpus.setName(path.get(i));
corpusGraph.addSubCorpus(corpus, subcorpus);
corpus = subcorpus;
}
document.setName(path.get(path.size() - 1));
document.setId("" + match_index);
corpusGraph.addDocument(corpus, document);
document.setDocumentGraph(graph);
match_index++;
}
// end if new key
// get node data
SNode node = createOrFindNewNode(resultSet, graph, allTextIDs, tokenTexts, tokenByIndex, nodeBySegmentationPath, key, nodeByRankID);
long rankID = longValue(resultSet, RANK_TABLE, "id");
long componentID = longValue(resultSet, COMPONENT_TABLE, "id");
if (!resultSet.wasNull()) {
nodeByRankID.put(rankID, node);
createRelation(resultSet, graph, nodeByRankID, node, numberOfRelations);
if (node instanceof SSpan) {
componentForSpan.put(node.getId(), new ComponentEntry(componentID, 'c', stringValue(resultSet, COMPONENT_TABLE, "namespace"), stringValue(resultSet, COMPONENT_TABLE, "name")));
}
}
}
// the last match needs a primary text, too
if (graph != null) {
createMissingSpanningRelations(graph, nodeByRankID, tokenByIndex, componentForSpan, numberOfRelations);
createPrimaryTexts(graph, allTextIDs, tokenTexts, tokenByIndex);
addOrderingRelations(graph, nodeBySegmentationPath);
}
} catch (Exception ex) {
log.error("could not map result set to SaltProject", ex);
}
return project;
}
use of org.corpus_tools.salt.common.SSpan in project ANNIS by korpling.
the class TimelineReconstructor method moveRelations.
private void moveRelations(SStructuredNode oldSpan, SToken newToken, Set<String> validSpanAnnos, String orderName) {
final List<SRelation> inRels = new LinkedList<>(oldSpan.getInRelations());
final List<SRelation> outRels = new LinkedList<>(oldSpan.getOutRelations());
final List<SToken> coveredByOldSpan = new LinkedList<>();
for (SRelation rel : outRels) {
if (rel instanceof SPointingRelation || rel instanceof SDominanceRelation) {
rel.setSource(newToken);
} else if (rel instanceof SSpanningRelation) {
coveredByOldSpan.add(((SSpanningRelation) rel).getTarget());
}
}
for (SRelation rel : inRels) {
if (rel instanceof SPointingRelation || rel instanceof SDominanceRelation) {
rel.setTarget(newToken);
}
}
// find the connected spans and connect them with the new token instead
for (SToken tok : coveredByOldSpan) {
if (tok.getInRelations() != null) {
for (SRelation<?, ?> rel : tok.getInRelations()) {
if (rel instanceof SSpanningRelation) {
boolean valid = false;
SSpan spanToMap = ((SSpanningRelation) rel).getSource();
if (virtualTokenizationFromNamespace) {
for (SAnnotation anno : spanToMap.getAnnotations()) {
if (anno.getNamespace() != null && anno.getNamespace().equals(orderName)) {
valid = true;
break;
}
}
} else {
for (String validAnno : validSpanAnnos) {
if (spanToMap.getAnnotation(validAnno) != null) {
valid = true;
break;
}
}
}
if (valid) {
graph.createRelation(spanToMap, newToken, SALT_TYPE.SSPANNING_RELATION, null);
}
}
}
}
}
}
Aggregations