forked from capjamesg/build-a-search-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (52 loc) · 1.47 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import string
import time
documents = [
{
"title": "tolerate it",
"lyric": "I made you my temple, my mural, my sky"
},
{
"title": "my tears ricochet",
"lyric": "And I still talk to you when I'm screaming at the sky",
},
{
"title": "The Bolter",
"lyric": "Started with a kiss"
}
]
def transform_text(text):
return text.lower().translate(str.maketrans("", "", string.punctuation))
index = {}
for i, doc in enumerate(documents):
lyric = transform_text(doc["lyric"])
for word in lyric.split():
if word not in index:
index[word] = set({})
index[word].add(i)
def search(query):
words = transform_text(query).split()
results = set()
for word in words:
if word in index:
results.update(index[word])
titles = [documents[idx] for idx in results]
return titles
start = time.time()
for _ in range(20):
search("sky")
end = time.time()
print("Time taken for index:", end - start)
def search_by_words(query):
lyric = transform_text(query).split()
results = []
for word in lyric:
for doc in documents:
if word in transform_text(doc["lyric"]):
results.append(doc)
titles = [doc["title"] for doc in results]
return titles
start = time.time()
for _ in range(20):
search_by_words("sky")
end = time.time()
print("Time taken for manual comparisons:", end - start)