}
private static final Object LOCK = new Object();
public SemanticGraph convertIntermediateGraph(List<CoreLabel> sentence) {
SemanticGraph graph = new SemanticGraph();
// first construct the actual nodes; keep them indexed by their index and copy count
// sentences such as "I went over the river and through the woods" have
// copys for "went" in the collapsed dependencies
TwoDimensionalMap<Integer, Integer, IndexedWord> nodeMap = TwoDimensionalMap.hashMap();
for (IntermediateNode in: nodes){
CoreLabel token = sentence.get(in.index - 1); // index starts at 1!
IndexedWord word;
if (in.copyAnnotation > 0) {
// TODO: if we make a copy wrapper CoreLabel, use it here instead
word = new IndexedWord(new CoreLabel(token));
word.setCopyCount(in.copyAnnotation);
} else {
word = new IndexedWord(token);
}
// for backwards compatibility - new annotations should have
// these fields set, but annotations older than August 2014 might not
if (word.docID() == null && in.docId != null) {
word.setDocID(in.docId);
}
if (word.sentIndex() < 0 && in.sentIndex >= 0) {
word.setSentIndex(in.sentIndex);
}
if (word.index() < 0 && in.index >= 0) {
word.setIndex(in.index);
}
nodeMap.put(word.index(), word.copyCount(), word);
graph.addVertex(word);
if (in.isRoot) {
graph.addRoot(word);
}
}
// add all edges to the actual graph
for(IntermediateEdge ie: edges){
IndexedWord source = nodeMap.get(ie.source, ie.sourceCopy);
if (source == null) {
throw new RuntimeIOException("Failed to find node " + ie.source + "-" + ie.sourceCopy);
}
IndexedWord target = nodeMap.get(ie.target, ie.targetCopy);
if (target == null) {
throw new RuntimeIOException("Failed to find node " + ie.target + "-" + ie.targetCopy);
}
assert(target != null);
synchronized (LOCK) {
// this is not thread-safe: there are static fields in GrammaticalRelation
GrammaticalRelation rel = GrammaticalRelation.valueOf(ie.dep);
graph.addEdge(source, target, rel, 1.0, ie.isExtra);
}
}
// compute root nodes if they weren't stored in the graph
if (!graph.isEmpty() && graph.getRoots().size() == 0){
graph.resetRoots();
}
return graph;
}