|
2 | 2 |
|
3 | 3 | import edu.stanford.nlp.ling.*;
|
4 | 4 | import edu.stanford.nlp.semgraph.*;
|
| 5 | +import edu.stanford.nlp.trees.GrammaticalRelation; |
5 | 6 | import edu.stanford.nlp.util.*;
|
6 | 7 |
|
7 | 8 | import static org.junit.Assert.assertEquals;
|
|
11 | 12 |
|
12 | 13 | import java.io.*;
|
13 | 14 | import java.util.ArrayList;
|
| 15 | +import java.util.HashMap; |
14 | 16 | import java.util.List;
|
| 17 | +import java.util.Map; |
15 | 18 | import java.util.Properties;
|
16 | 19 |
|
17 | 20 | import org.junit.Before;
|
@@ -111,10 +114,14 @@ public void testReadingInCoNLLUFile() throws ClassNotFoundException, IOException
|
111 | 114 | }
|
112 | 115 |
|
113 | 116 | // Compare sentence ids
|
| 117 | + // Check that the enhanced dependencies exist |
| 118 | + // (these sentences should all have them, but we will check it |
| 119 | + // more thoroughly for the Empty test sentence) |
114 | 120 | // Check number of keys on each sentence
|
115 | 121 | for (int i = 0; i < sentences.size(); ++i) {
|
116 | 122 | assertEquals(Integer.valueOf(i), sentences.get(i).get(CoreAnnotations.SentenceIndexAnnotation.class));
|
117 |
| - assertEquals(4, sentences.get(i).keySet().size()); |
| 123 | + assertTrue(sentences.get(i).containsKey(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class)); |
| 124 | + assertEquals(5, sentences.get(i).keySet().size()); |
118 | 125 | }
|
119 | 126 |
|
120 | 127 | // Check the document tokens and the sentence tokens lists are the same
|
@@ -319,12 +326,66 @@ public void testReadingInCoNLLUFile() throws ClassNotFoundException, IOException
|
319 | 326 | }
|
320 | 327 | }
|
321 | 328 |
|
322 |
| - public String emptiesPath = String.format("edu/stanford/nlp/pipeline/en-example.conllu"); |
| 329 | + public static final String emptiesPath = String.format("edu/stanford/nlp/pipeline/en-example.conllu"); |
323 | 330 |
|
324 |
| - String[] EXPECTED_ENGLISH_WORDS = { |
| 331 | + static final String[] EXPECTED_ENGLISH_WORDS = { |
325 | 332 | "Over", "300", "Iraqis", "are", "reported", "dead", "and", "500", "wounded", "in", "Fallujah", "alone", "."
|
326 | 333 | };
|
327 | 334 |
|
| 335 | + static final String[][] EXPECTED_ENHANCED_EDGES = { |
| 336 | + {"1", "2", "advmod"}, |
| 337 | + {"2", "3", "nummod"}, |
| 338 | + {"3", "5", "nsubj:pass"}, |
| 339 | + {"3", "6", "nsubj:xsubj"}, |
| 340 | + {"3", "8", "nsubj:pass"}, |
| 341 | + {"4", "5", "aux:pass"}, |
| 342 | + {"6", "5", "xcomp"}, |
| 343 | + {"7", "8", "cc"}, |
| 344 | + {"7", "8.1", "cc"}, |
| 345 | + {"8", "5", "conj:and"}, |
| 346 | + {"8", "8.1", "nsubj:pass"}, |
| 347 | + {"8", "9", "nsubj:xsubj"}, |
| 348 | + {"8.1", "5", "conj:and"}, |
| 349 | + {"9", "8.1", "xcomp"}, |
| 350 | + {"10", "11", "case"}, |
| 351 | + {"11", "5", "obl:in"}, |
| 352 | + {"12", "11", "advmod"}, |
| 353 | + {"13", "5", "punct"}, |
| 354 | + }; |
| 355 | + static final SemanticGraph EXPECTED_ENHANCED = buildEnhancedTest(); |
| 356 | + static SemanticGraph buildEnhancedTest() { |
| 357 | + Map<String, IndexedWord> graphNodes = new HashMap<>(); |
| 358 | + for (int i = 0; i < EXPECTED_ENGLISH_WORDS.length; ++i) { |
| 359 | + String index = Integer.toString(i+1); |
| 360 | + CoreLabel cl = new CoreLabel(); |
| 361 | + cl.setValue(EXPECTED_ENGLISH_WORDS[i]); |
| 362 | + cl.setIndex(i+1); |
| 363 | + cl.setSentIndex(0); |
| 364 | + graphNodes.put(index, new IndexedWord(cl)); |
| 365 | + } |
| 366 | + { |
| 367 | + String index = "8.1"; |
| 368 | + CoreLabel cl = new CoreLabel(); |
| 369 | + cl.setValue("reported"); |
| 370 | + cl.setIndex(8); |
| 371 | + cl.set(CoreAnnotations.EmptyIndexAnnotation.class, 1); |
| 372 | + cl.setSentIndex(0); |
| 373 | + graphNodes.put(index, new IndexedWord(cl)); |
| 374 | + } |
| 375 | + List<SemanticGraphEdge> edges = new ArrayList<>(); |
| 376 | + for (String[] edge : EXPECTED_ENHANCED_EDGES) { |
| 377 | + IndexedWord dep = graphNodes.get(edge[0]); |
| 378 | + IndexedWord gov = graphNodes.get(edge[1]); |
| 379 | + GrammaticalRelation reln = GrammaticalRelation.valueOf(edge[2]); |
| 380 | + edges.add(new SemanticGraphEdge(gov, dep, reln, 1.0, false)); |
| 381 | + } |
| 382 | + List<IndexedWord> roots = new ArrayList<>(); |
| 383 | + roots.add(graphNodes.get("5")); |
| 384 | + SemanticGraph enhancedParse = SemanticGraphFactory.makeFromEdges(edges); |
| 385 | + enhancedParse.setRoots(roots); |
| 386 | + return enhancedParse; |
| 387 | + } |
| 388 | + |
328 | 389 | @Test
|
329 | 390 | /**
|
330 | 391 | * Here we run fewer tests. Just make sure the EmptyToken is properly handled,
|
@@ -355,6 +416,9 @@ public void testReadingInEmpties() throws ClassNotFoundException, IOException {
|
355 | 416 | assertEquals(Integer.valueOf(1), empty.get(CoreAnnotations.EmptyIndexAnnotation.class));
|
356 | 417 | assertEquals(0, empty.sentIndex());
|
357 | 418 | assertEquals("reported", empty.value());
|
| 419 | + |
| 420 | + SemanticGraph enhanced = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class); |
| 421 | + assertEquals(EXPECTED_ENHANCED, enhanced); |
358 | 422 | }
|
359 | 423 |
|
360 | 424 | }
|
0 commit comments