use of annis.model.AnnotationGraph in project ANNIS by korpling.
the class LegacyGraphConverter method convertToResultSet.
public static AnnisResultSet convertToResultSet(SaltProject p) {
List<AnnotationGraph> annotationGraphs = convertToAOM(p);
AnnisResultSetImpl annisResultSet = new AnnisResultSetImpl();
for (AnnotationGraph annotationGraph : annotationGraphs) {
annisResultSet.add(new AnnisResultImpl(annotationGraph));
}
return annisResultSet;
}
use of annis.model.AnnotationGraph 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 annis.model.AnnotationGraph 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 annis.model.AnnotationGraph in project ANNIS by korpling.
the class AnnisGraphTools method getSyntaxGraphs.
public List<DirectedGraph<AnnisNode, Edge>> getSyntaxGraphs() {
AnnotationGraph ag = input.getResult().getGraph();
String namespace = input.getMappings().getProperty("node_ns", input.getNamespace());
String terminalName = input.getMappings().getProperty(TigerTreeVisualizer.TERMINAL_NAME_KEY);
String terminalNamespace = input.getMappings().getProperty(TigerTreeVisualizer.TERMINAL_NS_KEY);
List<DirectedGraph<AnnisNode, Edge>> resultGraphs = new ArrayList<>();
List<AnnisNode> rootNodes = new LinkedList<>();
for (AnnisNode n : ag.getNodes()) {
if (isRootNode(n, namespace)) {
rootNodes.add(n);
}
}
// sort root nodes according to their left-most covered token
HorizontalOrientation orientation = detectLayoutDirection(ag);
if (orientation == HorizontalOrientation.LEFT_TO_RIGHT) {
Collections.sort(rootNodes, new Comparator<AnnisNode>() {
@Override
public int compare(AnnisNode o1, AnnisNode o2) {
return Long.compare(o1.getLeftToken(), o2.getLeftToken());
}
});
} else if (orientation == HorizontalOrientation.RIGHT_TO_LEFT) {
Collections.sort(rootNodes, new Comparator<AnnisNode>() {
@Override
public int compare(AnnisNode o1, AnnisNode o2) {
return Long.compare(o2.getLeftToken(), o1.getLeftToken());
}
});
}
for (AnnisNode r : rootNodes) {
resultGraphs.add(extractGraph(ag, r, terminalNamespace, terminalName));
}
return resultGraphs;
}
use of annis.model.AnnotationGraph in project ANNIS by korpling.
the class LegacyGraphConverterTest method testConvertToAOM.
/**
* Test of convertToAOM method, of class LegacyGraphConverter.
*/
@Test
public void testConvertToAOM() throws SQLException {
SaltAnnotateExtractor saltExtractor = new SaltAnnotateExtractor() {
@Override
protected SolutionKey<?> createSolutionKey() {
PostgreSqlArraySolutionKey<Long> key = new PostgreSqlArraySolutionKey<>();
key.setKeyColumnName("key");
key.setIdColumnName("id");
return key;
}
};
CorpusPathExtractor corpusPathExtractor = new ArrayCorpusPathExtractor();
saltExtractor.setCorpusPathExtractor(corpusPathExtractor);
TestAnnotateSqlGenerator.setupOuterQueryFactsTableColumnAliases(saltExtractor);
List<Match> matches = new ArrayList<>();
matches.add(Match.parseFromString("salt:/pcc2/4282/#tok_155 tiger::pos::salt:/pcc2/4282#tok_156"));
MatchGroup matchGroup = new MatchGroup(matches);
SaltProject p = saltExtractor.extractData(new CsvResultSetProvider(annis.sqlgen.SaltAnnotateExtractorTest.class.getResourceAsStream("SampleAnnotateResult.csv")).getResultSet());
SaltAnnotateExtractor.addMatchInformation(p, matchGroup);
List<AnnotationGraph> expected = aomSqlGen.extractData(new CsvResultSetProvider(annis.sqlgen.SaltAnnotateExtractorTest.class.getResourceAsStream("SampleAnnotateResult.csv")).getResultSet());
List<AnnotationGraph> result = LegacyGraphConverter.convertToAOM(p);
assertEquals(expected.size(), result.size());
Iterator<AnnotationGraph> itGraphExpected = expected.iterator();
Iterator<AnnotationGraph> itGraphResult = result.iterator();
while (itGraphExpected.hasNext() && itGraphResult.hasNext()) {
AnnotationGraph graphExpected = itGraphExpected.next();
AnnotationGraph graphResult = itGraphResult.next();
List<AnnisNode> nodeListExpected = graphExpected.getNodes();
List<AnnisNode> nodeListResult = graphResult.getNodes();
assertEquals(nodeListExpected.size(), nodeListResult.size());
Collections.sort(nodeListExpected, new Comparator<AnnisNode>() {
@Override
public int compare(AnnisNode arg0, AnnisNode arg1) {
return Long.valueOf(arg0.getId()).compareTo(Long.valueOf(arg1.getId()));
}
});
Collections.sort(nodeListResult, new Comparator<AnnisNode>() {
@Override
public int compare(AnnisNode arg0, AnnisNode arg1) {
return Long.valueOf(arg0.getId()).compareTo(Long.valueOf(arg1.getId()));
}
});
Iterator<AnnisNode> itNodeExpected = nodeListExpected.iterator();
Iterator<AnnisNode> itNodeResult = nodeListResult.iterator();
while (itNodeExpected.hasNext() && itNodeResult.hasNext()) {
checkAnnisNodeEqual(itNodeExpected.next(), itNodeResult.next());
}
}
}
Aggregations