1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| from pylev import levenshtein
src = "kitty"
characters = ["kitty_b_body","karen_a_body","paul_b_body","kitty_a_body"]
dist_list = [levenshtein(src, char) for char in characters]
closest = [x for (_, x) in sorted(zip(dist_list,characters))]
print(closest[0])
# kitty_a
# 一般來說會拿來做比較的 asset, control, specific nodes 等等
# 都有制式化的命名, 而且會先 filter 出同類型的 entities 再做排序
# 如果不是的話排序結果可能會和預期的不同 e.g.
src = "kitty"
characters = ["kitty_var_a","karen","jonathan","kitty_var_b"]
dist_list = [levenshtein(src, char) for char in characters]
closest = [x for (_, x) in sorted(zip(dist_list,characters))]
print(closest)
# ["karen", "kitty_var_a", "kitty_var_b", "jonathan"]
# 上面這個例子中 karen 反而因為需求編輯次數最少而成為最接近 kitty 的字串
# 如果想做的是找出類似來源的字串, 且公司的命名規則是將 asset 的 unique
# name 排在首位, 可以單純比對和 src 同等長度的部分
src = "kitty"
src_len = len(src)
characters = ["kitty_var_a", "karen", "jonathan", "kitty_var_b"]
dist_list = [
levenshtein(
src, char[:
src_len
if src_len < len(char)
else len(char)
]
)
for char in characters
]
closest = [x for (_, x) in sorted(zip(dist_list, characters))]
print(closest)
# ["kitty_var_a", "kitty_var_b", "karen", "jonathan"]
|