Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negated disjunction - there is an infinite loop if the first clau… #1238

Merged
merged 1 commit into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/edu/stanford/nlp/trees/tregex/CoordinationPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,25 @@ public boolean matches() { // also known as "FUN WITH LOGIC"
}
return true;
}
} else {
// oops, this didn't work.
} else if (!myNode.isNegated()) {
// oops, this didn't work - positive conjunction version
children[currChild].resetChildIter();
// go backwards to see if we can continue matching from an
// earlier location.
--currChild;
if (currChild < 0) {
return myNode.isOptional();
}
} else {
// oops, this didn't work - negated disjunction version
// here we just fail
// any previous children had to fail to get to this point,
// which means those children have only the one correct state.
// backtracking to find other correct states is pointless
// and in fact causes an infinite loop of going backwards,
// then advancing back to this child and failing again
currChild = -1;
return myNode.isOptional();
}
}
} else {
Expand Down
16 changes: 16 additions & 0 deletions test/src/edu/stanford/nlp/trees/tregex/TregexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,22 @@ public void testOptional() {
assertFalse(matcher.find());
}

/**
* A user supplied an example of a negated disjunction which went into an infinite loop.
* Apparently no one had ever used a negated disjunction of tree structures before!
* <br>
* The problem was that the logic at the time tried to backtrack in
* the disjunction to find a better match, but that resulted in it
* going back and forth between the failed clause which was accepted
* and the successful clause which was rejected. The problem being
* that the first half of the disjunction doesn't match, so the
* pattern is successful up to that point, but the second half does
* match, causing the pattern to be rejected and restarted.
*/
public void testNegatedDisjunction() {
runTest("NP![</,/|.(JJ<else)]", "( (NP (NP (NN anyone)) (ADJP (JJ else))))", "(NP (NP (NN anyone)) (ADJP (JJ else)))");
}

/**
* Stores an input and the expected output. Obviously this is only
* expected to work with a given pattern, but this is a bit more
Expand Down