-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ProcessDependencyConverterRequestTest.java
72 lines (58 loc) · 3.25 KB
/
ProcessDependencyConverterRequestTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package edu.stanford.nlp.trees;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Test;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.pipeline.CoreNLPProtos;
import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.trees.Tree;
public class ProcessDependencyConverterRequestTest {
static CoreNLPProtos.DependencyConverterRequest buildRequest(String ... trees) {
CoreNLPProtos.DependencyConverterRequest.Builder builder = CoreNLPProtos.DependencyConverterRequest.newBuilder();
for (String tree : trees) {
Tree t = Tree.valueOf(tree);
builder.addTrees(ProtobufAnnotationSerializer.toFlattenedTree(t));
}
return builder.build();
}
static void checkResults(CoreNLPProtos.DependencyConverterResponse response, String ... expectedResults) {
Assert.assertEquals(expectedResults.length, response.getConversionsList().size());
for (int i = 0; i < expectedResults.length; ++i) {
CoreNLPProtos.DependencyGraph responseGraph = response.getConversionsList().get(i).getGraph();
CoreNLPProtos.FlattenedParseTree responseTree = response.getConversionsList().get(i).getTree();
Tree tree = ProtobufAnnotationSerializer.fromProto(responseTree);
List<CoreLabel> sentence = tree.taggedLabeledYield(false);
SemanticGraph expected = SemanticGraph.valueOf(expectedResults[i], i);
SemanticGraph graph = ProtobufAnnotationSerializer.fromProto(responseGraph, sentence, null);
//for (IndexedWord word : expected.vertexSet()) {
// System.out.println(word + " " + word.index() + " " + word.sentIndex() + " " + word.docID());
//}
//for (IndexedWord word : graph.vertexSet()) {
// System.out.println(word + " " + word.index() + " " + word.sentIndex() + " " + word.docID());
//}
//System.out.println(expected.toCompactString());
//System.out.println(graph.toCompactString());
Assert.assertEquals(expected, graph);
}
}
/** Test a single Tree turning into Dependencies */
@Test
public void testOneTree() {
CoreNLPProtos.DependencyConverterRequest request = buildRequest("(ROOT (S (NP (NNP Jennifer)) (VP (VBZ has) (NP (JJ nice) (NNS antennae)))))");
CoreNLPProtos.DependencyConverterResponse response = ProcessDependencyConverterRequest.processRequest(request);
checkResults(response, "[has/VBZ-1 nsubj>Jennifer/NNP-0 obj>[antennae/NNS-3 amod>nice/JJ-2]]");
}
/** Test two trees turning into Dependencies */
@Test
public void testTwoTrees() {
CoreNLPProtos.DependencyConverterRequest request = buildRequest("(ROOT (S (NP (NNP Jennifer)) (VP (VBZ has) (NP (JJ nice) (NNS antennae)))))",
"(ROOT (S (NP (PRP She)) (VP (VBZ is) (ADJP (RB hella) (JJ basic)) (ADVP (RB though)))))");
CoreNLPProtos.DependencyConverterResponse response = ProcessDependencyConverterRequest.processRequest(request);
checkResults(response,
"[has/VBZ-1 nsubj>Jennifer/NNP-0 obj>[antennae/NNS-3 amod>nice/JJ-2]]",
"[basic/JJ-3 nsubj>She/PRP-0 cop>is/VBZ-1 advmod>hella/RB-2 advmod>though/RB-4]");
}
}