"Python" Kingdoms word Frequency statistics

Source: Internet
Author: User
Tags ming

Import Jieba
txt = open (' C:/users/eternal/desktop/threekingdoms.txt ', ' R ', encoding= ' UTF-8 '). Read () #提前修改txt文件编码格式utf-8
excludes = {' General ', ' but said ', ' Jingzhou ', ' two ', ' not ', ' can't ', ' so '} #错误的名字
Words = jieba.lcut (TXT)
print (words)
Counts = {}
For word in words:
If Len (word) = = 1:
Continue
elif Word = = ' Zhuge Liang ' or word = = ' Hung Ming Yue ':
RWord = ' Hung Ming '
elif Word = = ' Guan Gong ' or word = = ' cloud length ':
RWord = = ' Guan Yu '
elif Word = = ' xuan de ' or word = = ' Xuan de Yue ':
RWord = ' Liu Bei '
elif Word = = ' Mengde ' or word = = ' Prime Minister ':
RWord = ' Caocao '
Else
RWord = Word
Counts[rword] = Counts.get (rword,0) + 1
For word in excludes:
Del Counts[word]
Items = List (Counts.items ())
Items.Sort (Key=lambda x:x[1],reverse=true)
Print (items)
For I in range (10):
Word,count = Items[i]
Print (' {0:<10}{1:>5} '. Format (Word,count))

"Python" Kingdoms word Frequency statistics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.