模糊检查列表中的每一项是否包含在给定字符串中
Fuzzy checking if each item of a list is contained in a given string
list = ['Apple','Banana','Cucumber']
string = 'The other day as I ate a bnana in the park'
for x in range(len(list)):
if list[x] in string:
do a little dance
这是我现在的代码的要点,尽管我的实际字符串和列表要长得多。该字符串是用户提交的,所以我不得不期待 misspelling/shorthand/CAPS 并且没有用我能想到的每个拼写错误或解析字符串的每个单词来填充我的列表,我不确定如何解决这个问题。
我正在寻找模糊包含 if 语句。我查看了 fuzzywuzzy 文档,我不确定如何让它在这种情况下工作。
有这样的功能吗?
threshold = 80
for x in range(len(list):
if fuzzy.contain(list[x],string) > threshold:
do a little dance:
感谢任何帮助。
来自文档:
threshold = 80
for x in range(len(list)):
if fuzzy.WRatio(list[x],string) > threshold:
do a little dance:
*免责声明我以前从未使用过 fuzzy
,但应该可以。
我在 fuzzywuzzy documentation
中找不到 contain
方法,所以我想到了这个。您按单词拆分短语,然后以 fuzzy
方式比较每个单词。根据您的特殊需要,您应该使用其他评级方法而不是 token_sort_ratio
和 threshold
值。您可以在他们的 github.
中找到更多信息
from fuzzywuzzy import fuzz
def fuzzy_contains_word(word, phrase, threshold):
for phrase_word in phrase.split():
if fuzz.token_sort_ratio(word, phrase_word) > threshold:
return True
return False
words = ['Apple','Banana', 'Cucumber']
user_input = 'The other day as I ate a bnana in the park'
threshold = 80
for word in words:
if fuzzy_contains_word(word, user_input, 80):
print(word, 'found in phrase: ', user_input)
>>> Banana found in phrase: The other day as I ate a bnana in the park
注意:我收到一条警告,说你应该安装 python-Levenshtein
包。
list = ['Apple','Banana','Cucumber']
string = 'The other day as I ate a bnana in the park'
for x in range(len(list)):
if list[x] in string:
do a little dance
这是我现在的代码的要点,尽管我的实际字符串和列表要长得多。该字符串是用户提交的,所以我不得不期待 misspelling/shorthand/CAPS 并且没有用我能想到的每个拼写错误或解析字符串的每个单词来填充我的列表,我不确定如何解决这个问题。
我正在寻找模糊包含 if 语句。我查看了 fuzzywuzzy 文档,我不确定如何让它在这种情况下工作。
有这样的功能吗?
threshold = 80
for x in range(len(list):
if fuzzy.contain(list[x],string) > threshold:
do a little dance:
感谢任何帮助。
来自文档:
threshold = 80
for x in range(len(list)):
if fuzzy.WRatio(list[x],string) > threshold:
do a little dance:
*免责声明我以前从未使用过 fuzzy
,但应该可以。
我在 fuzzywuzzy documentation
中找不到 contain
方法,所以我想到了这个。您按单词拆分短语,然后以 fuzzy
方式比较每个单词。根据您的特殊需要,您应该使用其他评级方法而不是 token_sort_ratio
和 threshold
值。您可以在他们的 github.
from fuzzywuzzy import fuzz
def fuzzy_contains_word(word, phrase, threshold):
for phrase_word in phrase.split():
if fuzz.token_sort_ratio(word, phrase_word) > threshold:
return True
return False
words = ['Apple','Banana', 'Cucumber']
user_input = 'The other day as I ate a bnana in the park'
threshold = 80
for word in words:
if fuzzy_contains_word(word, user_input, 80):
print(word, 'found in phrase: ', user_input)
>>> Banana found in phrase: The other day as I ate a bnana in the park
注意:我收到一条警告,说你应该安装 python-Levenshtein
包。