Python實(shí)現(xiàn)統(tǒng)計(jì)英文文章詞頻的方法分析-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站是專業(yè)的重慶網(wǎng)站建設(shè)公司,重慶接單;提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行重慶網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

應(yīng)用介紹:

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

思路分析:

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

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

fin = open('The_Magic_Skin _Honore_de_Balzac.txt') #the txt is up
#to you
lines=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 list
s is the article list
'''
def total_num(s):
  return len(s)
'''calculate the occurrence times of every word
t 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 word
w 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 dictionary
v 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 functions
output is the ten words with the largest frequency
'''
total = total_num(words_list())
print(word_sort(word_fre(word_dic(words_list())))[-10:])

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站欄目:Python實(shí)現(xiàn)統(tǒng)計(jì)英文文章詞頻的方法分析-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://muchs.cn/article44/dodohe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站營(yíng)銷、定制開發(fā)關(guān)鍵詞優(yōu)化、云服務(wù)器、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)