File tree 2 files changed +32
-4
lines changed
src/edu/stanford/nlp/semgraph/semgrex
test/src/edu/stanford/nlp/semgraph/semgrex
2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change 12
12
/**
13
13
* Stores attributes for a Semgrex NodePattern.
14
14
*<br>
15
- * Refactored out of the parser itself for a couple reason :
15
+ * Refactored out of the parser itself for a couple reasons :
16
16
*<ul>
17
17
*<li> Allows combining isRoot ($) with node restrictions (word:foo)
18
18
*<li> Can pass this object around, allowing for more refactoring in the semgrex parser
@@ -60,10 +60,10 @@ public boolean empty() {
60
60
}
61
61
62
62
public void setAttribute (String key , String value , boolean negated ) {
63
- if (positiveAttributes .contains (key )) {
64
- throw new SemgrexParseException ("Duplicate attribute " + key + " found in semgrex expression" );
65
- }
66
63
if (!negated ) {
64
+ if (positiveAttributes .contains (key )) {
65
+ throw new SemgrexParseException ("Duplicate attribute " + key + " found in semgrex expression" );
66
+ }
67
67
positiveAttributes .add (key );
68
68
}
69
69
attributes .add (new Triple (key , value , negated ));
Original file line number Diff line number Diff line change @@ -1207,6 +1207,34 @@ public void testIllegal() {
1207
1207
}
1208
1208
}
1209
1209
1210
+ public void testDuplicateConstraints () {
1211
+ // There should be an exception if the same attribute shows up
1212
+ // twice as a positive attribute
1213
+ // Although it isn't clear that's necessary,
1214
+ // since both portions could be regex which match different things
1215
+ try {
1216
+ String pattern = "{word:foo;word:bar}" ;
1217
+ SemgrexPattern semgrex = SemgrexPattern .compile (pattern );
1218
+ throw new RuntimeException ("This expression is now illegal" );
1219
+ } catch (SemgrexParseException e ) {
1220
+ // yay
1221
+ }
1222
+
1223
+ // this should parse since negative constraints which
1224
+ // match positive constraints are allowed
1225
+ String pattern = "{word:/.*i.*/;word!:/.*m.*/}" ;
1226
+ SemgrexPattern semgrex = SemgrexPattern .compile (pattern );
1227
+ runTest (pattern ,
1228
+ "[ate/NN subj>Bill/NN obj>[muffins compound>blueberry]]" ,
1229
+ "Bill/NN" );
1230
+
1231
+ pattern = "{word:/.*i.*/;word!:/.*z.*/}" ;
1232
+ semgrex = SemgrexPattern .compile (pattern );
1233
+ runTest (pattern ,
1234
+ "[ate/NN subj>Bill/NN obj>[muffins compound>blueberry]]" ,
1235
+ "Bill/NN" , "muffins" );
1236
+ }
1237
+
1210
1238
public void testAdjacent () {
1211
1239
// test using a colon expression so that the targeted nodes
1212
1240
// are the nodes which show up
You can’t perform that action at this time.
0 commit comments