Advertisement
Guest User

Untitled

a guest
May 25th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. """Count words."""
  2.  
  3. def count_words(text):
  4. """Count how many times each unique word occurs in text."""
  5. counts = dict() # dictionary of { <word>: <count> } pairs to return
  6.  
  7. # TODO: Convert to lowercase
  8.  
  9. # TODO: Split text into tokens (words), leaving out punctuation
  10. # (Hint: Use regex to split on non-alphanumeric characters)
  11.  
  12. # TODO: Aggregate word counts using a dictionary
  13.  
  14. return counts
  15.  
  16.  
  17. def test_run():
  18. with open("input.txt", "r") as f:
  19. text = f.read()
  20. counts = count_words(text)
  21. sorted_counts = sorted(counts.items(), key=lambda pair: pair[1], reverse=True)
  22.  
  23. print("10 most common words:\nWord\tCount")
  24. for word, count in sorted_counts[:10]:
  25. print("{}\t{}".format(word, count))
  26.  
  27. print("\n10 least common words:\nWord\tCount")
  28. for word, count in sorted_counts[-10:]:
  29. print("{}\t{}".format(word, count))
  30.  
  31.  
  32. if __name__ == "__main__":
  33. test_run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement