/// <summary> /// Given a /// <c>Tree</c> /// node /// <paramref name="t"/> /// , attempts to /// return a list of nodes to which node /// <paramref name="t"/> /// has this /// grammatical relation, with /// <paramref name="t"/> /// as the governor. /// </summary> /// <param name="t">Target for finding dependents of t related by this GR</param> /// <param name="root">The root of the Tree</param> /// <returns>A Collection of dependent nodes to which t bears this GR</returns> public virtual ICollection <TreeGraphNode> GetRelatedNodes(TreeGraphNode t, TreeGraphNode root, IHeadFinder headFinder) { ICollection <TreeGraphNode> nodeList = new ArraySet <TreeGraphNode>(); foreach (TregexPattern p in targetPatterns) { // cdm: I deleted: && nodeList.isEmpty() // Initialize the TregexMatcher with the HeadFinder so that we // can use the same HeadFinder through the entire process of // building the dependencies TregexMatcher m = p.Matcher(root, headFinder); while (m.FindAt(t)) { TreeGraphNode target = (TreeGraphNode)m.GetNode("target"); if (target == null) { throw new AssertionError("Expression has no target: " + p); } nodeList.Add(target); if (Debug) { log.Info("found " + this + "(" + t + "-" + t.HeadWordNode() + ", " + m.GetNode("target") + "-" + ((TreeGraphNode)m.GetNode("target")).HeadWordNode() + ") using pattern " + p); foreach (string nodeName in m.GetNodeNames()) { if (nodeName.Equals("target")) { continue; } log.Info(" node " + nodeName + ": " + m.GetNode(nodeName)); } } } } return(nodeList); }
// 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); }
private void FindTreePattern(Tree tree, TregexPattern tgrepPattern, ICollection <Pair <int, int> > foundPairs) { try { TregexMatcher m = tgrepPattern.Matcher(tree); while (m.Find()) { Tree t = m.GetMatch(); Tree np1 = m.GetNode("m1"); Tree np2 = m.GetNode("m2"); Tree np3 = null; if (tgrepPattern.Pattern().Contains("m3")) { np3 = m.GetNode("m3"); } AddFoundPair(np1, np2, t, foundPairs); if (np3 != null) { AddFoundPair(np2, np3, t, foundPairs); } } } catch (Exception e) { // shouldn't happen.... throw new Exception(e); } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { // find match and get its parent Tree targetNode = this.childMatcher[0].Evaluate(tree, tregex); Tree parent = targetNode.Parent(tree); // substitute original node for foot of auxiliary tree. Foot node is ignored AuxiliaryTree ft = this._enclosing.AdjunctionTree().Copy(this, tree.TreeFactory(), tree.Label().LabelFactory()); // log.info("ft=" + ft + "; ft.foot=" + ft.foot + "; ft.tree=" + ft.tree); Tree parentOfFoot = ft.foot.Parent(ft.tree); if (parentOfFoot == null) { AdjoinToFootNode.log.Info("Warning: adjoin to foot for depth-1 auxiliary tree has no effect."); return(tree); } int i = parentOfFoot.ObjectIndexOf(ft.foot); if (parent == null) { parentOfFoot.SetChild(i, targetNode); return(ft.tree); } else { int j = parent.ObjectIndexOf(targetNode); parent.SetChild(j, ft.tree); parentOfFoot.SetChild(i, targetNode); return(tree); } }
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); }
public virtual void ExtractNPorPRP(ICoreMap s, IList <Mention> mentions, ICollection <IntPair> mentionSpanSet, ICollection <IntPair> namedEntitySpanSet) { IList <CoreLabel> sent = s.Get(typeof(CoreAnnotations.TokensAnnotation)); Tree tree = s.Get(typeof(TreeCoreAnnotations.TreeAnnotation)); tree.IndexLeaves(); SemanticGraph basicDependency = s.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)); SemanticGraph enhancedDependency = s.Get(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)); if (enhancedDependency == null) { enhancedDependency = s.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)); } TregexPattern tgrepPattern = npOrPrpMentionPattern; TregexMatcher matcher = tgrepPattern.Matcher(tree); while (matcher.Find()) { Tree t = matcher.GetMatch(); IList <Tree> mLeaves = t.GetLeaves(); int beginIdx = ((CoreLabel)mLeaves[0].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)) - 1; int endIdx = ((CoreLabel)mLeaves[mLeaves.Count - 1].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)); //if (",".equals(sent.get(endIdx-1).word())) { endIdx--; } // try not to have span that ends with , IntPair mSpan = new IntPair(beginIdx, endIdx); if (!mentionSpanSet.Contains(mSpan) && (lang == Locale.Chinese || !InsideNE(mSpan, namedEntitySpanSet))) { // if(!mentionSpanSet.contains(mSpan) && (!insideNE(mSpan, namedEntitySpanSet) || t.value().startsWith("PRP")) ) { int dummyMentionId = -1; Mention m = new Mention(dummyMentionId, beginIdx, endIdx, sent, basicDependency, enhancedDependency, new List <CoreLabel>(sent.SubList(beginIdx, endIdx)), t); mentions.Add(m); mentionSpanSet.Add(mSpan); } } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { // find match and get its parent Tree targetNode = ChildMatcher[0].Evaluate(tree, tregex); Tree parent = targetNode.Parent(tree); // substitute original node for foot of auxiliary tree. Foot node is ignored AuxiliaryTree ft = node.AdjunctionTree().Copy(this); Tree parentOfFoot = ft.Foot.Parent(ft.Tree); if (parentOfFoot == null) { return(tree); } int i = parentOfFoot.ObjectIndexOf(ft.Foot); if (parent == null) { parentOfFoot.SetChild(i, targetNode); return(ft.Tree); } else { int j = parent.ObjectIndexOf(targetNode); parent.SetChild(j, ft.Tree); parentOfFoot.SetChild(i, targetNode); return(tree); } }
protected internal static void ExtractNPorPRP(ICoreMap s, IList <Mention> mentions, ICollection <IntPair> mentionSpanSet, ICollection <IntPair> namedEntitySpanSet) { IList <CoreLabel> sent = s.Get(typeof(CoreAnnotations.TokensAnnotation)); Tree tree = s.Get(typeof(TreeCoreAnnotations.TreeAnnotation)); tree.IndexLeaves(); SemanticGraph dependency = s.Get(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)); TregexPattern tgrepPattern = npOrPrpMentionPattern; TregexMatcher matcher = tgrepPattern.Matcher(tree); while (matcher.Find()) { Tree t = matcher.GetMatch(); IList <Tree> mLeaves = t.GetLeaves(); int beginIdx = ((CoreLabel)mLeaves[0].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)) - 1; int endIdx = ((CoreLabel)mLeaves[mLeaves.Count - 1].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)); if (",".Equals(sent[endIdx - 1].Word())) { endIdx--; } // try not to have span that ends with , IntPair mSpan = new IntPair(beginIdx, endIdx); if (!mentionSpanSet.Contains(mSpan) && !InsideNE(mSpan, namedEntitySpanSet)) { int dummyMentionId = -1; Mention m = new Mention(dummyMentionId, beginIdx, endIdx, dependency, new List <CoreLabel>(sent.SubList(beginIdx, endIdx)), t); mentions.Add(m); mentionSpanSet.Add(mSpan); } } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { // find match and get its parent Tree targetNode = ChildMatcher[0].Evaluate(tree, tregex); Tree parent = targetNode.Parent(tree); // substitute original node for foot of auxiliary tree. Foot node is ignored AuxiliaryTree ft = node.AdjunctionTree().Copy(this); Tree parentOfFoot = ft.Foot.Parent(ft.Tree); if (parentOfFoot == null) { return tree; } int i = parentOfFoot.ObjectIndexOf(ft.Foot); if (parent == null) { parentOfFoot.SetChild(i, targetNode); return ft.Tree; } else { int j = parent.ObjectIndexOf(targetNode); parent.SetChild(j, ft.Tree); parentOfFoot.SetChild(i, targetNode); return tree; } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree topNode = ChildMatcher[0].Evaluate(tree, tregex); Tree bottomNode = ChildMatcher[1].Evaluate(tree, tregex); if (topNode == tree) { if (bottomNode.Children().Length == 1) { return bottomNode.Children()[0]; } else { return null; } } Tree parent = topNode.Parent(tree); int i = Trees.ObjectEqualityIndexOf(parent, topNode); parent.RemoveChild(i); foreach (Tree child in bottomNode.Children()) { parent.AddChild(i, child); i++; } return tree; }
protected internal static void ExtractEnumerations(ICoreMap s, IList <Mention> mentions, ICollection <IntPair> mentionSpanSet, ICollection <IntPair> namedEntitySpanSet) { IList <CoreLabel> sent = s.Get(typeof(CoreAnnotations.TokensAnnotation)); Tree tree = s.Get(typeof(TreeCoreAnnotations.TreeAnnotation)); SemanticGraph dependency = s.Get(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)); TregexPattern tgrepPattern = enumerationsMentionPattern; TregexMatcher matcher = tgrepPattern.Matcher(tree); IDictionary <IntPair, Tree> spanToMentionSubTree = Generics.NewHashMap(); while (matcher.Find()) { matcher.GetMatch(); Tree m1 = matcher.GetNode("m1"); Tree m2 = matcher.GetNode("m2"); IList <Tree> mLeaves = m1.GetLeaves(); int beginIdx = ((CoreLabel)mLeaves[0].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)) - 1; int endIdx = ((CoreLabel)mLeaves[mLeaves.Count - 1].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)); spanToMentionSubTree[new IntPair(beginIdx, endIdx)] = m1; mLeaves = m2.GetLeaves(); beginIdx = ((CoreLabel)mLeaves[0].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)) - 1; endIdx = ((CoreLabel)mLeaves[mLeaves.Count - 1].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)); spanToMentionSubTree[new IntPair(beginIdx, endIdx)] = m2; } foreach (IntPair mSpan in spanToMentionSubTree.Keys) { if (!mentionSpanSet.Contains(mSpan) && !InsideNE(mSpan, namedEntitySpanSet)) { int dummyMentionId = -1; Mention m = new Mention(dummyMentionId, mSpan.Get(0), mSpan.Get(1), dependency, new List <CoreLabel>(sent.SubList(mSpan.Get(0), mSpan.Get(1))), spanToMentionSubTree[mSpan]); mentions.Add(m); mentionSpanSet.Add(mSpan); } } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree topNode = ChildMatcher[0].Evaluate(tree, tregex); Tree bottomNode = ChildMatcher[1].Evaluate(tree, tregex); if (topNode == tree) { if (bottomNode.Children().Length == 1) { return(bottomNode.Children()[0]); } else { return(null); } } Tree parent = topNode.Parent(tree); int i = Trees.ObjectEqualityIndexOf(parent, topNode); parent.RemoveChild(i); foreach (Tree child in bottomNode.Children()) { parent.AddChild(i, child); i++; } return(tree); }
public Tree TransformTree(Tree t) { TregexMatcher matcher = TregexMonthYear.Matcher(t); while (matcher.Find()) { Tree root = matcher.GetNode("root"); Tree month = matcher.GetNode("month"); Tree year = matcher.GetNode("year"); var children = new Tree[] { month, year }; root.SetChildren(children); matcher = TregexMonthYear.Matcher(t); } matcher = TregexMonthDayYear.Matcher(t); while (matcher.Find()) { Tree root = matcher.GetNode("root"); Tree month = matcher.GetNode("month"); Tree day = matcher.GetNode("day"); Tree comma = matcher.GetNode("comma"); Tree year = matcher.GetNode("year"); var children = new Tree[] { month, day, comma, year }; root.SetChildren(children); matcher = TregexMonthDayYear.Matcher(t); } return(t); }
/** * Combines all nodes between start and end into one subtree, then * replaces those nodes with the new subtree in the corresponding * location under parent */ public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree startChild = ChildMatcher[0].Evaluate(tree, tregex); Tree endChild = (ChildMatcher.Length == 2) ? ChildMatcher[1].Evaluate(tree, tregex) : startChild; Tree parent = startChild.Parent(tree); // sanity check if (parent != endChild.Parent(tree)) { throw new TsurgeonRuntimeException("Parents did not match for trees when applied to " + this); } AuxiliaryTree treeCopy = node.auxTree.Copy(this); // Collect all the children of the parent of the node we care // about. If the child is one of the nodes we care about, or // between those two nodes, we add it to a list of inner children. // When we reach the second endpoint, we turn that list of inner // children into a new node using the newly created label. All // other children are kept in an outer list, with the new node // added at the appropriate location. var children = new List <Tree>(); var innerChildren = new List <Tree>(); bool insideSpan = false; foreach (Tree child in parent.Children()) { if (child == startChild || child == endChild) { if (!insideSpan && startChild != endChild) { insideSpan = true; innerChildren.Add(child); } else { insideSpan = false; innerChildren.Add(child); // All children have been collected; place these beneath the foot of the auxiliary tree treeCopy.Foot.SetChildren(innerChildren); children.Add(treeCopy.Tree); } } else if (insideSpan) { innerChildren.Add(child); } else { children.Add(child); } } parent.SetChildren(children); return(tree); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToInsert = ChildMatcher[0].Evaluate(tree, tregex); Tuple<Tree, int> position = locationMatcher.Evaluate(tree, tregex); position.Item1.InsertDtr(this.node.needsCopy ? nodeToInsert.DeepCopy() : nodeToInsert, position.Item2); return tree; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToInsert = this.childMatcher[0].Evaluate(tree, tregex); Pair <Tree, int> position = this.locationMatcher.Evaluate(tree, tregex); position.First().InsertDtr(this._enclosing.needsCopy ? nodeToInsert.DeepCopy() : nodeToInsert, position.Second()); return(tree); }
/// <summary>Right now this outputs trees in PTB format.</summary> /// <remarks> /// Right now this outputs trees in PTB format. It outputs one tree /// at a time until we have output enough trees to fill the given /// file, then moves on to the next file. Trees are output in the /// order given in the <code>ids</code> list. /// <br /> /// Trees have their words replaced with the words' lemmas, if those /// lemmas exist. /// </remarks> /// <exception cref="System.IO.IOException"/> public static void OutputSplits(IList <string> ids, IDictionary <string, Tree> treeMap) { IQueue <int> fSizeQueue = new LinkedList <int>(Arrays.AsList(fSizes)); IQueue <string> fNameQueue = new LinkedList <string>(Arrays.AsList(fNames)); TregexPattern pBadTree = TregexPattern.Compile("@SENT <: @PUNC"); TregexPattern pBadTree2 = TregexPattern.Compile("@SENT <1 @PUNC <2 @PUNC !<3 __"); ITreeTransformer tt = new FTBCorrector(); int size = fSizeQueue.Remove(); string filename = fNameQueue.Remove(); log.Info("Outputing " + filename); PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"))); int outputCount = 0; foreach (string id in ids) { if (!treeMap.Contains(id)) { log.Info("Missing id: " + id); continue; } Tree tree = treeMap[id]; TregexMatcher m = pBadTree.Matcher(tree); TregexMatcher m2 = pBadTree2.Matcher(tree); if (m.Find() || m2.Find()) { log.Info("Discarding tree: " + tree.ToString()); continue; } // Punctuation normalization, etc. Tree backupCopy = tree.DeepCopy(); tree = tt.TransformTree(tree); if (tree.FirstChild().Children().Length == 0) { // Some trees have only punctuation. Tregex will mangle these. Don't throw those away. log.Info("Saving tree: " + tree.ToString()); log.Info("Backup: " + backupCopy.ToString()); tree = backupCopy; } if (LemmasAsLeaves || AddMorphoToLeaves) { MungeLeaves(tree, LemmasAsLeaves, AddMorphoToLeaves); } ReplacePOSTags(tree); writer.Println(tree.ToString()); ++outputCount; if (outputCount == size) { outputCount = 0; size = fSizeQueue.Remove(); filename = fNameQueue.Remove(); log.Info("Outputing " + filename); writer.Close(); writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"))); } } writer.Close(); }
/** * Combines all nodes between start and end into one subtree, then * replaces those nodes with the new subtree in the corresponding * location under parent */ public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree startChild = ChildMatcher[0].Evaluate(tree, tregex); Tree endChild = (ChildMatcher.Length == 2) ? ChildMatcher[1].Evaluate(tree, tregex) : startChild; Tree parent = startChild.Parent(tree); // sanity check if (parent != endChild.Parent(tree)) { throw new TsurgeonRuntimeException("Parents did not match for trees when applied to " + this); } AuxiliaryTree treeCopy = node.auxTree.Copy(this); // Collect all the children of the parent of the node we care // about. If the child is one of the nodes we care about, or // between those two nodes, we add it to a list of inner children. // When we reach the second endpoint, we turn that list of inner // children into a new node using the newly created label. All // other children are kept in an outer list, with the new node // added at the appropriate location. var children = new List<Tree>(); var innerChildren = new List<Tree>(); bool insideSpan = false; foreach (Tree child in parent.Children()) { if (child == startChild || child == endChild) { if (!insideSpan && startChild != endChild) { insideSpan = true; innerChildren.Add(child); } else { insideSpan = false; innerChildren.Add(child); // All children have been collected; place these beneath the foot of the auxiliary tree treeCopy.Foot.SetChildren(innerChildren); children.Add(treeCopy.Tree); } } else if (insideSpan) { innerChildren.Add(child); } else { children.Add(child); } } parent.SetChildren(children); return tree; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToMove = ChildMatcher[0].Evaluate(tree, tregex); Tree oldParent = nodeToMove.Parent(tree); oldParent.RemoveChild(Trees.ObjectEqualityIndexOf(oldParent, nodeToMove)); Tuple<Tree, int> position = locationMatcher.Evaluate(tree, tregex); position.Item1.InsertDtr(nodeToMove, position.Item2); return tree; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToInsert = ChildMatcher[0].Evaluate(tree, tregex); Tuple <Tree, int> position = locationMatcher.Evaluate(tree, tregex); position.Item1.InsertDtr(this.node.needsCopy ? nodeToInsert.DeepCopy() : nodeToInsert, position.Item2); return(tree); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { int newIndex = Coindexer.GenerateIndex(); foreach (TsurgeonMatcher child in ChildMatcher) { Tree node = child.Evaluate(tree, tregex); node.Label().SetValue(node.Label().Value() + CoindexationIntroductionString + newIndex); } return tree; }
/// <summary> /// Find the next point (preterminal) at which the given tree should /// be split. /// </summary> /// <param name="t"/> /// <returns> /// The endpoint of a subtree which should be extracted, or /// <see langword="null"/> /// if there are no subtrees which need to be /// extracted. /// </returns> internal static Tree FindSplitPoint(Tree t) { TregexMatcher m = pSplitPoint.Matcher(t); if (m.Find()) { return(m.GetMatch()); } return(null); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree result; NewNodeNames.TryGetValue(node.label, out result); if (result == null) { result = tregex.GetNode(node.label); } return result; }
//Delete sentence-initial punctuation //Delete sentence final punctuation that is preceded by punctuation (first time) //Delete sentence final punctuation that is preceded by punctuation (second time) //Convert remaining sentence-final punctuation to either . if it is not [.!?] //Delete medial, sentence-final punctuation //Now move the sentence-final mark under SENT //For those trees that lack a sentence-final punc, add one. //Finally, delete these punctuation marks, which I can't seem to kill otherwise... //A bad MWADV tree in the training set // Not sure why this got a label of X. Similar trees suggest it // should be A instead // This also seems to be mislabeled /// <param name="args"/> public static void Main(string[] args) { if (args.Length != 1) { log.Info("Usage: java " + typeof(Edu.Stanford.Nlp.International.French.Pipeline.FTBCorrector).FullName + " filename\n"); System.Environment.Exit(-1); } ITreeTransformer tt = new Edu.Stanford.Nlp.International.French.Pipeline.FTBCorrector(); File f = new File(args[0]); try { //These bad trees in the Candito training set should be thrown out: // (ROOT (SENT (" ") (. .))) // (ROOT (SENT (. .))) TregexPattern pBadTree = TregexPattern.Compile("@SENT <: @PUNC"); TregexPattern pBadTree2 = TregexPattern.Compile("@SENT <1 @PUNC <2 @PUNC !<3 __"); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8")); ITreeReaderFactory trf = new FrenchTreeReaderFactory(); ITreeReader tr = trf.NewTreeReader(br); int nTrees = 0; for (Tree t; (t = tr.ReadTree()) != null; nTrees++) { TregexMatcher m = pBadTree.Matcher(t); TregexMatcher m2 = pBadTree2.Matcher(t); if (m.Find() || m2.Find()) { log.Info("Discarding tree: " + t.ToString()); } else { Tree fixedT = tt.TransformTree(t); System.Console.Out.WriteLine(fixedT.ToString()); } } tr.Close(); System.Console.Error.Printf("Wrote %d trees%n", nTrees); } catch (UnsupportedEncodingException e) { Sharpen.Runtime.PrintStackTrace(e); } catch (FileNotFoundException e) { Sharpen.Runtime.PrintStackTrace(e); } catch (IOException e) { Sharpen.Runtime.PrintStackTrace(e); } catch (TregexParseException e) { Sharpen.Runtime.PrintStackTrace(e); } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { // find match Tree targetNode = ChildMatcher[0].Evaluate(tree, tregex); // put children underneath target in foot of auxilary tree AuxiliaryTree ft = node.AdjunctionTree().Copy(this); ft.Foot.SetChildren(targetNode.GetChildrenAsList()); // put children of auxiliary tree under target. root of auxiliary tree is ignored. root of original is maintained. targetNode.SetChildren(ft.Tree.GetChildrenAsList()); return tree; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToMove = ChildMatcher[0].Evaluate(tree, tregex); Tree oldParent = nodeToMove.Parent(tree); oldParent.RemoveChild(Trees.ObjectEqualityIndexOf(oldParent, nodeToMove)); Tuple <Tree, int> position = locationMatcher.Evaluate(tree, tregex); position.Item1.InsertDtr(nodeToMove, position.Item2); return(tree); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { if (node.invert ^ (tregex.GetNode(node.name) != null)) { foreach (TsurgeonMatcher child in ChildMatcher) { child.Evaluate(tree, tregex); } } return tree; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { int newIndex = this.coindexer.GenerateIndex(); foreach (TsurgeonMatcher child in this.childMatcher) { Tree node = child.Evaluate(tree, tregex); node.Label().SetValue(node.Label().Value() + CoindexNodes.coindexationIntroductionString + newIndex); } return(tree); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree result; NewNodeNames.TryGetValue(node.label, out result); if (result == null) { result = tregex.GetNode(node.label); } return(result); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { if (node.invert ^ (tregex.GetNode(node.name) != null)) { foreach (TsurgeonMatcher child in ChildMatcher) { child.Evaluate(tree, tregex); } } return(tree); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToMove = this.childMatcher[0].Evaluate(tree, tregex); Tree oldParent = nodeToMove.Parent(tree); oldParent.RemoveChild(Edu.Stanford.Nlp.Trees.Trees.ObjectEqualityIndexOf(oldParent, nodeToMove)); Pair <Tree, int> position = this.locationMatcher.Evaluate(tree, tregex); position.First().InsertDtr(nodeToMove, position.Second()); return(tree); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { bool prunedWholeTree = false; foreach (TsurgeonMatcher child in ChildMatcher) { Tree nodeToPrune = child.Evaluate(tree, tregex); if (PruneHelper(tree, nodeToPrune) == null) prunedWholeTree = true; } return prunedWholeTree ? null : tree; }
private static void ExtractNPorPRP(ICoreMap s, IList <Mention> mentions, ICollection <IntPair> mentionSpanSet, ICollection <IntPair> namedEntitySpanSet) { IList <CoreLabel> sent = s.Get(typeof(CoreAnnotations.TokensAnnotation)); Tree tree = s.Get(typeof(TreeCoreAnnotations.TreeAnnotation)); tree.IndexLeaves(); SemanticGraph basicDependency = s.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)); SemanticGraph enhancedDependency = s.Get(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)); if (enhancedDependency == null) { enhancedDependency = s.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)); } TregexPattern tgrepPattern = npOrPrpMentionPattern; TregexMatcher matcher = tgrepPattern.Matcher(tree); while (matcher.Find()) { Tree t = matcher.GetMatch(); IList <Tree> mLeaves = t.GetLeaves(); int beginIdx = ((CoreLabel)mLeaves[0].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)) - 1; int endIdx = ((CoreLabel)mLeaves[mLeaves.Count - 1].Label()).Get(typeof(CoreAnnotations.IndexAnnotation)); if (",".Equals(sent[endIdx - 1].Word())) { endIdx--; } // try not to have span that ends with , IntPair mSpan = new IntPair(beginIdx, endIdx); // if(!mentionSpanSet.contains(mSpan) && (!insideNE(mSpan, namedEntitySpanSet)) ) { if (!mentionSpanSet.Contains(mSpan) && (!InsideNE(mSpan, namedEntitySpanSet) || t.Value().StartsWith("PRP"))) { int dummyMentionId = -1; Mention m = new Mention(dummyMentionId, beginIdx, endIdx, sent, basicDependency, enhancedDependency, new List <CoreLabel>(sent.SubList(beginIdx, endIdx)), t); mentions.Add(m); mentionSpanSet.Add(mSpan); if (m.originalSpan.Count > 1) { bool isNE = true; foreach (CoreLabel cl in m.originalSpan) { if (!cl.Tag().StartsWith("NNP")) { isNE = false; } } if (isNE) { namedEntitySpanSet.Add(mSpan); } } } } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { // find match Tree targetNode = this.childMatcher[0].Evaluate(tree, tregex); // put children underneath target in foot of auxilary tree AuxiliaryTree ft = this._enclosing.AdjunctionTree().Copy(this, tree.TreeFactory(), tree.Label().LabelFactory()); ft.foot.SetChildren(targetNode.GetChildrenAsList()); // put children of auxiliary tree under target. root of auxiliary tree is ignored. root of original is maintained. targetNode.SetChildren(ft.tree.GetChildrenAsList()); return(tree); }
/// <summary> /// Returns the indices of all imperative verbs in the /// tree t. /// </summary> private static ICollection <int> GetImperatives(Tree t) { ICollection <int> imps = new HashSet <int>(); TregexMatcher matcher = ImperativePattern.Matcher(t); while (matcher.Find()) { IList <ILabel> verbs = matcher.GetMatch().Yield(); CoreLabel cl = (CoreLabel)verbs[0]; imps.Add(cl.Index()); } return(imps); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { bool prunedWholeTree = false; foreach (TsurgeonMatcher child in this.childMatcher) { Tree nodeToPrune = child.Evaluate(tree, tregex); if (PruneNode.PruneHelper(tree, nodeToPrune) == null) { prunedWholeTree = true; } } return(prunedWholeTree ? null : tree); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree result = this.newNodeNames[this._enclosing.label]; if (result == null) { result = tregex.GetNode(this._enclosing.label); } if (result == null) { FetchNode.log.Info("Warning -- null node fetched by Tsurgeon operation for node: " + this + " (either no node labeled this, or the labeled node didn't match anything)"); } return(result); }
internal virtual Pair <Tree, int> Evaluate(Tree tree, TregexMatcher tregex) { int newIndex; // initialized below Tree parent; // initialized below Tree relativeNode = this.childMatcher.Evaluate(tree, tregex); Matcher m = TreeLocation.daughterPattern.Matcher(this._enclosing.relation); if (m.Matches()) { newIndex = System.Convert.ToInt32(m.Group(1)) - 1; parent = relativeNode; if (this._enclosing.relation[1] == '-') { // backwards. newIndex = parent.Children().Length - newIndex; } } else { parent = relativeNode.Parent(tree); if (parent == null) { throw new Exception("Error: looking for a non-existent parent in tree " + tree + " for \"" + this.ToString() + '"'); } int index = parent.ObjectIndexOf(relativeNode); switch (this._enclosing.relation) { case "$+": { newIndex = index; break; } case "$-": { newIndex = index + 1; break; } default: { throw new Exception("Error: Haven't dealt with relation " + this._enclosing.relation + " yet."); } } } return(new Pair <Tree, int>(parent, newIndex)); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree result = tree; foreach (TsurgeonMatcher child in ChildMatcher) { Tree nodeToDelete = child.Evaluate(tree, tregex); if (nodeToDelete == tree) { result = null; } Tree parent = nodeToDelete.Parent(tree); parent.RemoveChild(Trees.ObjectEqualityIndexOf(parent, nodeToDelete)); } return result; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToRelabel = ChildMatcher[0].Evaluate(tree, tregex); switch (node.mode) { case RelabelMode.Fixed: { nodeToRelabel.Label().SetValue(node.newLabel); break; } case RelabelMode.Regex: { var label = new StringBuilder(); foreach (string chunk in node.replacementPieces) { if (VariablePattern.IsMatch(chunk)) { //String name = chunk.Substring(2, chunk.Length - 1); string name = chunk.Substring(2, chunk.Length - 3); //label.Append(Matcher.quoteReplacement(tregex.getVariableString(name))); label.Append(tregex.GetVariableString(name).Replace("'", "").Replace("\"", "")); } else if (NodePattern.IsMatch(chunk)) { //String name = chunk.Substring(2, chunk.Length - 1); string name = chunk.Substring(2, chunk.Length - 3); //label.Append(Matcher.quoteReplacement(tregex.getNode(name).value())); label.Append(tregex.GetNode(name).Value().Replace("'", "").Replace("\"", "")); } else { label.Append(chunk); } } //var m = node.labelRegex.Match(nodeToRelabel.label().value()); //nodeToRelabel.label().setValue(m.replaceAll(label.ToString())); var newS = node.labelRegex.Replace(nodeToRelabel.Label().Value(), label.ToString()); nodeToRelabel.Label().SetValue(/*m.replaceAll(label.ToString())*/ newS); break; } default: throw new ArgumentException("Unsupported relabel mode " + node.mode); } return(tree); }
/// <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); }
/// <summary> /// Returns null if one of the surgeries eliminates the tree entirely. /// The operated-on tree is not to be trusted in this instance. /// </summary> public override Tree Evaluate(Tree tree, TregexMatcher tregex) { if (Coindexer != null) { Coindexer.SetLastIndex(tree); } foreach (TsurgeonMatcher child in ChildMatcher) { tree = child.Evaluate(tree, tregex); if (tree == null) { return null; } } return tree; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { // find match and get its parent Tree targetNode = ChildMatcher[0].Evaluate(tree, tregex); Tree parent = targetNode.Parent(tree); // put children underneath target in foot of auxilary tree AuxiliaryTree ft = node.padjunctionTree.Copy(this); ft.Foot.SetChildren(targetNode.GetChildrenAsList()); // replace match with root of auxiliary tree if (parent == null) { return ft.Tree; } else { int i = parent.ObjectIndexOf(targetNode); parent.SetChild(i, ft.Tree); return tree; } }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree oldNode = ChildMatcher[0].Evaluate(tree, tregex); if (oldNode == tree) { if (node.children.Length > 2) { throw new TsurgeonRuntimeException( "Attempted to replace a root node with more than one node, unable to proceed"); } return ChildMatcher[1].Evaluate(tree, tregex); } Tree parent = oldNode.Parent(tree); int i = parent.ObjectIndexOf(oldNode); parent.RemoveChild(i); for (int j = 1; j < node.children.Length; ++j) { Tree newNode = ChildMatcher[j].Evaluate(tree, tregex); parent.InsertDtr(newNode.DeepCopy(), i + j - 1); } return tree; }
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); }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { return node.subTree.Copy(this).Tree; }
public override Tree Evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToRelabel = ChildMatcher[0].Evaluate(tree, tregex); switch (node.mode) { case RelabelMode.Fixed: { nodeToRelabel.Label().SetValue(node.newLabel); break; } case RelabelMode.Regex: { var label = new StringBuilder(); foreach (string chunk in node.replacementPieces) { if (VariablePattern.IsMatch(chunk)) { //String name = chunk.Substring(2, chunk.Length - 1); string name = chunk.Substring(2, chunk.Length - 3); //label.Append(Matcher.quoteReplacement(tregex.getVariableString(name))); label.Append(tregex.GetVariableString(name).Replace("'", "").Replace("\"", "")); } else if (NodePattern.IsMatch(chunk)) { //String name = chunk.Substring(2, chunk.Length - 1); string name = chunk.Substring(2, chunk.Length - 3); //label.Append(Matcher.quoteReplacement(tregex.getNode(name).value())); label.Append(tregex.GetNode(name).Value().Replace("'", "").Replace("\"", "")); } else { label.Append(chunk); } } //var m = node.labelRegex.Match(nodeToRelabel.label().value()); //nodeToRelabel.label().setValue(m.replaceAll(label.ToString())); var newS = node.labelRegex.Replace(nodeToRelabel.Label().Value(), label.ToString()); nodeToRelabel.Label().SetValue( /*m.replaceAll(label.ToString())*/newS); break; } default: throw new ArgumentException("Unsupported relabel mode " + node.mode); } return tree; }
/// <summary> /// Evaluates the surgery pattern against a {@link Tree} and a {@link TregexMatcher} /// that has been successfully matched against the tree. /// </summary> /// <param name="tree"> /// The {@link Tree} that has been matched upon; typically this tree will be destructively modified. /// </param> /// <param name="tregex">The successfully matched {@link TregexMatcher}.</param> /// <returns> /// Some node in the tree; depends on implementation and use of the specific subclass. /// </returns> public abstract Tree Evaluate(Tree tree, TregexMatcher tregex);