本文整理汇总了C#中Tree.Children方法的典型用法代码示例。如果您正苦于以下问题:C# Tree.Children方法的具体用法?C# Tree.Children怎么用?C# Tree.Children使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tree
的用法示例。
在下文中一共展示了Tree.Children方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyHelper
private Tuple<Tree, Tree> CopyHelper(Tree node, Dictionary<string, Tree> newNamesToNodes)
{
Tree clone;
Tree newFoot = null;
if (node.IsLeaf())
{
if (node == Foot)
{
// found the foot node; pass it up.
clone = node.TreeFactory().NewTreeNode(node.Label(), new List<Tree>(0));
newFoot = clone;
}
else
{
clone = node.TreeFactory().NewLeaf(node.Label().LabelFactory().NewLabel(node.Label()));
}
}
else
{
var newChildren = new List<Tree>(node.Children().Length);
foreach (Tree child in node.Children())
{
Tuple<Tree, Tree> newChild = CopyHelper(child, newNamesToNodes);
newChildren.Add(newChild.Item1);
if (newChild.Item2 != null)
{
newFoot = newChild.Item2;
}
}
clone = node.TreeFactory().NewTreeNode(node.Label().LabelFactory().NewLabel(node.Label()), newChildren);
}
if (nodesToNames.ContainsKey(node))
{
newNamesToNodes.Add(nodesToNames[node], clone);
}
return new Tuple<Tree, Tree>(clone, newFoot);
}
示例2: LeftEdge
private static bool LeftEdge(Tree t, Tree t1, int i)
{
if (t == t1)
{
return true;
}
else if (t1.IsLeaf())
{
int j = t1.Yield().Count; // so that empties don't add size
i = i + j;
return false;
}
else
{
foreach (Tree kid in t1.Children())
{
if (LeftEdge(t, kid, i))
{
return true;
}
}
return false;
}
}
示例3: TreeGraphNode
/// <summary>
/// Create a new <code>TreeGraphNode</code> having the same tree structure
/// and label values as an existing tree (but no shared storage).
/// Operates recursively to construct an entire subtree
/// </summary>
/// <param name="t">the tree to copy</param>
/// <param name="parent">the parent node</param>
protected TreeGraphNode(Tree t, TreeGraphNode parent)
{
this._parent = parent;
Tree[] tKids = t.Children();
int numKids = tKids.Length;
_children = new TreeGraphNode[numKids];
for (int i = 0; i < numKids; i++)
{
_children[i] = new TreeGraphNode(tKids[i], this);
if (t.IsPreTerminal())
{
// add the tags to the leaves
_children[i]._label.SetTag(t.Label().Value());
}
}
this._label = (CoreLabel) Mlf.NewLabel(t.Label());
}
示例4: RightEdge
private static bool RightEdge(Tree t, Tree t1, int i)
{
if (t == t1)
{
return true;
}
else if (t1.IsLeaf())
{
int j = t1.Yield().Count; // so that empties don't add size
i = i - j;
return false;
}
else
{
Tree[] kids = t1.Children();
for (int j = kids.Length - 1; j >= 0; j--)
{
if (RightEdge(t, kids[j], i))
{
return true;
}
}
return false;
}
}
示例5: readTree
/*/**
* Simple tree reading utility method. Given a tree formatted as a PTB string, returns a Tree made by a specific TreeFactory.
#1#
public static Tree readTree(string ptbTreeString, TreeFactory treeFactory) {
try {
PennTreeReader ptr = new PennTreeReader(new StringReader(ptbTreeString), treeFactory);
return ptr.readTree();
} catch (IOException ex) {
throw new SystemException(ex);
}
}*/
/**
* Simple tree reading utility method. Given a tree formatted as a PTB string, returns a Tree made by the default TreeFactory (LabeledScoredTreeFactory)
*/
/*public static Tree readTree(string str) {
return readTree(str, defaultTreeFactory);
}*/
/// <summary>
/// Converts the tree labels to CoreLabels.
/// We need this because we store additional info in the CoreLabel, like token span.
/// </summary>
public static void ConvertToCoreLabels(Tree tree)
{
ILabel l = tree.Label();
if (!(l is CoreLabel))
{
var cl = new CoreLabel();
cl.SetValue(l.Value());
tree.SetLabel(cl);
}
foreach (Tree kid in tree.Children())
{
ConvertToCoreLabels(kid);
}
}
示例6: FindFootNodeHelper
private static Tree FindFootNodeHelper(Tree t)
{
Tree foundDtr = null;
if (t.IsLeaf())
{
var match = FootNodeLabelPattern.Match(t.Label().Value());
if (match.Success)
{
t.Label().SetValue(match.Groups[1].Value);
return t;
}
else
{
return null;
}
}
foreach (Tree child in t.Children())
{
Tree thisFoundDtr = FindFootNodeHelper(child);
if (thisFoundDtr != null)
{
if (foundDtr != null)
{
throw new TsurgeonParseException("Error -- two foot nodes in subtree" + t.ToString());
}
else
{
foundDtr = thisFoundDtr;
}
}
}
/*Matcher m = escapedFootNodeCharacter.matcher(t.label().value());
t.label().setValue(m.replaceAll(footNodeCharacter));*/
var newS = EscapedFootNodeCharacter.Replace(t.Label().Value(), FootNodeCharacter);
t.Label().SetValue(newS);
return foundDtr;
}
示例7: IsWhQ
/// <summary>
/// Is the tree t a WH-question?
/// At present this is only true if the tree t is a SQ having a WH.* sister and headed by a SBARQ.
/// (It was changed to looser definition in Feb 2006.)
/// </summary>
private static bool IsWhQ(Tree t, Tree parent)
{
if (t == null)
{
return false;
}
bool toReturn = false;
if (t.Value().StartsWith(SQ))
{
if (parent != null && parent.Value().Equals(SBARQ))
{
Tree[] kids = parent.Children();
foreach (Tree kid in kids)
{
// looks for a WH.*
if (kid.Value().StartsWith("WH"))
{
toReturn = true;
}
}
}
}
return toReturn;
}
示例8: DetermineNonTrivialHead
/// <summary>
/// Determine which daughter of the current parse tree is the head.
/// It assumes that the daughters already have had their heads determined.
/// Uses special rule for VP heads
/// </summary>
/// <param name="t">
/// The parse tree to examine the daughters of.
/// This is assumed to never be a leaf
/// </param>
/// <returns>The parse tree that is the head</returns>
protected override Tree DetermineNonTrivialHead(Tree t, Tree parent)
{
string motherCat = Tlp.BasicCategory(t.Label().Value());
// Some conj expressions seem to make more sense with the "not" or
// other key words as the head. For example, "and not" means
// something completely different than "and". Furthermore,
// downstream code was written assuming "not" would be the head...
if (motherCat.Equals(CONJP))
{
var headOfConjpTregex = new TregexPattern[]
{
TregexPattern.Compile("CONJP < (CC <: /^(?i:but|and)$/ $+ (RB=head <: /^(?i:not)$/))"),
TregexPattern.Compile(
"CONJP < (CC <: /^(?i:but)$/ [ ($+ (RB=head <: /^(?i:also|rather)$/)) | ($+ (ADVP=head <: (RB <: /^(?i:also|rather)$/))) ])"),
TregexPattern.Compile(
"CONJP < (CC <: /^(?i:and)$/ [ ($+ (RB=head <: /^(?i:yet)$/)) | ($+ (ADVP=head <: (RB <: /^(?i:yet)$/))) ])"),
};
foreach (TregexPattern pattern in headOfConjpTregex)
{
TregexMatcher matcher = pattern.Matcher(t);
if (matcher.MatchesAt(t))
{
return matcher.GetNode("head");
}
}
// if none of the above patterns match, use the standard method
}
if (motherCat.Equals(SBARQ) || motherCat.Equals(SINV))
{
if (!makeCopulaHead)
{
var headOfCopulaTregex = new TregexPattern[]
{
// Matches phrases such as "what is wrong"
TregexPattern.Compile("SBARQ < (WHNP $++ (/^VB/ < " + EnglishPatterns.CopularWordRegex +
" $++ ADJP=head))"),
// matches WHNP $+ VB<copula $+ NP
// for example, "Who am I to judge?"
// !$++ ADJP matches against "Why is the dog pink?"
TregexPattern.Compile("SBARQ < (WHNP=head $++ (/^VB/ < " + EnglishPatterns.CopularWordRegex +
" $+ NP !$++ ADJP))"),
// Actually somewhat limited in scope, this detects "Tuesday it is",
// "Such a great idea this was", etc
TregexPattern.Compile("SINV < (NP=head $++ (NP $++ (VP < (/^(?:VB|AUX)/ < " +
EnglishPatterns.CopularWordRegex + "))))"),
};
foreach (TregexPattern pattern in headOfCopulaTregex)
{
TregexMatcher matcher = pattern.Matcher(t);
if (matcher.MatchesAt(t))
{
return matcher.GetNode("head");
}
}
}
// if none of the above patterns match, use the standard method
}
Tree[] tmpFilteredChildren = null;
// do VPs with auxiliary as special case
if ((motherCat.Equals(AbstractCollinsHeadFinder.VerbPhrase) || motherCat.Equals("SQ") || motherCat.Equals("SINV")))
{
Tree[] kids = t.Children();
// try to find if there is an auxiliary verb
// looks for auxiliaries
if (HasVerbalAuxiliary(kids, verbalAuxiliaries, true) || HasPassiveProgressiveAuxiliary(kids))
{
// string[] how = new string[] {Left, AbstractCollinsHeadFinder.VerbPhrase, CoordinationTransformer.Adjective, CoordinationTransformer.Noun};
// Including NP etc seems okay for copular sentences but is
// problematic for other auxiliaries, like 'he has an answer'
// But maybe doing ADJP is fine!
string[] how = { Left, AbstractCollinsHeadFinder.VerbPhrase, CoordinationTransformer.Adjective };
//tmpFilteredChildren = ArrayUtils.filter(kids, REMOVE_TMP_AND_ADV);
tmpFilteredChildren = kids.Where(k => RemoveTmpAndAdv(k)).ToArray();
Tree pti = TraverseLocate(tmpFilteredChildren, how, false);
if (pti != null)
{
return pti;
}
}
// looks for copular verbs
if (HasVerbalAuxiliary(kids, copulars, false) && ! IsExistential(t, parent) && ! IsWhQ(t, parent))
//.........这里部分代码省略.........
示例9: DetermineNonTrivialHead
/// <summary>
/// Called by determineHead and may be overridden in subclasses
/// if special treatment is necessary for particular categories.
/// </summary>
/// <param name="t">The tre to determine the head daughter of</param>
/// <param name="parent">The parent of t (or may be null)</param>
/// <returns>The head daughter of t</returns>
protected virtual Tree DetermineNonTrivialHead(Tree t, Tree parent)
{
Tree theHead = null;
string motherCat = Tlp.BasicCategory(t.Label().Value());
if (motherCat.StartsWith("@"))
{
motherCat = motherCat.Substring(1);
}
// We know we have nonterminals underneath
// (a bit of a Penn Treebank assumption, but).
// Look at label.
// a total special case....
// first look for POS tag at end
// this appears to be redundant in the Collins case since the rule already would do that
// Tree lastDtr = t.lastChild();
// if (tlp.basicCategory(lastDtr.label().value()).equals("POS")) {
// theHead = lastDtr;
// } else {
string[][] how = null;
var success = NonTerminalInfo.TryGetValue(motherCat, out how);
Tree[] kids = t.Children();
if (!success)
{
if (DefaultRule != null)
{
return TraverseLocate(kids, DefaultRule, true);
}
else
{
throw new ArgumentException("No head rule defined for " + motherCat + " using "
+ this.GetType().Name + " in " + t);
}
}
for (int i = 0; i < how.Length; i++)
{
bool lastResort = (i == how.Length - 1);
theHead = TraverseLocate(kids, how[i], lastResort);
if (theHead != null)
{
break;
}
}
return theHead;
}
示例10: ObjectEqualityIndexOf
/// <summary>
/// Returns the index of <code>daughter</code> in <code>parent</code> by ==.
/// Returns -1 if <code>daughter</code> not found.
/// </summary>
public static int ObjectEqualityIndexOf(Tree parent, Tree daughter)
{
for (int i = 0; i < parent.Children().Length; i++)
{
if (daughter == parent.Children()[i])
{
return i;
}
}
return -1;
}
示例11: while
/* applies a TreeVisitor to all projections (including the node itself) of a node in a Tree.
* Does nothing if head is not in root.
* @return the maximal projection of head in root.
*/
/*public static Tree applyToProjections(TreeVisitor v, Tree head, Tree root, HeadFinder hf) {
Tree projection = head;
Tree parent = projection.parent(root);
if (parent == null && projection != root) {
return null;
}
v.visitTree(projection);
if (projection == root) {
return root;
}
while (hf.determineHead(parent) == projection) {
projection = parent;
v.visitTree(projection);
if (projection == root) {
return root;
}
parent = projection.parent(root);
}
return projection;
}*/
/**
* gets the <code>n</code>th terminal in <code>tree</code>. The first terminal is number zero.
*/
/*public static Tree getTerminal(Tree tree, int n) {
return getTerminal(tree, new MutableInteger(0), n);
}
static Tree getTerminal(Tree tree, MutableInteger i, int n) {
if (i.intValue() == n) {
if (tree.isLeaf()) {
return tree;
} else {
return getTerminal(tree.children()[0], i, n);
}
} else {
if (tree.isLeaf()) {
i.set(i.intValue() + tree.yield().size());
return null;
} else {
foreach (Tree kid in tree.children()) {
Tree result = getTerminal(kid, i, n);
if (result != null) {
return result;
}
}
return null;
}
}
}*/
/**
* gets the <code>n</code>th preterminal in <code>tree</code>. The first terminal is number zero.
*/
/*public static Tree getPreTerminal(Tree tree, int n) {
return getPreTerminal(tree, new MutableInteger(0), n);
}
static Tree getPreTerminal(Tree tree, MutableInteger i, int n) {
if (i.intValue() == n) {
if (tree.isPreTerminal()) {
return tree;
} else {
return getPreTerminal(tree.children()[0], i, n);
}
} else {
if (tree.isPreTerminal()) {
i.set(i.intValue() + tree.yield().size());
return null;
} else {
foreach (Tree kid in tree.children()) {
Tree result = getPreTerminal(kid, i, n);
if (result != null) {
return result;
}
}
return null;
}
}
}*/
/// <summary>
/// Returns the syntactic category of the tree as a list of the syntactic categories of the mother and the daughters
/// </summary>
public static List<string> LocalTreeAsCatList(Tree t)
{
var l = new List<string>(t.Children().Length + 1);
l.Add(t.Label().Value());
for (int i = 0; i < t.Children().Length; i++)
{
l.Add(t.Children()[i].Label().Value());
}
return l;
}
示例12: TaggedLeafLabels
private static void TaggedLeafLabels(Tree t, List<CoreLabel> l)
{
if (t.IsPreTerminal())
{
var fl = (CoreLabel) t.GetChild(0).Label();
fl.Set(typeof (CoreAnnotations.TagLabelAnnotation), t.Label());
l.Add(fl);
}
else
{
foreach (Tree kid in t.Children())
{
TaggedLeafLabels(kid, l);
}
}
}
示例13: LeafLabels
private static void LeafLabels(Tree t, List<ILabel> l)
{
if (t.IsLeaf())
{
l.Add(t.Label());
}
else
{
foreach (Tree kid in t.Children())
{
LeafLabels(kid, l);
}
}
}
示例14: PreTerminals
private static void PreTerminals(Tree t, List<Tree> l)
{
if (t.IsPreTerminal())
{
l.Add(t);
}
else
{
foreach (Tree kid in t.Children())
{
PreTerminals(kid, l);
}
}
}
示例15: Leaves
private static void Leaves(Tree t, List<Tree> l)
{
if (t.IsLeaf())
{
l.Add(t);
}
else
{
foreach (Tree kid in t.Children())
{
Leaves(kid, l);
}
}
}