📜  导入fuzzywuzzy 模块jupyter notebook - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:25:12.313000             🧑  作者: Mango

导入fuzzywuzzy模块jupyter notebook - Shell-Bash

Fuzzywuzzy是一个Python库,用于模糊字符串匹配。它是基于Levenshtein距离(字符串中不同字符的数量)来计算字符串之间的相似度。

安装fuzzywuzzy模块

使用pip命令来安装fuzzywuzzy模块:

!pip install fuzzywuzzy
导入fuzzywuzzy模块

导入fuzzywuzzy模块,以便在用Python编写的应用程序中使用它:

from fuzzywuzzy import fuzz
from fuzzywuzzy import process
示例代码
简单的字符串匹配
from fuzzywuzzy import fuzz

# 两个相同字符串
text1 = "Hello World"
text2 = "Hello World"
print(fuzz.ratio(text1, text2)) # 100

# 模糊匹配的字符串
text3 = "Hello World!"
text4 = "Hello World!!"
print(fuzz.ratio(text3, text4)) # 95
模糊字符串匹配
from fuzzywuzzy import process

# 匹配字符串
text = "apple"
choices = ["grape", "banana", "watermelon", "orange", "orange juice"]
print(process.extract(text, choices, limit=2)) # [('apple', 90), ('grape', 0)]

# 匹配字典
text = "apple"
choices = {"grape": 0, "banana": 0, "watermelon": 0, "orange": 0, "orange juice": 0}
print(process.extractOne(text, choices)) # ('grape', 0)

使用fuzzywuzzy模块可以方便地进行一些模糊字符串匹配的操作,提高应用程序的准确性与效率。