Skip to content

Commit

Permalink
Fix the output of optional CoordinationPatterns in Tregex
Browse files Browse the repository at this point in the history
  • Loading branch information
AngledLuffa committed Jul 17, 2023
1 parent 9c67d39 commit a9965b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/edu/stanford/nlp/trees/tregex/CoordinationPattern.java
Expand Up @@ -45,10 +45,13 @@ public String toString() {
if (isNegated()) {
sb.append("!(");
}
if (isOptional()) {
sb.append("?(");
}
for (TregexPattern node : children) {
sb.append(node.toString());
}
if (isNegated()) {
if (isNegated() || isOptional()) {
sb.append(")");
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/edu/stanford/nlp/trees/tregex/DescriptionPattern.java
Expand Up @@ -266,6 +266,8 @@ public String toString() {
sb.append('=');
sb.append(name);
}
// TODO: is this space necessary? The CoordinationPattern could put spaces between terms
// Currently this results in extra blank spaces at the end of a pattern
sb.append(' ');
if (child != null) {
sb.append(child.toString());
Expand Down
9 changes: 9 additions & 0 deletions test/src/edu/stanford/nlp/trees/tregex/TregexTest.java
Expand Up @@ -1559,6 +1559,15 @@ public void testNegatedOptional() {
}
}

public void testOptionalToString() {
TregexPattern pattern;
pattern = TregexPattern.compile("A ?(< B < C)");
assertEquals("Root (A ?(< B < C ))", pattern.toString());

pattern = TregexPattern.compile("A ?< B");
assertEquals("Root (A ?< B )", pattern.toString());
}

/**
* 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!
Expand Down

0 comments on commit a9965b2

Please sign in to comment.