public static Tree ProcessPatternsOnTree(List <Tuple <TregexPattern, TsurgeonPattern> > ops, Tree t) { matchedOnTree = false; foreach (Tuple <TregexPattern, TsurgeonPattern> op in ops) { try { TregexMatcher m = op.Item1.Matcher(t); TsurgeonMatcher tsm = op.Item2.GetMatcher(); while (m.Find()) { matchedOnTree = true; t = tsm.Evaluate(t, m); if (t == null) { return(null); } m = op.Item1.Matcher(t); } } catch (NullReferenceException npe) { throw new SystemException( "Tsurgeon.processPatternsOnTree failed to match label for pattern: " + op.Item1 + ", " + op.Item2, npe); } } return(t); }
/// <summary> /// Tries to match a pattern against a tree. If it succeeds, apply the surgical operations contained in a {@link TsurgeonPattern}. /// </summary> /// <param name="matchPattern">A {@link TregexPattern} to be matched against a {@link Tree}.</param> /// <param name="p">A {@link TsurgeonPattern} to apply.</param> /// <param name="t">the {@link Tree} to match against and perform surgery on.</param> /// <returns>t, which has been surgically modified.</returns> public static Tree ProcessPattern(TregexPattern matchPattern, TsurgeonPattern p, Tree t) { TregexMatcher m = matchPattern.Matcher(t); TsurgeonMatcher tsm = p.GetMatcher(); while (m.Find()) { t = tsm.Evaluate(t, m); if (t == null) { break; } m = matchPattern.Matcher(t); } return(t); }
public Tuple <Tree, int> Evaluate(Tree tree, TregexMatcher tregex) { int newIndex = -1; Tree parent = null; Tree relativeNode = childMatcher.Evaluate(tree, tregex); //Matcher m = daughterPattern.matcher(relation); var isMatch = DaughterPattern.IsMatch(location.relation); if (/*m.matches()*/ isMatch) { newIndex = int.Parse(DaughterPattern.Match(location.relation).Groups[1].Value) - 1; parent = relativeNode; if (location.relation[1] == '-') // backwards. { newIndex = parent.Children().Length - newIndex; } } else { parent = relativeNode.Parent(tree); if (parent == null) { throw new SystemException("Error: looking for a non-existent parent in tree " + tree + " for \"" + ToString() + "\""); } int index = parent.ObjectIndexOf(relativeNode); if (location.relation.Equals("$+")) { newIndex = index; } else if (location.relation.Equals("$-")) { newIndex = index + 1; } else { throw new SystemException("Error: Haven't dealt with relation " + location.relation + " yet."); } } return(new Tuple <Tree, int>(parent, newIndex)); }