日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 英语词频统计软件_Python实现统计英文文章词频的方法分析

發布時間:2025/3/11 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 英语词频统计软件_Python实现统计英文文章词频的方法分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了Python實現統計英文文章詞頻的方法。分享給大家供大家參考,具體如下:

應用介紹:

統計英文文章詞頻是很常見的需求,本文利用python實現。

思路分析:

1、把英文文章的每個單詞放到列表里,并統計列表長度;

2、遍歷列表,對每個單詞出現的次數進行統計,并將結果存儲在字典中;

3、利用步驟1中獲得的列表長度,求出每個單詞出現的頻率,并將結果存儲在頻率字典中;

4、以字典鍵值對的“值”為標準,對字典進行排序,輸出結果(也可利用切片輸出頻率最大或最小的特定幾個,因為經過排序sorted()函數處理后,單詞及其頻率信息已經存儲在元組中,所有元組再組成列表。)

代碼實現:

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:])

PS:這里再為大家推薦2款相關統計工具供大家參考:

希望本文所述對大家Python程序設計有所幫助。

總結

以上是生活随笔為你收集整理的python 英语词频统计软件_Python实现统计英文文章词频的方法分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。