| 1 |
from typing import Collection, Optional, List |
| 2 |
|
| 3 |
from .natural_compare import natural_comparison_key |
| 4 |
|
| 5 |
__all__ = ["suggestion_list"] |
| 6 |
|
| 7 |
|
| 8 |
def suggestion_list(input_: str, options: Collection[str]) -> List[str]: |
| 9 |
"""Get list with suggestions for a given input. |
| 10 |
|
| 11 |
Given an invalid input string and list of valid options, returns a filtered list |
| 12 |
of valid options sorted based on their similarity with the input. |
| 13 |
""" |
| 14 |
options_by_distance = {} |
| 15 |
lexical_distance = LexicalDistance(input_) |
| 16 |
|
| 17 |
threshold = int(len(input_) * 0.4) + 1 |
| 18 |
for option in options: |
| 19 |
distance = lexical_distance.measure(option, threshold) |
| 20 |
if distance is not None: |
| 21 |
options_by_distance[option] = distance |
| 22 |
|
| 23 |
# noinspection PyShadowingNames |
| 24 |
return sorted( |
| 25 |
options_by_distance, |
| 26 |
key=lambda option: ( |
| 27 |
options_by_distance.get(option, 0), |
| 28 |
natural_comparison_key(option), |
| 29 |
), |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
class LexicalDistance: |
| 34 |
"""Computes the lexical distance between strings A and B. |
| 35 |
|
| 36 |
The "distance" between two strings is given by counting the minimum number of edits |
| 37 |
needed to transform string A into string B. An edit can be an insertion, deletion, |
| 38 |
or substitution of a single character, or a swap of two adjacent characters. |
| 39 |
|
| 40 |
This distance can be useful for detecting typos in input or sorting. |
| 41 |
""" |
| 42 |
|
| 43 |
_input: str |
| 44 |
_input_lower_case: str |
| 45 |
_input_list: List[int] |
| 46 |
_rows: List[List[int]] |
| 47 |
|
| 48 |
def __init__(self, input_: str): |
| 49 |
self._input = input_ |
| 50 |
self._input_lower_case = input_.lower() |
| 51 |
row_size = len(input_) + 1 |
| 52 |
self._input_list = list(map(ord, self._input_lower_case)) |
| 53 |
|
| 54 |
self._rows = [[0] * row_size, [0] * row_size, [0] * row_size] |
| 55 |
|
| 56 |
def measure(self, option: str, threshold: int) -> Optional[int]: |
| 57 |
if self._input == option: |
| 58 |
return 0 |
| 59 |
|
| 60 |
option_lower_case = option.lower() |
| 61 |
|
| 62 |
# Any case change counts as a single edit |
| 63 |
if self._input_lower_case == option_lower_case: |
| 64 |
return 1 |
| 65 |
|
| 66 |
a, b = list(map(ord, option_lower_case)), self._input_list |
| 67 |
a_len, b_len = len(a), len(b) |
| 68 |
if a_len < b_len: |
| 69 |
a, b = b, a |
| 70 |
a_len, b_len = b_len, a_len |
| 71 |
|
| 72 |
if a_len - b_len > threshold: |
| 73 |
return None |
| 74 |
|
| 75 |
rows = self._rows |
| 76 |
for j in range(b_len + 1): |
| 77 |
rows[0][j] = j |
| 78 |
|
| 79 |
for i in range(1, a_len + 1): |
| 80 |
up_row = rows[(i - 1) % 3] |
| 81 |
current_row = rows[i % 3] |
| 82 |
|
| 83 |
smallest_cell = current_row[0] = i |
| 84 |
for j in range(1, b_len + 1): |
| 85 |
cost = 0 if a[i - 1] == b[j - 1] else 1 |
| 86 |
|
| 87 |
current_cell = min( |
| 88 |
up_row[j] + 1, # delete |
| 89 |
current_row[j - 1] + 1, # insert |
| 90 |
up_row[j - 1] + cost, # substitute |
| 91 |
) |
| 92 |
|
| 93 |
if i > 1 and j > 1 and a[i - 1] == b[j - 2] and a[i - 2] == b[j - 1]: |
| 94 |
# transposition |
| 95 |
double_diagonal_cell = rows[(i - 2) % 3][j - 2] |
| 96 |
current_cell = min(current_cell, double_diagonal_cell + 1) |
| 97 |
|
| 98 |
if current_cell < smallest_cell: |
| 99 |
smallest_cell = current_cell |
| 100 |
|
| 101 |
current_row[j] = current_cell |
| 102 |
|
| 103 |
# Early exit, since distance can't go smaller than smallest element |
| 104 |
# of the previous row. |
| 105 |
if smallest_cell > threshold: |
| 106 |
return None |
| 107 |
|
| 108 |
distance = rows[a_len % 3][b_len] |
| 109 |
return distance if distance <= threshold else None |