File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed
src/edu/stanford/nlp/semgraph/semgrex
test/src/edu/stanford/nlp/semgraph/semgrex Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change 1212/**
1313 * Stores attributes for a Semgrex NodePattern.
1414 *<br>
15- * Refactored out of the parser itself for a couple reason :
15+ * Refactored out of the parser itself for a couple reasons :
1616 *<ul>
1717 *<li> Allows combining isRoot ($) with node restrictions (word:foo)
1818 *<li> Can pass this object around, allowing for more refactoring in the semgrex parser
@@ -60,10 +60,10 @@ public boolean empty() {
6060 }
6161
6262 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- }
6663 if (!negated ) {
64+ if (positiveAttributes .contains (key )) {
65+ throw new SemgrexParseException ("Duplicate attribute " + key + " found in semgrex expression" );
66+ }
6767 positiveAttributes .add (key );
6868 }
6969 attributes .add (new Triple (key , value , negated ));
Original file line number Diff line number Diff line change @@ -1207,6 +1207,34 @@ public void testIllegal() {
12071207 }
12081208 }
12091209
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+
12101238 public void testAdjacent () {
12111239 // test using a colon expression so that the targeted nodes
12121240 // are the nodes which show up
You can’t perform that action at this time.
0 commit comments