Skip to content

Commit

Permalink
The root of a tregex expression now keeps track of what variables it …
Browse files Browse the repository at this point in the history
…knows about
  • Loading branch information
AngledLuffa committed Mar 14, 2024
1 parent 79833b4 commit 7f70ad8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/trees/tregex/TregexParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TregexParser implements TregexParserConstants {
// keep track of which variables we've seen, so that we can reject
// some nonsense patterns such as ones that reset variables or link
// to variables that haven't been set
private Set<String> knownVariables = Generics.newHashSet();
Set<String> knownVariables = Generics.newHashSet();

public TregexParser(java.io.Reader stream,
Function<String, String> basicCatFunction,
Expand Down
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/trees/tregex/TregexParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TregexParser {
// keep track of which variables we've seen, so that we can reject
// some nonsense patterns such as ones that reset variables or link
// to variables that haven't been set
private Set<String> knownVariables = Generics.newHashSet();
Set<String> knownVariables = Generics.newHashSet();

public TregexParser(java.io.Reader stream,
Function<String, String> basicCatFunction,
Expand Down
10 changes: 10 additions & 0 deletions src/edu/stanford/nlp/trees/tregex/TregexPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ public abstract class TregexPattern implements Serializable {
private boolean neg; // = false;
private boolean opt; // = false;
private String patternString;
private Set<String> knownVariables;

void negate() {
neg = true;
Expand Down Expand Up @@ -484,6 +485,15 @@ void setPatternString(String patternString) {
this.patternString = patternString;
}

/** Only used by the TregexPatternCompiler to track the known variables in the tregex (and only at the root). Pseudo-final. */
void setKnownVariables(Set<String> knownVariables) {
this.knownVariables = knownVariables;
}

public Set<String> knownVariables() {
return Collections.unmodifiableSet(knownVariables);
}

/**
* @return A single-line string representation of the pattern
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public TregexPattern compile(String tregex) {
TregexParser parser = new TregexParser(new StringReader(tregex + '\n'),
basicCatFunction, headFinder);
pattern = parser.Root();
pattern.setKnownVariables(parser.knownVariables);
} catch (TokenMgrError tme) {
throw new TregexParseException("Could not parse " + tregex, tme);
} catch (ParseException e) {
Expand Down

0 comments on commit 7f70ad8

Please sign in to comment.