Guest User

Untitled

a guest
Dec 10th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. import collections
  2. import re
  3.  
  4. def count_words_in_text(file_path):
  5. with open(file_path, 'r') as filehandle:
  6. text = filehandle.read().lower()
  7. word_list = re.sub('[-,."\n()]', ' ', text).split(' ')
  8. word_count = collections.Counter(word_list)
  9.  
  10. for word, count in word_count.items():
  11. print(str(word) + ' (' + str(count)+')')
  12.  
  13. count_words_in_text('text.txt')
Add Comment
Please, Sign In to add comment