use of org.corpus_tools.salt.common.SToken in project ANNIS by korpling.
the class AutoTokQuery method analyzingQuery.
@Override
public void analyzingQuery(SaltProject saltProject) {
List<SToken> tokens = new ArrayList<>();
for (SCorpusGraph g : saltProject.getCorpusGraphs()) {
if (g != null) {
for (SDocument doc : g.getDocuments()) {
SDocumentGraph docGraph = doc.getDocumentGraph();
List<SNode> sNodes = docGraph.getNodes();
if (sNodes != null) {
for (SNode n : sNodes) {
if (n instanceof SToken) {
tokens.add((SToken) n);
}
}
}
}
}
}
// select one random token from the result
if (!tokens.isEmpty()) {
int tries = 10;
int r = new Random().nextInt(tokens.size() - 1);
String text = CommonHelper.getSpannedText(tokens.get(r));
while ("".equals(text) && tries > 0) {
r = new Random().nextInt(tokens.size() - 1);
text = CommonHelper.getSpannedText(tokens.get(r));
tries--;
}
if ("".equals(text)) {
finalAql = null;
} else {
finalAql = "\"" + text + "\"";
}
}
}
use of org.corpus_tools.salt.common.SToken in project ANNIS by korpling.
the class SingleResultPanel method getIds.
private MinMax getIds(SDocumentGraph graph) {
List<SToken> sTokens = graph.getTokens();
MinMax minMax = new MinMax();
minMax.min = Long.MAX_VALUE;
minMax.max = Long.MIN_VALUE;
if (segmentationName == null) {
minMax.segName = "tokens";
if (sTokens != null) {
for (SToken t : sTokens) {
SFeature feature = t.getFeature(ANNIS_NS, FEAT_RELANNIS_NODE);
if (feature != null && feature.getValue() instanceof RelannisNodeFeature) {
RelannisNodeFeature f = (RelannisNodeFeature) feature.getValue();
if (minMax.min > f.getTokenIndex()) {
minMax.min = f.getTokenIndex();
}
if (minMax.max < f.getTokenIndex()) {
minMax.max = f.getTokenIndex();
}
}
}
}
} else {
minMax.segName = segmentationName;
List<SNode> nodes = CommonHelper.getSortedSegmentationNodes(segmentationName, graph);
for (SNode n : nodes) {
RelannisNodeFeature f = RelannisNodeFeature.extract(n);
if (minMax.min > f.getSegIndex()) {
minMax.min = f.getSegIndex();
}
if (minMax.max < f.getSegIndex()) {
minMax.max = f.getSegIndex();
}
}
}
minMax.min++;
minMax.max++;
return minMax;
}
use of org.corpus_tools.salt.common.SToken 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);
}
}
}
}
}
}
use of org.corpus_tools.salt.common.SToken in project ANNIS by korpling.
the class TimelineReconstructor method addTimeline.
private void addTimeline() {
STimeline timeline = graph.createTimeline();
for (SToken virtualTok : graph.getSortedTokenByText()) {
timeline.increasePointOfTime();
// find all spans that are connected to this token and are part of an SOrderRelation
for (SRelation<?, ?> inRel : virtualTok.getInRelations()) {
if (inRel instanceof SSpanningRelation) {
SSpanningRelation spanRel = (SSpanningRelation) inRel;
SSpan overlappingSpan = spanRel.getSource();
if (overlappingSpan != null) {
spans2TimelinePos.put(overlappingSpan, timeline.getEnd());
}
}
}
nodesToDelete.add(virtualTok);
}
}
use of org.corpus_tools.salt.common.SToken in project ANNIS by korpling.
the class TimeHelper method getOverlappedTime.
/**
* Get start and end time from the overlapped {@link SToken}. The minimum
* start time and maximum end times are choosen.
* @param node The span to start from.
* @return A double array, will have length of 0 if no time annotations where found,
* one element of only start elements where found, 2 elements if both
* start and end time are found.
*/
public static double[] getOverlappedTime(SNode node) {
SGraph graph = node.getGraph();
final List<Double> startTimes = new LinkedList<>();
final List<Double> endTimes = new LinkedList<>();
List<SToken> token = new LinkedList<>();
if (node instanceof SToken) {
token.add((SToken) node);
} else {
List<SRelation<SNode, SNode>> outRelations = graph.getOutRelations(node.getId());
if (outRelations != null) {
for (SRelation<? extends SNode, ? extends SNode> e : outRelations) {
if (e instanceof SSpanningRelation) {
SToken tok = ((SSpanningRelation) e).getTarget();
token.add(tok);
}
}
}
// end for each out relations
}
for (SToken tok : token) {
SAnnotation anno = tok.getAnnotation(SaltUtil.createQName("annis", "time"));
if (anno != null && anno.getValue_STEXT() != null && !anno.getValue_STEXT().isEmpty() && !anno.getValue_STEXT().matches("\\-[0-9]*(\\.[0-9]*)?")) {
try {
String[] split = anno.getValue_STEXT().split("-");
if (split.length == 1) {
startTimes.add(Double.parseDouble(split[0]));
}
if (split.length == 2) {
startTimes.add(Double.parseDouble(split[0]));
endTimes.add(Double.parseDouble(split[1]));
}
} catch (NumberFormatException ex) {
log.debug("Invalid time annotation", ex);
}
}
}
if (startTimes.size() > 0 && endTimes.size() > 0) {
return new double[] { Collections.min(startTimes), Collections.max(endTimes) };
} else if (startTimes.size() > 0) {
return new double[] { Collections.min(startTimes) };
}
return new double[0];
}
Aggregations