Skip to content

Commit 2d1120e

Browse files
committed
Backreferencing works fine for relation names. Update the documentation and add a test of the feature
1 parent ebb4210 commit 2d1120e

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@
169169
*
170170
* It is also possible to name relations. For example, you can write the pattern
171171
* {@code {idx:1} >=reln {idx:2}} The name of the relation will then
172-
* be stored in the matcher and can be extracted with {@code getRelnName("reln")}
173-
* At present, though, there is no backreferencing capability such as with the
174-
* named nodes; this is only useful when using the API to extract the name of the
175-
* relation used when making the match.
172+
* be stored in the matcher and can be extracted with {@code getRelnName("reln")}.
173+
* If the relation is later referenced a second time, the type of
174+
* relation must be the same, or the potential match will not be
175+
* accepted.
176176
* <p>
177177
* In the case of ancestor and descendant relations, the <b>last</b>
178178
* relation in the sequence of relations is the name used.

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,56 @@ public void testNamedRelation() {
718718
assertFalse(matcher.find());
719719
}
720720

721+
/**
722+
* The named relation feature should incorporate backreferences
723+
*/
724+
public void testNamedRelationBackreference() {
725+
SemanticGraph graph = SemanticGraph.valueOf("[ate subj>Bill obj>[muffins compound>blueberry]]");
726+
727+
SemgrexPattern pattern = SemgrexPattern.compile("{}=A >=foo ({}=B >=foo {}=C)");
728+
SemgrexMatcher matcher = pattern.matcher(graph);
729+
assertFalse(matcher.find());
730+
731+
graph = SemanticGraph.valueOf("[ate dep> [Bill dep> cat]]");
732+
matcher = pattern.matcher(graph);
733+
assertTrue(matcher.find());
734+
assertEquals("ate", matcher.getNode("A").toString());
735+
assertEquals("Bill", matcher.getNode("B").toString());
736+
assertEquals("cat", matcher.getNode("C").toString());
737+
assertEquals("dep", matcher.getRelnString("foo"));
738+
assertFalse(matcher.find());
739+
740+
graph = SemanticGraph.valueOf("[ate cop> [Bill dep> cat]]");
741+
matcher = pattern.matcher(graph);
742+
assertFalse(matcher.find());
743+
744+
graph = SemanticGraph.valueOf("[ate dep> [Bill cop> cat]]");
745+
matcher = pattern.matcher(graph);
746+
assertFalse(matcher.find());
747+
748+
graph = SemanticGraph.valueOf("[antennae amod> big amod> blue]");
749+
pattern = SemgrexPattern.compile("{}=A >=foo {}=B >=foo ({}=C !== {}=B)");
750+
matcher = pattern.matcher(graph);
751+
assertTrue(matcher.find());
752+
assertEquals("antennae", matcher.getNode("A").toString());
753+
assertEquals("big", matcher.getNode("B").toString());
754+
assertEquals("blue", matcher.getNode("C").toString());
755+
assertEquals("amod", matcher.getRelnString("foo"));
756+
757+
assertTrue(matcher.find());
758+
assertEquals("antennae", matcher.getNode("A").toString());
759+
assertEquals("blue", matcher.getNode("B").toString());
760+
assertEquals("big", matcher.getNode("C").toString());
761+
assertEquals("amod", matcher.getRelnString("foo"));
762+
763+
assertFalse(matcher.find());
764+
765+
graph = SemanticGraph.valueOf("[antennae amod> big dep> blue]");
766+
pattern = SemgrexPattern.compile("{}=A >=foo {}=B >=foo ({}=C !== {}=B)");
767+
matcher = pattern.matcher(graph);
768+
assertFalse(matcher.find());
769+
}
770+
721771
public void testAttributeConjunction() {
722772
// A possible user submitted error: https://github.com/stanfordnlp/CoreNLP/issues/552
723773
// A match with both POS and word labeled should have both attributes on the same node

0 commit comments

Comments
 (0)