使用stanford NLP提取名词短语



我正试图使用Stanford NLP 从一个句子中找到主题/名词短语

例如:句子"白虎"我很想得到

主题/名词短语为:白虎。

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

我得到的结果是"老虎",这是不正确的。我过去运行的示例代码是

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);
}
}
}

非常感谢您的帮助。实现这一目标的任何其他想法。

看起来您正在从依赖树中向下查找NN.*。"白色"是一个JJ——一个形容词——在搜索NN.*时不会包含它。

你应该仔细阅读《斯坦福依赖性手册》,并决定哪些词性标签包含了你想要的内容。你还应该查看真实的语言数据,试图弄清楚你试图完成的任务中什么是重要的。关于:

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

在这种情况下,只需遍历树就会得到tiger black white。是否排除PP的?然后你会失去很多好的信息:

the tiger [with white fur]

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

你也应该完善你的基本语法。"白虎"是语言学家所说的名词短语或NP。语言学家很难将NP称为一个句子。一个句子中也经常有许多NP;有时,它们甚至相互嵌入。《斯坦福依赖性手册》是一个良好的开端。正如名字中所说,斯坦福依赖关系是基于依赖语法的思想,尽管还有其他方法可以带来不同的见解。

学习语言学家对句子结构的了解可以大大帮助你理解你试图提取的内容,或者——正如经常发生的那样——意识到你试图提取内容太难了,你需要找到一条新的解决方案。

最新更新