Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/textdistance/utils.py: 39%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

18 statements  

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)]))