嗨,我在NLTK 3中尝试这段代码:-不知何故,我设法修复了行-6来使用NLTK的第3版。但是for循环仍然没有返回任何内容。
import nltk
sample = """ some random text content with names and countries etc"""
sentences = nltk.sent_tokenize(sample)
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
chunked_sentences=nltk.chunk.ne_chunk_sents(tagged_sentences) #Managed to fix this to work with version_3
for i in chunked_sentences:
if hasattr(i,'label'):
if i.label()=='NE':
print i此外,如果我尝试调试,我会看到以下输出:
for i in chunked_sentences:
if hasattr(i,'label') and i.label:
print i.label
S
S
S
S
S
S
S
S那我怎么查"NE“呢?NLTK-3有什么问题,我真的无法理解out.Pls的帮助
发布于 2014-11-30 10:41:59
看来你在重复句子。我假设您想要迭代句子中包含的各个节点。
它应该是这样的:
for sentence in chunked_sentences:
for token in sentence:
if hasattr(token,'label') and token.label() == 'NE':
print token编辑:为了将来的参考,让我意识到你在重复句子这个事实的是,一个句子的根节点通常被标记为'S‘。
https://stackoverflow.com/questions/27212050
复制相似问题