Last active
April 4, 2019 13:31
-
-
Save Ayush-iitkgp/37374464dc2dbbb4e0cf134dcd003b06 to your computer and use it in GitHub Desktop.
Fineway
This file contains 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
# How to run the program | |
# 1. python code.py | |
import operator | |
import re | |
def print_word_count(filename): | |
regex = re.compile('[^a-zA-Z]') | |
word_count = {} | |
for line in open (filename, "r"): | |
for word in line.lower().split(): | |
word = regex.sub('', word) | |
if word in word_count: | |
word_count[word] = word_count[word] + 1 | |
else: | |
word_count[word] = 1 | |
output_file = open("result.txt","w+") | |
for word in sorted(word_count, key=word_count.get, reverse=True): | |
output_file.write("%s (%d)\n" % (word, word_count[word])) | |
output_file.close() | |
if __name__ == '__main__': | |
print_word_count("data.txt") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment