Python使用Fuzzywuzzy计算字符串之间的相似度,包括Levenshtein距离、Jaro距离等
环境搭建与准备工作:
1. 安装Python:首先,确保已经安装了Python,可以从官方网站(https://www.python.org/downloads/)下载并安装所需的Python版本。
2. 安装Fuzzywuzzy类库:Fuzzywuzzy是一个Python库,用于计算字符串之间的相似度。可以使用pip命令来安装Fuzzywuzzy库,打开终端或命令提示符窗口,运行以下命令:
pip install fuzzywuzzy
依赖的类库:
- fuzzywuzzy
- Levenshtein
数据样例(示例数据集):
对于字符串相似度计算,可以使用以下数据样例:
python
string1 = "Hello World"
string2 = "Hello World!"
string3 = "Hello Python"
完整的样例代码如下:
python
from fuzzywuzzy import fuzz, process
from fuzzywuzzy import fuzz
from Levenshtein import distance
# 示例数据
string1 = "Hello World"
string2 = "Hello World!"
string3 = "Hello Python"
# 计算字符串之间的相似度
# 使用fuzz模块的ratio函数计算相似度
ratio = fuzz.ratio(string1, string2)
print(f"Ratio similarity between '{string1}' and '{string2}' is: {ratio}")
# 使用fuzz模块的partial_ratio函数计算相似度
partial_ratio = fuzz.partial_ratio(string1, string2)
print(
f"Partial ratio similarity between '{string1}' and '{string2}' is: {partial_ratio}"
)
# 使用fuzz模块的token_sort_ratio函数计算相似度
token_sort_ratio = fuzz.token_sort_ratio(string1, string2)
print(
f"Token sort ratio similarity between '{string1}' and '{string2}' is: {token_sort_ratio}"
)
# 使用fuzz模块的token_set_ratio函数计算相似度
token_set_ratio = fuzz.token_set_ratio(string1, string2)
print(
f"Token set ratio similarity between '{string1}' and '{string2}' is: {token_set_ratio}"
)
# 使用Levenshtein类库计算Levenshtein距离
levenshtein_distance = distance(string1, string3)
print(
f"Levenshtein distance between '{string1}' and '{string3}' is: {levenshtein_distance}"
)
代码解析:
1. 导入fuzz和process模块中的函数用于计算字符串相似度。
2. 导入distance函数,用于计算字符串之间的Levenshtein距离。
3. 定义示例数据:string1、string2、string3。
4. 使用fuzz.ratio函数计算两个字符串的相似度。
5. 使用fuzz.partial_ratio函数计算两个字符串的部分相似度。
6. 使用fuzz.token_sort_ratio函数计算两个字符串中单词排序后的相似度。
7. 使用fuzz.token_set_ratio函数计算两个字符串中单词集合的相似度。
8. 使用Levenshtein.distance函数计算字符串之间的Levenshtein距离。
总结:
- Fuzzywuzzy类库提供了多种方法用于计算字符串之间的相似度,包括ratio、partial_ratio、token_sort_ratio和token_set_ratio等。
- Levenshtein类库提供了计算字符串之间Levenshtein距离的函数。
- 根据具体需求选择合适的方法进行字符串相似度计算,可以根据实际情况选择使用Fuzzywuzzy或Levenshtein类库计算相似度。