Returns - string Input - str: a string of words called document ---------- Indexes the document. It does this by performing two operations - add the document to the IdToDoc, then adds the words in the document to WordToId - takes in the docume
(self, document)
| 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) |
| 63 | document = document.split() |
| 64 | for word in document: |
| 65 | if word not in reverse_idx: |
| 66 | reverse_idx[word] = [row_id] |
| 67 | else: |
| 68 | if row_id not in reverse_idx[word]: |
| 69 | reverse_idx[word].append(row_id) |
| 70 | reverse_idx = json.dumps(reverse_idx) |
| 71 | cur = self.conn.cursor() |
| 72 | result = cur.execute( |
| 73 | "UPDATE WordToId SET value = (?) WHERE name='index'", (reverse_idx,) |
| 74 | ) |
| 75 | return "index successful" |
| 76 | |
| 77 | def _add_to_IdToDoc(self, document): |
| 78 | """ |
no test coverage detected