本文整理汇总了Java中edu.stanford.nlp.trees.Tree.children方法的典型用法代码示例。如果您正苦于以下问题:Java Tree.children方法的具体用法?Java Tree.children怎么用?Java Tree.children使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.stanford.nlp.trees.Tree
的用法示例。
在下文中一共展示了Tree.children方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toStringBuilder
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
static StringBuilder toStringBuilder(Tree tree, StringBuilder sb,
boolean printOnlyLabelValue, String offset) {
if (tree.isLeaf()) {
if (tree.label() != null) sb.append(printOnlyLabelValue ? tree.label().value() : tree.label());
return sb;
}
sb.append('(');
if (tree.label() != null) {
if (printOnlyLabelValue) {
if (tree.value() != null) sb.append(tree.label().value());
// don't print a null, just nothing!
} else {
sb.append(tree.label());
}
}
Tree[] kids = tree.children();
if (kids != null) {
for (Tree kid : kids) {
if (kid.isLeaf()) sb.append(' ');
else sb.append('\n').append(offset).append(' ');
toStringBuilder(kid, sb, printOnlyLabelValue,offset + " ");
}
}
return sb.append(')');
}
示例2: treeToDot
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
public String treeToDot()
{
String result="graph {\n";
Queue<Tree> q = new LinkedList<>();
q.add(this);
int a, b;
a=this.hashCode()*this.children().hashCode();
result+=" N_"+(a<0?-a%Integer.MAX_VALUE:a)+" [label=\""+this.label()+"\"];\n";
while(!q.isEmpty())
{
Tree t = q.remove();
for(Tree child: t.children())
{
a=t.hashCode()*t.children().hashCode();
if(child.children().length>0)
b=child.hashCode()*child.children().hashCode();
else
b=child.hashCode()*this.hashCode();
result+=" N_"+(b<0?-b%Integer.MAX_VALUE:b)+" [label=\""+child.label()+"\"];\n";
result+=" N_"+(a<0?-a%Integer.MAX_VALUE:a)+" -- "+"N_"+(b<0?-b%Integer.MAX_VALUE:b)+";\n";
q.add(child);
}
}
result+="}";
return result;
}
示例3: getClauses
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
public ArrayList<Clause> getClauses(Tree t)
{
ArrayList<Clause> retList = new ArrayList<Clause>();
for(Tree subTree: t)
{
if(subTree.label().value().equals("S"))
{
for(Tree tree:subTree.children())
{
retList.add(getLeavesOfRoot(tree));
}
break;
}
}
return retList;
}
示例4: sameLabelAtLevel
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
private boolean sameLabelAtLevel(Tree left, Tree right, int level)
{
if(left.isLeaf() || right.isLeaf())
return false;
if(level == 0)
return left.nodeString().equals(right.nodeString());
Tree[] leftChildren = left.children();
Tree[] rightChildren = right.children();
if(leftChildren.length != rightChildren.length)
return false;
for(int i = 0; i < leftChildren.length; i++)
{
if(!sameLabelAtLevel(leftChildren[i], rightChildren[i], level - 1))
return false;
}
return true;
}
示例5: removeEmptyNodes
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
public static Tree removeEmptyNodes(Tree tree)
{
Tree[] children = tree.children();
for (int i = 0; i < children.length; i++)
{
Tree child = children[i];
// heuristic: all leaf nodes have a special anchor symbol '<>' in the end.
// If we don't find this, then we should remove the node.
if (child.numChildren() == 0 && !child.label().value().contains("<>"))
{
// System.out.println("node " + child);
tree.removeChild(i);
} else
{
removeEmptyNodes(child);
}
}
return tree;
}
示例6: determineHead
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
public Tree determineHead(Tree t) {
if (t.isLeaf()) {
return null;
} else {
return t.children()[t.children().length - 1];
}
}
示例7: preorder
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
private static String preorder(Tree tree) {
List<Tree> queue = new LinkedList<>();
queue.add(tree);
while ( ! queue.isEmpty()) {
Tree currentNode = queue.remove(0);
if (currentNode.isLeaf())
continue;
Tree children[] = currentNode.children();
int childCount = children.length;
IndexedWord hw = (IndexedWord) currentNode.label();
List<FeatureNode> featureNodes = new ArrayList<>(childCount);
for (int i = 0; i < childCount; i++) {
featureNodes.add(new FeatureNode(children[i], hw));
queue.add(children[i]);
}
if (childCount < 8) {
Pair<Double, List<Integer>> result = search(featureNodes, new LinkedList<Integer>(), Double.NEGATIVE_INFINITY);
if (result != null) {
List<Integer> permutation = result.second;
List<Tree> newChildren = new ArrayList<>(Arrays.asList(children));
for (int i = 0; i < childCount; i++) {
int idx = permutation.get(i);
newChildren.set(idx, children[i]);
}
currentNode.setChildren(newChildren);
} else {
System.err.println("Warning: No path found.");
}
}
}
return StringUtils.join(tree.yieldWords());
}
示例8: addTreebankNodeChildrenToIndexes
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
private FSArray addTreebankNodeChildrenToIndexes(
TreebankNode parent,
JCas jCas,
List<CoreLabel> tokenAnns,
Tree tree) {
Tree[] childTrees = tree.children();
// collect all children (except leaves, which are just the words - POS tags are pre-terminals in
// a Stanford tree)
List<TreebankNode> childNodes = new ArrayList<TreebankNode>();
for (Tree child : childTrees) {
if (!child.isLeaf()) {
// set node attributes and add children (mutual recursion)
TreebankNode node = new TreebankNode(jCas);
node.setParent(parent);
this.addTreebankNodeToIndexes(node, jCas, child, tokenAnns);
childNodes.add(node);
}
}
// convert the child list into an FSArray
FSArray childNodeArray = new FSArray(jCas, childNodes.size());
for (int i = 0; i < childNodes.size(); ++i) {
childNodeArray.set(i, childNodes.get(i));
}
return childNodeArray;
}
示例9: indexOfChild
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
public static int indexOfChild(Tree parent, int childNodeNumber, Tree tree)
{
if(parent.children() == null)
return -1;
int i = 0;
for(Tree sibling : parent.children())
{
// if(sibling.equals(child))
if(sibling.nodeNumber(tree) == childNodeNumber)
return i;
i++;
}
return -1;
}
示例10: extractRelatedFeatures
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
private void extractRelatedFeatures(Tree t, int marker, HashMap<Integer, ArrayList<String>> featureSentence) {
//HashMap<String, Integer> features = new HashMap<String, Integer>();
int localmarker = marker;
if (t.label().value().equals("S") || t.label().value().equals("SBAR")) {
localmarker = this.sentenceMarker + 1;
this.sentenceMarker = localmarker;
marker++;
}
if (t.label().value().length() > 1 && t.label().value().equals(t.label().value().toLowerCase())) {
ArrayList<String> currentFeatures = featureSentence.get(marker);
if(currentFeatures == null) {
currentFeatures = new ArrayList<String>();
}
currentFeatures.add(t.label().value());
//features.put(t.label().value(), new Integer(1));
featureSentence.put(marker, currentFeatures);
}
for (Tree child: t.children()) {
HashMap<String, Integer> temp = new HashMap<String, Integer>();
this.extractRelatedFeatures(child, localmarker, featureSentence);
if (temp.size() > 0) {
Iterator<String> it = temp.keySet().iterator();
while(it.hasNext()) {
String currentFeature = (String) it.next();
//features.put(currentFeature, new Integer(1));
}
}
}
if(localmarker == 1) {
marker--;
}
//return features;
}
示例11: constructConstituent
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
/**
*
* @param root
* @param left
* @param right
* @param n
* is the length of the sentence is tokens.
* @param p
* @param tokenizationUUID
* @return The constituent ID
* @throws AnalyticException
*/
private static int constructConstituent(Tree root, int left,
int right, int n, Parse p, UUID tokenizationUUID, HeadFinder hf)
throws AnalyticException {
Constituent constituent = new Constituent();
constituent.setId(p.getConstituentListSize());
constituent.setTag(root.value());
constituent.setStart(left);
constituent.setEnding(right);
p.addToConstituentList(constituent);
Tree headTree = null;
if (!root.isLeaf()) {
try {
headTree = hf.determineHead(root);
} catch (java.lang.IllegalArgumentException iae) {
LOGGER.warn("Failed to find head, falling back on rightmost constituent.", iae);
headTree = root.children()[root.numChildren() - 1];
}
}
int i = 0, headTreeIdx = -1;
int leftPtr = left;
for (Tree child : root.getChildrenAsList()) {
int width = child.getLeaves().size();
int childId = constructConstituent(child, leftPtr, leftPtr
+ width, n, p, tokenizationUUID, hf);
constituent.addToChildList(childId);
leftPtr += width;
if (headTree != null && child == headTree) {
assert (headTreeIdx < 0);
headTreeIdx = i;
}
i++;
}
if (headTreeIdx >= 0)
constituent.setHeadChildIndex(headTreeIdx);
if (!constituent.isSetChildList())
constituent.setChildList(new ArrayList<Integer>());
return constituent.getId();
}
示例12: countPOSTokens
import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
public static IntCounter<String> countPOSTokens(Collection<Tree> trees) {
IntCounter<String> particleParents = new IntCounter<String>();
IntCounter<String> tokens = new IntCounter<String>();
for( Tree tree : trees ) {
// System.out.println("tree: " + tree);
Collection<Tree> leaves = TreeOperator.leavesFromTree(tree);
for( Tree leaf : leaves ) {
// Lowercase the token.
String token = leaf.getChild(0).value().toLowerCase();
String tag = leaf.value();
// Particles.
// e.g. (PRT (RP up))
if( tag.equals("RP") ) {
Tree gparent = leaf.ancestor(2, tree);
Tree gparentTree = gparent.children()[0];
String gVerbHead = gparentTree.getChild(0).value().toLowerCase();
char gTag = CalculateIDF.normalizePOS(gparentTree.value());
String headKey = CalculateIDF.createKey(gVerbHead + "_" + token, gTag);
// System.out.println("Particle verb " + headKey);
// System.out.println(" Incrementing " + headKey);
tokens.incrementCount(headKey);
particleParents.incrementCount(CalculateIDF.createKey(gVerbHead, gTag));
}
// All other words.
else {
// e.g. v-helping
String key = CalculateIDF.createKey(token, CalculateIDF.normalizePOS(tag));
tokens.incrementCount(key);
}
}
}
// Remove counts from verbs that are counted with their particles.
for( Map.Entry<String, Double> entry : particleParents.entrySet() ) {
// System.out.println(" Decrementing " + entry.getKey());
tokens.decrementCount(entry.getKey(), entry.getValue());
}
// System.out.println("Returning " + tokens);
return tokens;
}