簡體   English   中英

python nltk中的功能“ bigrams”不起作用

[英]The function 'bigrams' in python nltk not working

來自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>

bigrams函數返回了一個“ generator”對象。 這是一種Python數據類型,類似於List,但僅在需要時創建其元素。 如果要將生成器實現為列表,則需要將其顯式轉換為列表:

>>> 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']))

這意味着您需要以列表形式輸出雙字母組,您將獲得:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM