def _should_reject_request(self, query: TranslationQuery) -> bool: if query.budget_is_unconstrained(): return False if self.time_expense_tracker.size() > MIN_ACCEPTANCE_ENTRIES and \ self.time_expense_tracker.probability_of_being_lower(query.budget.time) < REQUEST_ACCEPTANCE_PROBABILITY: return True if self.estimate_costs(query).money > query.budget.money: return True return False
def _translate(self, query: TranslationQuery) -> TranslationResponse: responses = [None] * len(self.translators) threads = [] # Start a thread for each translator for idx, translator in enumerate(self.translators): translate_thread = threading.Thread(target=translate_worker, args=(translator, query, responses, idx)) translate_thread.start() threads.append(translate_thread) # Wait for all threads to complete if query.budget_is_unconstrained(): join_threads(threads) else: join_threads(threads, timeout_ms=query.budget.time) translations = [] money_costs = 0 # Process all the responses for idx, resp in enumerate(responses): if not threads[idx].is_alive(): if resp: #fixing issue #35 (https://github.com/zeeguu-ecosystem/Python-Translators/issues/35) translations = merge_translations(translations, resp.translations) money_costs += resp.costs.money # reorder translations such that the translation which is the same # as the original word is not the first translations = filter_empty_translations(translations) translations = order_by_quality(translations, query) return TranslationResponse(translations=translations, costs=TranslationCosts(money=money_costs))