Guest User

Untitled

a guest
Feb 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. def get_word_frequencies(filename):
  2. freqs = {}
  3. for line in open(filename):
  4. for char in line.split():
  5. if char in freqs:
  6. freqs[char] += 1
  7. else:
  8. freqs[char] = 1
  9. return freqs
  10.  
  11. from collections import Counter
  12. from string import ascii_letters
  13.  
  14. def get_word_frequencies(filename):
  15. with open(filename) as f:
  16. c = Counter(f.read())
  17. return {k:v for k,v in c.items() if k in ascii_letters}
  18.  
  19. c = get_word_frequencies('derp.py')
  20.  
  21. print(c)
  22. # {'o': 12, 'h': 1, 'C': 2, 't': 16, 'i': 18, 'y': 1, 'u': 5, 'f': 11, 'p': 6,
  23. # 'v': 2, 'c': 10, 'm': 7, 'n': 13, 'k': 3, 'd': 5, 'a': 6, 'q': 2, 'w': 3,
  24. # 's': 10, 'g': 3, 'r': 19, 'l': 6, 'e': 25}
  25.  
  26. for char in line:
  27.  
  28. for word in line.split():
  29. for char in word:
  30.  
  31. >>> import collections
  32. >>> print collections.Counter("hello how are you doing today?")
  33. Counter({' ': 5, 'o': 5, 'a': 2, 'e': 2, 'd': 2, 'h': 2, 'l': 2, 'y': 2, 'g': 1, 'i': 1, 'n': 1, 'r': 1, 'u': 1, 't': 1, 'w': 1, '?': 1})
  34.  
  35. import re
  36. from collections import Counter
  37.  
  38. with open('filename', 'r') as file:
  39. text = file.read()
  40.  
  41. text = re.sub('[s]+|()', ' ', text).split()
  42.  
  43. freqs = Counter(text)
Add Comment
Please, Sign In to add comment