本文整理汇总了Java中edu.berkeley.nlp.syntax.Trees类的典型用法代码示例。如果您正苦于以下问题:Java Trees类的具体用法?Java Trees怎么用?Java Trees使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Trees类属于edu.berkeley.nlp.syntax包,在下文中一共展示了Trees类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static void main(String[] args) throws Throwable {
Tree<String> goldTree = (new Trees.PennTreeReader(new StringReader(
"(ROOT (S (NP (DT the) (NN can)) (VP (VBD fell))))"))).next();
Tree<String> guessedTree = (new Trees.PennTreeReader(new StringReader(
"(ROOT (S (NP (DT the)) (VP (MB can) (VP (VBD fell)))))")))
.next();
LabeledConstituentEval<String> eval = new LabeledConstituentEval<String>(
Collections.singleton("ROOT"), new HashSet<String>());
RuleEval<String> rule_eval = new RuleEval<String>(
Collections.singleton("ROOT"), new HashSet<String>());
System.out.println("Gold tree:\n"
+ Trees.PennTreeRenderer.render(goldTree));
System.out.println("Guessed tree:\n"
+ Trees.PennTreeRenderer.render(guessedTree));
eval.evaluate(guessedTree, goldTree);
eval.display(true);
rule_eval.evaluate(guessedTree, goldTree);
rule_eval.display(true);
}
示例2: unAnnotateTreeSpecial
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static Tree<String> unAnnotateTreeSpecial(Tree<String> annotatedTree) {
// Remove intermediate nodes (labels beginning with "Y"
// Remove all material on node labels which follow their base symbol
// (cuts at the leftmost -, ^, or : character)
// Examples: a node with label @NP->DT_JJ will be spliced out, and a
// node with label NP^S will be reduced to NP
Tree<String> debinarizedTree = Trees.spliceNodes(annotatedTree,
new Filter<String>() {
public boolean accept(String s) {
return s.startsWith("Y");
}
});
Tree<String> unAnnotatedTree = (new Trees.FunctionNodeStripper())
.transformTree(debinarizedTree);
return unAnnotatedTree;
}
示例3: unAnnotateTree
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static Tree<String> unAnnotateTree(Tree<String> annotatedTree,
boolean keepFunctionLabel) {
// Remove intermediate nodes (labels beginning with "@"
// Remove all material on node labels which follow their base symbol
// (cuts at the leftmost -, ^, or : character)
// Examples: a node with label @NP->DT_JJ will be spliced out, and a
// node with label NP^S will be reduced to NP
Tree<String> debinarizedTree = Trees.spliceNodes(annotatedTree,
new Filter<String>() {
public boolean accept(String s) {
return s.startsWith("@") && !s.equals("@");
}
});
if (keepFunctionLabel)
return debinarizedTree;
Tree<String> unAnnotatedTree = (new Trees.FunctionNodeStripper())
.transformTree(debinarizedTree);
return unAnnotatedTree;
}
示例4: main
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static void main(String args[]) {
// test the binarization
Trees.PennTreeReader reader = new Trees.PennTreeReader(
new StringReader(
"((S (NP (DT the) (JJ quick) (JJ (AA (BB (CC brown)))) (NN fox)) (VP (VBD jumped) (PP (IN over) (NP (DT the) (JJ lazy) (NN dog)))) (. .)))"));
Tree<String> tree = reader.next();
System.out.println("tree");
System.out.println(Trees.PennTreeRenderer.render(tree));
for (Binarization binarization : Binarization.values()) {
System.out.println("binarization type " + binarization.name());
// print the binarization
try {
Tree<String> binarizedTree = binarizeTree(tree, binarization);
System.out
.println(Trees.PennTreeRenderer.render(binarizedTree));
System.out.println("unbinarized");
Tree<String> unBinarizedTree = unAnnotateTree(binarizedTree,
false);
System.out.println(Trees.PennTreeRenderer
.render(unBinarizedTree));
System.out.println("------------");
} catch (Error e) {
System.out.println("binarization not implemented");
}
}
}
示例5: readTrees
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static List<Tree<String>> readTrees(String basePath, int low,
int high, Charset charset) throws Exception {
Collection<Tree<String>> trees = PennTreebankReader.readTrees(basePath,
low, high, charset);
// System.out.println("in readTrees");
// normalize trees
Trees.TreeTransformer<String> treeTransformer = (keepFunctionLabels) ? new Trees.FunctionLabelRetainingTreeNormalizer()
: new Trees.StandardTreeNormalizer();
List<Tree<String>> normalizedTreeList = new ArrayList<Tree<String>>();
for (Tree<String> tree : trees) {
Tree<String> normalizedTree = treeTransformer.transformTree(tree);
normalizedTreeList.add(normalizedTree);
}
if (normalizedTreeList.size() == 0) {
throw new Exception("failed to load any trees at " + basePath
+ " from " + low + " to " + high);
}
return normalizedTreeList;
}
示例6: main
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
/**
* Go through trees and determine their heads and print them. Just for
* debuggin'. <br>
* Usage: <code>
* java edu.stanford.nlp.trees.CollinsHeadFinder treebankFilePath
* </code>
*
* @param args
* The treebankFilePath
*/
public static void main(String[] args) {
Trees.PennTreeReader reader = new Trees.PennTreeReader(
new StringReader(
"((S (NP (DT the) (JJ quick) (JJ (AA (BB (CC brown)))) (NN fox)) (VP (VBD jumped) (PP (IN over) (NP (DT the) (JJ lazy) (NN dog)))) (. .)))"));
Tree<String> tree = reader.next();
System.out.println("tree " + tree);
CollinsHeadFinder headFinder = new CollinsHeadFinder();
while (!tree.isLeaf()) {
Tree<String> head = headFinder.determineHead(tree);
System.out.println("head " + head);
tree = head;
}
}
示例7: labelTrees
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
private void labelTrees(Grammar grammar, List<Tree<String>> trainTrees,
List<List<String>> goldPOStags) {
List<Tree<String>> trainTreesNoGF = stripOffGF(trainTrees);
StateSetTreeList stateSetTrees = new StateSetTreeList(trainTreesNoGF,
grammar.numSubStates, false, tagNumberer);
int index = 0;
for (Tree<StateSet> stateSetTree : stateSetTrees) {
List<String> goldPOS = goldPOStags.get(index++);
Tree<String> labeledTree = guessGF(stateSetTree, grammar, goldPOS);
Tree<String> debinarizedTree = Trees.spliceNodes(labeledTree,
new Filter<String>() {
public boolean accept(String s) {
return s.startsWith("@");
}
});
System.out.println(debinarizedTree + "\n");
}
}
示例8: advance
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
private void advance() {
nextTreeIterator = null;
while (nextTreeIterator == null && fileIterator.hasNext()) {
try {
File file = fileIterator.next();
nextTreeIterator = new Trees.PennTreeReader(
new BufferedReader(new FileReader(file)));
} catch (FileNotFoundException e) {
}
}
}
示例9: main
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static void main(String[] args) {
Collection<Tree<String>> trees = readTrees(args[0]);
for (Tree<String> tree : trees) {
tree = (new Trees.StandardTreeNormalizer()).transformTree(tree);
System.out.println(Trees.PennTreeRenderer.render(tree));
}
}
示例10: debinarizeTree
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static Tree<String> debinarizeTree(Tree<String> annotatedTree) {
// Remove intermediate nodes (labels beginning with "@"
// Remove all material on node labels which follow their base symbol
// (cuts at the leftmost -, ^, or : character)
// Examples: a node with label @NP->DT_JJ will be spliced out, and a
// node with label NP^S will be reduced to NP
Tree<String> debinarizedTree = Trees.spliceNodes(annotatedTree,
new Filter<String>() {
public boolean accept(String s) {
return s.startsWith("@") && !s.equals("@");
}
});
return debinarizedTree;
}
示例11: main
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
/**
* @param args
*/
public static void main(String[] args) {
if (args.length < 3) {
System.out
.println("usage: java TreeGenerator <input file for grammar> <maxLength> <nTrees>\n");
System.exit(2);
}
String inFileName = args[0];
int maxLength = Integer.parseInt(args[1]);
int nTrees = Integer.parseInt(args[2]);
System.out.println("Loading grammar from " + inFileName + ".");
ParserData pData = ParserData.Load(inFileName);
if (pData == null) {
System.out.println("Failed to load grammar from file" + inFileName
+ ".");
System.exit(1);
}
grammar = pData.getGrammar();
lexicon = (SophisticatedLexicon) pData.getLexicon();
Numberer.setNumberers(pData.getNumbs());
tagNumberer = Numberer.getGlobalNumberer("tags");
grammar.splitRules();
int nGen = 0;
while (nGen < nTrees) {
Tree<String> artTree = generateTree(0, 0);
System.out.println(artTree.getYield().toString());
Tree<String> tree = TreeAnnotations.unAnnotateTree(artTree, false);
if (tree.getYield().size() > maxLength)
continue;
System.out.println("Generated tree of length "
+ tree.getYield().size() + ".\n"
+ Trees.PennTreeRenderer.render(tree) + "\n");
nGen++;
}
}
示例12: loadSingleFile
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
private void loadSingleFile(String path) throws Exception {
System.out.print("Loading trees from single file...");
InputStreamReader inputData = new InputStreamReader(
new FileInputStream(path), "UTF-8");
PennTreeReader treeReader = new PennTreeReader(inputData);
while (treeReader.hasNext()) {
trainTrees.add(treeReader.next());
}
Trees.TreeTransformer<String> treeTransformer = (keepFunctionLabels) ? new Trees.FunctionLabelRetainingTreeNormalizer()
: new Trees.StandardTreeNormalizer();
ArrayList<Tree<String>> normalizedTreeList = new ArrayList<Tree<String>>();
for (Tree<String> tree : trainTrees) {
Tree<String> normalizedTree = treeTransformer.transformTree(tree);
normalizedTreeList.add(normalizedTree);
}
if (normalizedTreeList.size() == 0) {
throw new Exception("failed to load any trees at " + path);
}
trainTrees = normalizedTreeList;
devTestTrees = trainTrees;
System.out.println("done");
// trainTrees.addAll(readTrees(path,-1,
// Integer.MAX_VALUE,Charset.defaultCharset()));
}
示例13: determineNonTrivialHead
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
protected Tree<String> determineNonTrivialHead(Tree<String> t) {
Tree<String> theHead = null;
String motherCat = Trees.FunctionNodeStripper.transformLabel(t);
if (DEBUG) {
System.err.println("Looking for head of " + t.getLabel());
}
// 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 = (String[][]) nonTerminalInfo.get(motherCat);
if (how == null) {
if (DEBUG) {
System.err.println("Warning: No rule found for " + motherCat);
}
if (defaultRule != null) {
return traverseLocate(t.getChildren(), defaultRule, true);
} else {
return null;
}
}
for (int i = 0; i < how.length; i++) {
boolean deflt = (i == how.length - 1);
theHead = traverseLocate(t.getChildren(), how[i], deflt);
if (theHead != null) {
break;
}
}
return theHead;
}
示例14: main
import edu.berkeley.nlp.syntax.Trees; //导入依赖的package包/类
public static void main(String[] args) {
Collection<Tree<String>> trees = readTrees(args[0],
Charset.defaultCharset());
for (Tree<String> tree : trees) {
tree = (new Trees.StandardTreeNormalizer()).transformTree(tree);
System.out.println(Trees.PennTreeRenderer.render(tree));
}
}