本文整理汇总了Java中edu.stanford.nlp.trees.Trees类的典型用法代码示例。如果您正苦于以下问题:Java Trees类的具体用法?Java Trees怎么用?Java Trees使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Trees类属于edu.stanford.nlp.trees包,在下文中一共展示了Trees类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readTreebank
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
public MemoryTreebank readTreebank() {
MemoryTreebank treebank = new MemoryTreebank(trf);
if (!new File(filename).isFile()) {
return treebank;
}
treebank.loadPath(filename);
// remove Parse failed
Iterator<Tree> itr = treebank.iterator();
while (itr.hasNext()) {
Tree t = itr.next();
Tree firstLeaf = Trees.getLeaf(t, 0);
if (firstLeaf.value().equals("Parse")) {
itr.remove();
}
}
return treebank;
}
示例2: evaluate
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
@Override
public Tree evaluate(Tree t, TregexMatcher m) {
Tree topNode = children[0].evaluate(t,m);
Tree bottomNode = children[1].evaluate(t,m);
if(Tsurgeon.verbose) {
System.err.println("Excising...original tree:");
t.pennPrint(System.err);
System.err.println("top: " + topNode + "\nbottom:" + bottomNode);
}
if(topNode==t)
return null;
Tree parent = topNode.parent(t);
if(Tsurgeon.verbose)
System.err.println("Parent: " + parent);
int i = Trees.objectEqualityIndexOf(parent,topNode);
parent.removeChild(i);
for(Tree child : bottomNode.children()) {
parent.addChild(i,child);
i++;
}
if(Tsurgeon.verbose)
t.pennPrint(System.err);
return t;
}
示例3: lexicalize_parse_tree
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
/** If one were to call any of these other methods to get a parse tree for some input sentence
* with the -outputFormatOptions flag of "lexicalize", they would receive their parse tree,
* in the -outputFormat of their choice, with every leaf marked with it's head word.
* This function does exactly that on an existing parse tree.
* NOTE that this WILL re-lexicalize a pre-lexicalized tree, so don't pass in a tree that
* has been lexicalized and expect to get back the same thing as what you passed in.
*/
public String lexicalize_parse_tree(String tree) throws TApplicationException
{
try
{
Tree parseTree = Tree.valueOf(tree);
Tree lexicalizedTree = Trees.lexicalize(parseTree, tlp.headFinder());
treePrinter = ParserUtil.setOptions(null, tlp); // use defaults
Function<Tree, Tree> a = TreeFunctions.getLabeledToDescriptiveCoreLabelTreeFunction();
lexicalizedTree = a.apply(lexicalizedTree);
return ParserUtil.TreeObjectToString(lexicalizedTree, treePrinter);
}
catch (Exception e)
{
// FIXME
throw new TApplicationException(TApplicationException.INTERNAL_ERROR, e.getMessage());
}
}
示例4: annotatePhraseStructureRecursively
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
/**
* Generate a SyntaxTreeNode Annotation corresponding to this Tree. Work
* recursively so that the annotations are actually generated from the bottom
* up, in order to build the consists list of annotation IDs.
*
* @param tree
* the current subtree
* @param rootTree
* the whole sentence, used to find the span of the current subtree
* @return a GATE Annotation of type "SyntaxTreeNode"
*/
protected Annotation annotatePhraseStructureRecursively(
AnnotationSet annotationSet, StanfordSentence stanfordSentence,
Tree tree, Tree rootTree) {
Annotation annotation = null;
Annotation child;
String label = tree.value();
List<Tree> children = tree.getChildrenAsList();
if(children.size() == 0) { return null; }
/* implied else */
/*
* following line generates ClassCastException IntPair span =
* tree.getSpan(); edu.stanford.nlp.ling.CategoryWordTag at
* edu.stanford.nlp.trees.Tree.getSpan(Tree.java:393) but I think it's a bug
* in the parser, so I'm hacking around it as follows.
*/
int startPos = Trees.leftEdge(tree, rootTree);
int endPos = Trees.rightEdge(tree, rootTree);
Long startNode = stanfordSentence.startPos2offset(startPos);
Long endNode = stanfordSentence.endPos2offset(endPos);
List<Integer> consists = new ArrayList<Integer>();
Iterator<Tree> childIter = children.iterator();
while(childIter.hasNext()) {
child =
annotatePhraseStructureRecursively(annotationSet, stanfordSentence,
childIter.next(), rootTree);
if((child != null) && (!child.getType().equals(inputTokenType))) {
consists.add(child.getId());
}
}
annotation =
annotatePhraseStructureConstituent(annotationSet, startNode, endNode,
label, consists, tree.depth());
return annotation;
}
示例5: annotatePhraseStructureRecursively
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
/**
* Generate a SyntaxTreeNode Annotation corresponding to this Tree. Work
* recursively so that the annotations are actually generated from the
* bottom up, in order to build the consists list of annotation IDs.
*
* @param tree the current subtree
* @param rootTree the whole sentence, used to find the span of the current subtree
* @return a GATE Annotation of type "SyntaxTreeNode"
*/
protected Annotation annotatePhraseStructureRecursively(AnnotationSet annotationSet, StanfordSentence stanfordSentence, Tree tree, Tree rootTree) {
Annotation annotation = null;
Annotation child;
String label = tree.value();
List<Tree> children = tree.getChildrenAsList();
if (children.size() == 0) {
return null;
}
/* implied else */
/* following line generates ClassCastException
* IntPair span = tree.getSpan();
* edu.stanford.nlp.ling.CategoryWordTag
* at edu.stanford.nlp.trees.Tree.getSpan(Tree.java:393)
* but I think it's a bug in the parser, so I'm hacking
* around it as follows. */
int startPos = Trees.leftEdge(tree, rootTree);
int endPos = Trees.rightEdge(tree, rootTree);
Long startNode = stanfordSentence.startPos2offset(startPos);
Long endNode = stanfordSentence.endPos2offset(endPos);
List<Integer> consists = new ArrayList<Integer>();
Iterator<Tree> childIter = children.iterator();
while (childIter.hasNext()) {
child = annotatePhraseStructureRecursively(annotationSet, stanfordSentence, childIter.next(), rootTree);
if ( (child != null) &&
(! child.getType().equals(inputTokenType) )) {
consists.add(child.getId());
}
}
annotation = annotatePhraseStructureConstituent(annotationSet, startNode, endNode, label, consists, tree.depth());
return annotation;
}
示例6: pruneHelper
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
private static Tree pruneHelper(Tree root, Tree nodeToPrune) {
if(nodeToPrune==root)
return null;
Tree parent = nodeToPrune.parent(root);
parent.removeChild(Trees.objectEqualityIndexOf(parent,nodeToPrune));
if(parent.children().length==0)
return pruneHelper(root,parent);
return root;
}
示例7: evaluate
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
@Override
public Tree evaluate(Tree t, TregexMatcher m) {
Tree result = t;
for (TsurgeonPattern child : children) {
Tree nodeToDelete = child.evaluate(t, m);
if (nodeToDelete == t) {
result = null;
}
Tree parent = nodeToDelete.parent(t);
parent.removeChild(Trees.objectEqualityIndexOf(parent,nodeToDelete));
}
return result;
}
示例8: evaluate
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
@Override
public Tree evaluate(Tree t, TregexMatcher m) {
Tree nodeToMove = children[0].evaluate(t,m);
Tree oldParent = nodeToMove.parent(t);
oldParent.removeChild(Trees.objectEqualityIndexOf(oldParent,nodeToMove));
Pair<Tree,Integer> position = l.evaluate(t,m);
position.first().insertDtr(nodeToMove,position.second());
return t;
}
示例9: firstLeaf
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
public static Tree firstLeaf(Tree t) {
return Trees.getLeaf(t, 0);
}
示例10: satisfies
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
@Override
boolean satisfies(Tree t1, Tree t2, Tree root) {
return Trees.rightEdge(t1, root) <= Trees.leftEdge(t2, root);
}
示例11: getNext
import edu.stanford.nlp.trees.Trees; //导入依赖的package包/类
private List<ScoredObject<Tree>> getNext()
{
try {
String line;
int parsesExpected = 0;
int sentenceId = lastSentenceId;
ScoredObject<Tree> curParse = null;
List<ScoredObject<Tree>> curParses = null;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.length() > 0) {
if (parsesExpected == 0) {
// Finished processing parses
String[] fields = wsDelimiter.split(line, 2);
parsesExpected = Integer.parseInt(fields[0]);
sentenceId = Integer.parseInt(fields[1]);
if (expectConsecutiveSentenceIds) {
if (sentenceId != lastSentenceId+1) {
if (lastSentenceId < sentenceId) {
StringBuilder sb = new StringBuilder("Missing sentences");
for (int i = lastSentenceId+1; i < sentenceId; i++) {
sb.append(" ").append(i);
}
logger.warning(sb.toString());
} else {
logger.warning("sentenceIds are not increasing (last="
+ lastSentenceId + ", curr=" + sentenceId + ")");
}
}
}
lastSentenceId = sentenceId;
curParses = new ArrayList<ScoredObject<Tree>>(parsesExpected);
} else {
if (curParse == null) {
// read score
double score = Double.parseDouble(line);
curParses.add(curParse = new ScoredObject<Tree>(null, score));
} else {
// Reading a parse
curParse.setObject(Trees.readTree(line));
curParse = null;
parsesExpected--;
if (parsesExpected == 0) {
return curParses;
}
}
}
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return null;
}