Last active
October 14, 2021 06:59
-
-
Save RaMSFT/dd2f5a5ae343a0f9a756b97555350ef6 to your computer and use it in GitHub Desktop.
This snippet of code finds most repeated words from a given text
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 | |
| ## 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 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() | |
| ## Empty dictionary | |
| result_dict = {} | |
| ## Process to find words - Start with first word, if word exists in dictionary key increment value by 1, else add the key to dictionary with value 1 | |
| for word in text_to_list: | |
| if word in result_dict.keys(): | |
| result_dict[word] += 1 | |
| else: | |
| result_dict[word] = 1 | |
| ## Sort and print words in descedning order based on number of appereances. | |
| print(sorted(result_dict.items(),key = lambda x: x[1], reverse=True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment