From 636f4ca85579f9c8157ff9e0fbe51f7f5ff11bb7 Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Thu, 26 Dec 2013 10:18:19 -0800 Subject: [PATCH] Minor --- 14-letterbox/tf-14.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/14-letterbox/tf-14.py b/14-letterbox/tf-14.py index 38906b2..bdd7696 100755 --- a/14-letterbox/tf-14.py +++ b/14-letterbox/tf-14.py @@ -1,5 +1,4 @@ #!/usr/bin/env python - import sys, re, operator, string class DataStorageManager(): @@ -15,20 +14,16 @@ class DataStorageManager(): raise Exception("Message not understood " + message[0]) def _init(self, path_to_file): - f = open(path_to_file) - self._data = f.read() - f.close() + with open(path_to_file) as f: + self._data = f.read() pattern = re.compile('[\W_]+') self._data = pattern.sub(' ', self._data).lower() def _words(self): - """ - Returns the list words in storage - """ + """ Returns the list words in storage""" data_str = ''.join(self._data) return data_str.split() - class StopWordManager(): """ Models the stop word filter """ _stop_words = [] @@ -42,9 +37,8 @@ class StopWordManager(): raise Exception("Message not understood " + message[0]) def _init(self): - f = open('../stop_words.txt') - self._stop_words = f.read().split(',') - f.close() + with open('../stop_words.txt') as f: + self._stop_words = f.read().split(',') self._stop_words.extend(list(string.ascii_lowercase)) def _is_stop_word(self, word): @@ -94,8 +88,8 @@ class WordFrequencyController(): 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] + for (w, c) in word_freqs[0:25]: + print w, ' - ', c # # The main function