Skip to content

Instantly share code, notes, and snippets.

@masahitojp
Created December 1, 2010 00:01
Show Gist options
  • Save masahitojp/722680 to your computer and use it in GitHub Desktop.
Save masahitojp/722680 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def inverted_index(index,no,str):
str_arr =str.split(" ")
for i in range(len(str_arr)):
try:
index[str_arr[i]]
except KeyError:
index[str_arr[i]] = []
index[str_arr[i]].append((no,i))
return index
def search(index,sarr):
if __name__ == "__main__":
str0 = "it is what it is"
str1 = "what is it"
str2 = "it is a banana"
str = [str0,str1,str2]
index = {}
for i in range(len(str)):
index = inverted_index(index,i,str[i])
print index
print search(index,sarr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment