Skip to content

Commit

Permalink
Removed regex, implemented a stricter letter searching.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikuyarihS committed Feb 5, 2020
1 parent a6341b1 commit c054790
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions bot/cogs/tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import re
import time
from typing import Dict, List, Optional

Expand All @@ -20,8 +19,6 @@
Channels.helpers
)

REGEX_NON_ALPHABET = re.compile(r"[^a-z]", re.IGNORECASE & re.MULTILINE)


class Tags(Cog):
"""Save new tags and fetch existing tags."""
Expand All @@ -46,13 +43,18 @@ async def _get_tags(self, is_forced: bool = False) -> None:
def _fuzzy_search(search: str, target: str) -> int:
"""A simple scoring algorithm based on how many letters are found / total, with order in mind."""
found, index = 0, 0
_search = REGEX_NON_ALPHABET.sub('', search.lower())
_target = REGEX_NON_ALPHABET.sub('', target.lower())
for letter in _search:
index = _target.find(letter, index)
if index == -1:
break
found += index > 0
_search = search.lower().replace(' ', '')
_targets = iter(target.lower())
_target = next(_targets)
try:
for letter in _search:
index = _target.find(letter, index)
while index == -1:
_target = next(_targets)
index = _target.find(letter)
found += 1
except StopIteration:
pass
return found / len(_search) * 100

def _get_suggestions(self, tag_name: str, thresholds: Optional[List[int]] = None) -> List[str]:
Expand Down

0 comments on commit c054790

Please sign in to comment.