Word Frequency Count Based On Two Words Using Python
There are many resources online that shows how to do a word count for single word like this and this and this and others... But I was not not able to find a concrete example for
Solution 1:
>>> from collections import Counter
>>> import re
>>>
>>> sentence = "I love TV show makes me happy, I love also comedy show makes me feel like flying"
>>> words = re.findall(r'\w+', sentence)
>>> two_words = [' '.join(ws) for ws in zip(words, words[1:])]
>>> wordscount = {w:f for w, f in Counter(two_words).most_common() if f > 1}
>>> wordscount
{'show makes': 2, 'makes me': 2, 'I love': 2}
Post a Comment for "Word Frequency Count Based On Two Words Using Python"