Skip to content

Commit 0de1865

Browse files
committed
Refactor checking matches for Attributes. Also, functionality change - if an Attribute is negated, accept the node not having any value at all
1 parent bff3588 commit 0de1865

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

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

+30-22
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,35 @@ public NodePattern(GraphRelation r, boolean negDesc,
118118
this.variableGroups = Collections.unmodifiableList(variableGroups);
119119
}
120120

121+
private boolean checkMatch(Attribute attr, boolean ignoreCase, String nodeValue) {
122+
if (nodeValue == null) {
123+
// treat non-existent attributes has having matched a negated expression
124+
// so for example, `cpos!:NUM` matches not having a cpos at all
125+
return attr.negated;
126+
}
127+
128+
// Get the node pattern
129+
Object toMatch = ignoreCase ? attr.caseless : attr.cased;
130+
boolean matches;
131+
if (toMatch instanceof Boolean) {
132+
matches = ((Boolean) toMatch);
133+
} else if (toMatch instanceof String) {
134+
if (ignoreCase) {
135+
matches = nodeValue.equalsIgnoreCase(toMatch.toString());
136+
} else {
137+
matches = nodeValue.equals(toMatch.toString());
138+
}
139+
} else if (toMatch instanceof Pattern) {
140+
matches = ((Pattern) toMatch).matcher(nodeValue).matches();
141+
} else {
142+
throw new IllegalStateException("Unknown matcher type: " + toMatch + " (of class + " + toMatch.getClass() + ")");
143+
}
144+
if (attr.negated) {
145+
matches = !matches;
146+
}
147+
return matches;
148+
}
149+
121150
@SuppressWarnings("unchecked")
122151
public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean ignoreCase) {
123152
// System.out.println(node.word());
@@ -152,29 +181,8 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
152181
nodeValue = value.toString();
153182
// }
154183
// System.out.println(nodeValue);
155-
if (nodeValue == null)
156-
return negDesc;
157-
158-
// Get the node pattern
159-
Object toMatch = ignoreCase ? attr.caseless : attr.cased;
160-
boolean matches;
161-
if (toMatch instanceof Boolean) {
162-
matches = ((Boolean) toMatch);
163-
} else if (toMatch instanceof String) {
164-
if (ignoreCase) {
165-
matches = nodeValue.equalsIgnoreCase(toMatch.toString());
166-
} else {
167-
matches = nodeValue.equals(toMatch.toString());
168-
}
169-
} else if (toMatch instanceof Pattern) {
170-
matches = ((Pattern) toMatch).matcher(nodeValue).matches();
171-
} else {
172-
throw new IllegalStateException("Unknown matcher type: " + toMatch + " (of class + " + toMatch.getClass() + ")");
173-
}
174-
if (attr.negated) {
175-
matches = !matches;
176-
}
177184

185+
boolean matches = checkMatch(attr, ignoreCase, nodeValue);
178186
if (!matches) {
179187
// System.out.println("doesn't match");
180188
// System.out.println("");

0 commit comments

Comments
 (0)