title: Concordance with line numbers
body: <p>Write a program that creates a concordance file - an index that tells you on which line of the file each word appears.   Call the function concord, and accept the input file name as an argument.  Write the output to a file called concord.txt.  If a word is on more than one line, the concordance will show you all of the lines containing that word.   Hint:  Use a dictionary keyed by each word to solve this problem, e.g.</p>
  
  <p>Input file contains:</p>
  
  <pre class="lang-none prettyprint-override"><code>I went to the restaurant yesterday.  Hello, I said, to the man who
  greeted me at the door.  Where is your restroom?  On my way to the
  restroom, I bumped into the waiter boy.  Excuse me, sir, I said.
  When I returned to the table, the meal was served.  These are the
  best clams I have ever eaten, I said.  My compliments to the chef.
  Unfortunately, I was arrested by the officer for not paying my bill.
  </code></pre>
  
  <p>Produces:</p>
  
  <pre class="lang-none prettyprint-override"><code>clams  [5]
     is  [2]
   chef  [5]
   ever  [5]
     at  [2]
   have  [5]
  table  [4]
   your  [2]
   best  [5]
    sir  [3]
   said  [1, 3, 5]
    for  [6]
    boy  [3]
   when  [4]
     by  [6]
     to  [1, 2, 4, 5]
    way  [2]
    was  [4, 6]
    ...
  </code></pre>
  
  <p>L08-8)  (5 points)
  Same code as above, but words in the concordance should be sorted when you print them.</p>
  
  <p>I have this so far, but it gives me the number of times the word appears not the line numbers.</p>
  
  <p>Python Code:</p>
  
  <pre><code>def main():
     """
         Main function
     """
     try:
         # Opening file for reading
         fp = open("d:\Python\input.txt");
  
         # Dictionary to hold words and their frequencies
         wordDict = {};
  
         # Reading data line by line
         for line in fp:
             # Splitting words on space
             words = line.split(" ");
  
             # Looping over each word in words
             for word in words:
  
                 # Considering only the words with length at-least 1
                 if len(word) &gt; 0:
                     # Converting to lower case
                     word = word.lower();
  
                     # Checking for existence of key in dict
                     if word in wordDict.keys():
                         # If already present, just update frequency
                         wordDict[word] += 1;
                     else:
                         # If new word, updating existing value
                         wordDict[word] = 1;
  
         # Closing file
         fp.close();
  
         # Looping over sorted keys of dictionary
         for key in sorted(wordDict):
             # Printing word frequency values
             print(" {0} : {1} ".format(key, wordDict[key]));
  
     except Exception as ex:
         # Handling exceptions
         print(ex);
  
  # Calling main function      
  main();
  </code></pre>