1. 程式人生 > 其它 >用python對modern family 摩登家庭 1~11季劇本臺詞的詞頻分析

用python對modern family 摩登家庭 1~11季劇本臺詞的詞頻分析

摩登家庭這部美劇學英語應該不模式,某寶上買了1~11季的臺詞,想對裡面得單詞出現頻率做個統計,高頻出現的單詞應該就是日常常用的,應該牢牢記住。出現次數太低的也可以不用學了。

分析程式用的是python語言。

其中單詞總量:23298個,分析結果以txt文字檔案儲存。詞頻結果下載

按流水號,單詞,出現頻率記錄。如下:

1 i 32743
2 you 30923
3 the 23733
4 a 21256
5 to 20402
6 and 13428
7 it 12405
8 that 12020
9 of 9744

....

23291 conspiring 1
23292 subletting 1
23293 coughed 1
23294 overnighted 1
23295 biologist 1
23296 waitressing 1
23297 secret's 1
23298 muriel 1

出現次數最高的單詞依然是i,you,the這類的。

程式碼如下:

 1 import os
 2 import re
 3 
 4 from docx import Document
 5 
 6 word_dic = {}
 7 for root, dirs, files in os.walk(r'H:\english study'):
 8     for file in files:
 9         file_path = '{}\\{}'.format(root, file)
10         print(file_path)
11         doc = Document(file_path)
12 for para in doc.paragraphs: 13 # print(para.text) 14 rst = re.findall(r'\b[a-zA-Z\']+\b', para.text) 15 if rst: 16 for word in rst: 17 word = word.lower() 18 count = word_dic.get(word) 19 if
count: 20 word_dic[word] = count + 1 21 else: 22 word_dic[word] = 1 23 sort_list = sorted(word_dic.items(), key=lambda x: x[1], reverse=True) 24 i = 1 25 26 with open('e:\\modern famile word sort.txt',mode='w',encoding='utf-8') as f: 27 for word in sort_list: 28 line='{} {} {}\n'.format(i, word[0], word[1]) 29 f.write(line) 30 i = i + 1 31 print('done, 單詞總量:{}'.format(len(sort_list)))