Massive renaming!
This commit is contained in:
21
23-passive-aggressive/README.md
Normal file
21
23-passive-aggressive/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
Style #22
|
||||
==============================
|
||||
|
||||
Constraints:
|
||||
|
||||
- Every single procedure and function checks the sanity of its
|
||||
arguments and refuses to continue when the arguments are
|
||||
unreasonable, jumping out of the function
|
||||
|
||||
- When calling out other functions, program functions only check for
|
||||
errors if they are in a position to react meaningully
|
||||
|
||||
- Error handling occurs at higher levels of function call chains,
|
||||
wherever it is meaningul to do so
|
||||
|
||||
Possible names:
|
||||
|
||||
- Passive aggressive
|
||||
- Exception
|
||||
|
||||
|
||||
84
23-passive-aggressive/tf-23-monadic.py
Normal file
84
23-passive-aggressive/tf-23-monadic.py
Normal file
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
# The PassiveAggressive class for this example
|
||||
#
|
||||
class TFPassiveAggressive:
|
||||
def __init__(self):
|
||||
self._e = None
|
||||
self._offending_func = None
|
||||
self._value = None
|
||||
|
||||
def bind(self, func):
|
||||
if self._e == None:
|
||||
try:
|
||||
self._value = func(self._value)
|
||||
except Exception as e:
|
||||
self._e = e
|
||||
self._offending_func = func
|
||||
return self
|
||||
|
||||
def printme(self):
|
||||
if self._e == None:
|
||||
print(self._value)
|
||||
else:
|
||||
print(self._e, " in ", self._offending_func.__name__)
|
||||
|
||||
#
|
||||
# The functions
|
||||
#
|
||||
def get_input(ignore):
|
||||
assert(len(sys.argv) > 1), "You idiot! I need an input file! I quit!"
|
||||
return sys.argv[1]
|
||||
|
||||
def extract_words(path_to_file):
|
||||
assert(type(path_to_file) is str), "I need a string! I quit!"
|
||||
assert(path_to_file), "I need a non-empty string! I quit!"
|
||||
|
||||
with open(path_to_file) as f:
|
||||
data = f.read()
|
||||
pattern = re.compile('[\W_]+')
|
||||
word_list = pattern.sub(' ', data).lower().split()
|
||||
return word_list
|
||||
|
||||
def remove_stop_words(word_list):
|
||||
assert(type(word_list) is list), "I need a list! I quit!"
|
||||
|
||||
with open('../stop_words.txt') as f:
|
||||
stop_words = f.read().split(',')
|
||||
# add single-letter words
|
||||
stop_words.extend(list(string.ascii_lowercase))
|
||||
return [w for w in word_list if not w in stop_words]
|
||||
|
||||
def frequencies(word_list):
|
||||
assert(type(word_list) is list), "I need a list! I quit!"
|
||||
assert(word_list != []), "I need a non-empty list! I quit!"
|
||||
|
||||
word_freqs = {}
|
||||
for w in word_list:
|
||||
if w in word_freqs:
|
||||
word_freqs[w] += 1
|
||||
else:
|
||||
word_freqs[w] = 1
|
||||
return word_freqs
|
||||
|
||||
def sort(word_freqs):
|
||||
assert(type(word_freqs) is dict), "I need a dictionary! I quit!"
|
||||
assert(word_freqs != {}), "I need a non-empty dictionary! I quit!"
|
||||
|
||||
return sorted(word_freqs.items(), key=operator.itemgetter(1), reverse=True)
|
||||
|
||||
def top25_freqs(word_freqs):
|
||||
assert(type(word_freqs) is list), "I need a list! I quit!"
|
||||
assert(word_freqs != {}), "I need a non-empty dictionary! I quit!"
|
||||
|
||||
top25 = ""
|
||||
for tf in word_freqs[0:25]:
|
||||
top25 += str(tf[0]) + ' - ' + str(tf[1]) + '\n'
|
||||
return top25
|
||||
|
||||
#
|
||||
# The main function
|
||||
#
|
||||
TFPassiveAggressive().bind(get_input).bind(extract_words).bind(remove_stop_words).bind(frequencies).bind(sort).bind(top25_freqs).printme()
|
||||
57
23-passive-aggressive/tf-23.py
Normal file
57
23-passive-aggressive/tf-23.py
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
# The functions
|
||||
#
|
||||
def extract_words(path_to_file):
|
||||
assert(type(path_to_file) is str), "I need a string! I quit!"
|
||||
assert(path_to_file), "I need a non-empty string! I quit!"
|
||||
|
||||
with open(path_to_file) as f:
|
||||
data = f.read()
|
||||
pattern = re.compile('[\W_]+')
|
||||
word_list = pattern.sub(' ', data).lower().split()
|
||||
return word_list
|
||||
|
||||
def remove_stop_words(word_list):
|
||||
assert(type(word_list) is list), "I need a list! I quit!"
|
||||
|
||||
with open('../stop_words.txt') as f:
|
||||
stop_words = f.read().split(',')
|
||||
# add single-letter words
|
||||
stop_words.extend(list(string.ascii_lowercase))
|
||||
return [w for w in word_list if not w in stop_words]
|
||||
|
||||
def frequencies(word_list):
|
||||
assert(type(word_list) is list), "I need a list! I quit!"
|
||||
assert(word_list != []), "I need a non-empty list! I quit!"
|
||||
|
||||
word_freqs = {}
|
||||
for w in word_list:
|
||||
if w in word_freqs:
|
||||
word_freqs[w] += 1
|
||||
else:
|
||||
word_freqs[w] = 1
|
||||
return word_freqs
|
||||
|
||||
def sort(word_freqs):
|
||||
assert(type(word_freqs) is dict), "I need a dictionary! I quit!"
|
||||
assert(word_freqs != {}), "I need a non-empty dictionary! I quit!"
|
||||
|
||||
return sorted(word_freqs.items(), key=operator.itemgetter(1), reverse=True)
|
||||
|
||||
#
|
||||
# The main function
|
||||
#
|
||||
try:
|
||||
assert(len(sys.argv) > 1), "You idiot! I need an input file! I quit!"
|
||||
word_freqs = sort(frequencies(remove_stop_words(extract_words(sys.argv[1]))))
|
||||
|
||||
assert(len(word_freqs) > 25), "OMG! Less than 25 words! I QUIT!"
|
||||
for tf in word_freqs[0:25]:
|
||||
print(tf[0], ' - ', tf[1])
|
||||
except Exception as e:
|
||||
print("Something wrong: {0}".format(e))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user