Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Without altering the human, robot instance solution in Clear category for Dialogues by kkkkk
VOWELS = "aeiouAEIOU"
class Chat:
def __init__(self):
self._text = ""
self._human = None
self._robot = None
def connect_human(self, human_obj):
"""Connect human to the chat."""
self._human = human_obj
def connect_robot(self, robot_obj):
"""Connect robot to the chat."""
self._robot = robot_obj
def show_dialogue(self, robot_dialogue=False):
dialogue = []
human_msgs = self._human.receive()
robot_msgs = self._robot.receive()
while True:
human_text = next(human_msgs)
robot_text = next(robot_msgs)
if not human_text and not robot_text:
break
if human_text:
if robot_dialogue:
human_text = ''.join("0" if symbol in VOWELS else "1"
for symbol in human_text)
dialogue.append(f'{self._human.id} said: {human_text}')
if robot_text:
if robot_dialogue:
robot_text = ''.join("0" if symbol in VOWELS else "1"
for symbol in robot_text)
dialogue.append(f'{self._robot.id} said: {robot_text}')
return "\n".join(dialogue)
def show_human_dialogue(self):
"""Return text in human dialogue form."""
return self.show_dialogue()
def show_robot_dialogue(self):
"""Return text in robot dialogue form.
Every vowel is replaced with a "0" and the other symbols should
be replaced with a "1".
"""
return self.show_dialogue(robot_dialogue=True)
class Interlocuter:
def __init__(self, name):
self.id = name
self._text = []
def send(self, text):
self._text.append(text)
def receive(self):
for text in self._text:
yield text
yield ""
class Human(Interlocuter):
def __init__(self, name):
Interlocuter.__init__(self, name)
class Robot(Interlocuter):
def __init__(self, serial_number):
Interlocuter.__init__(self, serial_number)
April 10, 2020
Comments: