HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/anaconda3/Lib/site-packages/textdistance/utils.py
# built-in
from itertools import permutations, product


__all__ = ['words_combinations', 'find_ngrams']


def words_combinations(f, *texts):
    m = float('Inf')
    # split by words
    texts = [t.split() for t in texts]
    # permutations
    texts = [permutations(words) for words in texts]
    # combinations
    for subtexts in product(*texts):
        if f.equality:
            words_min_cnt = len(min(subtexts, key=len))
            subtexts = [t[:words_min_cnt] for t in subtexts]
        subtexts = [' '.join(t) for t in subtexts]
        m = min(m, f(*subtexts))
    return m


def find_ngrams(input_list, n):
    return list(zip(*[input_list[i:] for i in range(n)]))