More inductive for infinite mirror
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
import re, sys, operator
|
import re, sys, operator
|
||||||
|
|
||||||
# Mileage may vary. If this crashes, make it lower
|
# Mileage may vary. If this crashes, make it lower
|
||||||
@@ -7,16 +8,23 @@ RECURSION_LIMIT = 9500
|
|||||||
sys.setrecursionlimit(RECURSION_LIMIT+10)
|
sys.setrecursionlimit(RECURSION_LIMIT+10)
|
||||||
|
|
||||||
def count(word_list, word_freqs):
|
def count(word_list, word_freqs):
|
||||||
|
# What to do with an empty list
|
||||||
if word_list == []:
|
if word_list == []:
|
||||||
return
|
return
|
||||||
|
# The base case, what to do with 1 word
|
||||||
if word_list[0] not in stopwords:
|
if len(word_list) == 1:
|
||||||
if word_list[0] in word_freqs:
|
word = word_list[0]
|
||||||
word_freqs[word_list[0]] += 1
|
if word not in stopwords:
|
||||||
else:
|
if word in word_freqs:
|
||||||
word_freqs[word_list[0]] = 1
|
word_freqs[word] += 1
|
||||||
|
else:
|
||||||
count(word_list[1:], word_freqs)
|
word_freqs[word] = 1
|
||||||
|
# The inductive case, what to do with a list of words
|
||||||
|
else:
|
||||||
|
# Process the head word
|
||||||
|
count([word_list[0]], word_freqs)
|
||||||
|
# Process the tail
|
||||||
|
count(word_list[1:], word_freqs)
|
||||||
|
|
||||||
stopwords = set(open('../stop_words.txt').read().split(','))
|
stopwords = set(open('../stop_words.txt').read().split(','))
|
||||||
words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower())
|
words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower())
|
||||||
|
|||||||
Reference in New Issue
Block a user