我正在使用Stanford NLP解析器获得以下输出。现在,我如何从输出中提取一个名词及其相应的形容词或任何与特定名词相关的所需信息。我希望所有的名词都应该和它们的形容词一起按顺序提取,这样我才能知道哪个形容词与文本中的哪个名词有关。
例如:
我需要从下面的输出中提取名词"Santosh“及其相应的形容词”帅气“。
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 发布于 2013-01-25 01:32:13
我刚刚开始和斯坦福解析器打交道,所以就拿这个做点小动作吧。
我要对提取一个名词及其相应的形容词或与该特定名词有关的任何所需信息:
生成句子的解析树。(看看ParserDemo.java如何做到这一点)。
解析树将如下所示:
(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 ]的含义是有帮助的。
我写了一些代码爬过一个解析树然后提取一些东西..。这可能会帮助你开始工作>
不能给你所有的密码,但这里有一些.
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);
}
}https://stackoverflow.com/questions/14392882
复制相似问题