Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created February 22, 2013 03:31
Show Gist options
  • Save aparrish/5010509 to your computer and use it in GitHub Desktop.
Save aparrish/5010509 to your computer and use it in GitHub Desktop.
a simple concordance: read a text file from standard input, create a dictionary relating each word in the input text to a list of lines in which that word occurs. when running the program, supply a single parameter on the command line to print out all lines in the input text containing that word.
import sys
concordance = dict()
for line in sys.stdin:
line = line.strip()
line_words = line.split(" ")
for word in line_words:
if word in concordance:
concordance[word].append(line)
else:
concordance[word] = [line]
print concordance
search_word = sys.argv[1]
if search_word in concordance:
print "\n".join(concordance[search_word])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment