1
1
import sqlite3
2
2
import test_data
3
3
import ast
4
+ import json
4
5
5
6
class SearchEngine :
6
7
"""
@@ -26,8 +27,8 @@ def __init__(self):
26
27
tables_exist = res .fetchone ()
27
28
28
29
if not tables_exist :
29
- self .conn .execute ("CREATE TABLE IdToDoc(id INTEGER PRIMARY KEY, document TEXT)" )
30
- self .conn .execute ('CREATE TABLE WordToId (name TEXT, value TEXT)' )
30
+ self .cur .execute ("CREATE TABLE IdToDoc(id INTEGER PRIMARY KEY, document TEXT)" )
31
+ self .cur .execute ('CREATE TABLE WordToId (name TEXT, value TEXT)' )
31
32
self .cur .execute ("INSERT INTO WordToId VALUES (?, ?)" , ("index" , "{}" ,))
32
33
# self.conn.commit()
33
34
@@ -55,13 +56,16 @@ def index_document(self, document):
55
56
"""
56
57
row_id = self ._add_to_IdToDoc (document )
57
58
reverse_idx = self .cur .execute ("SELECT value FROM WordToId WHERE name='index'" ).fetchone ()[0 ]
58
- reverse_idx = ast . literal_eval (reverse_idx )
59
+ reverse_idx = json . loads (reverse_idx )
59
60
document = document .split ()
60
61
for word in document :
61
62
if word not in reverse_idx :
62
- reverse_idx [word ] = set ( [row_id ])
63
+ reverse_idx [word ] = [row_id ]
63
64
else :
64
- reverse_idx .add (row_id )
65
+ if row_id not in reverse_idx [word ]: # incase the word has already been indexed
66
+ reverse_idx [word ].append (row_id )
67
+ reverse_idx = json .dumps (reverse_idx )
68
+ self .cur .execute ("UPDATE WordToId SET value = (?) WHERE name='index'" , (reverse_idx ,))
65
69
print (reverse_idx )
66
70
67
71
def _add_to_IdToDoc (self , document ):
@@ -73,7 +77,7 @@ def _add_to_IdToDoc(self, document):
73
77
into the db
74
78
- retrieve and return the row id of the inserted document
75
79
"""
76
- res = self .conn .execute ("INSERT INTO IdToDoc (document) VALUES (?)" , (document ,))
80
+ res = self .cur .execute ("INSERT INTO IdToDoc (document) VALUES (?)" , (document ,))
77
81
return res .lastrowid
78
82
79
83
0 commit comments