Skip to content

Commit 106489c

Browse files
authored
use specified costs when one of the strings is empty
If one of the strings is empty it's not correct to assume that the cost is the length of the other string since we the insertion and deletion costs are configurable and not necessarily 1.
1 parent a45c796 commit 106489c

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

strsimpy/weighted_levenshtein.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
# SOFTWARE.
2020

21+
from functools import reduce
2122
from .string_distance import StringDistance
2223

2324

@@ -52,9 +53,9 @@ def distance(self, s0, s1):
5253
if s0 == s1:
5354
return 0.0
5455
if len(s0) == 0:
55-
return len(s1)
56+
return reduce(lambda cost, char: cost + self.insertion_cost_fn(char), s1, 0)
5657
if len(s1) == 0:
57-
return len(s0)
58+
return reduce(lambda cost, char: cost + self.deletion_cost_fn(char), s0, 0)
5859

5960
v0, v1 = [0.0] * (len(s1) + 1), [0.0] * (len(s1) + 1)
6061

0 commit comments

Comments
 (0)