1
1
import sqlite3
2
2
import test_data
3
+ import ast
3
4
4
5
class SearchEngine :
5
6
"""
@@ -11,7 +12,7 @@ class SearchEngine:
11
12
12
13
def __init__ (self ):
13
14
"""
14
- Return - None
15
+ Returns - None
15
16
Input - None
16
17
----------
17
18
- Initialize database. we use sqlite3
@@ -20,18 +21,19 @@ def __init__(self):
20
21
connection object
21
22
"""
22
23
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'" )
25
26
tables_exist = res .fetchone ()
26
- # tables_exist = res.fetchall()
27
+
27
28
if not tables_exist :
28
29
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" , "{}" ,))
30
32
# self.conn.commit()
31
33
32
34
# cur.execute("INSERT INTO DocumentStore (document) VALUES (?)", (document1,))
33
35
# self.conn.commit()
34
- res = cur .execute ("SELECT name FROM sqlite_master" )
36
+ res = self . cur .execute ("SELECT name FROM sqlite_master" )
35
37
print (res .fetchall ())
36
38
# self.index = test_data['documents'][:-1]
37
39
#
@@ -51,15 +53,16 @@ def index_document(self, document):
51
53
- uses the id to call the method that adds the words of
52
54
the document to the index WordToId
53
55
"""
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 )
63
66
64
67
def _add_to_IdToDoc (self , document ):
65
68
"""
@@ -83,6 +86,6 @@ def _search_index(self):
83
86
84
87
if __name__ == "__main__" :
85
88
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 " )
87
90
se .index_document ("happiness is all you need" )
88
91
se .index_document ("no way should we be sad" )
0 commit comments