Statistical article frequency (Python implementation) __python

Source: Internet
Author: User

It is an important step to analyze the text by counting the repeated words, from the word frequency to analyze the content of the article.

This article will talk about how to use the python3.6 version to achieve the statistics of English article frequency, through this article also can have a certain understanding of Python dictionary operation.

Realization idea: 1. Enter the article

2. Establish an empty dictionary for Word frequency calculation

3. Calculate the word frequency for each line of text

4. Get the data from the dictionary to the list

5. Exchange position on the data in the list, and sort

6. Output results

Note: 1. This code can only achieve the word frequency statistics of English articles, because the Chinese text participle is also related to its semantics, the need to use Chinese word segmentation technology

2. The English article down on the Internet may have some not utf-8 encoding, and there are some characters in the article that might or may cause a decoding error (unicodedecodeerror: ' GBK ' codec can ' t decode byte 0xff in Position 0:illegal multibyte sequence)

The code implementation is as follows:

From string import punctuation #对文本的每一行计算词频的函数 def processline (line,wordcounts): #用空格替换标点符号 Line=replacepunctuati
        ONS (line) words = Line.split () to Word in Words:if word in wordcounts:wordcounts[word]+=1 Else:wordcounts[word]=1 def replacepunctuations (line): for-ch in line: #这里直接用了string的标点符 Number library. Replace punctuation marks with spaces if ch in punctuation:line=line.replace (CH, "") Return line def main (): infile =open ("Englishi.txt", ' R ') count=10 words=[] data=[] # establishes an empty dictionary for calculating the word frequency wordcounts={} for line in INFI Le:processline (Line.lower (), wordcounts) #这里line. Lower () is to convert uppercase to lowercase, to facilitate statistical frequency #从字典中获取数据对 pairs = List (Wordco Unts.items ()) #列表中的数据对交换位置, data pairs sort items = [[X,y]for (Y,X) in pairs] Items.Sort () #因为sort () functions are arranged from small to large, so range is From the last entry, take the For I in range (len (items)-1, Len (items)-count-1,-1): Print (items[i][1] + "\ T" + str (items[i][0 ]) Data.append (items[i][0]) words.append (items[i][1]) infile.close () if __name__ = = ' __main__ ': Main () 






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.