use of org.corpus_tools.salt.core.SFeature in project ANNIS by korpling.
the class Helper method calculateMarkedAndCoveredIDs.
public static Map<String, Long> calculateMarkedAndCoveredIDs(SDocument doc, List<SNode> segNodes, String segmentationName) {
Map<String, Long> initialCovered = new HashMap<>();
// add all covered nodes
for (SNode n : doc.getDocumentGraph().getNodes()) {
SFeature featMatched = n.getFeature(ANNIS_NS, FEAT_MATCHEDNODE);
Long match = featMatched == null ? null : featMatched.getValue_SNUMERIC();
if (match != null) {
initialCovered.put(n.getId(), match);
}
}
// calculate covered nodes
CoveredMatchesCalculator cmc = new CoveredMatchesCalculator(doc.getDocumentGraph(), initialCovered);
Map<String, Long> covered = cmc.getMatchedAndCovered();
if (segmentationName != null) {
// filter token
Map<SToken, Long> coveredToken = new HashMap<>();
for (Map.Entry<String, Long> e : covered.entrySet()) {
SNode n = doc.getDocumentGraph().getNode(e.getKey());
if (n instanceof SToken) {
coveredToken.put((SToken) n, e.getValue());
}
}
for (SNode segNode : segNodes) {
RelannisNodeFeature featSegNode = (RelannisNodeFeature) segNode.getFeature(ANNIS_NS, FEAT_RELANNIS_NODE).getValue();
if (!covered.containsKey(segNode.getId())) {
long leftTok = featSegNode.getLeftToken();
long rightTok = featSegNode.getRightToken();
// check for each covered token if this segment is covering it
for (Map.Entry<SToken, Long> e : coveredToken.entrySet()) {
RelannisNodeFeature featTok = (RelannisNodeFeature) e.getKey().getFeature(ANNIS_NS, FEAT_RELANNIS_NODE).getValue();
long entryTokenIndex = featTok.getTokenIndex();
if (entryTokenIndex <= rightTok && entryTokenIndex >= leftTok) {
// add this segmentation node to the covered set
covered.put(segNode.getId(), e.getValue());
break;
}
}
// end for each covered token
}
// end if not already contained
}
// end for each segmentation node
}
return covered;
}
use of org.corpus_tools.salt.core.SFeature in project ANNIS by korpling.
the class LegacyGraphConverter method convertToAnnotationGraph.
public static AnnotationGraph convertToAnnotationGraph(SDocument document) {
SDocumentGraph docGraph = document.getDocumentGraph();
SFeature featMatchedIDs = docGraph.getFeature(ANNIS_NS, FEAT_MATCHEDIDS);
Match match = new Match();
if (featMatchedIDs != null && featMatchedIDs.getValue_STEXT() != null) {
match = Match.parseFromString(featMatchedIDs.getValue_STEXT(), ',');
}
// get matched node names by using the IDs
List<Long> matchedNodeIDs = new ArrayList<>();
for (URI u : match.getSaltIDs()) {
SNode node = docGraph.getNode(u.toASCIIString());
if (node == null) {
// that's weird, fallback to the id
log.warn("Could not get matched node from id {}", u.toASCIIString());
matchedNodeIDs.add(-1l);
} else {
RelannisNodeFeature relANNISFeat = (RelannisNodeFeature) node.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE)).getValue();
matchedNodeIDs.add(relANNISFeat.getInternalID());
}
}
AnnotationGraph result = convertToAnnotationGraph(docGraph, matchedNodeIDs);
return result;
}
use of org.corpus_tools.salt.core.SFeature in project ANNIS by korpling.
the class LegacyGraphConverter method convertToAnnotationGraph.
public static AnnotationGraph convertToAnnotationGraph(SDocumentGraph docGraph, List<Long> matchedNodeIDs) {
Set<Long> matchSet = new HashSet<>(matchedNodeIDs);
AnnotationGraph annoGraph = new AnnotationGraph();
List<String> pathList = CommonHelper.getCorpusPath(docGraph.getDocument().getGraph(), docGraph.getDocument());
annoGraph.setPath(pathList.toArray(new String[pathList.size()]));
annoGraph.setDocumentName(docGraph.getDocument().getName());
Map<SNode, AnnisNode> allNodes = new HashMap<>();
for (SNode sNode : docGraph.getNodes()) {
SFeature featNodeRaw = sNode.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE));
if (featNodeRaw != null) {
RelannisNodeFeature featNode = (RelannisNodeFeature) featNodeRaw.getValue();
long internalID = featNode.getInternalID();
AnnisNode aNode = new AnnisNode(internalID);
for (SAnnotation sAnno : sNode.getAnnotations()) {
aNode.addNodeAnnotation(new Annotation(sAnno.getNamespace(), sAnno.getName(), sAnno.getValue_STEXT()));
}
aNode.setName(sNode.getName());
Set<SLayer> layers = sNode.getLayers();
if (!layers.isEmpty()) {
aNode.setNamespace(layers.iterator().next().getName());
}
RelannisNodeFeature feat = (RelannisNodeFeature) sNode.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE)).getValue();
if (sNode instanceof SToken) {
List<DataSourceSequence> seqList = docGraph.getOverlappedDataSourceSequence(sNode, SALT_TYPE.STEXT_OVERLAPPING_RELATION);
if (seqList != null) {
DataSourceSequence seq = seqList.get(0);
Preconditions.checkNotNull(seq, "DataSourceSequence is null for token %s", sNode.getId());
SSequentialDS seqDS = seq.getDataSource();
Preconditions.checkNotNull(seqDS, "SSequentalDS is null for token %s", sNode.getId());
Preconditions.checkNotNull(seqDS.getData(), "SSequentalDS data is null for token %s", sNode.getId());
String seqDSData = (String) seqDS.getData();
Preconditions.checkNotNull(seqDSData, "casted SSequentalDS data is null for token %s", sNode.getId());
Preconditions.checkNotNull(seq.getStart(), "SSequentalDS start is null for token %s", sNode.getId());
Preconditions.checkNotNull(seq.getEnd(), "SSequentalDS end is null for supposed token %s", sNode.getId());
int start = seq.getStart().intValue();
int end = seq.getEnd().intValue();
Preconditions.checkState(start >= 0 && start <= end && end <= seqDSData.length(), "Illegal start or end of textual DS for token (start %s, end: %s)", sNode.getId(), start, end);
String spannedText = seqDSData.substring(start, end);
Preconditions.checkNotNull(spannedText, "spanned text is null for supposed token %s (start: %s, end: %s)", sNode.getId(), start, end);
aNode.setSpannedText(spannedText);
aNode.setToken(true);
aNode.setTokenIndex(feat.getTokenIndex());
}
} else {
aNode.setToken(false);
aNode.setTokenIndex(null);
}
aNode.setCorpus(feat.getCorpusRef());
aNode.setTextId(feat.getTextRef());
aNode.setLeft(feat.getLeft());
aNode.setLeftToken(feat.getLeftToken());
aNode.setRight(feat.getRight());
aNode.setRightToken(feat.getRightToken());
if (matchSet.contains(aNode.getId())) {
aNode.setMatchedNodeInQuery((long) matchedNodeIDs.indexOf(aNode.getId()) + 1);
annoGraph.getMatchedNodeIds().add(aNode.getId());
} else {
aNode.setMatchedNodeInQuery(null);
}
annoGraph.addNode(aNode);
allNodes.put(sNode, aNode);
}
}
for (SRelation rel : docGraph.getRelations()) {
RelannisEdgeFeature featRelation = RelannisEdgeFeature.extract(rel);
if (featRelation != null) {
addRelation(rel, featRelation.getPre(), featRelation.getComponentID(), allNodes, annoGraph);
}
}
// add relations with empty relation name for every dominance relation
List<SDominanceRelation> dominanceRelations = new LinkedList<>(docGraph.getDominanceRelations());
for (SDominanceRelation rel : dominanceRelations) {
RelannisEdgeFeature featEdge = RelannisEdgeFeature.extract(rel);
if (featEdge != null && featEdge.getArtificialDominanceComponent() != null && featEdge.getArtificialDominancePre() != null) {
addRelation(SDominanceRelation.class, null, rel.getAnnotations(), rel.getSource(), rel.getTarget(), rel.getLayers(), featEdge.getArtificialDominancePre(), featEdge.getArtificialDominanceComponent(), allNodes, annoGraph);
}
}
return annoGraph;
}
use of org.corpus_tools.salt.core.SFeature 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.core.SFeature in project ANNIS by korpling.
the class SaltAnnotateExtractor method moveNodeProperties.
private void moveNodeProperties(SStructuredNode oldNode, SStructuredNode newNode, SGraph graph) {
Validate.notNull(oldNode);
Validate.notNull(newNode);
// step 1: collect every information that is need in a separate variable
String id = oldNode.getId();
String name = oldNode.getName();
Set<SAnnotation> annotations = new LinkedHashSet<>(oldNode.getAnnotations());
Set<SFeature> features = new LinkedHashSet<>(oldNode.getFeatures());
Set<SProcessingAnnotation> processingAnnotations = new LinkedHashSet<>(oldNode.getProcessingAnnotations());
Set<SMetaAnnotation> metaAnnotations = new LinkedHashSet<>(oldNode.getMetaAnnotations());
Set<SLayer> nodeLayers = new LinkedHashSet<>(oldNode.getLayers());
Multimap<SRelation, SLayer> layerOfRelation = ArrayListMultimap.create();
List<SRelation<SNode, SNode>> inRelations = new LinkedList<>(graph.getInRelations(oldNode.getId()));
List<SRelation<SNode, SNode>> outRelations = new LinkedList<>(graph.getOutRelations(oldNode.getId()));
// step 2: remove the old node from everything it is connected to
for (SRelation<SNode, SNode> rel : inRelations) {
if (rel.getLayers() != null) {
layerOfRelation.putAll(rel, rel.getLayers());
}
graph.removeRelation(rel);
}
for (SRelation<SNode, SNode> rel : outRelations) {
if (rel.getLayers() != null) {
layerOfRelation.putAll(rel, rel.getLayers());
}
graph.removeRelation(rel);
}
graph.removeNode(oldNode);
// step 3: add the new node to everything it should be connected to
newNode.setName(name);
newNode.setId(id);
graph.addNode(newNode);
for (SAnnotation anno : annotations) {
newNode.addAnnotation(anno);
}
for (SFeature feat : features) {
// filter the features, do not include salt::SNAME
if (!(SaltUtil.SALT_NAMESPACE.equals(feat.getNamespace()) && SaltUtil.FEAT_NAME.equals(feat.getName()))) {
newNode.addFeature(feat);
}
}
for (SProcessingAnnotation proc : processingAnnotations) {
newNode.addProcessingAnnotation(proc);
}
for (SMetaAnnotation meta : metaAnnotations) {
newNode.addMetaAnnotation(meta);
}
for (SLayer l : nodeLayers) {
l.addNode(newNode);
}
for (SRelation rel : inRelations) {
rel.setTarget(newNode);
graph.addRelation(rel);
if (layerOfRelation.containsKey(rel)) {
for (SLayer l : layerOfRelation.get(rel)) {
l.addRelation(rel);
}
}
}
for (SRelation rel : outRelations) {
rel.setSource(newNode);
graph.addRelation(rel);
if (layerOfRelation.containsKey(rel)) {
for (SLayer l : layerOfRelation.get(rel)) {
l.addRelation(rel);
}
}
}
}
Aggregations