Java Reference
In-Depth Information
Finding the word dependencies
The question is stored as a simple string:
String question =
"Who is the 32nd president of the United States?";
We will use the LexicalizedParser class as developed in the Finding word depend-
encies using the GrammaticalStructure class section. The relevant code is duplicated here
for your convenience:
String parserModel = ".../englishPCFG.ser.gz";
LexicalizedParser lexicalizedParser =
LexicalizedParser.loadModel(parserModel);
TokenizerFactory<CoreLabel> tokenizerFactory =
PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
Tokenizer<CoreLabel> tokenizer =
tokenizerFactory.getTokenizer(new
StringReader(question));
List<CoreLabel> wordList = tokenizer.tokenize();
Tree parseTree = lexicalizedParser.apply(wordList);
TreebankLanguagePack tlp =
lexicalizedParser.treebankLanguagePack();
GrammaticalStructureFactory gsf =
tlp.grammaticalStructureFactory();
GrammaticalStructure gs =
gsf.newGrammaticalStructure(parseTree);
List<TypedDependency> tdl =
gs.typedDependenciesCCprocessed();
System.out.println(tdl);
for (TypedDependency dependency : tdl) {
System.out.println("Governor Word: [" + dependency.gov()
+ "] Relation: [" + dependency.reln().getLongName()
+ "] Dependent Word: [" + dependency.dep() + "]");
}
When executed with the question, we get the following output:
Search WWH ::




Custom Search