我想将实际使用Stanford Parser 2.0.4的Java代码更新到更新的版本(3.6)
问题是函数“EnglishGrammaticalRelations.SUBJECT.isAncestor”在3.6中不起作用,我需要检查关系是否等于另一个关系的祖先。我使用Stanford 2.0.4的代码如下:
String sentence = "The company is a subsidiary of International Data Group";
Tree depTree;
SemanticGraph semanticGraph;
LexicalizedParser lp;
TokenizerFactory<CoreLabel> tokenizerFactory;
LexicalizedParserQuery lpq;
lp = LexicalizedParser
.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
lpq = lp.parserQuery();
List<CoreLabel> tokenizedSentence = tokenizerFactory.getTokenizer(
new StringReader(sentence)).tokenize();
lpq.parse(tokenizedSentence);
depTree = lpq.getBestParse();//getBestPCFGParse();
// use uncollapsed dependencies to facilitate tree creation
semanticGraph = SemanticGraphFactory.makeFromTree(depTree, false);
System.out.println("compact graph" + semanticGraph.toCompactString());
for (SemanticGraphEdge e : semanticGraph.edgeIterable()) {
if (EnglishGrammaticalRelations.SUBJECT.isAncestor(e.getRelation())) {
System.out.println("This is any subject");
}
}
使用Stanford 3.6需要进行一些修改:
String sentence = "The company is a subsidiary of International Data Group";
Tree depTree;
SemanticGraph semanticGraph;
LexicalizedParser lp;
TokenizerFactory<CoreLabel> tokenizerFactory;
LexicalizedParserQuery lpq;
lp = LexicalizedParser
.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
lpq = lp.lexicalizedParserQuery();
List<CoreLabel> tokenizedSentence = tokenizerFactory.getTokenizer(
new StringReader(sentence)).tokenize();
lpq.parse(tokenizedSentence);
depTree = lpq.getBestPCFGParse();//getBestParse();
// use uncollapsed dependencies to facilitate tree creation
semanticGraph = SemanticGraphFactory.makeFromTree(depTree, false);
System.out.println("compact graph" + semanticGraph.toCompactString());
for (SemanticGraphEdge e : semanticGraph.edgeIterable()) {
if (EnglishGrammaticalRelations.SUBJECT.isAncestor(e.getRelation())) {
System.out.println("This is any subject");
}
}
您能否给我一个正确的例子来使用或测试这个新版本?
答案 0 :(得分:0)
从版本3.5.2开始,CoreNLP中的默认依赖关系表示是新的Universal Dependencies表示。作为此更改的一部分,语法关系现在在UniversalEnglishGrammaticalRelations
类中定义,因此您必须更改上一个if语句,如下所示:
for (SemanticGraphEdge e : semanticGraph.edgeIterable()) {
if (UniversalEnglishGrammaticalRelations.SUBJECT.isAncestor(e.getRelation())) {
System.out.println("This is any subject");
}
}