>综合>狗狗>

盘点吃狗粮瞬间:使用词云图看看大家是怎样告白的

时间:2024-06-25 01:20:36/人气:101 ℃

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,,版权归原作者所有,如有问题请及时联系我们以作处理

作者:lzq603 来源:CSDN

本文链接:https://blog.csdn.net/lzq603/article/details/111704771

私信小编01即可获取大量Python学习资料

最近在抖音上发现某歌手的某程序员歌迷,对其歌词进行分析,发现了意外的收获:歌词中出现最多的词语“没有”。于是突发奇想,分析一下别人的表白,看看大家的表白构成。

开工

首先从tomcat中取出所有access的日志,查看日志内容。

经分析,所有在“/meetingroom/artqrcode?txt=”之后、在“&c”之前的内容为用户输入的内容。如图,%加数字是汉字经过URL编码后的结果,在处理时需进行解码。

数据预处理

这些日志中的数据很杂乱,很多并不是生成二维码的请求,所以需要将其过滤。下面这段代码从日志文件中提取日志中的有用数据,写到新文件honey_words.txt中去。

import reimport osfrom urllib import parse'''取该行中用户要生成二维码的内容'''def parse_out(line): # 正则匹配 match = re.search('/meetingroom/artqrcode\\?txt=(.*)&c', line) # 匹配对象match不是None说明已匹配成功 if match is not None: # 取第一个捕获组(正则表达式中括号里的内容) content = match.group(1) # 过滤链接内容 if not content.startswith('http'): # 以追加模式打开新文件 with open('honey_words.txt', 'a', encoding='utf-8') as fout: # URL解码 content = parse.unquote(content) # 写到新文件 fout.write(content '\n')'''遍历每个文件中的每一行,交由parse_out函数处理'''if __name__ == '__main__': filename_list = os.listdir('log') for filename in filename_list: # 以只读模式打开日志文件 with open('log/' filename, 'r', encoding='utf-8') as fo: lines = fo.readlines() for line in lines: # 解析内容并输出到新文件 parse_out(line)词频统计

为制作能够体现用词情况的词云图,需要统计每个词语在honey_words.txt文件中出现了多少次。这里首先对每一行文本使用jieba库进行分词,然后统计每个词语出现的次数,并过滤停用词。

import jiebaimport json# 统计每一个词语出现次数statistic = {}# 读取停用词fo = open('stopwords.txt', encoding='utf-8')stopwords = fo.read().split()stopwords = ['', ' ', '\n', ',']print(stopwords)fo.close()# 遍历每一行内容,进行分词,统计fo = open('honey_words.txt', encoding='utf-8')line = fo.readline()while line: seg_words = list(jieba.cut(line, cut_all=True)) for word in seg_words: # 过滤停用词 if word in stopwords: continue if word in statistic.keys(): statistic[word] = 1 else: statistic[word] = 1 line = fo.readline()# 按出现次数排序word_list = list(statistic)word_list.sort(key=lambda x:statistic[x], reverse=True)print(word_list)# 构造Jsonjson_data = {}for word in word_list: # 忽略出现次数过少的词语 if statistic[word] > 4: json_data[word] = statistic[word]print(json.dumps(json_data))生成词云

使用wordcloud插件配置好数据,就OK了,满满的狗粮啊,尽情享用吧!

首页/电脑版/地图
© 2024 CwBaiKe.Com All Rights Reserved.