From c2196f57dae217b2970e8c60d9bdabb463292588 Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Sun, 29 Dec 2013 20:59:09 -0800 Subject: [PATCH] Deleted unneeded comments and checks --- 18-no-commitment/README.md | 2 +- 18-no-commitment/plugins-src/frequencies1.py | 7 ------ 18-no-commitment/plugins-src/frequencies2.py | 4 --- 18-no-commitment/plugins-src/words1.py | 26 +++----------------- 18-no-commitment/plugins-src/words2.py | 5 ---- 18-no-commitment/tf-18.py | 9 ++----- 6 files changed, 7 insertions(+), 46 deletions(-) diff --git a/18-no-commitment/README.md b/18-no-commitment/README.md index baf49a7..7671df3 100644 --- a/18-no-commitment/README.md +++ b/18-no-commitment/README.md @@ -26,4 +26,4 @@ Possible names: - No commitment - Plugins -- Why one when we can have many? +- Dependency injection diff --git a/18-no-commitment/plugins-src/frequencies1.py b/18-no-commitment/plugins-src/frequencies1.py index 5171cbb..935b2ed 100644 --- a/18-no-commitment/plugins-src/frequencies1.py +++ b/18-no-commitment/plugins-src/frequencies1.py @@ -1,13 +1,6 @@ import operator def top25(word_list): - """ - Takes a list of words and returns a dictionary associating - words with frequencies of occurrence - """ - if type(word_list) is not list or word_list == []: - return {} - word_freqs = {} for w in word_list: if w in word_freqs: diff --git a/18-no-commitment/plugins-src/frequencies2.py b/18-no-commitment/plugins-src/frequencies2.py index 016ce66..5d1f008 100644 --- a/18-no-commitment/plugins-src/frequencies2.py +++ b/18-no-commitment/plugins-src/frequencies2.py @@ -1,10 +1,6 @@ import operator, collections def top25(word_list): - """ - Takes a list of words and returns a dictionary associating - words with frequencies of occurrence - """ counts = collections.Counter(w for w in word_list) return counts.most_common(25) diff --git a/18-no-commitment/plugins-src/words1.py b/18-no-commitment/plugins-src/words1.py index b90594e..c5abca2 100644 --- a/18-no-commitment/plugins-src/words1.py +++ b/18-no-commitment/plugins-src/words1.py @@ -1,31 +1,13 @@ import sys, re, string def extract_words(path_to_file): - """ - Takes a path to a file and returns the non-stop - words, after properly removing nonalphanumeric chars - and normalizing for lower case - """ - if type(path_to_file) is not str or not path_to_file: - return [] - - try: - with open(path_to_file) as f: - str_data = f.read() - except IOError as e: - print "I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror) - return [] - + with open(path_to_file) as f: + str_data = f.read() pattern = re.compile('[\W_]+') word_list = pattern.sub(' ', str_data).lower().split() - try: - with open('../stop_words.txt') as f: - stop_words = f.read().split(',') - except IOError as e: - print "I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror) - return [] - + with open('../stop_words.txt') as f: + stop_words = f.read().split(',') stop_words.extend(list(string.ascii_lowercase)) return [w for w in word_list if not w in stop_words] diff --git a/18-no-commitment/plugins-src/words2.py b/18-no-commitment/plugins-src/words2.py index f314f22..47e3e9c 100644 --- a/18-no-commitment/plugins-src/words2.py +++ b/18-no-commitment/plugins-src/words2.py @@ -1,11 +1,6 @@ import sys, re, string def extract_words(path_to_file): - """ - Takes a path to a file and returns the non-stop - words, after properly removing nonalphanumeric chars - and normalizing for lower case - """ words = re.findall('[a-z]{2,}', open(path_to_file).read().lower()) stopwords = set(open('../stop_words.txt').read().split(',')) return [w for w in words if w not in stopwords] diff --git a/18-no-commitment/tf-18.py b/18-no-commitment/tf-18.py index 13d8b46..aaf752c 100755 --- a/18-no-commitment/tf-18.py +++ b/18-no-commitment/tf-18.py @@ -1,5 +1,4 @@ #!/usr/bin/env python - import sys, ConfigParser, imp def load_plugins(): @@ -11,13 +10,9 @@ def load_plugins(): tfwords = imp.load_compiled('tfwords', words_plugin) tffreqs = imp.load_compiled('tffreqs', frequencies_plugin) -# -# The main function -# - load_plugins() word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1])) -for tf in word_freqs: - print tf[0], ' - ', tf[1] +for (w, c) in word_freqs: + print w, ' - ', c