Skip to content

Commit b5719a2

Browse files
committed
Allow negative constraints that match an existing positive constraint. Actually, there may not even be a reason to disallow two positive constraints if they are both regex
1 parent 821b8fd commit b5719a2

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/edu/stanford/nlp/semgraph/semgrex/NodeAttributes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
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));

test/src/edu/stanford/nlp/semgraph/semgrex/SemgrexTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)