gensim.corpora中Dictionaryd的用法

编程入门 行业动态 更新时间:2024-10-27 04:34:04

<a href=https://www.elefans.com/category/jswz/34/1764072.html style=gensim.corpora中Dictionaryd的用法"/>

gensim.corpora中Dictionaryd的用法

gensim.corpora中Dictionaryd的用法

调用

from gensim.corpora import Dictionary
dictionary = Dictionary(corpus)
这里的corpus需要时二维列表形式,如
corpus = [['我', '在', '玉龙雪山', '我', '我', '我', '我'], ['我', '喜欢', '玉龙雪山'], ['我', '还要', '去', '玉龙雪山']

得到的dictionary事宜个包含很多信息的集合,若要对其中信息进行查看可如下操作:
输出dictionary

print(str(dictionary))
>>>"Dictionary(75 unique tokens: ['\\n', '23', '、', '厂', '台积']...)"

查看 字典,{单词id,在所有文档中共出现几次}

print(dictionary.dfs)    #字典,{单词id,在多少文档中出现}
>>>{1: 7, 0: 1, 2: 3, 3: 1, 5: 1, 4: 1}

查看 字典,{单词id,在多少文档中出现}

print(dictionary.cfs)
>>>{1: 3, 0: 1, 2: 3, 3: 1, 5: 1, 4: 1}

查看 文档数目:

dictionary.num_docs     #文档数目
>>> 3

查看 dictionary 中 {词的id:词}

print (dict(dictionary. items()))
或者 
print(dictionary.id2token)
>>> {0: '在', 1: '我', 2: '玉龙雪山', 3: '喜欢', 4: '去', 5: '还要'}

查看 {词:词的id}

print(dictionary.token2id)
>>> {'在': 0, '我': 1, '玉龙雪山': 2, '喜欢': 3, '去': 4, '还要': 5}

查看共有多少个词,多少个不重复的词

print(dictionary.num_pos) #查看所有词的个数print(dictionary.num_nnz) #查看所有词的个数

添加新的语句:

newlist=jieba.lcut('美国以应对芯片危机,美国出现投票信任危机')
result, missing = dictionary.doc2bow(newlist, allow_update=True, return_missing=True) # allow_update : 更新当前字典;return_missing : 返回字典中不存在的词,result为b文章转换得到的词袋,列表[(单词id,词频)]
result
>>> [(6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 2), (13, 1), (14, 1)]  #词袋 [(单词id,单词出现频率)]
missing
>>>[] #不在字典中的词及其词频,字典[(单词,词频)]
print(dict(dictionary.items()))  #查看加入后的{词:词id}
>>>{0: '在', 1: '我', 2: '玉龙雪山', 3: '喜欢', 4: '去', 5: '还要', 6: '以', 7: '信任危机', 8: '出现', 9: '危机', 10: '应对', 11: '投票', 12: '美国', 13: '芯片', 14: ','}for id, freq in result:print(id, dictionary.id2token[id], freq) #查看bow的内容
>>>
6 以 1
7 信任危机 1
8 出现 1
9 危机 1
10 应对 1
11 投票 1
12 美国 2
13 芯片 1
14 , 1

过滤词频

过滤文档频率大于 no_below,小于 no_above*num_docs的词
dictionary.filter_extremes(no_below=2,no_above=0.5,keep_n=10)
print(dictionary.dfs) #查看结果

文章参考:该博客

更多推荐

gensim.corpora中Dictionaryd的用法

本文发布于:2023-06-13 04:32:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/674165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:gensim   corpora   Dictionaryd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!