use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class RSTImpl method createJsonEntry.
private JSONObject createJsonEntry(SNode currNode) {
JSONObject jsonData = new JSONObject();
StringBuilder sb = new StringBuilder();
// use a hash set so we don't get any duplicate entries
LinkedHashSet<SToken> token = new LinkedHashSet<>();
List<SRelation<SNode, SNode>> edges;
if (currNode instanceof SStructure) {
edges = currNode.getGraph().getOutRelations(currNode.getId());
// get all tokens directly dominated tokens and build a string
for (SRelation<SNode, SNode> sedge : edges) {
if (sedge.getTarget() instanceof SToken) {
token.add((SToken) sedge.getTarget());
}
}
// build strings
Iterator<SToken> tokIterator = token.iterator();
while (tokIterator.hasNext()) {
SToken tok = tokIterator.next();
String text = getText(tok);
String color = getHTMLColor(tok);
if (color != null) {
sb.append("<span style=\"color : ").append(color).append(";\">");
} else {
sb.append("<span>");
}
if (tokIterator.hasNext()) {
sb.append(text).append(" ");
} else {
sb.append(text);
}
sb.append("</span>");
}
}
try {
// build unique id, cause is used for an unique html element id.
jsonData.put("id", getUniStrId(currNode));
jsonData.put("name", currNode.getName());
/**
* additional data oject for edge labels and rendering sentences
*/
JSONObject data = new JSONObject();
JSONArray edgesJSON = getOutGoingEdgeTypeAnnotation(currNode);
// since we have found some tokens, it must be a sentence in RST.
if (token.size() > 0) {
data.put("sentence", sb.toString());
}
if (edgesJSON != null) {
data.put("edges", edgesJSON);
}
if (currNode instanceof SStructure && isSegment(currNode)) {
SProcessingAnnotation sentence_idx = currNode.getProcessingAnnotation(SENTENCE_INDEX + "::" + SENTENCE_INDEX);
int index = sentence_idx == null ? -1 : Integer.parseInt(sentence_idx.getValue_STEXT());
data.put(SENTENCE_LEFT, index);
data.put(SENTENCE_RIGHT, index);
}
jsonData.put("data", data);
} catch (JSONException ex) {
log.error("problems create entry for {}", currNode, ex);
}
return jsonData;
}
use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class CorefVisualizer method setReferent.
/**
* adds a Referent for all Nodes dominated or covered by outgoing Edges of AnnisNode a
* @param n the Node
* @param index index of the Referent
* @param value determines wheather the refered P-Edge is incoming (1) or outgoing (0)
*/
private void setReferent(SNode n, long index, int value) {
if (n instanceof SToken) {
SToken tok = (SToken) n;
if (!referentOfToken.containsKey(tok.getId())) {
HashMap<Long, Integer> newlist = new HashMap<Long, Integer>();
// globalindex?
newlist.put(index, value);
referentOfToken.put(tok.getId(), newlist);
} else {
referentOfToken.get(tok.getId()).put(globalIndex, value);
}
} else {
List<SRelation<SNode, SNode>> outEdges = n.getGraph().getOutRelations(n.getId());
if (outEdges != null) {
for (SRelation<? extends SNode, ? extends SNode> edge : outEdges) {
if (!(edge instanceof SPointingRelation)) {
if (edge.getSource() != null && edge.getTarget() != null) {
setReferent(edge.getTarget(), index, value);
}
}
}
}
}
}
use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class SaltAnnotateExtractorTest method testLayerRelations.
@Test
public void testLayerRelations() throws SQLException {
SaltProject project = instance.extractData(resultSetProviderSingleText.getResultSet());
assertNotNull(project);
SDocumentGraph g = project.getCorpusGraphs().get(0).getDocuments().get(0).getDocumentGraph();
// dep //
List<SRelation<SNode, SNode>> e = new ArrayList<>(g.getLayerByName("dep").get(0).getRelations());
Collections.sort(e, new EdgeComparator());
assertEquals(9, e.size());
assertEquals("tok_150", e.get(0).getSource().getName());
assertEquals("tok_151", e.get(0).getTarget().getName());
assertEquals("tok_152", e.get(1).getSource().getName());
assertEquals("tok_153", e.get(1).getTarget().getName());
assertEquals("tok_156", e.get(2).getSource().getName());
assertEquals("tok_154", e.get(2).getTarget().getName());
assertEquals("tok_156", e.get(3).getSource().getName());
assertEquals("tok_155", e.get(3).getTarget().getName());
assertEquals("tok_156", e.get(4).getSource().getName());
assertEquals("tok_157", e.get(4).getTarget().getName());
assertEquals("tok_157", e.get(5).getSource().getName());
assertEquals("tok_158", e.get(5).getTarget().getName());
assertEquals("tok_158", e.get(6).getSource().getName());
assertEquals("tok_160", e.get(6).getTarget().getName());
assertEquals("tok_160", e.get(7).getSource().getName());
assertEquals("tok_159", e.get(7).getTarget().getName());
assertEquals("tok_160", e.get(8).getSource().getName());
assertEquals("tok_161", e.get(8).getTarget().getName());
// exmaralda //
e = new ArrayList<>(g.getLayerByName("exmaralda").get(0).getRelations());
Collections.sort(e, new EdgeComparator());
assertEquals(30, e.size());
assertEquals("Focus_newInfSeg_10", e.get(0).getSource().getName());
assertEquals("tok_154", e.get(0).getTarget().getName());
assertEquals("Focus_newInfSeg_10", e.get(1).getSource().getName());
assertEquals("tok_155", e.get(1).getTarget().getName());
assertEquals("Focus_newInfSeg_10", e.get(2).getSource().getName());
assertEquals("tok_156", e.get(2).getTarget().getName());
assertEquals("Focus_newInfSeg_10", e.get(3).getSource().getName());
assertEquals("tok_157", e.get(3).getTarget().getName());
assertEquals("Focus_newInfSeg_10", e.get(4).getSource().getName());
assertEquals("tok_158", e.get(4).getTarget().getName());
assertEquals("Focus_newInfSeg_10", e.get(5).getSource().getName());
assertEquals("tok_159", e.get(5).getTarget().getName());
assertEquals("Focus_newInfSeg_10", e.get(6).getSource().getName());
assertEquals("tok_160", e.get(6).getTarget().getName());
assertEquals("Focus_newInfSeg_9", e.get(7).getSource().getName());
assertEquals("tok_150", e.get(7).getTarget().getName());
assertEquals("Focus_newInfSeg_9", e.get(8).getSource().getName());
assertEquals("tok_151", e.get(8).getTarget().getName());
assertEquals("Focus_newInfSeg_9", e.get(9).getSource().getName());
assertEquals("tok_152", e.get(9).getTarget().getName());
assertEquals("Inf-StatSeg_29", e.get(10).getSource().getName());
assertEquals("tok_150", e.get(10).getTarget().getName());
assertEquals("Inf-StatSeg_29", e.get(11).getSource().getName());
assertEquals("tok_151", e.get(11).getTarget().getName());
assertEquals("Inf-StatSeg_30", e.get(12).getSource().getName());
assertEquals("tok_155", e.get(12).getTarget().getName());
assertEquals("NPSeg_29", e.get(13).getSource().getName());
assertEquals("tok_150", e.get(13).getTarget().getName());
assertEquals("NPSeg_29", e.get(14).getSource().getName());
assertEquals("tok_151", e.get(14).getTarget().getName());
assertEquals("NPSeg_30", e.get(15).getSource().getName());
assertEquals("tok_155", e.get(15).getTarget().getName());
assertEquals("PPSeg_7", e.get(16).getSource().getName());
assertEquals("tok_150", e.get(16).getTarget().getName());
assertEquals("PPSeg_7", e.get(17).getSource().getName());
assertEquals("tok_151", e.get(17).getTarget().getName());
assertEquals("SentSeg_10", e.get(18).getSource().getName());
assertEquals("tok_154", e.get(18).getTarget().getName());
assertEquals("SentSeg_10", e.get(19).getSource().getName());
assertEquals("tok_155", e.get(19).getTarget().getName());
assertEquals("SentSeg_10", e.get(20).getSource().getName());
assertEquals("tok_156", e.get(20).getTarget().getName());
assertEquals("SentSeg_10", e.get(21).getSource().getName());
assertEquals("tok_157", e.get(21).getTarget().getName());
assertEquals("SentSeg_10", e.get(22).getSource().getName());
assertEquals("tok_158", e.get(22).getTarget().getName());
assertEquals("SentSeg_10", e.get(23).getSource().getName());
assertEquals("tok_159", e.get(23).getTarget().getName());
assertEquals("SentSeg_10", e.get(24).getSource().getName());
assertEquals("tok_160", e.get(24).getTarget().getName());
assertEquals("SentSeg_10", e.get(25).getSource().getName());
assertEquals("tok_161", e.get(25).getTarget().getName());
assertEquals("SentSeg_9", e.get(26).getSource().getName());
assertEquals("tok_150", e.get(26).getTarget().getName());
assertEquals("SentSeg_9", e.get(27).getSource().getName());
assertEquals("tok_151", e.get(27).getTarget().getName());
assertEquals("SentSeg_9", e.get(28).getSource().getName());
assertEquals("tok_152", e.get(28).getTarget().getName());
assertEquals("SentSeg_9", e.get(29).getSource().getName());
assertEquals("tok_153", e.get(29).getTarget().getName());
// mmax, only control samples //
e = new ArrayList<>(g.getLayerByName("mmax").get(0).getRelations());
Collections.sort(e, new EdgeComparator());
assertEquals(14, e.size());
assertEquals("primmarkSeg_60", e.get(1).getSource().getName());
assertEquals("tok_150", e.get(1).getTarget().getName());
assertEquals("sentenceSeg_50010", e.get(7).getSource().getName());
assertEquals("tok_158", e.get(7).getTarget().getName());
// tiger, only control samples //
e = new ArrayList<>(g.getLayerByName("tiger").get(0).getRelations());
Collections.sort(e, new EdgeComparator());
assertEquals(17, e.size());
assertEquals("const_59", e.get(9).getSource().getName());
assertEquals("tok_160", e.get(9).getTarget().getName());
assertEquals("const_61", e.get(16).getSource().getName());
assertEquals("tok_156", e.get(16).getTarget().getName());
// urml, only control samples //
e = new ArrayList<>(g.getLayerByName("rst").get(0).getRelations());
Collections.sort(e, new EdgeComparator());
assertEquals(20, e.size());
assertEquals("u0", e.get(0).getSource().getName());
assertEquals("u28", e.get(0).getTarget().getName());
assertEquals("u11", e.get(5).getSource().getName());
assertEquals("tok_153", e.get(5).getTarget().getName());
}
use of org.corpus_tools.salt.core.SRelation in project ANNIS by korpling.
the class SaltAnnotateExtractor method createRelation.
private void createRelation(ResultSet resultSet, SDocumentGraph graph, FastInverseMap<Long, SNode> nodeByRankID, SNode targetNode, AtomicInteger numberOfRelations) throws SQLException {
long parent = longValue(resultSet, RANK_TABLE, "parent");
if (resultSet.wasNull()) {
return;
}
long pre = longValue(resultSet, RANK_TABLE, "pre");
long componentID = longValue(resultSet, RANK_TABLE, "component_id");
String relationNamespace = stringValue(resultSet, COMPONENT_TABLE, "namespace");
if (relationNamespace == null) {
relationNamespace = "default_ns";
}
String relationName = stringValue(resultSet, COMPONENT_TABLE, "name");
String type = stringValue(resultSet, COMPONENT_TABLE, "type");
SStructuredNode sourceNode = (SStructuredNode) nodeByRankID.get(parent);
if (sourceNode == null) {
// the relation is not fully included in the result
return;
}
SLayer layer = findOrAddSLayer(relationNamespace, graph);
SRelation rel;
if (!resultSet.wasNull()) {
rel = findExistingRelation(graph, sourceNode, targetNode, relationName, layer);
if (rel == null) {
rel = createNewRelation(graph, sourceNode, targetNode, relationName, type, componentID, layer, pre, nodeByRankID, numberOfRelations);
}
// add relation annotations if relation was successfully created
if (rel != null) {
addRelationAnnotations(resultSet, rel);
}
}
}
use of org.corpus_tools.salt.core.SRelation 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