exclude aliases from appearing as duplicates

pull/1/head
qwewqa 4 years ago
parent 8d1efbac5b
commit aa16811c50
  1. 16
      miyu_bot/bot/master_asset_manager.py
  2. 30
      miyu_bot/commands/common/fuzzy_matching.py

@ -44,21 +44,21 @@ class MasterFilter:
self.default_filter = FuzzyFilteredMap(filter_function) self.default_filter = FuzzyFilteredMap(filter_function)
self.unrestricted_filter = FuzzyFilteredMap() self.unrestricted_filter = FuzzyFilteredMap()
for master in masters.values(): for master in masters.values():
name = romanize(naming_function(master)) name = naming_function(master)
if fallback_naming_function and self.default_filter.has_exact_unprocessed(name): if fallback_naming_function and self.default_filter.has_exact(name):
name = romanize(fallback_naming_function(master)) name = romanize(fallback_naming_function(master))
if self.default_filter.has_exact_unprocessed(name): if self.default_filter.has_exact(name):
continue continue
elif self.default_filter.has_exact_unprocessed(name): elif self.default_filter.has_exact(name):
continue continue
self.default_filter.set_unprocessed(name, master) self.default_filter[name] = master
self.unrestricted_filter.set_unprocessed(name, master) self.unrestricted_filter[name] = master
if aliases: if aliases:
for alias, mid in aliases.items(): for alias, mid in aliases.items():
master = masters[mid] master = masters[mid]
alias = romanize(alias) alias = romanize(alias)
self.default_filter.set_unprocessed(alias, master) self.default_filter.alias(alias, master)
self.unrestricted_filter.set_unprocessed(alias, master) self.unrestricted_filter.alias(alias, master)
def get(self, name_or_id: Union[str, int], ctx: Optional[commands.Context]): def get(self, name_or_id: Union[str, int], ctx: Optional[commands.Context]):
if ctx and ctx.channel.id in no_filter_channels: if ctx and ctx.channel.id in no_filter_channels:

@ -42,18 +42,13 @@ class FuzzyFilteredMap:
def has_exact(self, key): def has_exact(self, key):
return romanize(key) in self._values return romanize(key) in self._values
def has_exact_unprocessed(self, key):
return key in self._values
def __delitem__(self, key): def __delitem__(self, key):
k = romanize(key) k = romanize(key)
self._values.__delitem__(k) self._values.__delitem__(k)
self._stale = True self._stale = True
def __setitem__(self, key, value): def __setitem__(self, key, value):
self.set_unprocessed(romanize(key), value) key = romanize(key)
def set_unprocessed(self, key, value):
self._values[key] = value self._values[key] = value
new_cutoff = math.ceil(len(key) * 1.1) new_cutoff = math.ceil(len(key) * 1.1)
if new_cutoff > self.length_cutoff: if new_cutoff > self.length_cutoff:
@ -61,6 +56,9 @@ class FuzzyFilteredMap:
self.matcher.set_max_length(new_cutoff) self.matcher.set_max_length(new_cutoff)
self._stale = True self._stale = True
def alias(self, key, value):
self[key] = _Alias(value)
def __getitem__(self, key): def __getitem__(self, key):
start_time = timeit.default_timer() start_time = timeit.default_timer()
key = romanize(key) key = romanize(key)
@ -73,6 +71,8 @@ class FuzzyFilteredMap:
((matcher.score(key, item[0]), item) for item in self.filtered_items) ((matcher.score(key, item[0]), item) for item in self.filtered_items)
if score <= 0)[1][1] if score <= 0)[1][1]
self.logger.info(f'Found key "{key}" in time {timeit.default_timer() - start_time}.') self.logger.info(f'Found key "{key}" in time {timeit.default_timer() - start_time}.')
if isinstance(result, _Alias):
return result.value
return result return result
except ValueError: except ValueError:
self.logger.info(f'Found no results for key "{key}" in time {timeit.default_timer() - start_time}.') self.logger.info(f'Found no results for key "{key}" in time {timeit.default_timer() - start_time}.')
@ -84,12 +84,24 @@ class FuzzyFilteredMap:
self.logger.debug(f'Rejected key "{key}" due to length.') self.logger.debug(f'Rejected key "{key}" due to length.')
return [] return []
key = romanize(key) key = romanize(key)
values = [item[1] for score, item in values = [item[1].value if isinstance(item[1], _Alias) else item[1] for score, item in
sorted( sorted(
(self.matcher.score(key, item[0]), item) for item in self.filtered_items) (self.matcher.score(key, item[0]), item) for item in self.filtered_items)
if score <= 0] if score <= 0]
seen = set()
unique = []
for value in values:
if id(value) in seen:
continue
unique.append(value)
seen.add(id(value))
self.logger.info(f'Searched key "{key}" in time {timeit.default_timer() - start_time}.') self.logger.info(f'Searched key "{key}" in time {timeit.default_timer() - start_time}.')
return values return unique
class _Alias:
def __init__(self, value):
self.value = value
class FuzzyDictValuesView: class FuzzyDictValuesView:
@ -100,7 +112,7 @@ class FuzzyDictValuesView:
return item in self._map._values.values() and self._map.filter(item) return item in self._map._values.values() and self._map.filter(item)
def __iter__(self): def __iter__(self):
yield from (v for _, v in self._map.filtered_items) yield from (v for _, v in self._map.filtered_items if not isinstance(v, _Alias))
@dataclass @dataclass

Loading…
Cancel
Save