首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何找到使用斯坦福分析器或斯坦福CoreNLP的名词短语的语法关系?

如何找到使用斯坦福分析器或斯坦福CoreNLP的名词短语的语法关系?
EN

Stack Overflow用户
提问于 2015-04-17 12:42:01
回答 2查看 2.3K关注 0票数 3

我正在使用斯坦福大学CoreNLP试图找出名词短语的语法关系。

下面是一个示例:

有句“健身室脏了”。

我设法把“健身室”作为我的目标名词短语。我现在正在寻找一种方法来发现“脏”这个形容词与“健身室”有关系,而不仅仅是“房间”。

示例代码:

代码语言:javascript
运行
复制
private static void doSentenceTest(){
    Properties props = new Properties();
    props.put("annotators","tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    StanfordCoreNLP stanford = new StanfordCoreNLP(props);

    TregexPattern npPattern = TregexPattern.compile("@NP");

    String text = "The fitness room was dirty.";


    // create an empty Annotation just with the given text
    Annotation document = new Annotation(text);
    // run all Annotators on this text
    stanford.annotate(document);

    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {

        Tree sentenceTree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
        TregexMatcher matcher = npPattern.matcher(sentenceTree);

        while (matcher.find()) {
            //this tree should contain "The fitness room" 
            Tree nounPhraseTree = matcher.getMatch();
            //Question : how do I find that "dirty" has a relationship to the nounPhraseTree


        }

        // Output dependency tree
        TreebankLanguagePack tlp = new PennTreebankLanguagePack();
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        GrammaticalStructure gs = gsf.newGrammaticalStructure(sentenceTree);
        Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();

        System.out.println("typedDependencies: "+tdl); 

    }

}

我在句子中使用了斯坦福CoreNLP,提取了它的根树对象。在这个树对象上,我设法使用TregexPattern和TregexMatcher提取名词短语。这给了我一个包含实际名词短语的子树。我想知道的是在原来的句子中找到名词短语的修饰语。

typedDependecies ouptut为我提供了以下内容:

代码语言:javascript
运行
复制
typedDependencies: [det(room-3, The-1), nn(room-3, fitness-2), nsubj(dirty-5, room-3), cop(dirty-5, was-4), root(ROOT-0, dirty-5)]

在这里我可以看到nsubj(脏-5,房间-3),但我没有完整的名词短语作为主语。

我希望我说得够清楚。任何帮助都很感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-17 14:37:31

类型化依赖项do显示形容词“脏”适用于“健身室”:

代码语言:javascript
运行
复制
det(room-3, The-1)
nn(room-3, fitness-2)
nsubj(dirty-5, room-3)
cop(dirty-5, was-4)
root(ROOT-0, dirty-5)

“nn”标签是名词复合修饰语,表示“适合”是“”的修饰语。

您可以在斯坦福依赖手册中找到有关依赖项标记的详细信息。

票数 5
EN

Stack Overflow用户

发布于 2015-06-18 12:52:26

修改方法

代码语言:javascript
运行
复制
Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed(); with
Collection<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
or
Collection<TypedDependency> tdl = gs.allDependencies(); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29699550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档