async def answer_question(self, question: str, original_choices: List[str]): self.logger.info('Searching...') start_time = time() question_lower = question.lower() reverse = 'NOT' in question or 'NEVER' in question or 'NEITHER' in question or \ ('least' in question_lower and 'at least' not in question_lower) choice_groups = [[ choice.translate(self.punctuation_to_none), choice.translate(self.punctuation_to_space) ] for choice in original_choices] choices = sum(choice_groups, []) # Step 1: Search web for results keyword_start_time = time() question_keywords = self.find_keywords(question) if not self.simplified_output: self.logger.info(f'Question keywords: {question_keywords}') self.logger.debug( f'Keywords took {round(time() - keyword_start_time, 2)} seconds') search_start_time = time() links = await self.searcher.get_search_links( ' '.join(question_keywords), self.num_sites) self.logger.debug( f'Web search took {round(time() - search_start_time, 2)} seconds') self.logger.debug(f'Found links: {links}') # Step 2: Fetch links and clean up text fetch_start_time = time() link_texts = [ Searcher.html_to_visible_text(html).translate( self.punctuation_to_none) for html in await self.searcher.fetch_multiple(links) ] self.logger.debug( f'Fetching took {round(time() - fetch_start_time, 2)} seconds') # Step 3: Find best answer for all search methods post_process_start_time = time() answers = [] for search_method in self.search_methods_to_use: answer = await search_method(link_texts, choices, choice_groups, reverse) answers.append(answer) if answer: self.logger.info(answer, extra={'pre': colorama.Fore.BLUE}) else: self.logger.info('Tie', extra={'pre': colorama.Fore.BLUE}) self.logger.debug( f'Post-processing took {round(time() - post_process_start_time, 2)} seconds' ) self.logger.info( f'Search took {round(time() - start_time, 2)} seconds') return answers
async def answer_question(self, question, original_choices): self.logger.info("Searching...") start_time = time() question_lower = question.lower() reverse = "NOT" in question or "NEVER" in question or \ ("least" in question_lower and "at least" not in question_lower) choice_groups = [[ choice.translate(QuestionHandler.PUNCTUATION_TO_NONE), choice.translate(QuestionHandler.PUNCTUATION_TO_SPACE) ] for choice in original_choices] choices = sum(choice_groups, []) # Step 1: Search web for results keyword_start_time = time() question_keywords = self.find_keywords(question) if not self.simplified_output: self.logger.info(f"Question keywords: {question_keywords}") self.logger.debug( f"Keywords took {round(time() - keyword_start_time, 2)} seconds") search_start_time = time() links = self.searcher.get_search_links(" ".join(question_keywords), 5) self.logger.debug( f"Web search took {round(time() - search_start_time, 2)} seconds") self.logger.debug(f"Found links: {links}") # Step 2: Fetch links and clean up text fetch_start_time = time() link_texts = [ Searcher.html_to_visible_text(html).translate( QuestionHandler.PUNCTUATION_TO_NONE) for html in await self.searcher.fetch_multiple(links) ] self.logger.debug( f"Fetching took {round(time() - fetch_start_time, 2)} seconds") # Step 3: Find best answer for all search methods post_process_start_time = time() for search_method in self.search_methods_to_use: self.logger.info(search_method(link_texts, choices, choice_groups, reverse), extra={"pre": colorama.Fore.BLUE}) self.logger.debug( f"Post-processing took {round(time() - post_process_start_time, 2)} seconds" ) self.logger.info( f"Search took {round(time() - start_time, 2)} seconds")