1、导入库
import jieba
import jieba.posseg as psg
2、筛选特定特征的数据
data.loc[data['new']=='同款',['商品名称']] #定位某一个条件
str(data1['商品名称'][0]) #在规定范围内找出符合条件的数据
3、单词分词
data1=pd.DataFrame(data.loc[data['new']=='同款',['商品名称']])
xs=psg.lcut(str(data1['商品名称'][2]))
for x in xs:
print(x.word) #x.flag为词性,word为名词=“关键词”
将分词存储为关键词,在写入数据表中,进行后续的关键词统计
4、添加jieba库的新词
1)添加单个新词:jieba.add_word()
2)批量添加新词:
txt=“路径/txt的名称.txt”
jieba.load_userdict(txt)
二、zhon对文本内容有要求,不好用
Zhon — Zhon 1.1.5 documentation
三、词云1、统计次数
基于上述的“商品名称”,分析出“关键词”,确定好关键词的权重,如出现次数作为权重
import pandas
words = cut_word() #分词数据或另读取
df = pandas.DataFrame({'关键词':words})
words_count = df.groupby(by=['关键词'])['关键词'].agg({"出现次数":numpy.size})
words_count = words_count.reset_index().sort_values(by="出现次数",ascending=False)
print(words_count)
2、查看使用频率最高的前10个词:
print(words_count.head(10))
3、词云
1)导入库
import numpy as np
from scipy.misc import imread
from matplotlib import pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
2)导入图片
导入图片并读取,获取词云对象,同时设置词云背景颜色及字体格式
bimg = imread('图片.jpg')
wordcloud = WordCloud(background_color='white', mask=bimg, font_path='simhei.ttf')
3)映射与生成词云
# 将词语和频率转为字典,并映射到词云对象上
words = words_count.set_index("关键词").to_dict()
wordcloud = wordcloud.fit_words(words["出现次数"])
#生成词云图片并进行输出
bimgColors = ImageColorGenerator(bimg)
plt.axis("off")
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.show()



