def get_optimal_variant(self, variants, start_words, **kwargs):
if not start_words:<EOL><INDENT>return (choice(variants), {})<EOL><DEDENT>_variants = []<EOL>_weights = []<EOL>for tok in frozenset(variants):<EOL><INDENT>if not self.token_is_correct(tok):<EOL><INDENT>continue<EOL><DEDENT>weight = variants.count(tok)<EOL>for word in start_words:<EOL><INDENT>for token in self.ONLY_WORDS.finditer(word.strip().lower()):<EOL><INDENT>if token.group() == tok:<EOL><INDENT>weight <<= <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>_variants.append(tok)<EOL>_weights.append(weight)<EOL><DEDENT>if not _variants:<EOL><INDENT>return (choice(variants), {})<EOL><DEDENT>return (choices(_variants, weights=_weights, k=<NUM_LIT:1>)[<NUM_LIT:0>], {})<EOL>
Возвращает оптимальный вариант, из выборки.