Better passive aggressive style -- catch precondition failure too, and treat them all as annoying things that put us in a bad mood.

This commit is contained in:
Crista Lopes
2013-11-30 10:54:06 -08:00
parent 4bb057128d
commit 0b43231f64

View File

@@ -1,6 +1,8 @@
#!/usr/bin/env python
import sys, re, operator, string
angry = "^*(&%#@^! (I don't want this)"
#
# The PassiveAggressive class for this example
#
@@ -14,6 +16,8 @@ class TFPassiveAggressive:
if self._e == None:
try:
self._value = func(self._value)
if self._value == angry:
raise AssertionError(angry)
except Exception as e:
self._e = e
self._offending_func = func
@@ -29,9 +33,15 @@ class TFPassiveAggressive:
# The functions
#
def get_input(arg):
if len(sys.argv) <= 1:
return angry
return sys.argv[1]
def extract_words(path_to_file):
if type(path_to_file) != str or not path_to_file:
return angry
with open(path_to_file) as f:
data = f.read()
pattern = re.compile('[\W_]+')
@@ -39,6 +49,9 @@ def extract_words(path_to_file):
return word_list
def remove_stop_words(word_list):
if type(word_list) != list or not word_list:
return angry
with open('../stop_words.txt') as f:
stop_words = f.read().split(',')
# add single-letter words
@@ -46,6 +59,9 @@ def remove_stop_words(word_list):
return [w for w in word_list if not w in stop_words]
def frequencies(word_list):
if type(word_list) != list or not word_list:
return angry
word_freqs = {}
for w in word_list:
if w in word_freqs:
@@ -54,10 +70,16 @@ def frequencies(word_list):
word_freqs[w] = 1
return word_freqs
def sort(word_freq):
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
def sort(word_freqs):
if type(word_freqs) != dict or not word_freqs:
return angry
return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
def top25_freqs(word_freqs):
if type(word_freqs) != dict or not word_freqs:
return angry
top25 = ""
for tf in word_freqs[0:25]:
top25 += str(tf[0]) + ' - ' + str(tf[1]) + '\n'