1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| import matplotlib.pyplot as plt import matplotlib import jieba import jieba.analyse import xlwt import xlrd from wordcloud import WordCloud import numpy as np from collections import Counter
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] matplotlib.rcParams['axes.unicode_minus'] = False
def anylasescore(comment): score = [0, 0, 0, 0, 0, 0] count = 0 for va in comment: try: score[int(va[2])] += 1 count += 1 except Exception as e: continue print(score) label = '1分', '2分', '3分', '4分', '5分' color = 'blue', 'orange', 'yellow', 'green', 'red' size = [0, 0, 0, 0, 0] explode = [0, 0, 0, 0, 0] for i in range(1, 5): size[i] = score[i] * 100 / count explode[i] = score[i] / count / 10 pie = plt.pie(size, colors=color, explode=explode, labels=label, shadow=True, autopct='%1.1f%%') for font in pie[1]: font.set_size(8) for digit in pie[2]: digit.set_size(8) plt.axis('equal') plt.title(u'各个评分占比', fontsize=12) plt.legend(loc=0, bbox_to_anchor=(0.82, 1)) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=6) plt.savefig("评分统计.png") plt.show()
def getzhifang(map): x = [] y = [] for k, v in map.most_common(15): x.append(k) y.append(v) Xi = np.array(x) Yi = np.array(y)
width = 0.6 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(8, 6)) plt.bar(Xi, Yi, width, color='blue', label='热门词频统计', alpha=0.8, )
plt.xlabel("词频") plt.ylabel("次数") plt.savefig('词频统计.png') plt.show() return
def getciyun_most(map): x = [] y = [] for k, v in map.most_common(300): x.append(k) y.append(v) xi = x[0:150] xi = ' '.join(xi) print(xi) wc = WordCloud(background_color="white", width=1500, height=1200, font_path="simhei.ttf", max_font_size=150, random_state=50, ) my_wordcloud = wc.generate(xi) plt.imshow(my_wordcloud) my_wordcloud.to_file("词云展示1.jpg") xi = ' '.join(x[150:300]) my_wordcloud = wc.generate(xi) my_wordcloud.to_file("词云展示2.jpg")
plt.axis("off")
def anylaseword(comment): list = ['这个', '一个', '不少', '起来', '没有', '就是', '不是', '那个', '还是', '剧情', '这样', '那样', '这种', '那种', '故事', '人物', '什么'] print(list) commnetstr = '' c = Counter() index = 0 for va in comment: seg_list = jieba.cut(va[4], cut_all=False) index += 1 for x in seg_list: if len(x) > 1 and x != '\r\n': try: c[x] += 1 except: continue commnetstr += va[4] for (k, v) in c.most_common(): if v < 5 or k in list: c.pop(k) continue print(len(c), c) getzhifang(c) getciyun_most(c)
def anylase(): data = xlrd.open_workbook('姜子牙动漫电影.xls') table = data.sheets()[0] nrows = table.nrows comment = []
for i in range(nrows): comment.append(table.row_values(i)) anylasescore(comment) anylaseword(comment)
if __name__ == '__main__': anylase()
|