Java 从输出解析树中提取文本
我是nlp新手,我正在尝试使用斯坦福语法分析器从文本中提取(NP)句子,我想检索文本中标记(NP)的部分 如果一个零件被标记(NP),而里面的一个较小的零件也被标记(NP),我想取较小的零件 到目前为止,我通过以下方法实现了我的目标:Java 从输出解析树中提取文本,java,nlp,stanford-nlp,Java,Nlp,Stanford Nlp,我是nlp新手,我正在尝试使用斯坦福语法分析器从文本中提取(NP)句子,我想检索文本中标记(NP)的部分 如果一个零件被标记(NP),而里面的一个较小的零件也被标记(NP),我想取较小的零件 到目前为止,我通过以下方法实现了我的目标: private static ArrayList<Tree> extract(Tree t) { ArrayList<Tree> wanted = new ArrayList<Tree>(); if (t.lab
private static ArrayList<Tree> extract(Tree t)
{
ArrayList<Tree> wanted = new ArrayList<Tree>();
if (t.label().value().equals("NP") )
{
wanted.add(t);
for (Tree child : t.children())
{
ArrayList<Tree> temp = new ArrayList<Tree>();
temp=extract(child);
if(temp.size()>0)
{
int o=-1;
o=wanted.indexOf(t);
if(o!=-1)
wanted.remove(o);
}
wanted.addAll(temp);
}
}
else
for (Tree child : t.children())
wanted.addAll(extract(child));
return wanted;
}
我希望立即将输出设置为
“club barcelona”
,在没有标签的情况下,我尝试了.labels()代码>属性和.label().value()
它们返回标记,而不是您可以通过tr获得子树下的单词列表
tr.yield()
您可以使用语句中的方便方法将其转换为字符串形式:
Sentence.listToString(tr.yield())
你可以一边走一边走一棵树,但如果你想经常做这种事情,你可能想看看tregex,它可以通过声明性模式更容易地找到树中的特定节点,比如下面没有NP的NPs。做你想做的事情的一个好方法是:
Tree x = lp.apply("Christopher Manning owns club barcelona?");
TregexPattern NPpattern = TregexPattern.compile("@NP !<< @NP");
TregexMatcher matcher = NPpattern.matcher(x);
while (matcher.findNextMatchingNode()) {
Tree match = matcher.getMatch();
System.out.println(Sentence.listToString(match.yield()));
}
Tree x=lp.apply(“克里斯托弗·曼宁拥有巴塞罗那俱乐部?”);
TregexPattern NPpattern=TregexPattern.compile(“@NP!
Sentence.listToString(tr.yield())
Tree x = lp.apply("Christopher Manning owns club barcelona?");
TregexPattern NPpattern = TregexPattern.compile("@NP !<< @NP");
TregexMatcher matcher = NPpattern.matcher(x);
while (matcher.findNextMatchingNode()) {
Tree match = matcher.getMatch();
System.out.println(Sentence.listToString(match.yield()));
}