Skip to content

Commit 78cd918

Browse files
committed
Properly handle a graph of one word (previously caused a crash because there were no Edges from which to extract the roots)
1 parent c5d5548 commit 78cd918

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

itest/src/edu/stanford/nlp/pipeline/CoNLLUReaderITest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,37 @@ public void testReadingNoEnhanced() throws ClassNotFoundException, IOException {
451451
assertFalse(sentence.containsKey(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class));
452452
}
453453

454+
public static final String oneWordPath = String.format("edu/stanford/nlp/pipeline/en-example-oneword.conllu");
455+
456+
@Test
457+
/**
458+
* This test checks that the graphs are successfully constructed
459+
* when they are only one word big.
460+
* (Previously, such a case would throw an error.)
461+
*/
462+
public void testReadingOneWord() throws ClassNotFoundException, IOException {
463+
Annotation readInDocument = new CoNLLUReader(new Properties()).readCoNLLUFile(oneWordPath).get(0);
464+
465+
// this document only has one sentence
466+
List<CoreMap> sentences = readInDocument.get(CoreAnnotations.SentencesAnnotation.class);
467+
assertEquals(1, sentences.size());
468+
469+
CoreMap sentence = sentences.get(0);
470+
471+
// cursory check of the tokens
472+
List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
473+
assertEquals(1, tokens.size());
474+
assertEquals("...", tokens.get(0).value());
475+
476+
assertTrue(sentence.containsKey(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class));
477+
SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
478+
assertEquals(0, graph.edgeListSorted().size());
479+
assertEquals(1, graph.getRoots().size());
480+
481+
assertTrue(sentence.containsKey(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class));
482+
graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
483+
assertEquals(0, graph.edgeListSorted().size());
484+
assertEquals(1, graph.getRoots().size());
485+
}
486+
454487
}

src/edu/stanford/nlp/pipeline/CoNLLUReader.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -554,26 +554,29 @@ public CoreMap convertCoNLLUSentenceToCoreMap(CoNLLUDocument doc, CoNLLUSentence
554554
boolean hasEnhanced = false;
555555
// build SemanticGraphEdges for a basic graph
556556
List<SemanticGraphEdge> graphEdges = new ArrayList<>();
557+
List<IndexedWord> graphRoots = new ArrayList<>();
557558
for (int i = 0; i < lines.size(); i++) {
558559
List<String> fields = Arrays.asList(lines.get(i).split("\t"));
559560
// track whether any of these lines signify there is an enhanced graph
560561
hasEnhanced = hasEnhanced || !fields.get(CoNLLU_EnhancedField).equals("_");
561-
// skip the ROOT node
562-
if (fields.get(CoNLLU_GovField).equals("0"))
563-
continue;
564562
IndexedWord dependent = graphNodes.get(fields.get(CoNLLU_IndexField));
565-
IndexedWord gov = graphNodes.get(fields.get(CoNLLU_GovField));
566-
GrammaticalRelation reln = GrammaticalRelation.valueOf(fields.get(CoNLLU_RelnField));
567-
graphEdges.add(new SemanticGraphEdge(gov, dependent, reln, 1.0, false));
563+
if (fields.get(CoNLLU_GovField).equals("0")) {
564+
// no edges for the ROOT node
565+
graphRoots.add(dependent);
566+
} else {
567+
IndexedWord gov = graphNodes.get(fields.get(CoNLLU_GovField));
568+
GrammaticalRelation reln = GrammaticalRelation.valueOf(fields.get(CoNLLU_RelnField));
569+
graphEdges.add(new SemanticGraphEdge(gov, dependent, reln, 1.0, false));
570+
}
568571
}
569572
// build SemanticGraph
570-
SemanticGraph depParse = SemanticGraphFactory.makeFromEdges(graphEdges);
573+
SemanticGraph depParse = SemanticGraphFactory.makeFromEdges(graphEdges, graphRoots);
571574
// add dependency graph
572575
sentenceCoreMap.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, depParse);
573576

574577
if (hasEnhanced) {
575578
List<SemanticGraphEdge> enhancedEdges = new ArrayList<>();
576-
List<IndexedWord> roots = new ArrayList<>();
579+
List<IndexedWord> enhancedRoots = new ArrayList<>();
577580

578581
List<String> allLines = new ArrayList<>();
579582
allLines.addAll(lines);
@@ -585,16 +588,15 @@ public CoreMap convertCoNLLUSentenceToCoreMap(CoNLLUDocument doc, CoNLLUSentence
585588
for (String arc : arcs) {
586589
String[] arcPieces = arc.split(":", 2);
587590
if (arcPieces[0].equals("0")) {
588-
roots.add(dependent);
591+
enhancedRoots.add(dependent);
589592
} else {
590593
IndexedWord gov = graphNodes.get(arcPieces[0]);
591594
GrammaticalRelation reln = GrammaticalRelation.valueOf(arcPieces[1]);
592595
enhancedEdges.add(new SemanticGraphEdge(gov, dependent, reln, 1.0, false));
593596
}
594597
}
595598
}
596-
SemanticGraph enhancedParse = SemanticGraphFactory.makeFromEdges(enhancedEdges);
597-
enhancedParse.setRoots(roots);
599+
SemanticGraph enhancedParse = SemanticGraphFactory.makeFromEdges(enhancedEdges, enhancedRoots);
598600
sentenceCoreMap.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, enhancedParse);
599601
}
600602

src/edu/stanford/nlp/semgraph/SemanticGraphFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,29 @@ public static SemanticGraph makeFromEdges(Iterable<SemanticGraphEdge> edges) {
365365
return sg;
366366
}
367367

368+
/**
369+
* Given a list of edges, attempts to create and return a rooted SemanticGraph.
370+
*
371+
* TODO: throw Exceptions, or flag warnings on conditions for concern (no root, etc)
372+
*/
373+
public static SemanticGraph makeFromEdges(Iterable<SemanticGraphEdge> edges, Collection<IndexedWord> roots) {
374+
// Identify the root(s) of this graph
375+
SemanticGraph sg = new SemanticGraph();
376+
Collection<IndexedWord> vertices = getVerticesFromEdgeSet(edges);
377+
for (IndexedWord vertex : vertices) {
378+
sg.addVertex(vertex);
379+
}
380+
for (IndexedWord vertex : roots) {
381+
sg.addVertex(vertex);
382+
}
383+
for (SemanticGraphEdge edge : edges) {
384+
sg.addEdge(edge.getSource(),edge.getTarget(), edge.getRelation(), edge.getWeight(), edge.isExtra());
385+
}
386+
387+
sg.setRoots(roots);
388+
return sg;
389+
}
390+
368391
/**
369392
* Given an iterable set of edges, returns the set of vertices covered by these edges.
370393
*

0 commit comments

Comments
 (0)