Skip to content

Instantly share code, notes, and snippets.

@nathanchen
Forked from shenfeng/inverse-index.py
Last active December 19, 2015 13:29
Show Gist options
  • Select an option

  • Save nathanchen/5962846 to your computer and use it in GitHub Desktop.

Select an option

Save nathanchen/5962846 to your computer and use it in GitHub Desktop.
__author__ = 'feng'
import string
articles = [
'Familiarize Yourself with IntelliJ IDEA editor',
'While keeping the Ctrl key pressed, rotate the mouse wheel. As you rotate the mouse wheel forward',
'font size grows larger; as you rotate the mouse wheel backwards, font size decreases',
'In the popup frame, type Reset font size, and click Enter.',
'These operations apply to the active editor only. In the other editor tabs, font size is not affected.',
'There is no default keyboard shortcut associated with Reset font size action. However, you can create your',
'Place the caret in the editor.'
]
# prune punctuation
table = string.maketrans("","")
article_map = dict(zip(range(len(articles)), articles))
def test_trans(s):
return s.translate(table, string.punctuation)
def create_index():
index = {}
for id, article in article_map.items():
words = article.split()
for word in words:
word = test_trans(word.lower())
if word in index:
index[word].add(id)
else:
index[word] = set([id])
return index
def search_index(query):
keywords = query.split()
index = create_index()
for key, value in index.items():
print key, value
if keywords:
ids = index.get(keywords[0], set())
for q in keywords[1:]:
ids = ids & index.get(q, set())
for id in ids:
print article_map[id]
def main():
search_index("rotate mouse")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment