Skip to content

Instantly share code, notes, and snippets.

@Ayush-iitkgp
Last active April 4, 2019 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ayush-iitkgp/37374464dc2dbbb4e0cf134dcd003b06 to your computer and use it in GitHub Desktop.
Save Ayush-iitkgp/37374464dc2dbbb4e0cf134dcd003b06 to your computer and use it in GitHub Desktop.
Fineway
# 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