神剑山庄资源网 Design By www.hcban.com
统计十篇新闻TF-IDF
统计TF-IDF词频,每篇文章的 top10 的高频词存储为 json 文件
TF-IDF
TF-IDF(term frequency–inverse document frequency)是一种用于资讯检索与文本挖掘的常用加权技术。TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度。字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降。TF-IDF加权的各种形式常被搜索引擎应用,作为文件与用户查询之间相关程度的度量或评级。除了TF-IDF以外,互联网上的搜索引擎还会使用基于连结分析的评级方法,以确定文件在搜寻结果中出现的顺序。
假如一篇文件的总词语数是100个,而词语“母牛”出现了3次,那么“母牛”一词在该文件中的词频就是3/100=0.03。一个计算文件频率(DF)的方法是测定有多少份文件出现过“母牛”一词,然后除以文件集里包含的文件总数。所以,如果“母牛”一词在1,000份文件出现过,而文件总数是10,000,000份的话,其逆向文件频率就是log(10,000,000 / 1,000)=4。最后的TF-IDF的分数为0.03 * 4=0.12。 —— [ 维基百科 ]
博主选择的是chinadaily的十篇新闻.
1.使用http request请求
2.使用Beautiful Soup来抓取文章标题和内容
3.统计TF-IDF
4.保存到json文件中
代码块
@requires_authorization #coding=utf-8 import requests import bs4 import sys import math import json reload(sys) sys.setdefaultencoding('utf-8') url_list = ['http://www.chinadaily.com.cn/china/2016-04/20/content_24701635.htm', 'http://www.chinadaily.com.cn/china/2016-04/20/content_24700746.htm', 'http://www.chinadaily.com.cn/china/2016-04/20/content_24681482.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24675530.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24675455.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24674074.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24655536.htm', 'http://www.chinadaily.com.cn/china/2016-04/18/content_24643685.htm', 'http://www.chinadaily.com.cn/china/2016-04/18/content_24636917.htm', 'http://www.chinadaily.com.cn/china/2016-04/15/content_24562198.htm' ] articles_title = [] articles_content = [] for pos,url in enumerate(url_list): r = requests.get(url) soup1 = bs4.BeautifulSoup(r.text) soup2 = bs4.BeautifulSoup(str(soup1.find_all(id="Title_e"))) articles_title.append(soup2.h1.string) mystr = "" soup3 = bs4.BeautifulSoup(str(soup1.find_all(id="Content"))) for x in soup3.find_all("p"): mystr = mystr + x.string str_p = "" contents = [] for pos,x in enumerate(mystr): if x == '.' or x == ',': if pos < (len(mystr) - 1) and mystr[pos+1] >= '0' and mystr[pos+1] <= '9': str_p = str_p + x elif str_p == "": continue else: contents.append(str_p) str_p = "" elif x == '(' or x == ')' or x == ' ' or x == '"' or x == '[' or x == ']' or x == '-': if str_p == "": continue else: contents.append(str_p) str_p = "" else: str_p = str_p + x articles_content.append(contents) Dict_idf = {} DictList = [] for content in articles_content: Dict_tf = {} for x in content: if not Dict_tf.has_key(x): Dict_tf[x] = 1.0 if not Dict_idf.has_key(x): Dict_idf[x] = 1.0 else: Dict_idf[x] += 1.0 else: Dict_tf[x] += 1.0 for k, v in Dict_tf.items(): Dict_tf[k] = v / len(content) DictList.append(Dict_tf) for k, v in Dict_idf.items(): Dict_idf[k] = math.log(float(len(url_list)) / v) for pos,x in enumerate(DictList): for k,v in x.items(): DictList[pos][k] = v*Dict_idf[k] DictList[pos] = sorted(x.iteritems(), key=lambda d: d[1], reverse=True) """ [ [ article_titile:"XXXX" [ { word:"hello" value:3.5 } { word:"hello" value:3.5 } { word:"hello" value:3.5 } ... ] ] ] """ data = [] for pos in range(10): data2=[] data2.append("article_titile:") data2.append(articles_title[pos]) data2.append([{"word": k,"value":round(v,4)} for k,v in DictList[pos][:10]]) data.append(data2) # Writing JSON data with open('data.json', 'w') as f: json.dump(data, f)
使用json.cn查看数据:
github地址:https://github.com/mqsee/learngit
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
Python,爬取,TF,IDF
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Python爬取十篇新闻统计TF-IDF的评论...
更新日志
2024年10月02日
2024年10月02日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】