from collections import Counter # Example phrase phrase = "John is the son of John second. Second son of John second is William second." split_phrase = phrase.split() # create counter object Counter = Counter(split_phrase) most_occured_words = Counter.most_common(4) print(most_occured_words)
Here is what the above code is Doing:
1. We create a Counter object and pass the list of words as its argument.
2. We use the most_common() function and pass the number 4 as its argument. This returns the 4 most frequently occurring words.
3. We print the list of tuples returned by the most_common() function.