Deleted comments in style #08, cos they were actually wrong.

This commit is contained in:
Crista Lopes
2013-11-25 10:45:12 -08:00
parent ac49dfa51a
commit 1ecf5392bd

View File

@@ -1,55 +1,32 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys, re, operator, string import sys, re, operator, string
# #
# The functions # The functions
# #
def read_file(path_to_file, func): def read_file(path_to_file, func):
""" with open(path_to_file) as f:
Takes a path to a file and returns the entire data = f.read()
contents of the file as a string
"""
f = open(path_to_file)
data = f.read()
f.close()
return func(data, normalize) return func(data, normalize)
def filter_chars(str_data, func): def filter_chars(str_data, func):
"""
Takes a string and returns a copy with all nonalphanumeric chars
replaced by white space
"""
pattern = re.compile('[\W_]+') pattern = re.compile('[\W_]+')
return func(pattern.sub(' ', str_data), scan) return func(pattern.sub(' ', str_data), scan)
def normalize(str_data, func): def normalize(str_data, func):
"""
Takes a string and returns a copy with all characters in lower case
"""
return func(str_data.lower(), remove_stop_words) return func(str_data.lower(), remove_stop_words)
def scan(str_data, func): def scan(str_data, func):
"""
Takes a string and scans for words, returning
a list of words.
"""
return func(str_data.split(), frequencies) return func(str_data.split(), frequencies)
def remove_stop_words(word_list, func): def remove_stop_words(word_list, func):
""" Takes a list of words and returns a copy with all stop words removed """ with open('../stop_words.txt') as f:
f = open('../stop_words.txt') stop_words = f.read().split(',')
stop_words = f.read().split(',')
f.close()
# add single-letter words # add single-letter words
stop_words.extend(list(string.ascii_lowercase)) stop_words.extend(list(string.ascii_lowercase))
return func([w for w in word_list if not w in stop_words], sort) return func([w for w in word_list if not w in stop_words], sort)
def frequencies(word_list, func): def frequencies(word_list, func):
"""
Takes a list of words and returns a dictionary associating
words with frequencies of occurrence
"""
word_freqs = {} word_freqs = {}
for w in word_list: for w in word_list:
if w in word_freqs: if w in word_freqs:
@@ -59,11 +36,6 @@ def frequencies(word_list, func):
return func(word_freqs, no_op) return func(word_freqs, no_op)
def sort(word_freq, func): def sort(word_freq, func):
"""
Takes a dictionary of words and their frequencies
and returns a list of pairs where the entries are
sorted by frequency
"""
return func(sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True), None) return func(sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True), None)
def no_op(a, func): def no_op(a, func):
@@ -74,6 +46,6 @@ def no_op(a, func):
# #
word_freqs = read_file(sys.argv[1], filter_chars) word_freqs = read_file(sys.argv[1], filter_chars)
for tf in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print tf[0], ' - ', tf[1] print w, ' - ', c