Skip to content

Commit cd86419

Browse files
author
刘欣
committed
add edit_distance_of_list
1 parent 6107d71 commit cd86419

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def build_extensions(self):
9090

9191
setup(
9292
name='pylcs',
93-
version='0.0.5',
93+
version='0.0.6',
9494
author='Meteorix',
9595
author_email='[email protected]',
9696
url='https://github.com/Meteorix/pylcs',

src/main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ int levenshtein_distance(const string &str1, const string &str2) {
160160
}
161161

162162

163+
vector<int> levenshtein_distance_of_list(const string &str1, vector<string> &str_list){
164+
int size = str_list.size();
165+
vector<int> ls(size);
166+
for (int i = 0; i < size; i++){
167+
int l = levenshtein_distance(str1, str_list[i]);
168+
ls[i] = l;
169+
}
170+
return ls;
171+
}
172+
173+
163174
namespace py = pybind11;
164175

165176

@@ -188,4 +199,11 @@ PYBIND11_MODULE(pylcs, m) {
188199
Same As levenshtein_distance(): Levenshtein Distance of Two Strings
189200
)pbdoc");
190201

202+
m.def("levenshtein_distance_of_list", &levenshtein_distance_of_list, R"pbdoc(
203+
Levenshtein Distance of one string to a list of strings
204+
)pbdoc");
205+
206+
m.def("edit_distance_of_list", &levenshtein_distance_of_list, R"pbdoc(
207+
Levenshtein Distance of one string to a list of strings
208+
)pbdoc");
191209
}

tests/test_pylcs.py

+5
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ def test_edit_distance():
3232
assert pylcs.edit_distance("你好", "中国") == 2
3333
assert pylcs.edit_distance("aaa你好", "你好呀") == 4
3434

35+
36+
def test_edit_distance_of_list():
37+
assert pylcs.edit_distance_of_list("aaa", ["bbb"] * 10) == [3] * 10
38+
assert pylcs.edit_distance_of_list("aaa你好", ["你好呀"] * 10) == [4] * 10
39+
assert pylcs.edit_distance_of_list("aaa你好", ["bbb", "你好呀"] * 10) == [5, 4] * 10

0 commit comments

Comments
 (0)