diff options
| author | 2019-02-28 12:26:38 +0100 | |
|---|---|---|
| committer | 2019-02-28 12:26:38 +0100 | |
| commit | 59b2546e85d33a31393d12ab3fcbe03a51b23b12 (patch) | |
| tree | 6a50cba5f29a4e211d3885baa363e7b179a6c570 /bot/seasons/valentines/myvalenstate.py | |
| parent | Update whoisvalentine.py to address requested changes (diff) | |
Add .myvalenstate
Closes #108
This pull request adds a beta version of the command myvalenstate.
This command sends an embed containing a users valenstate to the channel
it has been called in. The process used here is to first put the username
into a spaceless, lower case form and than compare it to the countries
listed under valenstates.json, put into the same form, using an implementation
of the Levenshtein algorithm.
This beta version already implements everything needed for the command to
work properly. The follwing things are missing, not meant for the future
full implementation or are subject to change if needed:
- Short comments (25 missing)
- name parameter in myvalenstate (not meant for staying/Debug)
- Object names in valenstates.json (subject to change) (1)
- Docstring for levenshtein (subject to change)
- Docstring for myvalenstate (subject to change)
(1) - The object names can be put into the right case to omit the operation
operation on line 54 (lower_state = state.lower().replace(' ', ''))
Diffstat (limited to 'bot/seasons/valentines/myvalenstate.py')
| -rw-r--r-- | bot/seasons/valentines/myvalenstate.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/bot/seasons/valentines/myvalenstate.py b/bot/seasons/valentines/myvalenstate.py new file mode 100644 index 00000000..f646cbe4 --- /dev/null +++ b/bot/seasons/valentines/myvalenstate.py @@ -0,0 +1,80 @@ +import collections +import json +import logging +from pathlib import Path +from random import choice + +import discord +from discord.ext import commands + +from bot.constants import Colours + +log = logging.getLogger(__name__) + +with open(Path('bot', 'resources', 'valentines', 'valenstates.json'), 'r') as file: + STATES = json.load(file) + # STATES.sort() + + +class MyValenstate: + def __init__(self, bot): + self.bot = bot + + def levenshtein(self, source, goal): + """ + Calculates the Levenshtein Distance between source and goal. + """ + if len(source) < len(goal): + return self.levenshtein(goal, source) + if len(source) == 0: + return len(goal) + if len(goal) == 0: + return len(source) + + pre_row = list(range(0, len(source) + 1)) + for i, source_c in enumerate(source): + cur_row = [i + 1] + for j, goal_c in enumerate(goal): + if source_c != goal_c: + cur_row.append(min(pre_row[j], pre_row[j + 1], cur_row[j]) + 1) + else: + cur_row.append(min(pre_row[j], pre_row[j + 1], cur_row[j])) + pre_row = cur_row + return pre_row[-1] + + @commands.command() + async def myvalenstate(self, ctx, *, name=None): + eq_chars = collections.defaultdict(int) + if name is None: + author = ctx.message.author.name.lower().replace(' ', '') + else: + author = name.lower().replace(' ', '') + + for state in STATES.keys(): + lower_state = state.lower().replace(' ', '') + eq_chars[state] = self.levenshtein(author, lower_state) + + matches = [x for x, y in eq_chars.items() if y == min(eq_chars.values())] + valenstate = choice(matches) + matches.remove(valenstate) + leftovers = f"{', '.join(matches[:len(matches)-2])}, and {matches[len(matches)-1]}" + + embed = discord.Embed( + title=f'Your Valenstate is {valenstate} \u2764', + description=f'{STATES[valenstate]["text"]}', + colour=Colours.pink + ) + + if len(matches) > 1: + embed.add_field( + name="But there are more!", + value=f"You have {len(matches)} more matches, these being {leftovers}." + ) + embed.set_image( + url=STATES[valenstate]["flag"] + ) + await ctx.channel.send(embed=embed) + + +def setup(bot): + bot.add_cog(MyValenstate(bot)) |