Skip to content

Instantly share code, notes, and snippets.

@aparrish
Last active December 15, 2015 12:09
Show Gist options
  • Save aparrish/5258567 to your computer and use it in GitHub Desktop.
Save aparrish/5258567 to your computer and use it in GitHub Desktop.
making a "concordance" module—the wrong way to do it
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