10
10
import edu .stanford .nlp .ling .CoreAnnotations ;
11
11
import edu .stanford .nlp .ling .IndexedWord ;
12
12
import edu .stanford .nlp .semgraph .SemanticGraph ;
13
+ import edu .stanford .nlp .semgraph .SemanticGraphEdge ;
13
14
import edu .stanford .nlp .semgraph .semgrex .SemgrexPattern ;
14
15
import edu .stanford .nlp .trees .EnglishGrammaticalRelations ;
16
+ import edu .stanford .nlp .util .XMLUtils ;
15
17
16
18
public class SsurgeonTest {
17
19
@@ -31,15 +33,16 @@ public void readXMLEmptyPattern() {
31
33
assertEquals (pattern .size (), 1 );
32
34
}
33
35
36
+ static final String newline = System .getProperty ("line.separator" );
37
+
34
38
@ Test
35
- public void readXMLEdit () {
36
- String newline = System .getProperty ("line.separator" );
39
+ public void readXMLAddEdgeExecute () {
37
40
String doc = String .join (newline ,
38
41
"<ssurgeon-pattern-list>" ,
39
42
" <ssurgeon-pattern>" ,
40
43
" <uid>38</uid>" ,
41
44
" <notes>This is a simple test of addEdge</notes>" ,
42
- " <semgrex>{}=a1 > {}=a2</semgrex>" ,
45
+ " <semgrex>" + XMLUtils . escapeXML ( " {}=a1 > {}=a2" ) + " </semgrex>" ,
43
46
" <edit-list>addEdge -gov a1 -dep a2 -reln dep -weight 0.5</edit-list>" ,
44
47
" </ssurgeon-pattern>" ,
45
48
"</ssurgeon-pattern-list>" );
@@ -50,11 +53,37 @@ public void readXMLEdit() {
50
53
51
54
SemanticGraph sg = SemanticGraph .valueOf ("[A obj> B obj> C]" );
52
55
Collection <SemanticGraph > newSgs = pattern .execute (sg );
53
- // TODO: perhaps it would be better to have an execution scheme
54
- // where one graph has all possible modifications applied
55
56
assertEquals (newSgs .size (), 2 );
56
57
}
57
58
59
+
60
+ /**
61
+ * Test that AddEdge, when iterated, adds exactly one more edge
62
+ * between each parent/child pair if they matched the target relation
63
+ */
64
+ @ Test
65
+ public void readXMLAddEdgeIterate () {
66
+ String doc = String .join (newline ,
67
+ "<ssurgeon-pattern-list>" ,
68
+ " <ssurgeon-pattern>" ,
69
+ " <uid>38</uid>" ,
70
+ " <notes>This is a simple test of addEdge</notes>" ,
71
+ " <semgrex>" + XMLUtils .escapeXML ("{}=a1 >obj {}=a2" ) + "</semgrex>" ,
72
+ " <edit-list>addEdge -gov a1 -dep a2 -reln dep -weight 0.5</edit-list>" ,
73
+ " </ssurgeon-pattern>" ,
74
+ "</ssurgeon-pattern-list>" );
75
+ Ssurgeon inst = Ssurgeon .inst ();
76
+ List <SsurgeonPattern > patterns = inst .readFromString (doc );
77
+ assertEquals (patterns .size (), 1 );
78
+ SsurgeonPattern pattern = patterns .get (0 );
79
+
80
+ SemanticGraph sg = SemanticGraph .valueOf ("[A obj> B obj> C nsubj> [D obj> E]]" );
81
+ SemanticGraph newSg = pattern .iterate (sg );
82
+ SemanticGraph expected = SemanticGraph .valueOf ("[A-0 obj> B-1 dep> B-1 obj> C-2 dep> C-2 nsubj> [D-3 obj> E-4 dep> E-4]]" );
83
+
84
+ assertEquals (newSg , expected );
85
+ }
86
+
58
87
/**
59
88
* Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
60
89
* a semgrex pattern, and then the resulting actions over the named nodes in the
0 commit comments