本文整理汇总了Java中edu.stanford.nlp.parser.lexparser.LexicalizedParser.apply方法的典型用法代码示例。如果您正苦于以下问题:Java LexicalizedParser.apply方法的具体用法?Java LexicalizedParser.apply怎么用?Java LexicalizedParser.apply使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.stanford.nlp.parser.lexparser.LexicalizedParser
的用法示例。
在下文中一共展示了LexicalizedParser.apply方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: demoDP
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
/**
* demoDP demonstrates turning a file into tokens and then parse trees. Note
* that the trees are printed by calling pennPrint on the Tree object. It is
* also possible to pass a PrintWriter to pennPrint if you want to capture
* the output.
*
* file => tokens => parse trees
*/
public static void demoDP(LexicalizedParser lp, String filename) {
// This option shows loading, sentence-segmenting and tokenizing
// a file using DocumentPreprocessor.
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
// You could also create a tokenizer here (as below) and pass it
// to DocumentPreprocessor
for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
Tree parse = lp.apply(sentence);
parse.pennPrint();
System.out.println();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCCprocessed();
System.out.println(tdl);
System.out.println();
}
}
示例2: main
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
public static void main(String[] args) {
LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz");
lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
Tree parse = (Tree) lp.apply(Arrays.asList(sent));
parse.pennPrint();
System.out.println();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl);
System.out.println();
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
}
示例3: main
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
public static void main(String[] args) {
LexicalizedParser lp = new LexicalizedParser("parsers/englishFactored.ser.gz");
lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
Tree parse = (Tree) lp.apply("Try this sentence, which is slightly longer.");
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();
TypedDependency td = tdl.iterator().next();
TreeGraphNode node = td.dep();
node = (TreeGraphNode) node.parent();
node.deepCopy();
}
示例4: writeImage
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
public static void writeImage(String sentence, String outFile, int scale) throws Exception {
LexicalizedParser lp = null;
try {
lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
} catch (Exception e) {
System.err.println("Could not load file englishPCFG.ser.gz. Try placing this file in the same directory as Dependencee.jar");
return;
}
lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
TokenizerFactory<CoreLabel> tokenizerFactory =
PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
Tree tree = lp.apply(wordList);
writeImage(tree, outFile, scale);
}
示例5: testWriteImage
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
/**
* Test of writeImage method, of class Main.
*/
@Test
public void testWriteImage() throws Exception {
String text = "A quick brown fox jumped over the lazy dog.";
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
LexicalizedParser lp = LexicalizedParser.loadModel();
lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
TokenizerFactory<CoreLabel> tokenizerFactory =
PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(text)).tokenize();
Tree tree = lp.apply(wordList);
GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();
Main.writeImage(tdl, "image.png", 3);
assert (new File("image.png").exists());
}
示例6: demoDP
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
public static void demoDP(LexicalizedParser lp, String filename) {
// This option shows loading and sentence-segment and tokenizing
// a file using DocumentPreprocessor
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
// You could also create a tokenizer here (as below) and pass it
// to DocumentPreprocessor
for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
Tree parse = lp.apply(sentence);
parse.pennPrint();
System.out.println();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCCprocessed(true);
System.out.println(tdl);
System.out.println();
}
}
示例7: demoAPI
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
/**
* demoAPI demonstrates other ways of calling the parser with already
* tokenized text, or in some cases, raw text that needs to be tokenized as
* a single sentence. Output is handled with a TreePrint object. Note that
* the options used when creating the TreePrint can determine what results
* to print out. Once again, one can capture the output by passing a
* PrintWriter to TreePrint.printTree.
*
* difference: already tokenized text
*
*
*/
public static void demoAPI(LexicalizedParser lp) {
// This option shows parsing a list of correctly tokenized words
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
Tree parse = lp.apply(rawWords);
parse.pennPrint();
System.out.println();
// This option shows loading and using an explicit tokenizer
String sent2 = "Hey @Apple, pretty much all your products are amazing. You blow minds every time you launch a new gizmo."
+ " that said, your hold music is crap";
TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(
new CoreLabelTokenFactory(), "");
Tokenizer<CoreLabel> tok = tokenizerFactory
.getTokenizer(new StringReader(sent2));
List<CoreLabel> rawWords2 = tok.tokenize();
parse = lp.apply(rawWords2);
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
System.out.println(tdl);
System.out.println();
// You can also use a TreePrint object to print trees and dependencies
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
}
示例8: getGraph
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
public static Graph getGraph(String sentence) throws Exception {
LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
TokenizerFactory<CoreLabel> tokenizerFactory =
PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
Tree tree = lp.apply(wordList);
GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
Collection<TypedDependency> tdl = gs.typedDependencies();
return getGraph(tree, tdl);
}
示例9: demoAPI
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
public static void demoAPI(LexicalizedParser lp) {
// This option shows parsing a list of correctly tokenized words
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
Tree parse = lp.apply(rawWords);
parse.pennPrint();
System.out.println();
// This option shows loading and using an explicit tokenizer
String sent2 = "This is another sentence.";
TokenizerFactory<CoreLabel> tokenizerFactory =
PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
List<CoreLabel> rawWords2 =
tokenizerFactory.getTokenizer(new StringReader(sent2)).tokenize();
parse = lp.apply(rawWords2);
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
System.out.println(tdl);
System.out.println();
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
}
示例10: getDependencyByLine
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
public ArrayList<ArrayList<String>> getDependencyByLine(
LexicalizedParser lp, String filename, String authorfilename) {
ArrayList<ArrayList<String>> retArrayList = new ArrayList<ArrayList<String>>();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
BufferedReader authorReader = new BufferedReader(new FileReader(
authorfilename));
String line = "";
String author = "";
while ((line = br.readLine()) != null) {
author = authorReader.readLine();
Tokenizer<? extends HasWord> toke = tlp.getTokenizerFactory()
.getTokenizer(new StringReader(line));
List<? extends HasWord> sentence = toke.tokenize();
Tree parse = lp.apply(sentence);
List<Tree> childTrees = parse.getChildrenAsList();
Stack<Tree> treeStack = new Stack<Tree>();
treeStack.addAll(childTrees);
Label prevLabel = null;
Label curLabel = parse.label();
HashMap<Integer, Pair<Label, Label>> wordTagMap = new HashMap<Integer, Pair<Label, Label>>();
int depth = 1;
while (!treeStack.isEmpty()) {
Tree curTree = treeStack.pop();
prevLabel = curLabel;
curLabel = curTree.label();
childTrees = curTree.getChildrenAsList();
if (0 == childTrees.size()) {
// word node
wordTagMap.put(depth, new Pair<Label, Label>(curLabel,
prevLabel));
depth++;
} else {
treeStack.addAll(childTrees);
}
}
final int numWord = wordTagMap.size();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
for (TypedDependency typedDep : tdl) {
int govIndex = typedDep.gov().index();
int depIndex = typedDep.dep().index();
if (wordTagMap.containsKey(govIndex)
&& wordTagMap.containsKey(depIndex)) {
ArrayList<String> arrList = new ArrayList<String>();
arrList.add(typedDep.dep().nodeString());
arrList.add(wordTagMap.get(numWord
- typedDep.dep().index() + 1).snd.toString());
arrList.add(typedDep.reln().toString());
arrList.add(typedDep.gov().nodeString());
arrList.add(wordTagMap.get(numWord
- typedDep.gov().index() + 1).snd.toString());
arrList.add(author);
arrList.add(line);
retArrayList.add(arrList);
}
}
}
br.close();
authorReader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retArrayList;
}
示例11: getDependencyBySentence
import edu.stanford.nlp.parser.lexparser.LexicalizedParser; //导入方法依赖的package包/类
/**
* file => tokens => parse trees
*
* @param lp
* @param filename
* tuples
*/
public ArrayList<ArrayList<String>> getDependencyBySentence(
LexicalizedParser lp, String filename) {
ArrayList<ArrayList<String>> retArrayList = new ArrayList<ArrayList<String>>();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
Tree parse = lp.apply(sentence);
List<Tree> childTrees = parse.getChildrenAsList();
Stack<Tree> treeStack = new Stack<Tree>();
treeStack.addAll(childTrees);
Label prevLabel = null;
Label curLabel = parse.label();
HashMap<Integer, Pair<Label, Label>> wordTagMap = new HashMap<Integer, Pair<Label, Label>>();
int depth = 1;
while (!treeStack.isEmpty()) {
Tree curTree = treeStack.pop();
prevLabel = curLabel;
curLabel = curTree.label();
childTrees = curTree.getChildrenAsList();
if (0 == childTrees.size()) {
// word node
wordTagMap.put(depth, new Pair<Label, Label>(curLabel,
prevLabel));
depth++;
} else {
treeStack.addAll(childTrees);
}
}
final int numWord = wordTagMap.size();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
for (TypedDependency typedDep : tdl) {
int govIndex = typedDep.gov().index();
int depIndex = typedDep.dep().index();
if (wordTagMap.containsKey(govIndex)
&& wordTagMap.containsKey(depIndex)) {
ArrayList<String> arrList = new ArrayList<String>();
arrList.add(typedDep.dep().nodeString());
arrList.add(wordTagMap.get(numWord - typedDep.dep().index()
+ 1).snd.toString());
arrList.add(typedDep.reln().toString());
arrList.add(typedDep.gov().nodeString());
arrList.add(wordTagMap.get(numWord - typedDep.gov().index()
+ 1).snd.toString());
retArrayList.add(arrList);
}
}
}
return retArrayList;
}