Last active
February 18, 2020 11:45
-
-
Save MitchellKehn/abe3972197e55dd7b3f5a12873f4d9bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fuzzywuzzy import fuzz | |
import re | |
def partialNumberyRatio(stringA, stringB): | |
stringA = stringA.lower() | |
stringB = stringB.lower() | |
digitsA = "".join(sorted([char for char in stringA if char.isdigit()])) | |
digitsB = "".join(sorted([char for char in stringB if char.isdigit()])) | |
digitMatch = float(digitsA in digitsB) | |
match = fuzz.partial_ratio(stringA, stringB) | |
return match * digitMatch | |
print partialNumberyRatio("sweata", "gatorade_SweatMachine_4592") | |
print partialNumberyRatio("010", "sh010") | |
print partialNumberyRatio("010", "sh080") | |
print partialNumberyRatio("010", "sh020") | |
print partialNumberyRatio("010", "sh100") | |
print partialNumberyRatio("gfx010 1 600", "GVFX010_P01_060") | |
print partialNumberyRatio("10 1 600", "GVFX010_P01_060") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fuzzywuzzy import fuzz | |
import re | |
def findTokens(string): | |
"""split string into two sets of tokens, numbers and letters. preserves length by filling gaps with None""" | |
groups = [match.groupdict() for match in re.finditer("(?P<d>\d+)|(?P<c>[A-Za-z]+)", string)] | |
# return [group.get("d") or group.get("c") for group in groups] | |
return tuple([group.get("d") for group in groups]), tuple([group.get("c") for group in groups]) | |
def findNumbers(string): | |
"""get list of all numbers in a string""" | |
return re.findall("\d+", string) | |
def partialNumberyRatio(stringA, stringB): | |
stringA = stringA.lower() | |
stringB = stringB.lower() | |
aNums = "".join(findNumbers(stringA)) | |
numbersMatch = inStringInOrder(aNums, stringB) | |
textMatch = fuzz.partial_ratio(stringA, stringB) | |
print stringA, stringB, int(textMatch * numbersMatch) | |
return int(textMatch * numbersMatch) | |
def inStringInOrder(search, string): | |
"""@return: True if the characters in search exist in order in string""" | |
query = ".*".join(search) | |
return not (re.search(query, string) is None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment