python解析复杂word_Python数据分析及可视化实例之词袋word2bow(28)

# coding: utf-8

# In[1]:

import logging

from gensim import corpora

import re

import jieba

from collections import defaultdict

from pprint import pprint # pretty-printer

logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.INFO)

# In[2]:

documents = ["Human machine interface for lab abc computer applications",

"A survey of user opinion of computer system response time",

"The EPS user interface management system",

"System and human system engineering testing of EPS",

"Relation of user perceived response time to error measurement",

"The generation of random binary unordered trees",

"The intersection graph of paths in trees",

"Graph minors IV Widths of trees and well quasi ordering",

"Graph minors A survey"]

# In[3]:

stoplist = set('for a of the and to in'.split()) # 删除几个简易的停用词,中文上结巴分词哈

texts = [[word for word in document.lower().split() if word not in stoplist]

for document in documents]

texts

# In[4]:

# 去掉只出现一次的单词

frequency = defaultdict(int)

for text in texts:

for token in text:

frequency[token] += 1

texts = [[token for token in text if frequency[token] > 1]

for text in texts]

texts

# In[5]:

dictionary = corpora.Dictionary(texts) # 将文档存入字典

# In[8]:

get_ipython().magic('pinfo2 dictionary')

# In[6]:

dictionary.token2id # 单次词频键值对,可以直接用来生成词云

# In[7]:

dictionary.dfs

# In[9]:

dictionary.filter_tokens()

# In[10]:

dictionary.compactify()

# In[12]:

dictionary.save('../../tmp/deerwester.dict')

# In[13]:

# 输出dictionary中个单词的出现频率

def PrintDictionary():

token2id = dictionary.token2id

dfs = dictionary.dfs

token_info = {}

for word in token2id:

token_info[word] = dict(

word = word,

id = token2id[word],

freq = dfs[token2id[word]]

)

token_items = token_info.values()

token_items = sorted(token_items, key = lambda x:x['id'])

print('The info of dictionary: ')

pprint(token_items)

# In[14]:

# 测试 ditonary的doc2bow功能,转化为one-hot presentation

new_doc = "Human computer interaction"

new_vec = dictionary.doc2bow(new_doc.lower().split())

print(new_vec) # interaction" 没有在字典中,所以忽略了

# In[15]:

# 将文本转化为 doc2bow 形式的数组

corpus = [dictionary.doc2bow(text) for text in texts]

# In[16]:

corpus # 词带中对应词出现的次数,如(2, 1)代表词带中的2号词出现了1次

# In[17]:

corpora.MmCorpus.serialize('../../tmp/deerwester.mm', corpus) # 保存至本地

# 除了MmCorpus以外,还有SvmLightCorpus等以各种格式存入磁盘

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值