Skip to main content
 首页 » 编程设计

python之如何从 NLTK 树中获取标记

2025年05月04日16over140

所以我把这棵树还给我了

Tree('S', [('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('test', 'NN'), (',', ','), Tree('PERSON', [('Stackoverflow', 'NNP'), ('Users', 'NNP')]), ('.', '.')]) 

我可以像这样把它变成一个漂亮的 python 列表

sentence = "This is a test, Stackoverflow Users." 
tokens = nltk.word_tokenize(sentence) 
tagged = nltk.pos_tag(tokens) 
entities = nltk.chunk.ne_chunk(tagged) 
tree = repr(entities) # THIS VARIABLE IS THE TREE THAT IS RETURNED TO ME 
# below this point it's about turning the tree into a python list 
tree = (("[" + tree[5:-1] + "]")).replace("Tree", "").replace(")", "]").replace("(", "[") 
tree = ast.literal_eval(tree) #you'll need to import ast (included with python) 

现在,树变量是这样的:

['S', [['This', 'DT'], ['is', 'VBZ'], ['a', 'DT'], ['test', 'NN'], [',', ','], ['ORGANIZATION', [['Stackoverflow', 'NNP']]], ['users', 'NNS'], ['.', '.']]] 

当我尝试遍历并获取句子的字符串时,我得到

"This is a test, ORGANIZATION." 

而不是想要的

"This is a test, Stackoverflow users." 

我不能简单地使用句子变量,我需要能够从这个列表列表中取回句子。任何代码片段或建议将不胜感激。

请您参考如下方法:

>>> from nltk import Tree 
>>> yourtree = Tree('S', [('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('test', 'NN'), (',', ','), Tree('PERSON', [('Stackoverflow', 'NNP'), ('Users', 'NNP')]), ('.', '.')]) 
>>> yourtree.leaves() 
[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('test', 'NN'), (',', ','), ('Stackoverflow', 'NNP'), ('Users', 'NNP'), ('.', '.')] 
>>> tokens, pos = zip(*yourtree.leaves()) 
>>> tokens 
('This', 'is', 'a', 'test', ',', 'Stackoverflow', 'Users', '.') 
>>> pos 
('DT', 'VBZ', 'DT', 'NN', ',', 'NNP', 'NNP', '.') 

另请参阅:How to Traverse an NLTK Tree object?