* representation.
* @param docid A docid must be supplied, as it is not saved by the serialized representation.
* @return A semantic graph corresponding to the saved object, on the provided sentence.
*/
private SemanticGraph fromProto(CoreNLPProtos.DependencyGraph proto, List<CoreLabel> sentence, String docid) {
SemanticGraph graph = new SemanticGraph();
// first construct the actual nodes; keep them indexed by their index
// This block is optimized as one of the places which take noticeable time
// in datum caching
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(CoreNLPProtos.DependencyGraph.Node in: proto.getNodeList()){
min = in.getIndex() < min ? in.getIndex() : min;
max = in.getIndex() > max ? in.getIndex() : max;
}
TwoDimensionalMap<Integer, Integer, IndexedWord> nodes = TwoDimensionalMap.hashMap();
for(CoreNLPProtos.DependencyGraph.Node in: proto.getNodeList()){
CoreLabel token = sentence.get(in.getIndex() - 1); // index starts at 1!
IndexedWord word;
if (in.hasCopyAnnotation() && in.getCopyAnnotation() > 0) {
// TODO: if we make a copy wrapper CoreLabel, use it here instead
word = new IndexedWord(new CoreLabel(token));
word.set(CopyAnnotation.class, in.getCopyAnnotation());
} 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 && docid != null) {
word.setDocID(docid);
}
if (word.sentIndex() < 0 && in.getSentenceIndex() >= 0) {
word.setSentIndex(in.getSentenceIndex());
}
if (word.index() < 0 && in.getIndex() >= 0) {
word.setIndex(in.getIndex());
}
assert in.getIndex() == word.index();
nodes.put(in.getIndex(), in.getCopyAnnotation(), word);
graph.addVertex(word);
}
// add all edges to the actual graph
for(CoreNLPProtos.DependencyGraph.Edge ie: proto.getEdgeList()){
IndexedWord source = nodes.get(ie.getSource(), ie.getSourceCopy());
assert(source != null);
IndexedWord target = nodes.get(ie.getTarget(), ie.getTargetCopy());
assert(target != null);
synchronized (globalLock) {
// this is not thread-safe: there are static fields in GrammaticalRelation
assert ie.hasDep();
GrammaticalRelation rel = GrammaticalRelation.valueOf(ie.getDep(), fromProto(ie.getLanguage()));
graph.addEdge(source, target, rel, 1.0, ie.hasIsExtra() && ie.getIsExtra());
}
}
if (proto.getRootCount() > 0) {
Collection<IndexedWord> roots = new ArrayList<IndexedWord>();
for(int rootI : proto.getRootList()){
roots.add(nodes.get(rootI, 0)); // copies should never be roots...
}
graph.setRoots(roots);
} else {
// Roots were not saved away
// compute root nodes if non-empty
if(!graph.isEmpty()){
graph.resetRoots();
}
}
return graph;
}