Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 模糊启动_Python_Startswith - Fatal编程技术网

Python 模糊启动

Python 模糊启动,python,startswith,Python,Startswith,是否有一种方法可以执行模糊的“startswith”,这样,如果第一个字符串以接近第二个字符串的某个开头,它将返回true?我的第一个想法是使用编辑距离阈值,但我不确定在startswith的上下文中如何做到这一点 例如: first_str = "My nam is Hello World" second_str = "My name is" first_str.startswith(second_str) == True 模糊模糊可以帮助你 >>> fro

是否有一种方法可以执行模糊的“startswith”,这样,如果第一个字符串以接近第二个字符串的某个开头,它将返回true?我的第一个想法是使用编辑距离阈值,但我不确定在startswith的上下文中如何做到这一点

例如:

first_str = "My nam is Hello World"
second_str = "My name is"        
first_str.startswith(second_str) == True

模糊模糊可以帮助你

>>> from fuzzywuzzy import fuzz
>>> fuzz.partial_ratio("my name is joran","my nam is")
您需要
pip安装fuzzyfuzzy
然后您只需要选择一个“True”的比率,这并不一定意味着“it startswith”,但我们可以使用一个helper函数来实现这一点

def fuzzy_startswith(needle,haystack):
    n_words = len(needle.split())
    haystack_startswith = " ".join(haystack.split()[:n_words])
    return fuzz.ratio(needle,haystack_startswith)

fuzzy_startswith("my nam is","my name is joran")

模糊模糊可以帮助你

>>> from fuzzywuzzy import fuzz
>>> fuzz.partial_ratio("my name is joran","my nam is")
您需要
pip安装fuzzyfuzzy
然后您只需要选择一个“True”的比率,这并不一定意味着“it startswith”,但我们可以使用一个helper函数来实现这一点

def fuzzy_startswith(needle,haystack):
    n_words = len(needle.split())
    haystack_startswith = " ".join(haystack.split()[:n_words])
    return fuzz.ratio(needle,haystack_startswith)

fuzzy_startswith("my nam is","my name is joran")

这取决于什么算是
关闭
。你可能想研究一下,你能不能提供更多的模糊开始的例子,返回值
True
,允许多少差异,顺序是否重要?我想我的阈值应该是大于0.8左右的Levenshtein距离比。如果有帮助的话,我可以添加更多的例子。这取决于什么算
close
。你可能想研究一下,你能不能提供更多的模糊开始的例子,返回值
True
,允许多少差异,顺序是否重要?我想我的阈值应该是大于0.8左右的Levenshtein距离比。如果有帮助的话,我可以添加更多的示例。此外,您可能希望将所有内容都转换为相同的大小写,例如
.upper()
.lower()
fuzzyfuzzy
似乎取决于大小写(默认情况下)。这可能会产生不太直观的结果:
haystack='MY NAME is joran'
返回40
haystack='my uncle is joran'
返回74。此外,您可能希望将所有内容都转换为相同的大小写,例如
.upper()
.lower()
fuzzyfuzzy
似乎取决于大小写(默认情况下)。这可能会产生不太直观的结果:
haystack='MY NAME is joran'
返回40
haystack='我叔叔是joran'
返回74。