栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python商品名称文本分析-jieba

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python商品名称文本分析-jieba

一、jieba库

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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1001191.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号