本文整理汇总了C#中Tree.Label方法的典型用法代码示例。如果您正苦于以下问题:C# Tree.Label方法的具体用法?C# Tree.Label怎么用?C# Tree.Label使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tree
的用法示例。
在下文中一共展示了Tree.Label方法的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: IsVerbalAuxiliary
private bool IsVerbalAuxiliary(Tree preterminal, Set<string> verbalSet, bool allowJustTagMatch)
{
if (preterminal.IsPreTerminal())
{
ILabel kidLabel = preterminal.Label();
string tag = null;
if (kidLabel is IHasTag)
{
tag = ((IHasTag) kidLabel).Tag();
}
if (tag == null)
{
tag = preterminal.Value();
}
ILabel wordLabel = preterminal.FirstChild().Label();
string word = null;
if (wordLabel is IHasWord)
{
word = ((IHasWord) wordLabel).GetWord();
}
if (word == null)
{
word = wordLabel.Value();
}
string lcWord = word.ToLower();
if (allowJustTagMatch && unambiguousAuxiliaryTags.Contains(tag) ||
verbalTags.Contains(tag) && verbalSet.Contains(lcWord))
{
return true;
}
}
return false;
}
示例3: IsExistential
/// <summary>
/// Checks whether the tree t is an existential constituent
/// There are two cases:
/// -- affirmative sentences in which "there" is a left sister of the VP
/// -- questions in which "there" is a daughter of the SQ.
/// </summary>
private bool IsExistential(Tree t, Tree parent)
{
bool toReturn = false;
string motherCat = Tlp.BasicCategory(t.Label().Value());
// affirmative case
if (motherCat.Equals(AbstractCollinsHeadFinder.VerbPhrase) && parent != null)
{
//take t and the sisters
Tree[] kids = parent.Children();
// iterate over the sisters before t and checks if existential
foreach (Tree kid in kids)
{
if (!kid.Value().Equals(AbstractCollinsHeadFinder.VerbPhrase))
{
List<ILabel> tags = kid.PreTerminalYield();
foreach (ILabel tag in tags)
{
if (tag.Value().Equals(PartsOfSpeech.ExistentialThere))
{
toReturn = true;
}
}
}
else
{
break;
}
}
}
// question case
else if (motherCat.StartsWith(SQ) && parent != null)
{
//take the daughters
Tree[] kids = parent.Children();
// iterate over the daughters and checks if existential
foreach (Tree kid in kids)
{
if (!PartsOfSpeech.IsVerb(kid.Value()))
{
//not necessary to look into the verb
List<ILabel> tags = kid.PreTerminalYield();
foreach (ILabel tag in tags)
{
if (tag.Value().Equals(PartsOfSpeech.ExistentialThere))
{
toReturn = true;
}
}
}
}
}
return toReturn;
}
示例4: 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))
//.........这里部分代码省略.........
示例5: TransformCc
/// <summary>
/// If things match, this method destructively changes the children list
/// of the tree t. When this method is called, t is an NP and there must
/// be at least two children to the right of ccIndex.
/// </summary>
/// <param name="t">The tree to transform a conjunction in</param>
/// <param name="ccIndex">The index of the CC child</param>
/// <returns>t</returns>
private static Tree TransformCc(Tree t, int ccIndex)
{
// use the factories of t to create new nodes
ITreeFactory tf = t.TreeFactory();
ILabelFactory lf = t.Label().LabelFactory();
Tree[] ccSiblings = t.Children();
//check if other CC
var ccPositions = new List<int>();
for (int i = ccIndex + 1; i < ccSiblings.Length; i++)
{
if (ccSiblings[i].Value().StartsWith(PartsOfSpeech.CoordinatingConjunction) && i < ccSiblings.Length - 1)
{
// second conjunct to ensure that a CC we add isn't the last child
ccPositions.Add(i);
}
}
// a CC b c ... -> (a CC b) c ... with b not a DT
string beforeSibling = ccSiblings[ccIndex - 1].Value();
if (ccIndex == 1 &&
(beforeSibling == PartsOfSpeech.Determiner
|| beforeSibling == PartsOfSpeech.Adjective
|| beforeSibling == PartsOfSpeech.Adverb
|| !(ccSiblings[ccIndex + 1].Value() == PartsOfSpeech.Determiner))
&& !(beforeSibling.StartsWith("NP")
|| beforeSibling.Equals("ADJP")
|| beforeSibling == PartsOfSpeech.NounPlural))
{
// && (ccSiblings.Length == ccIndex + 3 || !ccPositions.isEmpty())) { // something like "soya or maize oil"
string leftHead = GetHeadTag(ccSiblings[ccIndex - 1]);
//create a new tree to be inserted as first child of t
Tree left = tf.NewTreeNode(lf.NewLabel(leftHead), null);
for (int i = 0; i < ccIndex + 2; i++)
{
left.AddChild(ccSiblings[i]);
}
// remove all the children of t before ccIndex+2
for (int i = 0; i < ccIndex + 2; i++)
{
t.RemoveChild(0);
}
// if stuff after (like "soya or maize oil and vegetables")
// we need to put the tree in another tree
if (ccPositions.Any())
{
bool comma = false;
int index = ccPositions[0];
if (ccSiblings[index - 1].Value() == PartsOfSpeech.Comma)
{
//to handle the case of a comma ("soya and maize oil, and vegetables")
index = index - 1;
comma = true;
}
string head = GetHeadTag(ccSiblings[index - 1]);
if (ccIndex + 2 < index)
{
Tree tree = tf.NewTreeNode(lf.NewLabel(head), null);
tree.AddChild(0, left);
int k = 1;
for (int j = ccIndex + 2; j < index; j++)
{
t.RemoveChild(0);
tree.AddChild(k, ccSiblings[j]);
k++;
}
t.AddChild(0, tree);
}
else
{
t.AddChild(0, left);
}
Tree rightTree = tf.NewTreeNode(lf.NewLabel(Noun), null);
int start = 2;
if (comma)
{
start++;
}
while (start < t.NumChildren())
{
Tree sib = t.GetChild(start);
t.RemoveChild(start);
rightTree.AddChild(sib);
}
t.AddChild(rightTree);
}
//.........这里部分代码省略.........
示例6: 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;
}
示例7: 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());
}
示例8: 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);
}
}
示例9: TreeToLatexEvenHelper
private static int TreeToLatexEvenHelper(Tree t, StringBuilder c, StringBuilder h, int n,
int nextN, int indent, int curDepth, int maxDepth)
{
var sb = new StringBuilder();
for (int i = 0; i < indent; i++)
sb.Append(" ");
h.Append('\n').Append(sb);
int tDepth = t.Depth();
if (tDepth == 0 && tDepth + curDepth < maxDepth)
{
for (int pad = 0; pad < maxDepth - tDepth - curDepth; pad++)
{
h.Append("{\\ntnode{pad}{}, ");
}
}
h.Append("{\\ntnode{z").Append(n).Append("}{").Append(t.Label()).Append('}');
if (!t.IsLeaf())
{
for (int k = 0; k < t.Children().Length; k++)
{
h.Append(", ");
c.Append("\\nodeconnect{z").Append(n).Append("}{z").Append(nextN).Append("}\n");
nextN = TreeToLatexEvenHelper(t.Children()[k], c, h, nextN, nextN + 1, indent + 1, curDepth + 1,
maxDepth);
}
}
if (tDepth == 0 && tDepth + curDepth < maxDepth)
{
for (int pad = 0; pad < maxDepth - tDepth - curDepth; pad++)
{
h.Append('}');
}
}
h.Append('}');
return nextN;
}
示例10: TreeToLatexHelper
private static int TreeToLatexHelper(Tree t, StringBuilder c, StringBuilder h,
int n, int nextN, int indent)
{
var sb = new StringBuilder();
for (int i = 0; i < indent; i++)
sb.Append(" ");
h.Append('\n').Append(sb);
h.Append("{\\")
.Append(t.IsLeaf() ? "" : "n")
.Append("tnode{z")
.Append(n)
.Append("}{")
.Append(t.Label())
.Append('}');
if (!t.IsLeaf())
{
for (int k = 0; k < t.Children().Length; k++)
{
h.Append(", ");
c.Append("\\nodeconnect{z").Append(n).Append("}{z").Append(nextN).Append("}\n");
nextN = TreeToLatexHelper(t.Children()[k], c, h, nextN, nextN + 1, indent + 1);
}
}
h.Append('}');
return nextN;
}
示例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: 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;
}
示例15: Test
/// <summary>
/// Doesn't accept nodes that are A over A nodes (perhaps due to
/// empty removal or are EDITED nodes).
/// </summary>
public bool Test(Tree t)
{
if (t.IsLeaf() || t.IsPreTerminal())
{
return true;
}
// The special switchboard non-terminals clause
if ("EDITED".Equals(t.Label().Value()) || "CODE".Equals(t.Label().Value()))
{
return false;
}
if (t.NumChildren() != 1)
{
return true;
}
return
! (t.Label() != null && t.Label().Value() != null &&
t.Label().Value().Equals(t.GetChild(0).Label().Value()));
}