def get_optimal_variant(self, variants, start_words, **kwargs):
        """
        Возвращает оптимальный вариант, из выборки.
        """

        if not start_words:
            return (choice(variants), {})

        _variants = []
        _weights = []
        for tok in frozenset(variants):
            if not self.token_is_correct(tok):
                continue
            weight = variants.count(tok)
            for word in start_words:
                for token in self.ONLY_WORDS.finditer(word.strip().lower()):
                    if token.group() == tok:
                        weight <<= 1
            _variants.append(tok)
            _weights.append(weight)

        if not _variants:
            return (choice(variants), {})

        return (choices(_variants, weights=_weights, k=1)[0], {})
def get_optimal_variant(self, variants, start_words, **kwargs):
        """
        Возвращает оптимальный вариант, из выборки.
        """

        if not start_words:
            return (choice(variants), {})

        _variants = []
        _weights = []
        for tok in frozenset(variants):
            if not self.token_is_correct(tok):
                continue
            weight = variants.count(tok)
            for word in start_words:
                for token in self.ONLY_WORDS.finditer(word.strip().lower()):
                    if token.group() == tok:
                        weight <<= 1
            _variants.append(tok)
            _weights.append(weight)

        if not _variants:
            return (choice(variants), {})

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