python nltk中的函数“bigrams”不工作

2024-04-25 14:55:54 发布

您现在位置:Python中文网/ 问答频道 /正文

nltk中的函数bigrams返回以下消息

即使nltk是导入的,并且它的其他函数也在工作。有什么想法吗?谢谢。

>>> import nltk
>>> nltk.download()
showing info http://www.nltk.org/nltk_data/
True
>>> from nltk import bigrams
>>> bigrams(['more', 'is', 'said', 'than', 'done'])
<generator object bigrams at 0x0000000002E64240>

Tags: 函数fromorgimportinfotruehttp消息
2条回答

函数bigrams返回了一个“generator”对象;这是一个Python数据类型,它类似于一个列表,但只在需要时创建其元素。如果要将生成器实现为列表,则需要将其显式转换为列表:

>>> list(bigrams(['more', 'is', 'said', 'than', 'done']))
[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]
<generator object bigrams at 0x0000000002E64240>

当此指令出现时,表示已创建大图并准备好显示。现在,如果你想让它们显示,只需把你的指令放在:

list(bigrams(['more', 'is', 'said', 'than', 'done']))

这意味着您需要以列表的形式输出bigrams,您将得到:

[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]

相关问题 更多 >

    热门问题