您现在的位置是:热点 >>正文
手把手教你对文本文件进行分词、词频统计和可视化(附源码)
热点71522448人已围观
简介...
大家好!我是Python进阶者。
前言
前几天一个在校大学生问了一些关于词频、分词和可视化方面的问题,结合爬虫,确实可以做点东西出来,可以玩玩,还是蛮不错的,这里整理成一篇文章,分享给大家。
本文主要涉及的库有爬虫库requests、词频统计库collections、数据处理库numpy、结巴分词库jieba 、可视化库pyecharts等等。
一、数据来源
关于数据方面,这里直接是从新闻平台上进行获取的文本信息,其实这个文本文件可以拓展开来,你可以自定义文本,也可以是报告,商业报告,政治报告等,也可以是新闻平台,也可以是论文,也可以是微博热评,也可以是网易云音乐热评等等,只要涉及到大量文本的手把,都可月引用本文的代码,进行词频分词、统计、可视化等。
二、数据获取
数据获取十分简单,一个简单的爬虫和存储就可以搞定,这里以一篇新闻为例进行演示,代码如下:
import re import collections # 词频统计库 import numpy as np # numpy数据处理库 import jieba # 结巴分词 import requests from bs4 im手教port BeautifulSoup from pyecharts import options as opts from pyecharts.charts import WordCloud from pyecharts.globals import SymbolType import warnings warnings.filterwarnings('ignore') 你对r=requests.get("https://m.thepaper.cn/baijiahao_11694997",timeout=10) r.encoding="utf-8" s=BeautifulSoup(r.text,"html.parser") f=open("报告.txt","w",encoding="utf-8") L=s.find_all("p") for c in L: f.write("{} ".format(c.text)) &文本nbsp; f.close()
代码运行之后,在本地会得到一个【报告.txt】文件,文件内容就是网站上的文本信息。如果你想获取其他网站上的文本,需要更改下链接和提取规则。
三、词频统计
接下来就是词频统计了,代码如下所示。
# 读取文件 fn = open("./报告.txt","r",encoding="utf-8") string_data = fn.read() fn.close() # 文本预处理 # 定义正则表达式匹配模式&n文件bsp;pattern = re.compile(u' |,|/|。| |.|-|:|;|)|(|?|"') string_data = re.sub(pattern,'',string_data) # 将符合模式的字符去除 # 文本分词 # 精确模式分词 seg_list_exact = jieba.cut(string_data,cut_all=False) object_list = [进行] # 自定义去除词库 remove_words = [u'的',u'Ҫ', u'“',u'”',u'和',u',',u'Ϊ',u'是', '以' u'随着', u'对于', u'对',u'等',u'能',u'都',u'。', &分词nbsp; u' ',u'、',u'中',u'在',u'了',u'通常',u'如果',u'我', u'她',u'(',u')',u'他',u'你',u'?',u'—',u'就', &、词nbsp; u'着',u'˵',u'上',u'这', u'那',u'有', u'Ҳ', u'ʲô', u'·', u'将', u'没有', u'到', u'不', u'ȥ'] for word频统 in seg_list_exact: if word not in remove_words: object_list.append(word) # 词频统计 # 对分词做词频统计 word_counts = collections.Counter(object_list) # 获计和取前30最高频的词 word_counts_all = word_counts.most_common() word_counts_top30 = word_counts.most_common(30) print("2021年政府工作报告一共有%d个词"%len(word_counts)) print(word_counts_top30)
首先读取文本信息,之后对文本进行预处理,提取文字信息,并且可以自定义词库,作为停用词,之后将获取到的词频做词频统计,获取前30最高频的词,并进行打印,输出结果如下可视图所示。
四、可视化
接下来就是可视化部分了,这里直接上代码,如下所示。
import pyecharts from pyecharts.charts import Line from pyecharts import options as opts # 示例数据 cate = [i[0] for i in word_counts_top30] data1 = [i[化(1] for i in word_counts_top30] line = (Line() .add_xaxis(cate) .add_yaxis('词频', data1, 附源 markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")])) .set_global_opts(title_opts=opts.TitleOpts(title="词频统计Top30", subtitle=""), xaxis_opt码)s=opts.AxisOpts(name_rotate=60,axislabel_opts={"rotate":45})) ) line.render_notebook()
输出结果是一个线图,看上去还不错。
五、总结
本文基于Python网络爬虫获取到的文本文件,通过词频、分词和可视化等处理,完成一个较为简单的项目,欢迎大家积极尝试。在代码实现过程中,如果有遇到任何问题,请加我好友,我帮助解决哦!
Tags:
相关文章
阿里云宣布第三轮大模型降价,通义千问视觉理解模型价格大幅降低超80%
热点阿里云今日宣布了本年度第三轮大模型价格降价,其中通义千问视觉理解模型的价格全线降价超过80%,为用户提供了更优惠的价格。 在此次降价中,Qwen-VL-Plus模型的降价幅度达到了81%,其输入价格降...
【热点】
阅读更多谷歌 Workspace 推出 AI 助手,助力办公效率提升
热点在本周的谷歌云峰会上,科技巨头谷歌宣布对其订阅型生产力套件 Workspace 进行了一系列重大更新,所有新功能均由其全新的 AI 系统 Workspace Intelligence 驱动。这些更新旨...
【热点】
阅读更多标签: 面壁智能 MiniCPM 中国电信 大模型
热点云梦级2月28日云梦级消息,近日,面壁智能完成马年春节之后首轮融资,迎来新春“开门红”。 本轮融资规模数亿元,由中国电信领投、中信金石、中信私募跟投。其中,中国电信作为战略投资方,将与面壁智能展开深度...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.simcoemuskokahealth.org/SearchResults.aspx?SearchQuery=小旋风万能蜘蛛池【tg:seogoogle987】,Lph
- https://www.sic-info.org/vi/?s=小旋风万能蜘蛛池【tg:seogoogle987】,ToA
- https://store.line.me/search/sticker/ja?q=小旋风万能蜘蛛池【tg:seogoogle987】RFP
- https://www.libros.unam.mx/catalogsearch/result/?cat=37&dir=asc&format=20&lit=20&manufacturer=3230&mode=list&order=year_edition&q=小旋风万能蜘蛛池【tg:seogoogle987】,U4X
- https://www.lezhin.com/ko/tags/小旋风万能蜘蛛池【tg:seogoogle987】,z7a
- https://www.springfieldlibrary.org/?s=小旋风万能蜘蛛池【tg:seogoogle987】,fzL
- https://www.woorden.org/spreekwoord.php?woord=小旋风万能蜘蛛池【tg:seogoogle987】,MWP
- https://www.londontheatredirect.com/search/小旋风万能蜘蛛池【tg:seogoogle987】,Foy
- https://www.toy-people.com/?s=小旋风万能蜘蛛池【tg:seogoogle987】,3ZH
- https://www.roberthalf.com/us/en/jobs/all/小旋风万能蜘蛛池【tg:seogoogle987】tUA
- https://www.qylaws.com/search/小旋风万能蜘蛛池【tg:seogoogle987】,mDb
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=小旋风万能蜘蛛池【tg:seogoogle987】,GDX
- https://www.swanagerailway.co.uk/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,Pgn
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=小旋风万能蜘蛛池【tg:seogoogle987】1B9
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=小旋风万能蜘蛛池【tg:seogoogle987】,aSu
- https://www.partilerdunyasi.com/index.php?route=product/search&tag=小旋风万能蜘蛛池【tg:seogoogle987】,Lal
- https://www.teamlab.art/ko/?submit=小旋风万能蜘蛛池【tg:seogoogle987】,k5Q
- https://www.roberthalf.com/us/en/jobs/all/小旋风万能蜘蛛池【tg:seogoogle987】YXs
- https://www.reginatangoshoes.com/en/catalogsearch/result/index/?T=1686132113407&q=小旋风万能蜘蛛池【tg:seogoogle987】,NB0
- https://www.localfootdoc.com/?s=小旋风万能蜘蛛池【tg:seogoogle987】,Sjn
- https://www.ucycle.com/search/=小旋风万能蜘蛛池【tg:seogoogle987】,K5j
- https://www.teamlab.art/?submit=小旋风万能蜘蛛池【tg:seogoogle987】,VI2
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=小旋风万能蜘蛛池【tg:seogoogle987】,IyV
- https://www.takachi-enclosure.com/search?eqYB=gnI31Ou&text=小旋风万能蜘蛛池【tg:seogoogle987】,nqL
- https://www.remit.co.jp/vi/?s=小旋风万能蜘蛛池【tg:seogoogle987】Plq
- https://www.teacherspayteachers.com/Browse/Price-Range/Free/Search:小旋风万能蜘蛛池【tg:seogoogle987】,Q6u
- https://www.jusoen.com/addreng.asp?p1=小旋风万能蜘蛛池【tg:seogoogle987】GGr
- https://www.kaiyueleather.com/search/小旋风万能蜘蛛池【tg:seogoogle987】,AaY
- https://www.mingwai.edu.hk/?langzh-hant&s=小旋风万能蜘蛛池【tg:seogoogle987】,Y24
- https://www.tahiti-infos.com/search/小旋风万能蜘蛛池【tg:seogoogle987】,nPU
- https://www.otakuninjahero.com/en/search/小旋风万能蜘蛛池【tg:seogoogle987】,kJk
- https://www.xvideos.com/tags/小旋风万能蜘蛛池【tg:seogoogle987】,FFd
- https://www.zutari.com/search/小旋风万能蜘蛛池【tg:seogoogle987】,sY2
- https://www.mintos.org/?s=小旋风万能蜘蛛池【tg:seogoogle987】,Mss
- https://www.teamlab.art/it/?submit=小旋风万能蜘蛛池【tg:seogoogle987】,Jxb
- https://www.abbreviations.com/小旋风万能蜘蛛池【tg:seogoogle987】NbC
- https://hsmoa.com/search?query=小旋风万能蜘蛛池【tg:seogoogle987】7NC
- https://www.lee.com/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,CoD
- https://www.sbaad.no/?s=小旋风万能蜘蛛池【tg:seogoogle987】,htg
- https://www.livehindustan.com/tags/小旋风万能蜘蛛池【tg:seogoogle987】,sV3
- https://www.smics.com/tc/site/search?token=小旋风万能蜘蛛池【tg:seogoogle987】,E3Y
- https://www.openspaceinstitute.org/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,MfN
- https://www.paxnet.co.kr/search/jongmok?kwd=小旋风万能蜘蛛池【tg:seogoogle987】B3E
- https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=小旋风万能蜘蛛池【tg:seogoogle987】,zkx
- https://www.therapeuticassociates.com/?cat=5&s=小旋风万能蜘蛛池【tg:seogoogle987】,pxC
- https://www.sonepar.ch/fr/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,Ro0
- https://www.pixiv.net/tags/小旋风万能蜘蛛池【tg:seogoogle987】,stt
- https://www.sdcard.org/ja/?s=小旋风万能蜘蛛池【tg:seogoogle987】,H35
- https://www.ng-slo.si/si/iskanje?cat=114&q=小旋风万能蜘蛛池【tg:seogoogle987】,gxh
- https://www.seforall.org/search?keywords=小旋风万能蜘蛛池【tg:seogoogle987】,4Zq