| 5 | |
| 6 | # Rewrite Docstore lookup to match original implementation from author. |
| 7 | class CustomDocstoreExplorer(DocstoreExplorer): |
| 8 | def __init__(self, docstore: Docstore): |
| 9 | super().__init__(docstore) |
| 10 | self.lookup_str = "" |
| 11 | self.lookup_index = 0 |
| 12 | |
| 13 | def lookup(self, term: str) -> str: |
| 14 | """Lookup a term in document (if saved).""" |
| 15 | if self.document is None: |
| 16 | raise ValueError("Cannot lookup without a successful search first") |
| 17 | if term.lower() != self.lookup_str: |
| 18 | self.lookup_str = term.lower() |
| 19 | self.lookup_index = 0 |
| 20 | else: |
| 21 | self.lookup_index += 1 |
| 22 | lookups = [p for p in self._sentence if self.lookup_str in p.lower()] |
| 23 | if len(lookups) == 0: |
| 24 | return "No Results" |
| 25 | elif self.lookup_index >= len(lookups): |
| 26 | return "No More Results" |
| 27 | else: |
| 28 | result_prefix = f"(Result {self.lookup_index + 1}/{len(lookups)})" |
| 29 | return f"{result_prefix} {lookups[self.lookup_index]}" |
| 30 | |
| 31 | @property |
| 32 | def _sentence(self) -> List[str]: |
| 33 | if self.document is None: |
| 34 | raise ValueError("Cannot get paragraphs without a document") |
| 35 | return self.document.page_content.split(".") |