An Inverted Index is a data structure used to create full text search.



Task

Given a set of text files, implement a program to create an inverted index.

Also create a user interface to do a search using that inverted index which returns a list of files that contain the query term / terms.

The search index can be in memory.




DefaultDict[String, Set[String]] index
 
F parse_file(fname, fcontents)
   L(word) fcontents.split(re:‘\W’)
      :index[word.lowercase()].add(fname)
 
L(fname, fcontents) [(‘inv1.txt’, ‘It is what it is.’),
                     (‘inv2.txt’, ‘What is it?’),
                     (‘inv3.txt’, ‘It is a banana!’)]
   parse_file(fname, fcontents)
 
L(w) [‘cat’, ‘is’, ‘banana’, ‘it’, ‘what’]
   print("\nEnter a word to search for: (q to quit): "w)
   I w C index
      print(‘'#.' found in #..’.format(w, sorted(Array(index[w]))))
   E
      print(‘'#.' not found.’.format(w))