統計十篇新聞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-8import requestsimport bs4import sysimport mathimport jsonreload(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 datawith open('data.json', 'w') as f: json.dump(data, f)
新聞熱點
疑難解答