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
|
# 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:
|
with open(path_to_file) as f:
|
||||||
data = f.read()
|
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_]+')
|
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):
|
def normalize(str_data, func):
|
||||||
func(str_data.lower(), word_freqs, remove_stop_words)
|
func(str_data.lower(), remove_stop_words)
|
||||||
|
|
||||||
def scan(str_data, word_freqs, func):
|
def scan(str_data, func):
|
||||||
func(str_data.split(), word_freqs, frequencies)
|
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:
|
with open('../stop_words.txt') as f:
|
||||||
stop_words = f.read().split(',')
|
stop_words = f.read().split(',')
|
||||||
# add single-letter words
|
# add single-letter words
|
||||||
stop_words.extend(list(string.ascii_lowercase))
|
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 = {}
|
wf = {}
|
||||||
for w in word_list:
|
for w in word_list:
|
||||||
if w in wf:
|
if w in wf:
|
||||||
wf[w] += 1
|
wf[w] += 1
|
||||||
else:
|
else:
|
||||||
wf[w] = 1
|
wf[w] = 1
|
||||||
func(wf, word_freqs, no_op)
|
func(wf, format)
|
||||||
|
|
||||||
def sort(wf, word_freqs, func):
|
def sort(wf, func):
|
||||||
word_freqs.extend(func(sorted(wf.iteritems(), key=operator.itemgetter(1), reverse=True), None))
|
func(sorted(wf.iteritems(), key=operator.itemgetter(1), reverse=True), print_all)
|
||||||
|
|
||||||
def no_op(a, func):
|
def no_op(func):
|
||||||
return a
|
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
|
# The main function
|
||||||
#
|
#
|
||||||
word_freqs = []
|
read_file(sys.argv[1], filter_chars)
|
||||||
read_file(sys.argv[1], word_freqs, filter_chars)
|
|
||||||
|
|
||||||
for (w, c) in word_freqs[0:25]:
|
|
||||||
print w, ' - ', c
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user