Created
October 14, 2021 07:21
-
-
Save RaMSFT/7a83f45604b4b902fec3593a0645b57f to your computer and use it in GitHub Desktop.
Most Common words using counter method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Import Regular Expression - Used to replace all special characters other than alphanumeric | |
| import re | |
| ## Import Counter from collections | |
| from collections import Counter | |
| ## Input | |
| giventext = "This is Medium article presented by ramstkp in the month of October. On the day of writing it was cold, and autumn started early this in the october month. October month is relatively less cold compared to winter months" | |
| ## Replacing all other characters other than alphanumerics | |
| giventext = re.sub('[^a-zA-Z0-9 \n]', '', giventext) | |
| ## Converting to lower and splitting the text to list by word (split by space) | |
| text_to_list = giventext.lower().split() | |
| ## User Counter method from collections | |
| result = Counter(text_to_list) | |
| ## get most common words from result counter | |
| print(result.most_common(5)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment