使用斯坦福NLP提取名词短语
我试图用斯坦福NLP从一个句子中找到主题/名词短语 我很想得到“白虎”这句话 主题/名词短语为:白虎 为此,我使用pos标记器。我的示例代码如下 我得到的结果是“老虎”,这是不正确的。我以前运行的示例代码是使用斯坦福NLP提取名词短语,nlp,stanford-nlp,sentiment-analysis,pos-tagger,Nlp,Stanford Nlp,Sentiment Analysis,Pos Tagger,我试图用斯坦福NLP从一个句子中找到主题/名词短语 我很想得到“白虎”这句话 主题/名词短语为:白虎 为此,我使用pos标记器。我的示例代码如下 我得到的结果是“老虎”,这是不正确的。我以前运行的示例代码是 public static void main(String[] args) throws IOException { Properties props = new Properties(); props.setProperty("annotators", "
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);
}
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
Properties props=新属性();
props.setProperty(“注释器”、“标记化、ssplit、解析”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
注释=新注释(“白虎”);
管道注释(注释);
列出句子=注释
.get(CoreAnnotations.SentencesAnotation.class);
System.out.println(“传感器的大小是……”
+句子。大小();
for(CoreMap句子:句子){
System.out.println(“感觉是…”+句子.toString());
Tree-Tree=句子.get(TreeAnnotation.class);
PrintWriter out=新的PrintWriter(System.out);
println(“解析的第一句是:”);
树。打印(输出);
System.out.println(“它是否在这里……1111”);
TregexPattern模式=TregexPattern.compile(“@NP”);
TregexMatcher matcher=pattern.matcher(树);
while(matcher.find()){
Tree match=matcher.getMatch();
List leaves1=match.getChildrenAsList();
StringBuilder StringBuilder=新的StringBuilder();
对于(树木1:树叶1){
字符串val=tree1.label().value();
如果(val.equals(“NN”)| val.equals(“NNS”)
||val.equals(“NNP”)| val.equals(“NNPS”)){
树nn[]=tree1.children();
字符串ss=句子.listToString(nn[0].yield());
stringbuilder.append(ss.append)(“”);
}
}
System.out.println(“最终的stringbilder是…””
+stringbuilder);
}
}
}
非常感谢您提供的任何帮助。如果您有任何其他想法可以实现此目标。看起来您正在从依赖关系树向下搜索
NN.*
。
“白色”是一个JJ
——一个形容词——搜索NN.*
时不包括它
您应该仔细查看,并确定词性标签包含您要查找的内容。你还应该看看真实的语言数据,试图找出你要完成的任务中什么是重要的。那么:
the tiger [with the black one] [who was white]
在这种情况下,只需遍历树,就会得到老虎黑-白
。是否排除PP
?然后你会失去很多好信息:
the tiger [with white fur]
我不确定你想完成什么,但要确保你想做的事情受到了正确的限制
你也应该改进一下你的基本语法。“白虎”是语言学家称之为名词短语或NP
。语言学家很难把NP
称为句子。在一个句子中也经常有许多NP
s;有时,它们甚至彼此嵌入。斯坦福依赖手册是一个良好的开端。正如名字中所说,斯坦福大学的依赖关系是基于的理念,尽管有一些不同的观点
学习语言学家对句子结构的了解可以极大地帮助你理解你想要提取的东西,或者像经常发生的那样,认识到你想要提取的东西太难了,你需要找到新的解决办法