是否有python的soundex函数,如果没有,你会如何制作soundex代码?
Soundex
Code Letters
1 B, F, P, V
2 C, G, J, K, Q, S, X, Z
3 D, T
4 L
5 M, N
6 R
SKIP A, E, H, I, O, U, W, Y, H, W, and Y
例如:
杰克逊= J250华盛顿= W252
Clement = C455
Ashcraft = A261
吴= W000
答案 0 :(得分:4)
你可以使用水母
sudo pip install jellyfish
print "Soundex\t\t=", jellyfish.soundex("Ala ma kaca")
>Soundex = A452
#...
>Metaphone = AL M KK
>NYSIIS = AL
>Match rating codex = ALMKC
答案 1 :(得分:3)
是的,你可以使用Fuzzy这是一个实现一些语音算法的python库。
sudo pip install fuzzy
>>> import fuzzy
>>> soundex = fuzzy.Soundex(4)
>>> soundex("Jackson")
'J250'
>>> soundex("Washington")
'W252'
>>> soundex("Clement")
'C453'
>>> soundex("Ashcraft")
'A261'
>>> soundex("Wu")
'W000'
答案 2 :(得分:1)
直接使用下面的soundex()
函数,无需安装任何包!
从包中提取的片段 Jellyfish > _jellyfish.py
示例
print(soundex('kent')) # K530
print(soundex('Paul')) # P400
print(soundex('amnesty')) # A523
代码
import unicodedata
def soundex(s):
if not s:
return ""
s = unicodedata.normalize("NFKD", s)
s = s.upper()
replacements = (
("BFPV", "1"),
("CGJKQSXZ", "2"),
("DT", "3"),
("L", "4"),
("MN", "5"),
("R", "6"),
)
result = [s[0]]
count = 1
# find would-be replacment for first character
for lset, sub in replacements:
if s[0] in lset:
last = sub
break
else:
last = None
for letter in s[1:]:
for lset, sub in replacements:
if letter in lset:
if sub != last:
result.append(sub)
count += 1
last = sub
break
else:
if letter != "H" and letter != "W":
# leave last alone if middle letter is H or W
last = None
if count == 4:
break
result += "0" * (4 - count)
return "".join(result)