Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_word_frequencies(filename):
- freqs = {}
- for line in open(filename):
- for char in line.split():
- if char in freqs:
- freqs[char] += 1
- else:
- freqs[char] = 1
- return freqs
- from collections import Counter
- from string import ascii_letters
- def get_word_frequencies(filename):
- with open(filename) as f:
- c = Counter(f.read())
- return {k:v for k,v in c.items() if k in ascii_letters}
- c = get_word_frequencies('derp.py')
- print(c)
- # {'o': 12, 'h': 1, 'C': 2, 't': 16, 'i': 18, 'y': 1, 'u': 5, 'f': 11, 'p': 6,
- # 'v': 2, 'c': 10, 'm': 7, 'n': 13, 'k': 3, 'd': 5, 'a': 6, 'q': 2, 'w': 3,
- # 's': 10, 'g': 3, 'r': 19, 'l': 6, 'e': 25}
- for char in line:
- for word in line.split():
- for char in word:
- >>> import collections
- >>> print collections.Counter("hello how are you doing today?")
- 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})
- import re
- from collections import Counter
- with open('filename', 'r') as file:
- text = file.read()
- text = re.sub('[s]+|()', ' ', text).split()
- freqs = Counter(text)
Add Comment
Please, Sign In to add comment