public DescriptionPattern(Relation newRelation, Edu.Stanford.Nlp.Trees.Tregex.DescriptionPattern oldPattern) { this.rel = newRelation; this.negDesc = oldPattern.negDesc; this.isLink = oldPattern.isLink; this.linkedName = oldPattern.linkedName; this.stringDesc = oldPattern.stringDesc; this.descriptionMode = oldPattern.descriptionMode; this.descPattern = oldPattern.descPattern; this.exactMatch = oldPattern.exactMatch; this.stringFilter = oldPattern.stringFilter; this.name = oldPattern.name; this.SetChild(oldPattern.child); this.basicCatFunction = oldPattern.basicCatFunction; this.variableGroups = oldPattern.variableGroups; }
public DescriptionPattern(Relation rel, bool negDesc, string desc, string name, bool useBasicCat, Func <string, string> basicCatFunction, IList <Pair <int, string> > variableGroups, bool isLink, string linkedName) { // what size string matchers to use before switching to regex for // disjunction matches // todo: conceptually final, but we'd need to rewrite TregexParser // to make it so. // also conceptually final, but it depends on the child // specifies the groups in a regex that are captured as matcher-global string variables // for example, /^:$/ // for example, /^[$]$/ // for example, /^-NONE-$/ // for example, /^JJ/ this.rel = rel; this.negDesc = negDesc; this.isLink = isLink; this.linkedName = linkedName; if (desc != null) { stringDesc = desc; // TODO: factor out some of these blocks of code if (desc.Equals("__") || desc.Equals("/.*/") || desc.Equals("/^.*$/")) { descriptionMode = DescriptionPattern.DescriptionMode.Anything; descPattern = null; exactMatch = null; stringFilter = null; } else { if (SingleWordPattern.Matcher(desc).Matches()) { // Expressions are written like this to put special characters // in the tregex matcher, but a regular expression is less // efficient than a simple string match descriptionMode = DescriptionPattern.DescriptionMode.Exact; descPattern = null; Java.Util.Regex.Matcher matcher = SingleWordPattern.Matcher(desc); matcher.Matches(); string matchedGroup = null; for (int i = 1; i <= matcher.GroupCount(); ++i) { if (matcher.Group(i) != null) { matchedGroup = matcher.Group(i); break; } } exactMatch = matchedGroup; stringFilter = null; } else { //log.info("DescriptionPattern: converting " + desc + " to " + exactMatch); if (MultiWordPattern.Matcher(desc).Matches()) { Java.Util.Regex.Matcher matcher = MultiWordPattern.Matcher(desc); matcher.Matches(); string matchedGroup = null; for (int i = 1; i <= matcher.GroupCount(); ++i) { if (matcher.Group(i) != null) { matchedGroup = matcher.Group(i); break; } } matchedGroup = matchedGroup.ReplaceAll("\\\\", string.Empty); if (matchedGroup.Split("[|]").Length > MaxStringMatcherSize) { descriptionMode = DescriptionPattern.DescriptionMode.Pattern; descPattern = Pattern.Compile(Sharpen.Runtime.Substring(desc, 1, desc.Length - 1)); exactMatch = null; stringFilter = null; } else { //log.info("DescriptionPattern: not converting " + desc); descriptionMode = DescriptionPattern.DescriptionMode.Strings; descPattern = null; exactMatch = null; stringFilter = new ArrayStringFilter(ArrayStringFilter.Mode.Exact, matchedGroup.Split("[|]")); } } else { //log.info("DescriptionPattern: converting " + desc + " to " + stringFilter); if (CaseInsensitivePattern.Matcher(desc).Matches()) { Java.Util.Regex.Matcher matcher = CaseInsensitivePattern.Matcher(desc); matcher.Matches(); string matchedGroup = null; for (int i = 1; i <= matcher.GroupCount(); ++i) { if (matcher.Group(i) != null) { matchedGroup = matcher.Group(i); break; } } matchedGroup = matchedGroup.ReplaceAll("\\\\", string.Empty); if (matchedGroup.Split("[|]").Length > MaxStringMatcherSize) { descriptionMode = DescriptionPattern.DescriptionMode.Pattern; descPattern = Pattern.Compile(Sharpen.Runtime.Substring(desc, 1, desc.Length - 1)); exactMatch = null; stringFilter = null; } else { //log.info("DescriptionPattern: not converting " + desc); descriptionMode = DescriptionPattern.DescriptionMode.Strings; descPattern = null; exactMatch = null; stringFilter = new ArrayStringFilter(ArrayStringFilter.Mode.CaseInsensitive, matchedGroup.Split("[|]")); } } else { //log.info("DescriptionPattern: converting " + desc + " to " + stringFilter); if (PrefixPattern.Matcher(desc).Matches()) { Java.Util.Regex.Matcher matcher = PrefixPattern.Matcher(desc); matcher.Matches(); string matchedGroup = null; for (int i = 1; i <= matcher.GroupCount(); ++i) { if (matcher.Group(i) != null) { matchedGroup = matcher.Group(i); break; } } if (matchedGroup.Split("\\|").Length > MaxStringMatcherSize) { descriptionMode = DescriptionPattern.DescriptionMode.Pattern; descPattern = Pattern.Compile(Sharpen.Runtime.Substring(desc, 1, desc.Length - 1)); exactMatch = null; stringFilter = null; } else { //log.info("DescriptionPattern: not converting " + desc); descriptionMode = DescriptionPattern.DescriptionMode.Strings; descPattern = null; exactMatch = null; stringFilter = new ArrayStringFilter(ArrayStringFilter.Mode.Prefix, matchedGroup.Split("[|]")); } } else { //log.info("DescriptionPattern: converting " + desc + " to " + stringFilter); if (desc.Matches("/.*/")) { descriptionMode = DescriptionPattern.DescriptionMode.Pattern; descPattern = Pattern.Compile(Sharpen.Runtime.Substring(desc, 1, desc.Length - 1)); exactMatch = null; stringFilter = null; } else { if (desc.IndexOf('|') >= 0) { // patterns which contain ORs are a special case; we either // promote those to regex match or make a string matcher out // of them. for short enough disjunctions, a simple string // matcher can be more efficient than a regex. string[] words = desc.Split("[|]"); if (words.Length <= MaxStringMatcherSize) { descriptionMode = DescriptionPattern.DescriptionMode.Strings; descPattern = null; exactMatch = null; stringFilter = new ArrayStringFilter(ArrayStringFilter.Mode.Exact, words); } else { descriptionMode = DescriptionPattern.DescriptionMode.Pattern; descPattern = Pattern.Compile("^(?:" + desc + ")$"); exactMatch = null; stringFilter = null; } } else { // raw description descriptionMode = DescriptionPattern.DescriptionMode.Exact; descPattern = null; exactMatch = desc; stringFilter = null; } } } } } } } } else { if (name == null && linkedName == null) { throw new AssertionError("Illegal description pattern. Does not describe a node or link/name a variable"); } stringDesc = " "; descriptionMode = null; descPattern = null; exactMatch = null; stringFilter = null; } this.name = name; SetChild(null); this.basicCatFunction = (useBasicCat ? basicCatFunction : null); // System.out.println("Made " + (negDesc ? "negated " : "") + "DescNode with " + desc); this.variableGroups = variableGroups; }