From 0eba477adaabcfb9bfd1d737f092963bf2600526 Mon Sep 17 00:00:00 2001 From: John Bauer Date: Thu, 14 Mar 2024 00:17:37 -0700 Subject: [PATCH] The root of a tregex expression now keeps track of what variables it knows about --- src/edu/stanford/nlp/trees/tregex/TregexParser.java | 2 +- src/edu/stanford/nlp/trees/tregex/TregexParser.jj | 2 +- src/edu/stanford/nlp/trees/tregex/TregexPattern.java | 6 ++++++ .../stanford/nlp/trees/tregex/TregexPatternCompiler.java | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/edu/stanford/nlp/trees/tregex/TregexParser.java b/src/edu/stanford/nlp/trees/tregex/TregexParser.java index 87d96aad90..a557b323f9 100644 --- a/src/edu/stanford/nlp/trees/tregex/TregexParser.java +++ b/src/edu/stanford/nlp/trees/tregex/TregexParser.java @@ -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 knownVariables = Generics.newHashSet(); + Set knownVariables = Generics.newHashSet(); public TregexParser(java.io.Reader stream, Function basicCatFunction, diff --git a/src/edu/stanford/nlp/trees/tregex/TregexParser.jj b/src/edu/stanford/nlp/trees/tregex/TregexParser.jj index d2f5add723..63ded86b8a 100644 --- a/src/edu/stanford/nlp/trees/tregex/TregexParser.jj +++ b/src/edu/stanford/nlp/trees/tregex/TregexParser.jj @@ -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 knownVariables = Generics.newHashSet(); + Set knownVariables = Generics.newHashSet(); public TregexParser(java.io.Reader stream, Function basicCatFunction, diff --git a/src/edu/stanford/nlp/trees/tregex/TregexPattern.java b/src/edu/stanford/nlp/trees/tregex/TregexPattern.java index 426b082566..9d094d758d 100644 --- a/src/edu/stanford/nlp/trees/tregex/TregexPattern.java +++ b/src/edu/stanford/nlp/trees/tregex/TregexPattern.java @@ -361,6 +361,7 @@ public abstract class TregexPattern implements Serializable { private boolean neg; // = false; private boolean opt; // = false; private String patternString; + private Set knownVariables; void negate() { neg = true; @@ -484,6 +485,11 @@ 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 knownVariables) { + this.knownVariables = knownVariables; + } + /** * @return A single-line string representation of the pattern */ diff --git a/src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java b/src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java index 661d0bc24a..8520d81b0d 100644 --- a/src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java +++ b/src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java @@ -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) {