作为文本分析,其中常用库jieba、wordcloud等用法演示,以作记录~~
import os
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import jieba
import jieba.analyse
import wordcloud
from PIL import Image
# 显示中文字体
font_path = '/Library/Fonts/Songti.ttc'
# 设置显示中文所需字体
plt.rcParams['font.family'] = 'Songti SC'
# 设置负号的正常显示
plt.rcParams['axes.unicode_minus'] = False
%matplotlib inline
# 设置清晰度
%config InlineBackend.figure_format = 'retina'
# 采集的某品牌商品的最新100条评论
comment_pd = pd.read_excel('.././datas/JD-Comments.xlsx')
comment_pd.info()
# 转换成文本内容
comment_content = ' '.join(comment_pd.comment.to_list())
jieba, “结巴”中文分词,最好的Python中文分词组件
# 设置停用词库
jieba.analyse.set_stop_words('.././datas/stop_words.txt')
# 基于TextRanks算法关键词提取
comment_words = jieba.analyse.textrank(comment_content, topK=300, withWeight=True)
# 基于TF-IDF算法的关键词提取
comment_words2 = jieba.analyse.extract_tags(comment_content, topK=300, withWeight=True)
# 仅使用关键词,忽略词频
word_list = []
for item in comment_words:
word_list.append(item[0])
words = ' '.join(word_list)
# 列表数据转换为Dict
frequency_words = {}
for item in comment_words2:
frequency_words[item[0]] = item[1]
# 默认参数,最简单例子
wc = wordcloud.WordCloud(font_path=font_path)
wc.generate(words)
# 绘图展示
plt.figure(figsize=(12, 9))
plt.imshow(wc, interpolation='bilinear')
# 关闭XY轴
plt.axis('off')
plt.show()
# 默认参数,最简单例子
wc = wordcloud.WordCloud(font_path=font_path, max_font_size=50)
wc.generate(words)
# 绘图展示
plt.figure(figsize=(12, 9))
plt.imshow(wc, interpolation='bilinear')
# 关闭XY轴
plt.axis('off')
plt.show()
# 选择一个图片
mask = np.array(Image.open('.././datas/china-map.jpeg'))
wc = wordcloud.WordCloud(font_path=font_path, background_color='white', max_font_size=50, mask=mask,
contour_width=1, contour_color='grey')
wc.generate(words)
# 绘图展示
plt.figure(figsize=(12, 9))
plt.imshow(wc, interpolation='bilinear')
# 关闭XY轴
plt.axis('off')
plt.show()
wc = wordcloud.WordCloud(font_path=font_path, background_color='white', max_font_size=100, mask=mask,
contour_width=1, contour_color='grey')
wc.generate_from_frequencies(frequency_words)
# 绘图展示
plt.figure(figsize=(12, 9))
plt.imshow(wc, interpolation='bilinear')
# 关闭XY轴
plt.axis('off')
plt.show()
wc = wordcloud.WordCloud(font_path=font_path, background_color='white', max_font_size=100, mask=mask,
contour_width=1, contour_color='grey')
wc.generate_from_frequencies(frequency_words)
# 从图像创建颜色
image_colors = wordcloud.ImageColorGenerator(mask)
# 绘图展示
plt.figure(figsize=(12, 9))
plt.imshow(wc.recolor(color_func=image_colors), interpolation='bilinear')
# 关闭XY轴
plt.axis('off')
plt.show()
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)
wc = wordcloud.WordCloud(font_path=font_path, background_color='white', max_words=100, max_font_size=100,
mask=mask, contour_width=1, contour_color='grey')
wc.generate_from_frequencies(frequency_words)
# 默认颜色
default_colors = wc.to_array()
# 绘图展示
plt.figure(figsize=(12, 9))
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3), interpolation='bilinear')
# 关闭XY轴
plt.axis('off')
# 默认图
plt.figure(figsize=(12, 9))
plt.imshow(default_colors, interpolation='bilinear')
# 关闭XY轴
plt.axis('off')
plt.show()