Skip to content

Commit 4c9270c

Browse files
committed
updated add to idtodoc method
1 parent 446fd9a commit 4c9270c

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

Search_Engine/backend.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sqlite3
22
import test_data
3+
import ast
34

45
class SearchEngine:
56
"""
@@ -11,7 +12,7 @@ class SearchEngine:
1112

1213
def __init__(self):
1314
"""
14-
Return - None
15+
Returns - None
1516
Input - None
1617
----------
1718
- Initialize database. we use sqlite3
@@ -20,18 +21,19 @@ def __init__(self):
2021
connection object
2122
"""
2223
self.conn = sqlite3.connect("searchengine.db")
23-
cur = self.conn.cursor()
24-
res = cur.execute("SELECT name FROM sqlite_master WHERE name='IdToDoc'")
24+
self.cur = self.conn.cursor()
25+
res = self.cur.execute("SELECT name FROM sqlite_master WHERE name='IdToDoc'")
2526
tables_exist = res.fetchone()
26-
# tables_exist = res.fetchall()
27+
2728
if not tables_exist:
2829
self.conn.execute("CREATE TABLE IdToDoc(id INTEGER PRIMARY KEY, document TEXT)")
29-
self.conn.execute('CREATE TABLE WordToId (store TEXT)')
30+
self.conn.execute('CREATE TABLE WordToId (name TEXT, value TEXT)')
31+
self.cur.execute("INSERT INTO WordToId VALUES (?, ?)", ("index", "{}",))
3032
# self.conn.commit()
3133

3234
# cur.execute("INSERT INTO DocumentStore (document) VALUES (?)", (document1,))
3335
# self.conn.commit()
34-
res = cur.execute("SELECT name FROM sqlite_master")
36+
res = self.cur.execute("SELECT name FROM sqlite_master")
3537
print(res.fetchall())
3638
# self.index = test_data['documents'][:-1]
3739
#
@@ -51,15 +53,16 @@ def index_document(self, document):
5153
- uses the id to call the method that adds the words of
5254
the document to the index WordToId
5355
"""
54-
self._add_to_IdToDoc(document)
55-
# self._add_to_WordToId(document)
56-
# doc_num = 1
57-
# for word in document:
58-
# if word not in self.index:
59-
# self.index[word] = set([doc_num])
60-
# else:
61-
# self.index.add(doc_num)
62-
# print(self.index)
56+
row_id = self._add_to_IdToDoc(document)
57+
reverse_idx = self.cur.execute("SELECT value FROM WordToId WHERE name='index'").fetchone()[0]
58+
reverse_idx = ast.literal_eval(reverse_idx)
59+
document = document.split()
60+
for word in document:
61+
if word not in reverse_idx:
62+
reverse_idx[word] = set([row_id])
63+
else:
64+
reverse_idx.add(row_id)
65+
print(reverse_idx)
6366

6467
def _add_to_IdToDoc(self, document):
6568
"""
@@ -83,6 +86,6 @@ def _search_index(self):
8386

8487
if __name__ == "__main__":
8588
se = SearchEngine()
86-
se.index_document("we should all strive to be happy")
89+
se.index_document("we should all strive to be happy and happy again")
8790
se.index_document("happiness is all you need")
8891
se.index_document("no way should we be sad")

0 commit comments

Comments
 (0)