Skip to content

Commit

Permalink
Reimplement fuzzy search logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-Guo committed Apr 29, 2024
1 parent d2c6bb6 commit 65d3c90
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions fastf1/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,31 +932,46 @@ def _strict_event_search(self, name: str):

def _fuzzy_event_search(self, name: str) -> "Event":

def _remove_common_words(event_name):
common_words = ["formula 1", str(self.year), "grand prix", "gp"]
event_name = event_name.casefold()

for word in common_words:
event_name = event_name.replace(word, "")

return event_name.replace(" ", "")

def _matcher_strings(ev):
strings = list()
if 'Location' in ev:
strings.append(ev['Location'])
strings.append(ev['Location'].casefold())
if 'Country' in ev:
strings.append(ev['Country'])
strings.append(ev['Country'].casefold())
if 'EventName' in ev:
strings.append(ev['EventName'].replace("Grand Prix", ""))
strings.append(_remove_common_words(ev["EventName"]))
if 'OfficialEventName' in ev:
strings.append(ev['OfficialEventName']
.replace("FORMULA 1", "")
.replace(str(self.year), "")
.replace("GRAND PRIX", ""))
strings.append(_remove_common_words(ev["OfficialEventName"]))
return strings

user_input = name
name = _remove_common_words(name)
max_ratio = 0
index = 0
for i, event in self.iterrows():
ratio = max(
[fuzz.ratio(val.casefold(), name.casefold())
[fuzz.ratio(val, name)
for val in _matcher_strings(event)]
)
if ratio > max_ratio:
max_ratio = ratio
index = i

if max_ratio != 100:
_logger.warning((
"Correcting user input "
f"'{user_input}' to'{self.loc[index].EventName}'"
)
)
return self.loc[index]

def get_event_by_name(
Expand Down

0 comments on commit 65d3c90

Please sign in to comment.