Python基礎知識(3)——中英文文檔詞頻統計__Python

來源:互聯網
上載者:User
1.英文文檔詞頻統計

英文文檔詞頻以英文原著愛麗絲夢遊仙境為例,統計每個詞在整部小說中出現的頻率,並按詞頻從大到小進行排序。由於整本書所包含單詞較多,為了便於展示,只輸出詞頻大於10的單詞。
代碼如下所示

# -*- coding: utf-8 -*-"""Created on Thu Jun 15 21:13:17 2017@author: zch"""import string#讀取英文原著alicepath = 'E:/Python/data/NLP/alice.txt'with open(path,'r',encoding= 'utf-8') as text:    #將所有的英文字母轉換成小寫    words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]#轉換成集合形式words_index = set(words)#使用字典統計詞頻counts_dict = {index:words.count(index) for index in words_index}#按照詞頻從高到低排序for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True):    if counts_dict[word] > 10:        print('{} -- {} times'.format(word,counts_dict[word]))

輸出結果如下圖所示:

可以看到,在書中出現頻次最高的十個單詞依次為:”the“、“and”、“to”、“a”、”she“、”it“、“of”、“said”、”i“、”alice“。 2.中文文檔詞頻統計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.