使用stanford NLP提取名词短语

时间:2015-04-13 21:48:13

标签: nlp stanford-nlp sentiment-analysis pos-tagger

我试图使用斯坦福NLP从句子中找到主题/名词短语

例如:我希望得到的句子“白老虎”

主题/ Nound短语为:白虎。

为此,我使用了pos tagger。我的示例代码如下。

我得到的结果是“老虎”,这是不正确的。我以前运行的示例代码是

public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize,ssplit,parse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        Annotation annotation = new Annotation("the white tiger)");
        pipeline.annotate(annotation);
        List<CoreMap> sentences = annotation
                .get(CoreAnnotations.SentencesAnnotation.class);
        System.out.println("the size of the senetence is......"
                + sentences.size());
        for (CoreMap sentence : sentences) {
            System.out.println("the senetence is..." + sentence.toString());
            Tree tree = sentence.get(TreeAnnotation.class);
            PrintWriter out = new PrintWriter(System.out);
            out.println("The first sentence parsed is:");
            tree.pennPrint(out);
            System.out.println("does it comes here.....1111");
            TregexPattern pattern = TregexPattern.compile("@NP");
            TregexMatcher matcher = pattern.matcher(tree);
            while (matcher.find()) {
                Tree match = matcher.getMatch();
                List<Tree> leaves1 = match.getChildrenAsList();
                StringBuilder stringbuilder = new StringBuilder();
                for (Tree tree1 : leaves1) {
                    String val = tree1.label().value();
                    if (val.equals("NN") || val.equals("NNS")
                            || val.equals("NNP") || val.equals("NNPS")) {
                        Tree nn[] = tree1.children();
                        String ss = Sentence.listToString(nn[0].yield());
                        stringbuilder.append(ss).append(" ");

                    }
                }
                System.out.println("the final stringbilder is ...."
                        + stringbuilder);
            }

        }

    }

任何帮助都非常感谢。还有其他想法可以实现这一目标。

1 个答案:

答案 0 :(得分:1)

看起来你正在寻找NN.*的依赖树。 “white”是一个JJ - 一个形容词 - 不会包含搜索NN.*

您应该仔细查看Stanford Dependencies Manual并确定哪些词性标记包含您要查找的内容。您还应该查看真实的语言数据,以试图弄清楚您要完成的任务中的重要事项。怎么样:

the tiger [with the black one] [who was white]

在这种情况下,简单地遍历树将为您提供tiger black white。排除PP的?然后你失去了很多好消息:

the tiger [with white fur]

我不确定你想要完成什么,但要确保你正在尝试做的事情以正确的方式受到限制。

你也应该改进你的基本语法。 “白老虎”是语言学家称之为名词短语或NP。你很难让语言学家给NP一个句子打个电话。句子中通常还有很多NP个;有时候,它们甚至会彼此嵌入。斯坦福依赖手册是一个良好的开端。正如在名称中一样,斯坦福依赖关系基于dependency grammar的概念,尽管有other approaches可以为表格带来不同的见解。

了解语言学家对句子结构的了解可以帮助你明显地获得你想要提取的东西,或者 - 经常发生 - 意识到你想要提取的东西太难以及你需要找到解决方案的新途径。

相关问题