From 9a7f21e3c0aa7b16c2549087f0b85197985831e8 Mon Sep 17 00:00:00 2001 From: Kamyar Dabiri Date: Tue, 2 Feb 2021 22:54:16 -0800 Subject: [PATCH 1/2] fixed the end of line bug --- 09-kick-forward/tf-09.py | 2 +- 10-the-one/tf-10.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/09-kick-forward/tf-09.py b/09-kick-forward/tf-09.py index b07fe70..6d2ccd8 100755 --- a/09-kick-forward/tf-09.py +++ b/09-kick-forward/tf-09.py @@ -21,7 +21,7 @@ def scan(str_data, func): def remove_stop_words(word_list, func): with open('../stop_words.txt') as f: - stop_words = f.read().split(',') + stop_words = f.read().strip('\n').split(',') # add single-letter words stop_words.extend(list(string.ascii_lowercase)) func([w for w in word_list if not w in stop_words], sort) diff --git a/10-the-one/tf-10.py b/10-the-one/tf-10.py index 29f78c0..b9905f1 100755 --- a/10-the-one/tf-10.py +++ b/10-the-one/tf-10.py @@ -35,7 +35,7 @@ def scan(str_data): def remove_stop_words(word_list): with open('../stop_words.txt') as f: - stop_words = f.read().split(',') + stop_words = f.read().strip('\n').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] From 275bb3171b00fabcfb806fd930ada6140bccb1c4 Mon Sep 17 00:00:00 2001 From: Kamyar Dabiri Date: Tue, 16 Feb 2021 16:51:42 -0800 Subject: [PATCH 2/2] fixed reading stop word yours --- 28-lazy-rivers/tf-28.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/28-lazy-rivers/tf-28.py b/28-lazy-rivers/tf-28.py index 21460c3..59ffad1 100755 --- a/28-lazy-rivers/tf-28.py +++ b/28-lazy-rivers/tf-28.py @@ -1,11 +1,15 @@ #!/usr/bin/env python -import sys, operator, string +import sys +import operator +import string + def characters(filename): for line in open(filename): for c in line: yield c + def all_words(filename): start_char = True for c in characters(filename): @@ -15,7 +19,8 @@ def all_words(filename): # We found the start of a word word = c.lower() start_char = False - else: pass + else: + pass else: if c.isalnum(): word += c.lower() @@ -24,12 +29,15 @@ def all_words(filename): start_char = True yield word + def non_stop_words(filename): - stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase)) + stopwords = set(open( + '../stop_words.txt').read().strip('\n').split(',') + list(string.ascii_lowercase)) for w in all_words(filename): if not w in stopwords: yield w + def count_and_sort(filename): freqs, i = {}, 1 for w in non_stop_words(filename): @@ -38,6 +46,8 @@ def count_and_sort(filename): yield sorted(freqs.items(), key=operator.itemgetter(1), reverse=True) i = i+1 yield sorted(freqs.items(), key=operator.itemgetter(1), reverse=True) + + # # The main function # @@ -45,4 +55,3 @@ for word_freqs in count_and_sort(sys.argv[1]): print("-----------------------------") for (w, c) in word_freqs[0:25]: print(w, '-', c) -