首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从斯坦福语法分析器的输出中提取名词及其对应形容词

从斯坦福语法分析器的输出中提取名词及其对应形容词
EN

Stack Overflow用户
提问于 2013-01-18 05:24:29
回答 1查看 2.5K关注 0票数 2

我正在使用Stanford NLP解析器获得以下输出。现在,我如何从输出中提取一个名词及其相应的形容词或任何与特定名词相关的所需信息。我希望所有的名词都应该和它们的形容词一起按顺序提取,这样我才能知道哪个形容词与文本中的哪个名词有关。

例如:

我需要从下面的输出中提取名词"Santosh“及其相应的形容词”帅气“。

代码语言:javascript
复制
nn(Santosh-2, Kosgi-1)
nsubj(handsome-4, Santosh-2)
cop(handsome-4, is-3)
root(ROOT-0, handsome-4)
aux(sent-6, has-5)
rcmod(handsome-4, sent-6)
det(email-8, an-7)
dobj(sent-6, email-8)
nn(University-11, Stanford-10)
prep_to(sent-6, University-11 
EN

回答 1

Stack Overflow用户

发布于 2013-01-25 01:32:13

我刚刚开始和斯坦福解析器打交道,所以就拿这个做点小动作吧。

我要对提取一个名词及其相应的形容词或与该特定名词有关的任何所需信息:

生成句子的解析树。(看看ParserDemo.java如何做到这一点)。

https://wiki.csc.calpoly.edu/CSC-581-S11-06/browser/trunk/Stanford/stanford-parser-2011-04-20/src/edu/stanford/nlp/parser/lexparser/demo/ParserDemo.java?rev=2

解析树将如下所示:

代码语言:javascript
复制
  (ROOT
    (S
     (NP (JJ handsome) (NNP Joe) (NNP Blow))
     (VP (VBD sent)
      (NP (DT an) (NN email))
      (PP (TO to)
        (NP (PRP$ his) (JJ congressional) (NN representative))))))

就像这样的一句话:英俊的乔?布劳给他的国会代表发了一封电子邮件。

然后编写一些代码,递归地在解析树中下降,并选择'NP‘片段。

例如,其中一个片段是(NP (JJ英俊) (NNP Joe) (NNP NNP))

一旦你有了这个片段,你可以收集所有的形容词和任何其他你感兴趣的修饰语。了解代码[ http://bulba.sdsu.edu/jeanette/thesis/PennTags.html ]的含义是有帮助的。

我写了一些代码爬过一个解析树然后提取一些东西..。这可能会帮助你开始工作>

不能给你所有的密码,但这里有一些.

代码语言:javascript
复制
static {
    nounNodeNames = new ArrayList<String>();

    nounNodeNames.add( "NP");
    nounNodeNames.add( "NPS");
    nounNodeNames.add( "FW");
    nounNodeNames.add( "NN");
    nounNodeNames.add( "NNS");
    nounNodeNames.add( "NNP");
    nounNodeNames.add( "NNPS");
}


public  List<NounPhrase> extractPhrasesFromString(Tree tree, String originalString) {
    List<NounPhrase> foundPhraseNodes = new ArrayList<NounPhrase>();

    collect(tree, foundPhraseNodes);
    logger.debug("parsing " + originalString + " yields " + foundPhraseNodes.size() + " noun node(s).");
    if (foundPhraseNodes.size() == 0) {
        foundPhraseNodes.add(new NounPhrase(tree, originalString));
    }
    return  foundPhraseNodes;
}

private void collect(Tree tree, List<NounPhrase> foundPhraseNodes) {
    if (tree == null || tree.isLeaf()) {
        return;
    }


    Label label = tree.label();
    if (label instanceof CoreLabel) {
        CoreLabel coreLabel = ((CoreLabel) label);

        String text = ((CoreLabel) label).getString(CoreAnnotations.OriginalTextAnnotation.class);
        logger.debug(" got text: " + text);
        if (text.equals("THE")) {
            logger.debug(" got THE text: " + text);
        }

        String category = coreLabel.getString(CoreAnnotations.CategoryAnnotation.class);
        if (nounNodeNames.contains(category)) {
            NounPhrase phrase = null;
            String phraseString = flatten(tree);
            if ((phrase = stringToNounPhrase.get(phraseString)) == null) {
                phrase = new NounPhrase(tree, phraseString);
                stringToNounPhrase.put(phraseString, phrase);
            }

            if (! foundPhraseNodes.contains(phrase)) {
                logger.debug("adding found noun phrase to list: {}", phrase.debug());
                foundPhraseNodes.add(phrase);
            } else {
                logger.debug("on list already, so skipping found noun phrase: {}", phrase.debug());
            }
        }
    }


    List<Tree> kids = tree.getChildrenAsList();
    for (Tree kid : kids) {
        collect(kid, foundPhraseNodes);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14392882

复制
相关文章

相似问题

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