question No. 0004: any plain text file in English, counting the number of occurrences of the word.
Idea: match the words and numbers of the response with regular expressions, then let counter calculate the word frequency of the words, and then use the Most_common method to return a list of tuples that contain the word and the number of occurrences of that word in chronological order.
0004. Statistical words. PY
#!/usr/bin/env python#coding: Utf-8ImportRe fromCollectionsImportCounterfilesource ='/home/bill/desktop/testarticle ' def getmostcommonword(articlefilesource): "Enter an English plain text file to count the number of occurrences of the word "Pattern =R ' [a-za-z]+|\$?\d+%?$ '] withOpen (Articlefilesource) asF:r = Re.findall (Pattern,f.read ())returnCounter (R). Most_common ()if__name__ = =' __main__ ':PrintGetmostcommonword (Filesource)
One of the articles is a piece of news that you pick from the BBC.
Python Show-me-the-code The No. 0004 question statistical word