From 49655700e19bca80b7ed5076e94ef2bf1e7f4a52 Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Sun, 22 Sep 2013 11:09:51 -0700 Subject: [PATCH] Added style #8 --- 08-letterbox/README.md | 20 ++++++++ 08-letterbox/tf-08.py | 104 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 08-letterbox/README.md create mode 100644 08-letterbox/tf-08.py diff --git a/08-letterbox/README.md b/08-letterbox/README.md new file mode 100644 index 0000000..e00c41c --- /dev/null +++ b/08-letterbox/README.md @@ -0,0 +1,20 @@ +Style #8 +============================== + +Constraints: + +- The larger problem is decomposed into 'things' that make sense for + the problem domain + +- Each 'thing' is a capsule of data that exposes one single procedure, + namely the ability to receive and dispatch messages that are sent to + it + +- Message dispatch can result in sending the message to another capsule + +Possible names: + +- Letterbox +- Messaging style +- Objects +- Actors diff --git a/08-letterbox/tf-08.py b/08-letterbox/tf-08.py new file mode 100644 index 0000000..d0a37aa --- /dev/null +++ b/08-letterbox/tf-08.py @@ -0,0 +1,104 @@ +import sys, re, operator, string + +class DataStorageManager(): + """ Models the contents of the file """ + _data = '' + + def dispatch(self, message): + if message[0] == 'init': + return self._init(message[1]) + elif message[0] == 'words': + return self._words() + else: + raise Exception("Message not understood " + message[0]) + + def _init(self, path_to_file): + f = open(path_to_file) + self._data = f.read() + f.close() + pattern = re.compile('[\W_]+') + self._data = pattern.sub(' ', self._data).lower() + + def _words(self): + """ + Returns the list words in storage + """ + data_str = ''.join(self._data) + return data_str.split() + + +class StopWordManager(): + """ Models the stop word filter """ + _stop_words = [] + + def dispatch(self, message): + if message[0] == 'init': + return self._init() + elif message[0] == 'is_stop_word': + return self._is_stop_word(message[1]) + else: + raise Exception("Message not understood " + message[0]) + + def _init(self): + f = open('../stop_words.txt') + self._stop_words = f.read().split(',') + f.close() + self._stop_words.extend(list(string.ascii_lowercase)) + + def _is_stop_word(self, word): + return word in self._stop_words + +class WordFrequencyManager(): + """ Keeps the word frequency data """ + _word_freqs = {} + + def dispatch(self, message): + if message[0] == 'increment_count': + return self._increment_count(message[1]) + elif message[0] == 'sorted': + return self._sorted() + else: + raise Exception("Message not understood " + message[0]) + + def _increment_count(self, word): + if word in self._word_freqs: + self._word_freqs[word] += 1 + else: + self._word_freqs[word] = 1 + + def _sorted(self): + return sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) + +class WordFrequencyController(): + + def dispatch(self, message): + if message[0] == 'init': + return self._init(message[1]) + elif message[0] == 'run': + return self._run() + else: + raise Exception("Message not understood " + message[0]) + + def _init(self, path_to_file): + self._storage_manager = DataStorageManager() + self._stop_word_manager = StopWordManager() + self._word_freq_manager = WordFrequencyManager() + self._storage_manager.dispatch(['init', path_to_file]) + self._stop_word_manager.dispatch(['init']) + + def _run(self): + for w in self._storage_manager.dispatch(['words']): + if not self._stop_word_manager.dispatch(['is_stop_word', w]): + self._word_freq_manager.dispatch(['increment_count', w]) + + word_freqs = self._word_freq_manager.dispatch(['sorted']) + for tf in word_freqs[0:25]: + print tf[0], ' - ', tf[1] + +# +# The main function +# +wfcontroller = WordFrequencyController() +wfcontroller.dispatch(['init', sys.argv[1]]) +wfcontroller.dispatch(['run']) +