Made the recursive program a bit simpler
This commit is contained in:
@@ -11,29 +11,24 @@ def count(word_list, stopwords, wordfreqs):
|
|||||||
# What to do with an empty list
|
# What to do with an empty list
|
||||||
if word_list == []:
|
if word_list == []:
|
||||||
return
|
return
|
||||||
# The base case, what to do with 1 word
|
# The inductive case, what to do with a list of words
|
||||||
if len(word_list) == 1:
|
else:
|
||||||
|
# Process the head word
|
||||||
word = word_list[0]
|
word = word_list[0]
|
||||||
if word not in stopwords:
|
if word not in stopwords:
|
||||||
if word in word_freqs:
|
if word in word_freqs:
|
||||||
wordfreqs[word] += 1
|
wordfreqs[word] += 1
|
||||||
else:
|
else:
|
||||||
wordfreqs[word] = 1
|
wordfreqs[word] = 1
|
||||||
# The inductive case, what to do with a list of words
|
|
||||||
else:
|
|
||||||
# Process the head word
|
|
||||||
count([word_list[0]], stopwords, wordfreqs)
|
|
||||||
# Process the tail
|
# Process the tail
|
||||||
count(word_list[1:], stopwords, wordfreqs)
|
count(word_list[1:], stopwords, wordfreqs)
|
||||||
|
|
||||||
def wf_print(wordfreq):
|
def wf_print(wordfreq):
|
||||||
if wordfreq == []:
|
if wordfreq == []:
|
||||||
return
|
return
|
||||||
if len(wordfreq) == 1:
|
else:
|
||||||
(w, c) = wordfreq[0]
|
(w, c) = wordfreq[0]
|
||||||
print w, '-', c
|
print w, '-', c
|
||||||
else:
|
|
||||||
wf_print([wordfreq[0]])
|
|
||||||
wf_print(wordfreq[1:])
|
wf_print(wordfreq[1:])
|
||||||
|
|
||||||
stop_words = set(open('../stop_words.txt').read().split(','))
|
stop_words = set(open('../stop_words.txt').read().split(','))
|
||||||
|
|||||||
Reference in New Issue
Block a user