Stanford nlp 如何检查英语语法关系?

Stanford nlp 如何检查英语语法关系?,stanford-nlp,Stanford Nlp,我想将实际使用Stanford Parser 2.0.4的Java代码更新为新版本(3.6) 问题是“EnglishGrammaticRelations.SUBJECT.isAncestor”函数在3.6中不起作用,我需要检查一个关系是否等于另一个关系或是另一个关系的祖先。我的斯坦福2.0.4代码如下: String sentence = "The company is a subsidiary of International Data Group"; Tree depTree;

我想将实际使用Stanford Parser 2.0.4的Java代码更新为新版本(3.6)

问题是“EnglishGrammaticRelations.SUBJECT.isAncestor”函数在3.6中不起作用,我需要检查一个关系是否等于另一个关系或是另一个关系的祖先。我的斯坦福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");
        }

    }
String语句=“本公司是国际数据集团的子公司”;
树木;
语义图语义图;
词汇化语法分析器;
TokenizerFactory TokenizerFactory;
词汇化ParserQuery lpq;
lp=词汇化语法分析器
.loadModel(“edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz”);
tokenizerFactory=PTBTokenizer.factory(新的CoreLabelTokenFactory(),“”);
lpq=lp.parserQuery();
List TokenizedSession=tokenizerFactory.getTokenizer(
新的StringReader(句子)).tokenize();
解析(标记化句子);
depTree=lpq.getBestParse()//getBestPCFGParse();
//使用未聚合的依赖项来促进树的创建
semanticGraph=SemanticGraphFactory.makeFromTree(depTree,false);
System.out.println(“紧凑图”+semanticGraph.toCompactString());
for(semanticGraph边缘:semanticGraph.edgeitable()){
if(englishgrammaticrelations.SUBJECT.isAncestor(e.getrelations()){
System.out.println(“这是任何主题”);
}
}
使用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");
        }

    }
String语句=“本公司是国际数据集团的子公司”;
树木;
语义图语义图;
词汇化语法分析器;
TokenizerFactory TokenizerFactory;
词汇化ParserQuery lpq;
lp=词汇化语法分析器
.loadModel(“edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz”);
tokenizerFactory=PTBTokenizer.factory(新的CoreLabelTokenFactory(),“”);
lpq=lp.lexicalizedParserQuery();
List TokenizedSession=tokenizerFactory.getTokenizer(
新的StringReader(句子)).tokenize();
解析(标记化句子);
depTree=lpq.getBestPCFGParse()//getBestParse();
//使用未聚合的依赖项来促进树的创建
semanticGraph=SemanticGraphFactory.makeFromTree(depTree,false);
System.out.println(“紧凑图”+semanticGraph.toCompactString());
for(semanticGraph边缘:semanticGraph.edgeitable()){
if(englishgrammaticrelations.SUBJECT.isAncestor(e.getrelations()){
System.out.println(“这是任何主题”);
}
}

您能给我一个正确的例子来使用或测试这个新版本吗?

由于版本3.5.2,CoreNLP中的默认依赖关系表示就是新的表示形式。作为此更改的一部分,语法关系现在在
通用英语语法关系
类中定义,因此您必须更改最后一个if语句,如下所示:

for (SemanticGraphEdge e : semanticGraph.edgeIterable()) {
  if (UniversalEnglishGrammaticalRelations.SUBJECT.isAncestor(e.getRelation())) {
    System.out.println("This is any subject");
  }
}

自版本3.5.2以来,CoreNLP中的默认依赖关系表示形式是新的表示形式。作为此更改的一部分,语法关系现在在
通用英语语法关系
类中定义,因此您必须更改最后一个if语句,如下所示:

for (SemanticGraphEdge e : semanticGraph.edgeIterable()) {
  if (UniversalEnglishGrammaticalRelations.SUBJECT.isAncestor(e.getRelation())) {
    System.out.println("This is any subject");
  }
}

非常感谢塞巴斯蒂安,他工作得很好。但是,在我的代码中有一些不推荐或已消失的函数(例如,EnglishGrammaticRelations.PREPOSITIONAL_Complete),我需要更改或找到类似的操作。是的,在UD中对介词短语的处理已更改,因此不再存在
pcomp
关系。看看这些解释了斯坦福依赖项和通用依赖项之间的区别。谢谢,我会看一看指南。非常感谢你,塞巴斯蒂安,工作很好。但是,在我的代码中有一些不推荐或已消失的函数(例如,EnglishGrammaticRelations.PREPOSITIONAL_Complete),我需要更改或找到类似的操作。是的,在UD中对介词短语的处理已更改,因此不再存在
pcomp
关系。看看这些解释了斯坦福依赖项和通用依赖项之间的区别。谢谢,我将看看指导原则