Python Example: Kingdoms txt text word frequency analysis

Source: Internet
Author: User
Tags ming

0x00 Preface

I can't find anything to write! Today, a Tanzhou, a lecturer in Daniel, said a text frequency analysis

I basically copied it all over again.

In the middle encounter some small problems self-Baidu pits complete the following:

Effect Demo

0X01 Preparation Environment and introduction

python3.x version Free

Installing the Jieba Library

Pip Install Jieba

Jieba of three modes:

1. Precision mode lcut function, return a word list

2. Full mode

3. Search engine mode

Frequency:

< word >:< occurrences > key-value pairs

IPO Description Imput Output process

Input: Read the contents of kingdoms from a file

Processing: Using Jiedb for Word segmentation, dictionary data structure Statistics The frequency of the occurrence of words

Output: Most of the first 10 words appear in the article

Code:

First step: Read the file

Step Two: Participle

Step Three: Statistics

Fourth Step: Sort

the introduction is complete! Then enter the actual combat it! 0x02 Combat

The complete code is as follows:

1 ImportJieba2 3Content = Open ('Kingdoms. txt','R', encoding='Utf-8'). Read ()4Words =jieba.lcut (content)#participle5excludes={"General","but said","Two people","The aorta","Horse","Don't know","The Emperor","shouting","the public will","not be","My Lord","Shu Bing","saw","How to","deliberations","Viceroy","a person","Hanzhong","Dare not","centaurs","Your Majesty","Wei Bing","World","Today","around","Dong Wu","so","Jingzhou","can't","so","exultation","Lead Soldiers","Next day","Sergeant","Military Horse"}#excluded Words6words=jieba.lcut (content)7counts={}8 9  forWordinchwords:Ten     ifLen (Word) = = 1:#exclude single-character participle results One         Continue A     elifWord = ='Hung Ming' orWord = ='Ming Yue': -Real_word ='Hung Ming' -     elifWord = ='Kuan Kung' orWord = ='Cloud Length': theReal_word ='Guan Yu' -     elifWord = ='Mengde' orWord = ='Prime Minister': -Real_word ='Caocao' -     elifWord = ='Hyun Tak' orWord = ='Xuan de Yue': +Real_word ='Liu Bei' -     Else: +Real_word =Word ACounts[word] = counts.get (Word, 0) + 1 at  -  -  -  forWordinchexcludes: -     del(Counts[word]) -items=list (Counts.items ()) inItems.Sort (key=LambdaX:x[1],reverse=True) -  forIinchRange (10): toWord,count=Items[i] +     Print("{0:<10}{1:>5}". Format (Word,count))
0x03 Precautions

① encountered in the course of execution: ' GBK ' codec can ' t decode byte 0x82 in position 20:illegal multibyte sequence coding error: Content = Open ("C:\\user S\\geek\\desktop\\python.txt "," R ", encoding= ' Utf-8 ')

Here is the TXT file i downloaded "kingdoms" is ASCII, how to do it! After the search learned that to normal operation will have to change the TXT encoding to UTF-8 form to run successfully, how to do it!

First: Open txt text → file → Save As → encode →utf-8→ determine the completion of the first pit. Here we go! With the above code you succeeded, but I met far more than 2, so I'm going to write it together.

② Pit ②, here it is! He is in the online lecture ah, no txt kingdoms file how to do it! There's no way to find a kingdoms. Text attached: http://vdisk.weibo.com/s/AfY-rVkr38Gg

③ download Good later can be happy to play, but I have to say but, to ask me why? Allow me one by one! 15 word groups too many will appear! I

after the run will appear a lot of irrelevant words, line, no way can only clean, but I clean most still have, really no way, this for practiced hand to use so I dropped to 10 phrases, good, then want to complete the name of the phrase? There is an increase in the number of words that need to be excluded, so this is the end of the play.

0x04 Curtain Call Finish

Python Example: Kingdoms txt text word frequency analysis

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.