Skip to content

Commit 9e2ff5c

Browse files
committed
Most basic connection of saving the values, not testing checking the values yet
1 parent 0851981 commit 9e2ff5c

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ private Attribute buildRegexAttribute(String key, String value, boolean negated,
166166
}
167167
}
168168

169-
private boolean checkMatch(Attribute attr, boolean ignoreCase, String nodeValue, VariableStrings tempVariableStrings) {
169+
private boolean checkMatch(Attribute attr, boolean ignoreCase, String nodeValue,
170+
VariableStrings variableStrings, VariableStrings tempVariableStrings) {
170171
if (nodeValue == null) {
171172
// treat non-existent attributes has having matched a negated expression
172173
// so for example, `cpos!:NUM` matches not having a cpos at all
@@ -188,7 +189,20 @@ private boolean checkMatch(Attribute attr, boolean ignoreCase, String nodeValue,
188189
Matcher matcher = ((Pattern) toMatch).matcher(nodeValue);
189190
if (matcher.matches()) {
190191
matches = true;
191-
// TODO: check the VariableStrings here
192+
for (Pair<Integer, String> varGroup : attr.variableGroups) {
193+
String existingString = variableStrings.getString(varGroup.second());
194+
if (existingString == null) {
195+
existingString = tempVariableStrings.getString(varGroup.second());
196+
}
197+
String matchedString = matcher.group(varGroup.first());
198+
if (existingString != null && !existingString.equals(matchedString)) {
199+
matches = false;
200+
break;
201+
}
202+
if (matchedString != null) {
203+
tempVariableStrings.setVar(varGroup.second(), matchedString);
204+
}
205+
}
192206
} else {
193207
matches = false;
194208
}
@@ -203,7 +217,7 @@ private boolean checkMatch(Attribute attr, boolean ignoreCase, String nodeValue,
203217

204218
@SuppressWarnings("unchecked")
205219
public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean ignoreCase,
206-
VariableStrings tempVariableStrings) {
220+
VariableStrings variableStrings, VariableStrings tempVariableStrings) {
207221
// System.out.println(node.word());
208222
if (isRoot) {
209223
// System.out.println("checking root");
@@ -237,7 +251,7 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
237251
// }
238252
// System.out.println(nodeValue);
239253

240-
boolean matches = checkMatch(attr, ignoreCase, nodeValue, tempVariableStrings);
254+
boolean matches = checkMatch(attr, ignoreCase, nodeValue, variableStrings, tempVariableStrings);
241255

242256
if (!matches) {
243257
// System.out.println("doesn't match");
@@ -265,7 +279,7 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
265279
}
266280

267281
// TODO: not connected to varGroups yet
268-
boolean matches = checkMatch(attr, ignoreCase, nodeValue, tempVariableStrings);
282+
boolean matches = checkMatch(attr, ignoreCase, nodeValue, variableStrings, tempVariableStrings);
269283
if (!matches) {
270284
return negDesc;
271285
}
@@ -519,7 +533,7 @@ private void goToNextNodeMatch() {
519533
} else {
520534
boolean found = myNode.nodeAttrMatch(nextMatch,
521535
hyp ? sg : sg_aligned,
522-
ignoreCase, tempVariableStrings);
536+
ignoreCase, variableStrings, tempVariableStrings);
523537
if (found) {
524538
// nodeAttrMatch already checks negDesc, so no need to
525539
// check for that here
@@ -530,7 +544,7 @@ private void goToNextNodeMatch() {
530544
} else { // try to match the description pattern.
531545
boolean found = myNode.nodeAttrMatch(nextMatch,
532546
hyp ? sg : sg_aligned,
533-
ignoreCase, tempVariableStrings);
547+
ignoreCase, variableStrings, tempVariableStrings);
534548
if (found) {
535549
// nodeAttrMatch already checks negDesc, so no need to
536550
// check for that here

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
import java.util.ArrayList;
77
import java.util.HashMap;
8+
import java.util.HashSet;
89
import java.util.List;
910
import java.util.Map;
1011
import java.util.Set;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
1114

1215
import edu.stanford.nlp.ling.CoreAnnotations;
1316
import edu.stanford.nlp.ling.IndexedWord;
@@ -1570,9 +1573,16 @@ public void testBatchUniq() {
15701573
}
15711574

15721575
public void testVariableGroups() {
1573-
outputResults("{word:/(ate)/#1%name}",
1574-
"[ate subj>Bill obj>[muffins compound>blueberry]]",
1575-
"ate");
1576+
SemgrexPattern pattern = SemgrexPattern.compile("{word:/(.*ill.*)/#1%name}");
1577+
SemanticGraph graph = SemanticGraph.valueOf("[ate-2 subj> Bill-1 obj>[muffins-5 compound> Blueberry-3 compound> filled-4]]");
1578+
Set<String> matches = new HashSet<>();
1579+
SemgrexMatcher matcher = pattern.matcher(graph);
1580+
while (matcher.find()) {
1581+
assertNotNull(matcher.variableStrings.getString("name"));
1582+
matches.add(matcher.variableStrings.getString("name"));
1583+
}
1584+
Set<String> expectedMatches = Stream.of("Bill", "filled").collect(Collectors.toCollection(HashSet::new));
1585+
assertEquals(expectedMatches, matches);
15761586
}
15771587

15781588
public static void outputBatchResults(SemgrexPattern pattern, List<CoreMap> sentences) {

0 commit comments

Comments
 (0)