public override bool Equals(object other) { if (this == other) { return(true); } if (!(other is Edu.Stanford.Nlp.Trees.CollinsDependency)) { return(false); } Edu.Stanford.Nlp.Trees.CollinsDependency otherDep = (Edu.Stanford.Nlp.Trees.CollinsDependency)other; return(modifier.Equals(otherDep.modifier) && head.Equals(otherDep.head) && relation.Equals(otherDep.relation)); }
/// <summary>This method assumes that a start symbol node has been added to the tree.</summary> /// <param name="t">The tree</param> /// <param name="hf">A head finding algorithm.</param> /// <returns>A set of dependencies</returns> private static ICollection <Edu.Stanford.Nlp.Trees.CollinsDependency> ExtractFromTree(Tree t, string startSymbol, IHeadFinder hf, bool normPOS) { if (t == null || startSymbol.Equals(string.Empty) || hf == null) { return(null); } ICollection <Edu.Stanford.Nlp.Trees.CollinsDependency> deps = Generics.NewHashSet(); if (t.Value().Equals(startSymbol)) { t = t.FirstChild(); } bool mustProcessRoot = true; foreach (Tree node in t) { if (node.IsLeaf() || node.NumChildren() < 2) { continue; } Tree headDaughter = hf.DetermineHead(node); Tree head = node.HeadTerminal(hf); if (headDaughter == null || head == null) { log.Info("WARNING: CollinsDependency.extractFromTree() could not find root for:\n" + node.PennString()); } else { //Make dependencies if (mustProcessRoot) { mustProcessRoot = false; CoreLabel startLabel = MakeStartLabel(startSymbol); deps.Add(new Edu.Stanford.Nlp.Trees.CollinsDependency(new CoreLabel(head.Label()), startLabel, new CollinsRelation(startSymbol, startSymbol, node.Value(), CollinsRelation.Direction.Right))); } CollinsRelation.Direction dir = CollinsRelation.Direction.Left; foreach (Tree daughter in node.Children()) { if (daughter.Equals(headDaughter)) { dir = CollinsRelation.Direction.Right; } else { Tree headOfDaughter = daughter.HeadTerminal(hf); string relParent = (normPOS && node.IsPreTerminal()) ? normPOSLabel : node.Value(); string relHead = (normPOS && headDaughter.IsPreTerminal()) ? normPOSLabel : headDaughter.Value(); string relModifier = (normPOS && daughter.IsPreTerminal()) ? normPOSLabel : daughter.Value(); Edu.Stanford.Nlp.Trees.CollinsDependency newDep = new Edu.Stanford.Nlp.Trees.CollinsDependency(new CoreLabel(headOfDaughter.Label()), new CoreLabel(head.Label()), new CollinsRelation(relParent, relHead, relModifier, dir)); deps.Add(newDep); } } } } //TODO Combine the indexing procedure above with yield here so that two searches aren't performed. if (t.Yield().Count != deps.Count) { System.Console.Error.Printf("WARNING: Number of extracted dependencies (%d) does not match yield (%d):\n", deps.Count, t.Yield().Count); log.Info(t.PennString()); log.Info(); int num = 0; foreach (Edu.Stanford.Nlp.Trees.CollinsDependency dep in deps) { log.Info(num++ + ": " + dep.ToString()); } } return(deps); }