Try to search for wiki page. If page exists, return the page summary, and a PageWithLookups object. If page does not exist, return similar entries. Args: entity: entity string. Returns: a Document object or error message.
(self, entity: str, is_retry: bool = False)
| 142 | return obs |
| 143 | |
| 144 | def search(self, entity: str, is_retry: bool = False) -> Union[str, Document]: |
| 145 | """Try to search for wiki page. |
| 146 | |
| 147 | If page exists, return the page summary, and a PageWithLookups object. |
| 148 | If page does not exist, return similar entries. |
| 149 | |
| 150 | Args: |
| 151 | entity: entity string. |
| 152 | |
| 153 | Returns: a Document object or error message. |
| 154 | """ |
| 155 | s = time.time() |
| 156 | entity = str(entity) |
| 157 | entity_ = entity.replace(" ", "+") |
| 158 | search_url = f"https://en.wikipedia.org/w/index.php?search={entity_}" |
| 159 | response_text = requests.get(search_url).text |
| 160 | |
| 161 | result = self.post_process(response_text, entity) |
| 162 | |
| 163 | if "Similar:" in result: |
| 164 | alternative = self._get_alternative(result) |
| 165 | entity_ = alternative.replace(" ", "+") |
| 166 | search_url = f"https://en.wikipedia.org/w/index.php?search={entity_}" |
| 167 | response_text = requests.get(search_url).text |
| 168 | |
| 169 | result = self.post_process( |
| 170 | response_text, entity, skip_retry_when_postprocess=True |
| 171 | ) |
| 172 | |
| 173 | if "Similar:" in result: |
| 174 | result = "Could not find " + entity + "." |
| 175 | |
| 176 | if self.benchmark and not is_retry: |
| 177 | # we only benchmark the outermost call |
| 178 | self.all_times.append(round(time.time() - s, 2)) |
| 179 | |
| 180 | return result |
| 181 | |
| 182 | async def asearch( |
| 183 | self, entity: str, is_retry: bool = False |
no test coverage detected