// hack-in field for seeing whether there was a match. public static Tree ProcessPatternsOnTree(IList <Pair <TregexPattern, TsurgeonPattern> > ops, Tree t) { matchedOnTree = false; foreach (Pair <TregexPattern, TsurgeonPattern> op in ops) { try { TregexMatcher m = op.First().Matcher(t); TsurgeonMatcher tsm = op.Second().Matcher(); while (m.Find()) { matchedOnTree = true; t = tsm.Evaluate(t, m); if (t == null) { return(null); } m = op.First().Matcher(t); } } catch (ArgumentNullException npe) { throw new Exception("Tsurgeon.processPatternsOnTree failed to match label for pattern: " + op.First() + ", " + op.Second(), npe); } } return(t); }
/// <summary>Tries to match a pattern against a tree.</summary> /// <remarks> /// Tries to match a pattern against a tree. If it succeeds, apply the surgical operations contained in a /// <see cref="TsurgeonPattern"/> /// . /// </remarks> /// <param name="matchPattern"> /// A /// <see cref="Edu.Stanford.Nlp.Trees.Tregex.TregexPattern"/> /// to be matched against a /// <see cref="Edu.Stanford.Nlp.Trees.Tree"/> /// . /// </param> /// <param name="p"> /// A /// <see cref="TsurgeonPattern"/> /// to apply. /// </param> /// <param name="t"> /// the /// <see cref="Edu.Stanford.Nlp.Trees.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.Matcher(); while (m.Find()) { t = tsm.Evaluate(t, m); if (t == null) { break; } m = matchPattern.Matcher(t); } return(t); }