本文整理匯總了Java中edu.stanford.nlp.trees.Tree.isLeaf方法的典型用法代碼示例。如果您正苦於以下問題:Java Tree.isLeaf方法的具體用法?Java Tree.isLeaf怎麽用?Java Tree.isLeaf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類edu.stanford.nlp.trees.Tree
的用法示例。
在下文中一共展示了Tree.isLeaf方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: findTreeLocation
import edu.stanford.nlp.trees.Tree; //導入方法依賴的package包/類
public static Object findTreeLocation(Tree tree, int leafPos, Integer currentPos) {
if( tree.isLeaf() ) {
if( leafPos == currentPos.intValue() ) {
return tree;
}
return new Integer(currentPos.intValue()+1);
} else {
List<Tree> children = tree.getChildrenAsList();
for( Tree child : children ) {
Object obj = findTreeLocation(child, leafPos, currentPos);
if( obj != null && obj instanceof Integer ) {
currentPos = (Integer)obj;
} else return obj; // found the tree
}
return currentPos;
}
}
示例3: 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;
}
示例4: 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];
}
}
示例5: subSentence
import edu.stanford.nlp.trees.Tree; //導入方法依賴的package包/類
/**
* 返回一個s節點下麵的完整子句
*
* @param tree
* @param sb
*/
private void subSentence(Tree tree, StringBuilder sb) {
if (tree.isLeaf()) {
sb.append(tree.nodeString() + " ");
return;
} else {
List<Tree> childTrees = tree.getChildrenAsList();
for (Tree child : childTrees) {
this.subSentence(child, sb);
}
}
}
示例6: subSentence
import edu.stanford.nlp.trees.Tree; //導入方法依賴的package包/類
/**
* 返回一個s節點下麵的完整子句
* @param tree
* @param sb
*/
private void subSentence(Tree tree, StringBuilder sb){
if(tree.isLeaf()){
sb.append(tree.nodeString() + " ");
return;
}else{
final List<Tree> childTrees = tree.getChildrenAsList();
for (final Tree child : childTrees) {
this.subSentence(child, sb);
}
}
}
示例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: 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();
}