Show
Simple Python script without the use of heavy text processing libraries to extract most common words from a corpus.What is the most used word in all of Shakespeare plays? Was ‘king’ more often used than ‘Lord’ or vice versa? To answer these type of fun questions, one often needs to quickly examine and plot most frequent words in a text file (often downloaded from open source portals such as Project Gutenberg). However, if you search on the web or on Stackoverflow, you will most probably see examples of nltk and use of CountVectorizer. While they are incredibly powerful and fun to use, the matter of the fact is, you don’t need them if the only thing you want is to extract most common words appearing in a single text corpus. Below, I am showing a very simple Python 3 code snippet to do just that — using only a dictionary and simple string manipulation methods. Feel free to copy the code and use your own stopwords to make it better! import collections An example of the code output and plot of the 10 most frequently used words in the corpus. The text is ‘Pride and Prejudice’ and you can see the familiar names of Elizabeth and Mr. Darcy! :) Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you. Given the data set, we can find k number of most frequent words. The solution of this problem already present as Find the k most frequent words from a file. But we can solve this problem very efficiently in Python with the help of some high performance modules. In order to do this, we’ll use a high performance data type module, which is collections. This
module got some specialized container datatypes and we will use counter class from this module. Input : "John is the son of John second. Second son of John second is William second." Output : [('second', 4), ('John', 3), ('son', 2), ('is', 2)] Explanation : 1. The string will converted into list like this : ['John', 'is', 'the', 'son', 'of', 'John', 'second', 'Second', 'son', 'of', 'John', 'second', 'is', 'William', 'second'] 2. Now 'most_common(4)' will return four most frequent words and its count in tuple. Input : "geeks for geeks is for geeks. By geeks and for the geeks." Output : [('geeks', 5), ('for', 3)] Explanation : most_common(2) will return two most frequent words and their count. Recommended: Please try your approach on {IDE} first, before moving on to the solution.Approach :
Below is Python implementation of above approach :
Output : [('Geeks', 5), ('to', 4), ('and', 4), ('article', 3)] How do I find the most common word in a text Python?Approach :. Import Counter class from collections module.. Split the string into list using split(), it will return the lists of words.. Now pass the list to the instance of Counter class.. The function 'most-common()' inside Counter will return the list of most frequent words from list and its count.. How do I find the most repeated words in a string in python?Use the Counter() function (which gives the frequency of words as a key-value pairs), to calculate the frequency (number of times the word has occurred) of all the words. Create a variable to store the maximum frequency. Loop in the above words frequency dictionary using the for loop.
How do you find common words in Python?Practical Data Science using Python. convert s0 and s1 into lowercase.. s0List := a list of words in s0.. s1List := a list of words in s1.. convert set from words in s0List and s1List, then intersect them to get common words, and return the count of the intersection result.. How do I find the most frequently used words?WordCounter is a web tool where you can cut and paste a body of text to the text boxes and counts the most frequently used words in the text. This is a quick tool for making sure the text does not contains any overuse words. Wordcounter ranks the most frequently used words in any given body of text.
|