Avoid creating word_freqs outside and passing it around.
This commit is contained in:
@@ -4,49 +4,54 @@ import sys, re, operator, string
|
||||
#
|
||||
# The functions
|
||||
#
|
||||
def read_file(path_to_file, word_freqs, func):
|
||||
def read_file(path_to_file, func):
|
||||
with open(path_to_file) as f:
|
||||
data = f.read()
|
||||
func(data, word_freqs, normalize)
|
||||
func(data, normalize)
|
||||
|
||||
def filter_chars(str_data, word_freqs, func):
|
||||
def filter_chars(str_data, func):
|
||||
pattern = re.compile('[\W_]+')
|
||||
func(pattern.sub(' ', str_data), word_freqs, scan)
|
||||
func(pattern.sub(' ', str_data), scan)
|
||||
|
||||
def normalize(str_data, word_freqs, func):
|
||||
func(str_data.lower(), word_freqs, remove_stop_words)
|
||||
def normalize(str_data, func):
|
||||
func(str_data.lower(), remove_stop_words)
|
||||
|
||||
def scan(str_data, word_freqs, func):
|
||||
func(str_data.split(), word_freqs, frequencies)
|
||||
def scan(str_data, func):
|
||||
func(str_data.split(), frequencies)
|
||||
|
||||
def remove_stop_words(word_list, word_freqs, func):
|
||||
def remove_stop_words(word_list, func):
|
||||
with open('../stop_words.txt') as f:
|
||||
stop_words = f.read().split(',')
|
||||
# add single-letter words
|
||||
stop_words.extend(list(string.ascii_lowercase))
|
||||
func([w for w in word_list if not w in stop_words], word_freqs, sort)
|
||||
func([w for w in word_list if not w in stop_words], sort)
|
||||
|
||||
def frequencies(word_list, word_freqs, func):
|
||||
def frequencies(word_list, func):
|
||||
wf = {}
|
||||
for w in word_list:
|
||||
if w in wf:
|
||||
wf[w] += 1
|
||||
else:
|
||||
wf[w] = 1
|
||||
func(wf, word_freqs, no_op)
|
||||
func(wf, format)
|
||||
|
||||
def sort(wf, word_freqs, func):
|
||||
word_freqs.extend(func(sorted(wf.iteritems(), key=operator.itemgetter(1), reverse=True), None))
|
||||
def sort(wf, func):
|
||||
func(sorted(wf.iteritems(), key=operator.itemgetter(1), reverse=True), print_all)
|
||||
|
||||
def no_op(a, func):
|
||||
return a
|
||||
def no_op(func):
|
||||
return
|
||||
|
||||
def format(word_freqs, func):
|
||||
text = ""
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
text = text + w + ' - ' + str(c) + '\n'
|
||||
func(text, no_op)
|
||||
|
||||
def print_all(text, func):
|
||||
print text
|
||||
func(None)
|
||||
|
||||
#
|
||||
# The main function
|
||||
#
|
||||
word_freqs = []
|
||||
read_file(sys.argv[1], word_freqs, filter_chars)
|
||||
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
|
||||
read_file(sys.argv[1], filter_chars)
|
||||
Reference in New Issue
Block a user