It works by building a reverse index store that maps words to an id. To find the document(s) that contain a certain search term, we then take an intersection of the ids
| 3 | |
| 4 | |
| 5 | class SearchEngine: |
| 6 | """ |
| 7 | It works by building a reverse index store that maps |
| 8 | words to an id. To find the document(s) that contain |
| 9 | a certain search term, we then take an intersection |
| 10 | of the ids |
| 11 | """ |
| 12 | |
| 13 | def __init__(self): |
| 14 | """ |
| 15 | Returns - None |
| 16 | Input - None |
| 17 | ---------- |
| 18 | - Initialize database. we use sqlite3 |
| 19 | - Check if the tables exist, if not create them |
| 20 | - maintain a class level access to the database |
| 21 | connection object |
| 22 | """ |
| 23 | self.conn = sqlite3.connect("searchengine.sqlite3", autocommit=True) |
| 24 | cur = self.conn.cursor() |
| 25 | res = cur.execute("SELECT name FROM sqlite_master WHERE name='IdToDoc'") |
| 26 | tables_exist = res.fetchone() |
| 27 | |
| 28 | if not tables_exist: |
| 29 | self.conn.execute( |
| 30 | "CREATE TABLE IdToDoc(id INTEGER PRIMARY KEY, document TEXT)" |
| 31 | ) |
| 32 | self.conn.execute("CREATE TABLE WordToId (name TEXT, value TEXT)") |
| 33 | cur.execute( |
| 34 | "INSERT INTO WordToId VALUES (?, ?)", |
| 35 | ( |
| 36 | "index", |
| 37 | "{}", |
| 38 | ), |
| 39 | ) |
| 40 | |
| 41 | def index_document(self, document): |
| 42 | """ |
| 43 | Returns - string |
| 44 | Input - str: a string of words called document |
| 45 | ---------- |
| 46 | Indexes the document. It does this by performing two |
| 47 | operations - add the document to the IdToDoc, then |
| 48 | adds the words in the document to WordToId |
| 49 | - takes in the document (str) |
| 50 | - passes the document to a method to add the document |
| 51 | to IdToDoc |
| 52 | - retrieves the id of the inserted document |
| 53 | - uses the id to call the method that adds the words of |
| 54 | the document to the reverse index WordToId if the word has not |
| 55 | already been indexed |
| 56 | """ |
| 57 | row_id = self._add_to_IdToDoc(document) |
| 58 | cur = self.conn.cursor() |
| 59 | reverse_idx = cur.execute( |
| 60 | "SELECT value FROM WordToId WHERE name='index'" |
| 61 | ).fetchone()[0] |
| 62 | reverse_idx = json.loads(reverse_idx) |