#! /usr/bin/env python
import json

#{ 'document': ['terms', 'terms'],
#  'document2': ['1','2','3'],
#}
def simple_search_engine(index, query):
    return [doc_name for doc_name, terms in index.iteritems() if query in terms]

def weighted_search_engine(index, query):
    results = {}
    search_terms = query.split()
    for doc_name, terms in index.iteritems():
        matches = 0
        for search_term in search_terms:
            if search_term in terms:
                matches += 1

        rating = float(matches) / len(search_terms)
        results[doc_name] = rating

    return results

if __name__ == '__main__':
    search_query = raw_input('Search terms:')
    index = json.load(open('index.txt'))
    print(
        'Simple search engine returns: %s' %
        simple_search_engine(index=index, query=search_query)
    )

    # New line for readability
    print('')

    print(
        'Weighted search engine returns: %s' %
        weighted_search_engine(index=index, query=search_query)
    )

@@ -27,16 +27,31 @@
 ort json
+%0Aimport difflib
 %0A%0A#%7B 'do
@@ -609,16 +609,527 @@
 esults%0A%0A
+def guessy_weighted_search_engine(index, query):%0A    results = %7B%7D%0A    search_terms = query.split()%0A    for doc_name, terms in index.iteritems():%0A        matches = 0%0A        for search_term in search_terms:%0A            search_termys = difflib.get_close_matches(search_term, terms)%0A            for search_termy in search_termys:%0A                if search_termy in terms:%0A                    matches += 1%0A%0A        rating = float(matches) / len(search_terms)%0A        results%5Bdoc_name%5D = rating%0A%0A    return results%0A%0A
 if __nam
@@ -1455,32 +1455,32 @@
  returns: %25s' %25%0A
-
         weighted
@@ -1513,28 +1513,185 @@
 , query=search_query)%0A    )%0A
+%0A    print('')%0A    print(%0A        'Guessy weighted search engine returns: %25s' %25%0A        guessy_weighted_search_engine(index=index, query=search_query)%0A    )%0A

add guessywessyies