我已导入NLTK库,我正在尝试生成一个解析树,该树在文档中生成一组命名实体。这是代码示例:
train_text = state_union.raw("2005-GWBush.txt")
sample_text = state_union.raw("2006-GWBush.txt")
custom_sent_tokenizer = PunktSentenceTokenizer(train_text)
tokenized = custom_sent_tokenizer.tokenize(sample_text)
#print tokenized
def process_content():
try:
for i in tokenized:
words = nltk.word_tokenize(i)
tagged = nltk.pos_tag(words)
namedEnt = nltk.ne_chunk(tagged)
namedEnt.draw()
except Exception as e:
print (str(e))
process_content()
当我运行此代码时," namedEnt.draw()"方法不生成输出。没有打开的窗口显示已命名实体的已解析文档树。谁能告诉我哪里出错了? 另外,当我打印(namedEnt)时,它给了我正确的输出。它打印命名实体的块。