Skip to content

Commit 10fa22d

Browse files
committed
added comment for find_documents method
1 parent 7c05aee commit 10fa22d

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Search_Engine/backend.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,16 @@ def __init__(self):
3030
self.conn.execute("CREATE TABLE IdToDoc(id INTEGER PRIMARY KEY, document TEXT)")
3131
self.conn.execute('CREATE TABLE WordToId (name TEXT, value TEXT)')
3232
cur.execute("INSERT INTO WordToId VALUES (?, ?)", ("index", "{}",))
33-
# self.conn.commit()
3433

35-
# cur.execute("INSERT INTO DocumentStore (document) VALUES (?)", (document1,))
36-
# self.conn.commit()
3734
cur = self.conn.cursor()
38-
res = cur.execute("SELECT name FROM sqlite_master")
35+
# res = cur.execute("SELECT name FROM sqlite_master")
3936
# print(res.fetchall())
4037
# self.index = test_data['documents'][:-1]
4138
#
4239

4340
def index_document(self, document):
4441
"""
45-
Returns -
42+
Returns - <sqlite3.Cursor object>
4643
Input - str: a string of words called document
4744
----------
4845
Indexes the document. It does this by performing two
@@ -53,7 +50,8 @@ def index_document(self, document):
5350
to IdToDoc
5451
- retrieves the id of the inserted document
5552
- uses the id to call the method that adds the words of
56-
the document to the index WordToId
53+
the document to the reverse index WordToId if the word has not
54+
already been indexed
5755
"""
5856
row_id = self._add_to_IdToDoc(document)
5957
cur = self.conn.cursor()
@@ -64,12 +62,12 @@ def index_document(self, document):
6462
if word not in reverse_idx:
6563
reverse_idx[word] = [row_id]
6664
else:
67-
if row_id not in reverse_idx[word]: # incase the word has already been indexed
65+
if row_id not in reverse_idx[word]:
6866
reverse_idx[word].append(row_id)
6967
reverse_idx = json.dumps(reverse_idx)
7068
cur = self.conn.cursor()
71-
cur.execute("UPDATE WordToId SET value = (?) WHERE name='index'", (reverse_idx,))
72-
# print(reverse_idx)
69+
result = cur.execute("UPDATE WordToId SET value = (?) WHERE name='index'", (reverse_idx,))
70+
return(result)
7371

7472
def _add_to_IdToDoc(self, document):
7573
"""
@@ -85,6 +83,16 @@ def _add_to_IdToDoc(self, document):
8583
return res.lastrowid
8684

8785
def find_documents(self, search_term):
86+
"""
87+
Returns - <class method>: the return value of the _find_documents_with_idx method
88+
Input - str: a string of words called `search_term`
89+
---------
90+
- retrieve the reverse index
91+
- use the words contained in the search term to find all the idxs
92+
that contain the word
93+
- use idxs to call the _find_documents_with_idx method
94+
- return the result of the called method
95+
"""
8896
cur = self.conn.cursor()
8997
reverse_idx = cur.execute("SELECT value FROM WordToId WHERE name='index'").fetchone()[0]
9098
reverse_idx = json.loads(reverse_idx)
@@ -104,9 +112,9 @@ def find_documents(self, search_term):
104112
if not common_idx_of_docs: # the search term does not exist
105113
return []
106114

107-
return self._documents_with_idx(common_idx_of_docs)
115+
return self._find_documents_with_idx(common_idx_of_docs)
108116

109-
def _documents_with_idx(self, idxs):
117+
def _find_documents_with_idx(self, idxs):
110118
idxs = list(idxs)
111119
cur = self.conn.cursor()
112120
sql="SELECT document FROM IdToDoc WHERE id in ({seq})".format(
@@ -119,7 +127,7 @@ def _documents_with_idx(self, idxs):
119127
if __name__ == "__main__":
120128
se = SearchEngine()
121129
se.index_document("we should all strive to be happy and happy again")
122-
se.index_document("happiness is all you need")
130+
print(se.index_document("happiness is all you need"))
123131
se.index_document("no way should we be sad")
124132
se.index_document("a cheerful heart is a happy one")
125-
se.find_documents("happy")
133+
print(se.find_documents("happy"))

0 commit comments

Comments
 (0)