Skip to content

Commit 7139004

Browse files
committed
fix uninitialized variable
1 parent 0e6466d commit 7139004

File tree

6 files changed

+63
-8
lines changed

6 files changed

+63
-8
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.0.1

src/py_string_metric.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ PyObject* levenshtein(PyObject* /*self*/, PyObject* args, PyObject* keywds)
132132
}
133133
}
134134

135+
// todo improve error message for None
135136
if (!valid_str(py_s1, "s1") || !valid_str(py_s2, "s2")) {
136137
return NULL;
137138
}
@@ -141,7 +142,7 @@ PyObject* levenshtein(PyObject* /*self*/, PyObject* args, PyObject* keywds)
141142

142143
std::size_t result = mpark::visit(LevenshteinVisitor(insert_cost, delete_cost, replace_cost, (std::size_t)max),
143144
s1_view, s2_view);
144-
145+
145146
if (result == (std::size_t)-1) {
146147
return PyLong_FromLong(-1);
147148
}
@@ -249,6 +250,7 @@ PyObject* hamming(PyObject* /*self*/, PyObject* args, PyObject* keywds)
249250
return NULL;
250251
}
251252

253+
// todo improve error message for None
252254
if (!valid_str(py_s1, "s1") || !valid_str(py_s2, "s2")) {
253255
return NULL;
254256
}

src/py_string_metric.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ Insertion = 1, Deletion = 1, Substitution = 1:
6464
Myers algorithm is described in [3]_
6565
6666
67-
Insertion = 1, Deletion = 1, Substitution = 2:
67+
Insertion = 1, Deletion = 1, Substitution >= Insertion + Deletion:
68+
when ``Substitution >= Insertion + Deletion`` set
69+
``Substitution = Insertion + Deletion``
70+
since every Substitution can be performed as Insertion + Deletion
71+
so in this case treat Substitution as 2
72+
6873
- if max is 0 the similarity can be calculated using a direct comparision,
6974
since no difference between the strings is allowed. The time complexity of
7075
this algorithm is ``O(N)``.
@@ -151,9 +156,8 @@ R"(normalized_levenshtein($module, s1, s2, weights = (1, 1, 1), processor = None
151156
152157
Calculates a normalized levenshtein distance using custom
153158
costs for insertion, deletion and substitution. So far only the following
154-
combinations are supported:
155-
- weights = (1, 1, 1)
156-
- weights = (1, 1, 2)
159+
weights are supported:
160+
- weights = (1, 1, N) with N >= 1
157161
158162
further combinations might be supported in the future
159163

src/rapidfuzz-cpp

Submodule rapidfuzz-cpp updated from 45d277a to ec59b2d

src/rapidfuzz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"""
44
__author__ = "Max Bachmann"
55
__license__ = "MIT"
6-
__version__ = "1.0.0"
6+
__version__ = "1.0.1"
77

88
from rapidfuzz import process, fuzz, utils, levenshtein, string_metric

tests/test_string_metric.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import unittest
5+
import pytest
6+
7+
from rapidfuzz import string_metric
8+
9+
def test_empty_string():
10+
"""
11+
when both strings are empty this is a perfect match
12+
"""
13+
assert string_metric.levenshtein("", "") == 0
14+
assert string_metric.levenshtein("", "", (1,1,0)) == 0
15+
assert string_metric.levenshtein("", "", (1,1,2)) == 0
16+
assert string_metric.levenshtein("", "", (1,1,5)) == 0
17+
assert string_metric.levenshtein("", "", (3,7,5)) == 0
18+
19+
def test_simple_unicode_tests():
20+
"""
21+
some very simple tests using unicode with scorers
22+
to catch relatively obvious implementation errors
23+
"""
24+
s1 = u"ÁÄ"
25+
s2 = "ABCD"
26+
assert string_metric.levenshtein(s1, s2) == 4
27+
assert string_metric.levenshtein(s1, s2, (1,1,0)) == 2
28+
assert string_metric.levenshtein(s1, s2, (1,1,2)) == 6
29+
assert string_metric.levenshtein(s1, s2, (1,1,5)) == 6
30+
assert string_metric.levenshtein(s1, s2, (3,7,5)) == 24
31+
32+
assert string_metric.levenshtein(s1, s1) == 0
33+
assert string_metric.levenshtein(s1, s1, (1,1,0)) == 0
34+
assert string_metric.levenshtein(s1, s1, (1,1,2)) == 0
35+
assert string_metric.levenshtein(s1, s1, (1,1,5)) == 0
36+
assert string_metric.levenshtein(s1, s1, (3,7,5)) == 0
37+
38+
def test_help():
39+
"""
40+
test that all help texts can be printed without throwing an exception,
41+
since they are implemented in C++ aswell
42+
"""
43+
help(string_metric.levenshtein)
44+
help(string_metric.normalized_levenshtein)
45+
help(string_metric.hamming)
46+
help(string_metric.normalized_hamming)
47+
48+
if __name__ == '__main__':
49+
unittest.main()

0 commit comments

Comments
 (0)