如何处理对ram来说太大的字典(马尔可夫链)?

2024-06-09 10:33:19 发布

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

我正在用python训练一个20 GB+的大型语料库,开发一个Markov链文本生成器。它接受纯文本并将其分成两对,当前选定的两个单词和后面的单词。这是列表的一部分:

[(('as', 'a'), 'subject'), (('a', 'subject'), 'for'), (('subject', 'for'), 'the'), (('for', 'the'), 'remarks'), (('the', 'remarks'), 'of'), (('remarks', 'of'), 'the'), (('of', 'the'), 'evening'), (('the', 'evening'), 'the'), (('evening', 'the'), 'perpetuation'), (('the', 'perpetuation'), 'of'), (('perpetuation', 'of'), 'our'), (('of', 'our'), 'political')]

然后我将其放入字典中,这样如果有一个关键字(两个单词)出现多次,我就可以将该值作为具有多个值的数组:

{('as', 'a'): ['subject', 'nation'], ('a', 'subject'): ['for'], ('subject', 'for'): ['the'], ('for', 'the'): ['remarks', 'executive', 'government', 'laws', 'redress', 'sake', 'constitution'], ('the', 'remarks'): ['of']}

我再次对添加到字典中的数千个文件执行此操作,过了一段时间,它对于ram来说太大了。有没有办法把这样的字典存储在磁盘上? 数据结构:

  • 得快点
  • 一经制作即可编辑
  • 易于搜索

我认为我最好的想法是一个数据库或csv文件,但我读到他们是不切实际的


Tags: 文件ofthe文本for字典asour