fork download
  1. from re import finditer
  2. from collections import Counter
  3. from sys import argv
  4.  
  5. #data = open(argv[1], 'rt', encoding='cp1251')
  6. data = ['abcd, def -ref +\n', 'abcd+(ref)\n', 'abcd\n']
  7.  
  8. wordcount = Counter()
  9. for line in data:
  10. for word in finditer('[^-+().,:\s;><"\'?!]+', line):
  11. w = word.group()
  12. #print(w)
  13. wordcount[w] += 1
  14. for count, word in sorted(((c, w) for w, c in wordcount.items()), reverse=True):
  15. print(word, count)
  16.  
Success #stdin #stdout 0.02s 10344KB
stdin
Standard input is empty
stdout
abcd 3
ref 2
def 1