Gensim字典实现

6
我对gensim字典实现感到好奇。我有以下代码:
    def build_dictionary(documents):
        dictionary = corpora.Dictionary(documents)
        dictionary.save('/tmp/deerwester.dict') # store the dictionary
        return dictionary    

我查看了文件deerwester.dict,它看起来像这样:

8002 6367 656e 7369 6d2e 636f 7270 6f72
612e 6469 6374 696f 6e61 7279 0a44 6963
7469 6f6e 6172 790a 7101 2981 7102 7d71
0328 5508 6e75 6d5f 646f 6373 7104 4b09
5508 ...

然而,以下代码

my_dict = dictionary.load('/tmp/deerwester.dict') 
print my_dict.token2id #view dictionary

产生这个结果:

{'minors': 30, 'generation': 22, 'testing': 16, 'iv': 29, 'engineering': 15, 'computer': 2, 'relation': 20, 'human': 3, 'measurement': 18, 'unordered': 25, 'binary': 21, 'abc': 0, 'ordering': 31, 'graph': 26, 'system': 10, 'machine': 6, 'quasi': 32, 'random': 23, 'paths': 28, 'error': 17, 'trees': 24, 'lab': 5, 'applications': 1, 'management': 14, 'user': 12, 'interface': 4, 'intersection': 27, 'response': 8, 'perceived': 19, 'widths': 34, 'well': 33, 'eps': 13, 'survey': 9, 'time': 11, 'opinion': 7}

所以我的问题是,由于我在.dict文件中看不到实际的单词,那里存储的所有十六进制值是什么?这是一种超级压缩格式吗?我很好奇,因为如果是的话,我应该考虑从现在开始使用它。

1个回答

11

给定这个例子:

>>> from gensim import corpora
>>> docs = ["this is a foo bar", "you are a foo"]
>>> texts = [[i for i in doc.lower().split()] for doc in docs]
>>> print texts
[['this', 'is', 'a', 'foo', 'bar'], ['you', 'are', 'a', 'foo']]

>>> dictionary = corpora.Dictionary(texts)
>>> dictionary.save('foobar.txtdic')
如果您使用gensim.corpora.dictionary.save_as_text() (请参见https://github.com/piskvorky/gensim/blob/develop/gensim/corpora/dictionary.py),则应该得到以下文本文件:
0   a   2
5   are 1
1   bar 1
2   foo 2
3   is  1
4   this    1
6   you 1

如果您使用默认的 gensim.corpora.dictionary.save(),它会保存到一个被pickle序列化的二进制文件中。请参见https://github.com/piskvorky/gensim/blob/develop/gensim/utils.py中的 class SaveLoad(object)

有关pickle的信息,请参见http://docs.python.org/2/library/pickle.html#pickle-example


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接