Created
February 22, 2013 03:31
-
-
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.
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
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