Clea dust out of 09
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, re, operator, string
|
import sys, re, operator, string
|
||||||
|
|
||||||
#
|
#
|
||||||
# The monadic class for this example
|
# The One class for this example
|
||||||
#
|
#
|
||||||
class TFTheOne:
|
class TFTheOne:
|
||||||
def __init__(self, v):
|
def __init__(self, v):
|
||||||
@@ -20,49 +19,28 @@ def printm(monad):
|
|||||||
# The functions
|
# The functions
|
||||||
#
|
#
|
||||||
def read_file(path_to_file):
|
def read_file(path_to_file):
|
||||||
"""
|
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 data
|
return data
|
||||||
|
|
||||||
def filter_chars(str_data):
|
def filter_chars(str_data):
|
||||||
"""
|
|
||||||
Takes a string and returns a copy with all nonalphanumeric chars
|
|
||||||
replaced by white space
|
|
||||||
"""
|
|
||||||
pattern = re.compile('[\W_]+')
|
pattern = re.compile('[\W_]+')
|
||||||
return pattern.sub(' ', str_data)
|
return pattern.sub(' ', str_data)
|
||||||
|
|
||||||
def normalize(str_data):
|
def normalize(str_data):
|
||||||
"""
|
|
||||||
Takes a string and returns a copy with all characters in lower case """
|
|
||||||
return str_data.lower()
|
return str_data.lower()
|
||||||
|
|
||||||
def scan(str_data):
|
def scan(str_data):
|
||||||
"""
|
|
||||||
Takes a string and scans for words, returning
|
|
||||||
a list of words.
|
|
||||||
"""
|
|
||||||
return str_data.split()
|
return str_data.split()
|
||||||
|
|
||||||
def remove_stop_words(word_list):
|
def remove_stop_words(word_list):
|
||||||
""" 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 [w for w in word_list if not w in stop_words]
|
return [w for w in word_list if not w in stop_words]
|
||||||
|
|
||||||
def frequencies(word_list):
|
def frequencies(word_list):
|
||||||
"""
|
|
||||||
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:
|
||||||
@@ -72,16 +50,8 @@ def frequencies(word_list):
|
|||||||
return word_freqs
|
return word_freqs
|
||||||
|
|
||||||
def sort(word_freq):
|
def sort(word_freq):
|
||||||
"""
|
|
||||||
Takes a dictionary of words and their frequencies and returns a list
|
|
||||||
of pairs where the entries are sorted by frequency
|
|
||||||
"""
|
|
||||||
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
|
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||||
|
|
||||||
def print_freqs(word_freqs):
|
|
||||||
for tf in word_freqs[0:25]:
|
|
||||||
print tf[0], ' - ', tf[1]
|
|
||||||
|
|
||||||
def top25_freqs(word_freqs):
|
def top25_freqs(word_freqs):
|
||||||
top25 = ""
|
top25 = ""
|
||||||
for tf in word_freqs[0:25]:
|
for tf in word_freqs[0:25]:
|
||||||
|
|||||||
Reference in New Issue
Block a user