【问题标题】:Why adding documents to gensim Dictionary gets slow when reaching 2 million words?为什么在达到 200 万字时将文档添加到 gensim Dictionary 会变慢?
【发布时间】:2018-02-09 11:47:28
【问题描述】:

我注意到,将文档添加到 gensim Dictionary 时,执行时间从 0.2 秒跃升至 6 秒以上,达到 200 万字。

下面的代码是一个简单的例子。我遍历 int 并在每次迭代时将数字添加到字典中。

from gensim import corpora
import time



dict_transcript = corpora.Dictionary()


for i in range(1,10000000):

    start_time = time.time()

    doc = [str(i)]

    dict_transcript.add_documents([doc])

    print("Iter "+str(i)+" done in " + str(time.time() - start_time) + ' w/ '+str(len(doc)) + ' words and dico size ' +
          str(len(dict_transcript)))

当达到 200 万字时,我确实得到了以下输出:

Iter 1999999 done in 0.0 w/ 1 words and dico size 1999999
Iter 2000000 done in 0.0 w/ 1 words and dico size 2000000
Iter 2000001 done in 0.0 w/ 1 words and dico size 2000001
Iter 2000002 done in 7.940511226654053 w/ 1 words and dico size 2000001

有什么原因吗?有谁知道如何绕过这个问题? 我在一个大语料库上使用这本字典,我将它标记为二元组,所以我希望字典有几百万行。

非常感谢

【问题讨论】:

    标签: python dictionary nlp gensim


    【解决方案1】:

    看看gensim documentation

    gensim.corpora.dictionary.Dictionary(documents=None, prune_at=2000000)

    prune_at(int,可选)- 唯一词的总数。字典将保留不超过 prune_at 的单词。

    prune_at=None 或设置为适合您用例的整数。

    【讨论】:

    • prune_at 参数是如何工作的?我的意思是,它如何影响运行时间?我自己一直在尝试不同的 prune_at 值,它不会影响字典的大小。
    猜你喜欢
    • 2018-01-23
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多