Jul-26-2020, 12:23 PM
(This post was last modified: Jul-26-2020, 12:23 PM by deanhystad.)
I can see where this code would be confusing since it contains errors and what it does is really odd.
def checkIfContains(name, response): name = name.lower(); response = response.lower(); # This code creates a bunch of substrings, all 5 characters in length, # except the last substring that is only 4 characters in length because # the range is wrong Should be "for i in range(len(response)-4)" stringFragments = [] for i in range(0,len(response)-3): stringFragments.append(response[i:i+5]) hasSubstring = False; # This error fixes the previous error by ignoring the last # substring. Should be # for substr in stringFragments: # if substr in name: # return True # return False for i in range(0,len(stringFragments)-1): if stringFragments[i] in name: hasSubstring = True; return hasSubstringI do not understand what the code is supposed to do and I don't think this logic does it. The code creates a bunch of 5 character long strings and returns True if any of those substrings are in name. If you are trying to do a near match (match strings that are close to the same), take a look at difflib. There are also a lot of python libraries used in natural language processing that handle this problem (look for "string distance")