Skip to content

Commit

Permalink
Add suggestion when a move isn't found
Browse files Browse the repository at this point in the history
  • Loading branch information
TLNBS2405 committed Dec 20, 2023
1 parent d0dad7d commit 2878558
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ async def self(interaction: discord.Interaction, character_name: str, move: str)
await interaction.response.send_message(embed=moves_embed, ephemeral=False)
else:
character_move = json_movelist_reader.get_move(move, move_list)
move_embed = embed.move_embed(character, character_move)
await interaction.response.send_message(embed=move_embed, ephemeral=False)
if character_move:
move_embed = embed.move_embed(character, character_move)
await interaction.response.send_message(embed=move_embed, ephemeral=False)
else:
similar_moves = json_movelist_reader.get_similar_moves(character_move,move_list)
similar_moves_embed = embed.similar_moves_embed(similar_moves,character_name)
await interaction.response.send_message(embed=similar_moves_embed, ephemeral=False)
else:
error_embed = embed.error_embed(f'Character {original_character_name} does not exist.')
await interaction.response.send_message(embed=error_embed, ephemeral=False)
Expand Down
12 changes: 12 additions & 0 deletions src/module/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

MOVE_NOT_FOUND_TITLE = 'Move not found'
SUCCESS_COLOR = discord.Colour.from_rgb(50,168,82)
WARNING_COLOR = discord.Colour.from_rgb(253,218,13)
ERROR_COLOR = discord.Colour.from_rgb(220,20,60)

def _upper_first_letter(input :str) -> str:
Expand All @@ -15,6 +16,17 @@ def _upper_first_letter(input :str) -> str:
else:
return input

def similar_moves_embed(similar_moves, character_name):

command_list = []
for i in range(len(similar_moves)):
command_list.append(f'**{i + 1}**. {similar_moves[i]["input"]}')

embed = discord.Embed(title=MOVE_NOT_FOUND_TITLE, colour=WARNING_COLOR,
description='Similar moves from {}\n{}'
.format(character_name, '\n'.join(command_list)))
return embed

def move_list_embed(character, moves, move_type):
"""Returns the embed message for a list of moves matching to a special move type"""
desc_string = ''
Expand Down
52 changes: 51 additions & 1 deletion src/module/json_movelist_reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

from difflib import SequenceMatcher
from heapq import nlargest as _nlargest

from src.resources import const
import os, json

Expand Down Expand Up @@ -45,7 +49,6 @@ def get_move(input: str, character_movelist: dict):
result = list(filter(lambda x: (_is_command_in_alias(input, x)), character_movelist))
if result:
result[0]['input'] = result[0]['input'].replace("\\", "")
print(result[0])
return result[0]
return {}

Expand All @@ -67,3 +70,50 @@ def get_by_move_type(move_type: str, move_list: dict) -> list:
return list(set(result))
else:
return []


def _get_close_matches_indexes(word, possibilities, n=3, cutoff=0.6):
"""Use SequenceMatcher to return a list of the indexes of the best
"good enough" matches. word is a sequence for which close matches
are desired (typically a string).
possibilities is a list of sequences against which to match word
(typically a list of strings).
Optional arg n (default 3) is the maximum number of close matches to
return. n must be > 0.
Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
that don't score at least that similar to word are ignored.
"""

if not n > 0:
raise ValueError("n must be > 0: %r" % (n,))
if not 0.0 <= cutoff <= 1.0:
raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
result = []
s = SequenceMatcher()
s.set_seq2(word)
for idx, x in enumerate(possibilities):
s.set_seq1(x)
if s.real_quick_ratio() >= cutoff and \
s.quick_ratio() >= cutoff and \
s.ratio() >= cutoff:
result.append((s.ratio(), idx))

# Move the best scorers to head of list
result = _nlargest(n, result)

# Strip scores for the best n matches
return [x for score, x in result]

def get_similar_moves(move :str, move_list: dict) -> list[str]:

command_list = []
for move in move_list:
command_list.append(move["input"])

moves_indexes = _get_close_matches_indexes(_simplify_input(move["input"]), map(_simplify_input, command_list), 5, 0.7)

result = []
for index in moves_indexes:
result.append(move_list[index])

return result
6 changes: 6 additions & 0 deletions src/module/test/test_json_movelist_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def test_get_movelist_from_json(self):
result = json_movelist_reader.get_movelist("azucena")
self.assertEqual(result[0]["id"],"Azucena-1")

def test_get_similar_moves(self):

move_list = json_movelist_reader.get_movelist("azucena")
similar_moves = json_movelist_reader.get_similar_moves("fff3+4",move_list)
self.assertTrue(similar_moves[0])


def test_get_move(self):

Expand Down

0 comments on commit 2878558

Please sign in to comment.