Created
October 13, 2021 02:39
-
-
Save mouseroot/b0b225d753d02ad02d3a4cecdab5a5cf to your computer and use it in GitHub Desktop.
Simplex Chat Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Bot Skeleton | |
import sys | |
class Bot: | |
"""Bot""" | |
def __init__(self,name) -> None: | |
self.name = name | |
self.memory = {} | |
self.running = True | |
self.words = [] | |
self.last_command = "" | |
self.dialog = { | |
"grettings":[], | |
"goodbye":[] | |
} | |
def load_dialog(self, dialog_type: str, filename: str) -> None: | |
with open(filename,"r") as dialog_file: | |
self.dialog[dialog_type] = dialog_file.readlines() | |
def get_random_dialog(self, dialog_type): | |
pass | |
def say(self,text: str) -> None: | |
"""Bot output""" | |
print(f"{self.name}\t{text}") | |
def input(self,text: str) -> None: | |
"""Takes input""" | |
try: | |
response = input(text) | |
self.last_command = response | |
self.words = response.split(" ") | |
self.words = [word.lower() for word in self.words] | |
except KeyboardInterrupt: | |
self.running = False | |
sys.exit(0) | |
def words_within(self, check_list: list) -> bool: | |
"""Check if ALL the words in check_list are in the words array""" | |
valid = True | |
for item in check_list: | |
if item in self.words: | |
valid = True | |
else: | |
valid = False | |
return valid | |
return valid | |
def any_words(self, check_list: list) -> bool: | |
"""Check if ANY of the check_list are in the words array""" | |
valid = False | |
for item in check_list: | |
if item in self.words: | |
return True | |
return valid | |
def get_next_word(self,word,offset=1): | |
"""Get the NEXT word | |
If the offset is given you can get the next word + offset | |
""" | |
index = self.words.index(word) | |
return self.words[index+offset] | |
def get_next_phrase(self, word): | |
"""Gets the remaining words after the given word""" | |
index = self.words.index(word) | |
return self.words[index:] | |
def get_prev_word(self,word,offset=1): | |
"""Get the PREVIOUS word | |
If the offset if given you can get previous word - offset | |
""" | |
index = self.words.index(word) | |
return self.words[index-offset] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment