国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實(shí)現(xiàn)統(tǒng)計(jì)英文文章詞頻的方法分析

2020-02-16 00:57:19
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)統(tǒng)計(jì)英文文章詞頻的方法。分享給大家供大家參考,具體如下:

應(yīng)用介紹:

統(tǒng)計(jì)英文文章詞頻是很常見的需求,本文利用python實(shí)現(xiàn)。

思路分析:

1、把英文文章的每個單詞放到列表里,并統(tǒng)計(jì)列表長度;
2、遍歷列表,對每個單詞出現(xiàn)的次數(shù)進(jìn)行統(tǒng)計(jì),并將結(jié)果存儲在字典中;
3、利用步驟1中獲得的列表長度,求出每個單詞出現(xiàn)的頻率,并將結(jié)果存儲在頻率字典中;
4、以字典鍵值對的“值”為標(biāo)準(zhǔn),對字典進(jìn)行排序,輸出結(jié)果(也可利用切片輸出頻率最大或最小的特定幾個,因?yàn)榻?jīng)過排序sorted()函數(shù)處理后,單詞及其頻率信息已經(jīng)存儲在元組中,所有元組再組成列表。)

代碼實(shí)現(xiàn):

fin = open('The_Magic_Skin _Honore_de_Balzac.txt') #the txt is up#to youlines=fin.readlines()fin.close()'''transform the article into word list'''def words_list():  chardigit='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '  all_lines = ''  for line in lines:    one_line=''    for ch in line:      if ch in chardigit:        one_line = one_line + ch    all_lines = all_lines + one_line  return all_lines.split()'''calculate the total number of article lists is the article list'''def total_num(s):  return len(s)'''calculate the occurrence times of every wordt is the article list'''def word_dic(t):  fre_dic = dict()  for i in range(len(t)):    fre_dic[t[i]] = fre_dic.get(t[i],0) + 1  return fre_dic'''calculate the occurrence times of every wordw is dictionary of the occurrence times of every word'''def word_fre(w):  for key in w:    w[key] = w[key] / total  return w'''sort the dictionaryv is the frequency of words'''def word_sort(v):  sort_dic = sorted(v.items(), key = lambda e:e[1])  return sort_dic'''This is entrance of functionsoutput is the ten words with the largest frequency'''total = total_num(words_list())print(word_sort(word_fre(word_dic(words_list())))[-10:])

PS:這里再為大家推薦2款相關(guān)統(tǒng)計(jì)工具供大家參考:

在線字?jǐn)?shù)統(tǒng)計(jì)工具:
http://tools.jb51.net/code/zishutongji

在線字符統(tǒng)計(jì)與編輯工具:
http://tools.jb51.net/code/char_tongji

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汽车| 改则县| 台南县| 肇州县| 奇台县| 措美县| 白水县| 清徐县| 余干县| 双辽市| 庆元县| 潞城市| 灵石县| 江达县| 会宁县| 四会市| 绥宁县| 英吉沙县| 专栏| 大理市| 含山县| 右玉县| 永登县| 枣强县| 龙陵县| 呼伦贝尔市| 安徽省| 五河县| 盱眙县| 武鸣县| 洛隆县| 梁河县| 宜阳县| 荥经县| 专栏| 漳平市| 进贤县| 浦北县| 贺州市| 武冈市| 安康市|