1from __future__ import annotations
2
3# built-in
4from itertools import permutations, product
5from typing import Sequence
6
7
8__all__ = ['words_combinations', 'find_ngrams']
9
10
11def words_combinations(f, *texts) -> float:
12 m = float('Inf')
13 # split by words
14 texts = [t.split() for t in texts]
15 # permutations
16 texts = [permutations(words) for words in texts]
17 # combinations
18 for subtexts in product(*texts):
19 if f.equality:
20 words_min_cnt = len(min(subtexts, key=len))
21 subtexts = [t[:words_min_cnt] for t in subtexts]
22 subtexts = [' '.join(t) for t in subtexts]
23 m = min(m, f(*subtexts))
24 return m
25
26
27def find_ngrams(input_list: Sequence, n: int) -> list[tuple]:
28 return list(zip(*[input_list[i:] for i in range(n)]))