Last active
December 15, 2015 12:09
-
-
Save aparrish/5258567 to your computer and use it in GitHub Desktop.
making a "concordance" module—the wrong way to do it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
concordance = dict() | |
def tokenize(line): | |
return line.split(" ") | |
def feed(line): | |
words = tokenize(line) | |
for word in words: | |
if word not in concordance: | |
concordance[word] = 0 | |
concordance[word] += 1 | |
def count_for_word(word): | |
if word in concordance: | |
return concordance[word] | |
else: | |
return 0 | |
def unique_words(): | |
return concordance.keys() | |
if __name__ == '__main__': | |
import sys | |
check_word = sys.argv[1] | |
for line in sys.stdin: | |
line = line.strip() | |
feed(line) | |
print "count for " + check_word + ": " + str(count_for_word(check_word)) | |
print "unique words: " + str(len(unique_words())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment