Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import sys, os
  2.  
  3. print ""
  4. print "Running Script..."
  5. print ""
  6. print "This program analyzes word frequency in a file and"
  7. print "prints a report on the n most frequent words."
  8. print ""
  9.  
  10. filename = raw_input("File to analyze? ")
  11. if os.path.isfile(filename):
  12. print "The file", filename, "exists!"
  13. else:
  14. print "The file", filename, "doesn't exist!"
  15. sys.exit()
  16. print ""
  17. output = raw_input("Output analysis of how many words? ")
  18.  
  19. readfile = open(filename, 'r+')
  20.  
  21. words = readfile.read().split()
  22. wordcount = {}
  23. for word in words:
  24. if word in wordcount:
  25. wordcount[word] += 1
  26. else:
  27. wordcount[word] = 1
  28.  
  29. sortbyfreq = sorted(wordcount,key=wordcount.get,reverse=True)
  30. for word in sortbyfreq:
  31. print "%-20s %10d" % (word, wordcount[word])
  32.  
  33. limit = {enter number}
  34. counter = 0
  35. for word in sortbyfreq:
  36. print "%-20s %10d" % (word, wordcount[word])
  37. counter += 1
  38. if counter >= limit:
  39. break
  40.  
  41. from collections import Counter
  42. sortbyfreq = Counter(words) # Instead of the wordcount dictionary + for loop.
  43.  
  44. n = int(raw_input('How many?: '))
  45. for item, count in sortbyfreq.most_common(n):
  46. print "%-20s %10d" % (item, count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement