diff options
| author | 2020-02-05 11:48:07 +0700 | |
|---|---|---|
| committer | 2020-02-05 11:48:07 +0700 | |
| commit | c054790975670ee9e2b1855590d01491f3732b33 (patch) | |
| tree | 94439aeafb400db58ebb8c44f162322d0fb29a3c | |
| parent | Increased default thresholds from just [100, 80] to [100, 90, 80, 70, 60] (diff) | |
Removed regex, implemented a stricter letter searching.
| -rw-r--r-- | bot/cogs/tags.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 8122f739e..eaf307569 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -1,5 +1,4 @@ import logging -import re import time from typing import Dict, List, Optional @@ -20,8 +19,6 @@ TEST_CHANNELS = ( Channels.helpers ) -REGEX_NON_ALPHABET = re.compile(r"[^a-z]", re.IGNORECASE & re.MULTILINE) - class Tags(Cog): """Save new tags and fetch existing tags.""" @@ -46,13 +43,18 @@ class Tags(Cog): 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]: |