- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
两个词之间相似度的定量描述符是否基于它们的发音/发音,类似于 Levenshtein 距离?
我知道 soundex 给 similar sounding 相同的 ID单词,但据我所知,它不是单词之间差异的定量描述符。
from jellyfish import soundex
print(soundex("two"))
print(soundex("to"))
最佳答案
您可以结合语音编码和字符串比较算法。事实上,jellyfish
两者都提供。
设置库示例
from jellyfish import soundex, metaphone, nysiis, match_rating_codex,\
levenshtein_distance, damerau_levenshtein_distance, hamming_distance,\
jaro_similarity
from itertools import groupby
import pandas as pd
import numpy as np
dataList = ['two','too','to','fourth','forth','dessert',
'desert','Byrne','Boern','Smith','Smyth','Catherine','Kathryn']
sounds_encoding_methods = [soundex, metaphone, nysiis, match_rating_codex]
比较不同的拼音编码
report = pd.DataFrame([dataList]).T
report.columns = ['word']
for i in sounds_encoding_methods:
print(i.__name__)
report[i.__name__]= report['word'].apply(lambda x: i(x))
print(report)
soundex metaphone nysiis match_rating_codex
word
two T000 TW TW TW
too T000 T T T
to T000 T T T
fourth F630 FR0 FART FRTH
forth F630 FR0 FART FRTH
dessert D263 TSRT DASAD DSRT
desert D263 TSRT DASAD DSRT
Byrne B650 BRN BYRN BYRN
Boern B650 BRN BARN BRN
Smith S530 SM0 SNAT SMTH
Smyth S530 SM0 SNYT SMYTH
Catherine C365 K0RN CATARAN CTHRN
Kathryn K365 K0RN CATRYN KTHRYN
您可以看到语音编码在使单词具有可比性方面做得非常好。您可能会看到不同的情况,并根据您的情况选择其中一种。
现在我将采用上述方法并尝试使用 levenshtein_distance 找到最接近的匹配项,但我也可以使用其他方法。
"""Select the closer by algorithm
for instance levenshtein_distance"""
report2 = pd.DataFrame([dataList]).T
report2.columns = ['word']
report.set_index('word',inplace=True)
report2 = report.copy()
for sounds_encoding in sounds_encoding_methods:
report2[sounds_encoding.__name__] = np.nan
matched_words = []
for word in dataList:
closest_list = []
for word_2 in dataList:
if word != word_2:
closest = {}
closest['word'] = word_2
closest['similarity'] = levenshtein_distance(report.loc[word,sounds_encoding.__name__],
report.loc[word_2,sounds_encoding.__name__])
closest_list.append(closest)
report2.loc[word,sounds_encoding.__name__] = pd.DataFrame(closest_list).\
sort_values(by = 'similarity').head(1)['word'].values[0]
print(report2)
soundex metaphone nysiis match_rating_codex
word
two too too too too
too two to to to
to two too too too
fourth forth forth forth forth
forth fourth fourth fourth fourth
dessert desert desert desert desert
desert dessert dessert dessert dessert
Byrne Boern Boern Boern Boern
Boern Byrne Byrne Byrne Byrne
Smith Smyth Smyth Smyth Smyth
Smyth Smith Smith Smith Smith
Catherine Kathryn Kathryn Kathryn Kathryn
Kathryn Catherine Catherine Catherine Catherine
从上面可以清楚地看到,语音编码和字符串比较算法之间的组合可以非常简单。
关于python - 通过声音的相似性来确定弦之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66715423/
例如,如果我有 >>> name = f"{os.path.splitext(os.path.basename('/some/long/path/I/donot/need/to/some/config
我正在学习教程并试图了解此处发生的情况。所以我们首先将“消息”编码为字节,然后取其长度并将其向左对齐(?)10,然后对整个内容进行编码? 我试图在我的设备中使用这个部分,它不理解 f-strings,
我敢肯定这个问题被问了很多,但我只是想确保没有更好的方法来做到这一点。 基本上,我有一个 const char* 指向一个以 null 结尾的 C 字符串。我有另一个函数,它需要一个指向具有相同字符的
我使用的是最新的稳定版 PyCharm 2016.1.4 和 Python 3.6a1。每当我使用“f-strings”(PEP-498)时,PyCharm 都会提示 f 是一个 Unresolved
我发现了一些显然是为 python 3.6 设计的代码。 出于某种原因,我需要在 python 3.5 环境中运行该代码。据推测,这两个版本应该是向后兼容的。然而,原始代码在 f-strings 中滥
我是一名优秀的程序员,十分优秀!