This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_logic.py
91 lines (68 loc) · 3.18 KB
/
bot_logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from wordle_solver import WORDLE_WORD_LENGTH, status_help_message, wordle_solver
class bot_logic:
DEFAULT_SUGGEST_N_WORDS = 10
flag_enter_word = True
flag_enter_result = False
entered_word, entered_result = "", ""
possible_words = {}
def __init__(self):
self.solver = wordle_solver()
def respond(self, input_text):
if input_text.lower() in ("hi", "hey", "yoyoyo"):
return "Hello"
input_text = input_text.lower() # Change to lower characters
input_text = input_text.strip() # remove leading/ending characters
if ' ' in input_text:
return "Oops! Looks like you entered more than 1 word"
if len(input_text) != WORDLE_WORD_LENGTH:
return "Oh no! You need to enter a " + str(WORDLE_WORD_LENGTH) + "-letter word"
if self.flag_enter_word:
if not input_text.isalpha():
return "This doesn't look like a word :("
self.entered_word = input_text
self.toggle_status()
return "What did Wordle say about the word?"
elif self.flag_enter_result:
if not input_text.isdecimal():
return "Hmm...I don't quite understand this format :(\nType /format for more info"
# maybe use inline kb here(??)
#fixme: code supposed to check that no other digits are input
# except for 0, 1, 2
'''
if not any((d in set("012")) for d in input_text):
return "wrong digit(s)"
'''
if input_text == "11111":
return "Yay!!! You got the correct answer already!\n(Say /thanks or /start on another word)"
self.entered_result = input_text
return self.run_solver()
return "default-response"
def run_solver(self):
assert len(self.entered_word) != 0
assert len(self.entered_result) != 0
self.possible_words = self.solver.try_word({ self.entered_word : self.entered_result })
self.toggle_status()
return self.suggest_words()
def suggest_words(self, num = DEFAULT_SUGGEST_N_WORDS):
length = len(self.possible_words.items())
limit = (num if num < length else length)
if length <= 0:
return "Sorry, I can't think of anything :'("
# Retrieve suggestions from dict
suggestions = list(self.possible_words.items())[:limit]
reply = 'I suggest trying "' + suggestions[0][0] + '"\n\n'
reply += "You can also try:"
for word_pair in suggestions:
if word_pair != suggestions[0]:
reply += "\n" + word_pair[0]
#TODO: can also display numbers to users (to make a better choice)
return reply
def result_format_message_str(self):
return status_help_message()
def toggle_status(self):
self.flag_enter_word = not self.flag_enter_word
self.flag_enter_result = not self.flag_enter_result
if __name__ == "__main__":
bot = bot_logic()
while(True):
print(bot.respond(input("> ")))