1- from typing import Collection , List
1+ from typing import Collection , Optional , List
22
33__all__ = ["suggestion_list" ]
44
@@ -14,9 +14,9 @@ def suggestion_list(input_: str, options: Collection[str]) -> List[str]:
1414
1515 input_threshold = len (input_ ) // 2
1616 for option in options :
17- distance = lexical_distance .measure (option )
1817 threshold = max (input_threshold , len (option ) // 2 , 1 )
19- if distance <= threshold :
18+ distance = lexical_distance .measure (option , threshold )
19+ if distance is not None :
2020 options_by_distance [option ] = distance
2121
2222 # noinspection PyShadowingNames
@@ -46,7 +46,7 @@ def __init__(self, input_: str):
4646 row_size = len (input_ ) + 1
4747 self ._rows = [[0 ] * row_size , [0 ] * row_size , [0 ] * row_size ]
4848
49- def measure (self , option : str ) :
49+ def measure (self , option : str , threshold : int ) -> Optional [ int ] :
5050 if self ._input == option :
5151 return 0
5252
@@ -59,6 +59,9 @@ def measure(self, option: str):
5959 a , b = option_lower_case , self ._input_lower_case
6060 a_len , b_len = len (a ), len (b )
6161
62+ if abs (a_len - b_len ) > threshold :
63+ return None
64+
6265 rows = self ._rows
6366 for j in range (0 , b_len + 1 ):
6467 rows [0 ][j ] = j
@@ -67,7 +70,7 @@ def measure(self, option: str):
6770 up_row = rows [(i - 1 ) % 3 ]
6871 current_row = rows [i % 3 ]
6972
70- current_row [0 ] = i
73+ smallest_cell = current_row [0 ] = i
7174 for j in range (1 , b_len + 1 ):
7275 cost = 0 if a [i - 1 ] == b [j - 1 ] else 1
7376
@@ -82,6 +85,15 @@ def measure(self, option: str):
8285 double_diagonal_cell = rows [(i - 2 ) % 3 ][j - 2 ]
8386 current_cell = min (current_cell , double_diagonal_cell + 1 )
8487
88+ if current_cell < smallest_cell :
89+ smallest_cell = current_cell
90+
8591 current_row [j ] = current_cell
8692
87- return rows [a_len % 3 ][b_len ]
93+ # Early exit, since distance can't go smaller than smallest element
94+ # of the previous row.
95+ if smallest_cell > threshold :
96+ return None
97+
98+ distance = rows [a_len % 3 ][b_len ]
99+ return distance if distance <= threshold else None
0 commit comments