使用泡菜储存巨大的双字词典 [英] Saving huge bigram dictionary to file using pickle

查看:88
本文介绍了使用泡菜储存巨大的双字词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友写了这个小程序。
textFile 的大小是1.2GB(价值7年的报纸)。
他成功地设法创建字典,但他无法使用pickle(程序挂起)将其写入文件。

  import sys 
import string
import cPickle as pickle

biGramDict = {}

textFile = open(str(sys.argv [1])) 'r')
biGramDictFile = open(str(sys.argv [2]),'w')


textFile中的行:
if(line .find('< s>')!= - 1):
old = None
在textFile中的line2:
if(line2.find('< / s>') != - 1):
break
else:
line2 = line2.strip()
如果line2不在string.punctuation中:
如果old!= None:
如果old不在biGramDict中:
biGramDict [old] = {}
如果line2不在biGramDict [旧]中:
biGramDict [old] [line2] = 0
biGramDict [old] [line2] + = 1
old = line2

textF ile.close()

打印去腌...
pickle.dump(biGramDict,biGramDictFile,2)

打印pickle完成。现在加载...

biGramDictFile.close()
biGramDictFile = open(str(sys.argv [2]),'r')

newBiGramDict = pickle.load(biGramDictFile)

提前感谢



编辑

对于任何有兴趣的人,我将简要解释这个程序的作用。
假设你的文件格式大致如下:

 < s> 
您好

世界

< ; / s>
< s>
您好

munde

< / s>
< s>
世界
支配

< / s>
< s>
总计
世界
支配

< / s>




  • < s> 是句子分隔符。

  • 每行一个字。



一个biGramDictionary生成供以后使用。

这样的东西:

  { 
Hello:{World:1,munde:1},
Wo rld:{domination:2},
Total:{World:1},
}

希望这有帮助。现在的策略改为使用mysql,因为sqlite不工作(可能是因为大小)

解决方案

意思是写完整(小)对象。你的字典有点大,甚至连内存,你最好使用一个数据库,所以你可以一个一个地存储和检索条目,而不是一次。



您可以从Python使用的一些良好且易于集成的单一文件数据库格式是 SQLite 或其中一个 DBM变体。最后一个行为就像一个字典(即您可以读取和写入键/值对),但使用磁盘作为存储,而不是1.2 GB的内存。


a friend of mine wrote this little progam. the textFile is 1.2GB in size (7 years worth of newspapers). He successfully manages to create the dictionary but he cannot write it to a file using pickle(program hangs).

import sys
import string
import cPickle as pickle

biGramDict = {}

textFile = open(str(sys.argv[1]), 'r')
biGramDictFile = open(str(sys.argv[2]), 'w')


for line in textFile:
   if (line.find('<s>')!=-1):
      old = None
      for line2 in textFile:
         if (line2.find('</s>')!=-1):
            break
         else:
            line2=line2.strip()
            if line2 not in string.punctuation:
               if old != None:
                  if old not in biGramDict:
                     biGramDict[old] = {}
                  if line2 not in biGramDict[old]:
                     biGramDict[old][line2] = 0
                  biGramDict[old][line2]+=1
               old=line2

textFile.close()

print "going to pickle..."    
pickle.dump(biGramDict, biGramDictFile,2)

print "pickle done. now load it..."

biGramDictFile.close()
biGramDictFile = open(str(sys.argv[2]), 'r')

newBiGramDict = pickle.load(biGramDictFile)

thanks in advance.

EDIT
for anyone interested i will briefly explain what this program does. assuming you have a file formated roughly like this:

<s>
Hello
,
World
!
</s>
<s>
Hello
,
munde
!
</s>
<s>
World
domination
.
</s>
<s>
Total
World
domination
!
</s>

  • <s> are sentences separators.
  • one word per line.

a biGramDictionary is generated for later use.
something like this:

{
 "Hello": {"World": 1, "munde": 1}, 
 "World": {"domination": 2},
 "Total": {"World": 1},
}

hope this helps. right now the strategy changed to using mysql because sqlite just wasn't working (probably because of the size)

解决方案

Pickle is only meant to write complete (small) objects. Your dictionary is a bit large to even hold in memory, you'd better use a database instead so you can store and retrieve entries one by one instead of all at once.

Some good and easily integratable singe-file database formats you can use from Python are SQLite or one of the DBM variants. The last one acts just like a dictionary (i.e. you can read and write key/value-pairs) but uses the disk as storage rather than 1.2 GBs of memory.

这篇关于使用泡菜储存巨大的双字词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆