def start_payment(ctx: rs.ContextWrapper): payment_option = ctx[prop_payment_option] if payment_option == PaymentOptions.PAYPAL: ctx[rawio.prop_out] = verbaliser.get_random_phrase("paypal_option") return rs.Emit(wipe=True) elif payment_option == PaymentOptions.COIN: ctx[rawio.prop_out] = verbaliser.get_random_phrase("coin_option") return rs.Emit(wipe=True)
def ask_payment_method(ctx: rs.ContextWrapper): flavor_scoop_tuple_list = ctx[prop_flavor_scoop_tuple_list] complete_order, complete_cost = get_complete_order_and_cost( flavor_scoop_tuple_list) ctx[rawio.prop_out] = verbaliser.get_random_phrase("payment").format( cost=complete_cost, order=complete_order) return rs.Emit(wipe=True)
def name_the_label(ctx: rs.ContextWrapper): if (ctx[prop_waiting_for_label] or (ctx[prop_guess_attempt_count] > 0 and ctx[prop_guess_attempt_count] < 3)): message = ctx[prop_label_subscriber] if message: confidence = message.confidence[ ctx[prop_guess_attempt_count]] label = message.label[ctx[prop_guess_attempt_count]] if confidence < 0.5: conf_expr = verbaliser.get_random_phrase( "charades_lower_confidence_prediction") elif confidence < 0.9: conf_expr = verbaliser.get_random_phrase( "charades_higher_confidence_prediction") else: conf_expr = verbaliser.get_random_phrase( "charades_highest_confidence_prediction") ctx[rawio.prop_out] = conf_expr + " " + str(label) +\ " " + verbaliser.get_random_phrase( "charades_ask_for_feedback") logger.info( f"Label named {label}, attempt {ctx[prop_guess_attempt_count]}" ) ctx[prop_waiting_for_label] = False ctx[prop_feedback_received] = False ctx[prop_guess_attempt_count] = ctx[ prop_guess_attempt_count] + 1 return rs.Emit() else: ctx[rawio.prop_out] = BROKEN_MESSAGE else: return rs.Resign()
def payment_process(ctx: rs.ContextWrapper): @rs.receptor(ctx_wrap=ctx, write=rawio.prop_out) def say(ctx_input, value: str): ctx_input[rawio.prop_out] = value payment_option = ctx[prop_payment_option] price = ctx[prop_price] amount_paid, error_message = payment_communication( price, payment_option, ctx[prop_flavor_scoop_tuple_list]) if amount_paid == 0: say(verbaliser.get_random_phrase("no_payment")) elif amount_paid < price: say( verbaliser.get_random_phrase("amount_left").format( amount_left_over=amount_in_euros_and_cents(price - amount_paid))) ctx[prop_price] = price - amount_paid elif amount_paid == price: say(verbaliser.get_random_phrase("perfect_amount")) ctx[prop_payment_success] = True elif amount_paid > price: say( verbaliser.get_random_phrase("better_amount").format( amount_too_much=amount_in_euros_and_cents(amount_paid - price))) ctx[prop_payment_success] = True elif error_message: say(verbaliser.get_random_phrase("error_payment")) return rs.Emit(wipe=True)
def prompt_order(ctx: rs.ContextWrapper): has_already_asked = ctx[prop_suggested_ice_cream] if not has_already_asked: ctx[rawio.prop_out] = verbaliser.get_random_question( 'greet_general') ctx[prop_suggested_ice_cream] = True return rs.Emit(wipe=True)
def react_to_feedback(ctx: rs.ContextWrapper): if ctx[prop_game_in_progress]: rand = random.random() if ctx[nlp.prop_yesno].yes(): ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_winning_exclamations") if rand < 0.5: emotion = SUNGLASSES_ON_EMOTION ctx[prop_sunglasses_on] = True else: emotion = HEARTS_EMOTION if ctx.conf(key=USE_EMOTIONS): emo_client(emotion) ctx[prop_feedback_received] = True logger.info("correct") elif ctx[nlp.prop_yesno].no(): if ctx[prop_guess_attempt_count] < 3: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_new_guess_attempt") ctx[prop_another_attempt] = True logger.info("wrong guess") else: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_losing_exclamations") if ctx.conf(key=USE_EMOTIONS): emo_client(SHY_EMOTION) ctx[prop_feedback_received] = True else: ctx[prop_feedback_received] = False return rs.Emit() else: return rs.Resign()
def clarify_feedback(ctx: rs.ContextWrapper): if ctx[prop_game_in_progress] and not ctx[prop_feedback_received]: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_misunderstanding") + " " + \ verbaliser.get_random_phrase("charades_ask_for_feedback") return rs.Emit() else: return rs.Resign()
def yesno_detection(ctx: rs.ContextWrapper): if (ctx[nlp.prop_yesno].yes() or ctx[nlp.prop_yesno].no()) \ and not (FLAVORS & set(ctx[nlp.prop_lemmas]) or SCOOP_SYNONYMS & set(ctx[nlp.prop_lemmas])): # this is needed for a phrase like "yes, i want two scoops of chocolate" to not go directly into preparing phase ctx[prop_yesno_detection] = True ctx[prop_yesno_detection] = DetectionStates.IN return rs.Emit(wipe=True) ctx[prop_yesno_detection] = DetectionStates.OUT
def check_scoops_flavor_combined(ctx: rs.ContextWrapper): if -1 in prop_scoops.read(): ctx[rawio.prop_out] = verbaliser.get_random_phrase("error_scoops") ctx[prop_flavors] = [] ctx[prop_scoops] = [] elif (prop_flavors.read() and prop_scoops.read() and len(prop_flavors.read()) == len(prop_scoops.read()))\ or (not prop_flavors.read() and not prop_scoops.read() and prop_flavor_scoop_tuple_list.read()): current_order = [ x for x in zip(prop_flavors.read(), prop_scoops.read()) ] add_orders_together(current_order, prop_flavor_scoop_tuple_list.read()) ctx[prop_flavor_scoop_tuple_list] = current_order ctx[prop_flavors] = [] ctx[prop_scoops] = [] possibly_complete_order, _ = get_complete_order_and_cost( prop_flavor_scoop_tuple_list.read()) ctx[rawio.prop_out] = verbaliser.get_random_phrase( "legit_order").format(order=possibly_complete_order) return rs.Emit(wipe=True) elif len(prop_flavors.read()) > len(prop_scoops.read()): current_order = [(prop_flavors.read()[i], prop_scoops.read()[i]) for i in range(0, len(prop_scoops.read()))] add_orders_together(current_order, prop_flavor_scoop_tuple_list.read()) ctx[prop_flavor_scoop_tuple_list] = current_order ctx[prop_flavors] = prop_flavors.read()[len(prop_scoops.read()):] ctx[prop_scoops] = [] ctx[rawio.prop_out] = verbaliser.get_random_phrase("need_scoop") \ .format(flavor=fix_pronunciation(prop_flavors.read()[0])) return rs.Emit(wipe=True) elif len(prop_flavors.read()) < len(prop_scoops.read()): current_order = [(prop_flavors.read()[i], prop_scoops.read()[i]) for i in range(0, len(prop_flavors.read()))] add_orders_together(current_order, prop_flavor_scoop_tuple_list.read()) ctx[prop_flavor_scoop_tuple_list] = current_order ctx[prop_scoops] = prop_scoops.read()[len(prop_flavors.read()):] ctx[prop_flavors] = [] if prop_scoops.read()[0] == 1: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "need_flavor").format(scoop=prop_scoops.read()[0], s="") else: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "need_flavor").format(scoop=prop_scoops.read()[0], s="s") return rs.Emit(wipe=True)
def offer_game(ctx: rs.ContextWrapper): if not ctx[prop_game_in_progress]: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_offer_game") ctx[prop_game_stopped] = False return rs.Emit() else: rs.Resign()
def recognize_intent_charades(ctx: rs.ContextWrapper): ''' reacts to mentioning charades with a sig_mentioned_charades signal ''' if ('charade' in ctx[nlp.prop_lemmas] and not ctx[prop_game_in_progress]): return rs.Emit() else: return rs.Resign()
def after_scooping(ctx: rs.ContextWrapper): if (ROS_AVAILABLE and client.gh and client.get_result() is None) \ or (not ROS_AVAILABLE and not scooping_communication.stop_feedback): return rs.Resign() if ROS_AVAILABLE and client.gh and client.get_result( ) is not None and not client.get_result().success: ctx[rawio.prop_out] = verbaliser.get_random_phrase("unexpected") else: return rs.Emit(wipe=True)
def ask_to_continue(ctx: rs.ContextWrapper): if ctx[prop_feedback_received]: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_offer_another_round") if ctx[prop_sunglasses_on] and ctx.conf(key=USE_EMOTIONS): emo_client(SUNGLASSES_ON_EMOTION) ctx[prop_sunglasses_on] = False return rs.Emit() else: return rs.Resign()
def feedback_state(ctx: rs.ContextWrapper): if ROS_AVAILABLE and client.gh and (client.get_result() is not None) \ or (not ROS_AVAILABLE and scooping_communication.stop_feedback): return rs.Emit(wipe=True) if ROS_AVAILABLE and scooping_communication.feedback is not None: finished_scoops, status_message = scooping_communication.feedback if not scooping_communication.last_scooping_feedback_array == finished_scoops: scooping_communication.last_scooping_feedback_array = finished_scoops if status_message == "more time": ctx[rawio.prop_out] = verbaliser.get_random_phrase("time") else: scoops_left = finished_scoops.count(False) if scoops_left == 1: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "scoops_left").format(c=scoops_left, s="") elif scoops_left > 1: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "scoops_left").format(c=scoops_left, s="s") return rs.Emit(wipe=True)
def detect_payment_option(ctx: rs.ContextWrapper): detected_payment_option = False tokens = ctx[nlp.prop_tokens] triples = ctx[nlp.prop_triples] lemmas = ctx[nlp.prop_lemmas] logger.info("Entering payment detection") if triples[0].match_either_lemma(subj={"i"}) and \ triples[0].match_either_lemma(pred=PAY_SYNONYMS) and \ triples[0].match_either_lemma(obj=PAYMENT_OPTION_SYNONYMS) and \ not NEGATION_SYNONYMS & set(lemmas): # this case holds when customer answers the payment question using phrases like # "i would like to pay with cash please" # "i pay with paypal" # "can i pay with cash?" # it does not hold whenever the customer negates his expression, i.e. # "i don't want to pay with cash" # "i won't pay using paypal" detected_payment_option = True elif triples[0].match_either_lemma(pred=PAYMENT_OPTION_SYNONYMS) and \ not NEGATION_SYNONYMS & set(lemmas): # this case holds when the customer answers the payment question with # "paypal please" # "cash" detected_payment_option = True elif triples[0].match_either_lemma(pred={"with", "by", "in"}) and \ triples[0].match_either_lemma(obj=PAYMENT_OPTION_SYNONYMS) and \ not NEGATION_SYNONYMS & set(lemmas): # this case holds when the customer answers the payment question with # "paypal please" # "by cash" # "in coins" detected_payment_option = True elif triples[0].match_either_lemma(pred={"with", "by", "in"}) and \ triples[0].match_either_lemma(obj={"please"}) and \ not NEGATION_SYNONYMS & set(lemmas): # this case holds when the customer answers the payment question with # "with paypal please" detected_payment_option = True elif triples[0].match_either_lemma(pred={"let"}) and \ triples[0].match_either_lemma(obj=PAYMENT_OPTION_SYNONYMS) and \ not NEGATION_SYNONYMS & set(lemmas): # this case holds when customer answers the payment question using phrases like # "let me pay with cash please" detected_payment_option = True elif triples[0].match_either_lemma(obj=PAYMENT_OPTION_SYNONYMS) and \ not NEGATION_SYNONYMS & set(lemmas): detected_payment_option = True if detected_payment_option: logger.info("Exiting payment detection") ctx[prop_payment_option] = PaymentOptions.PAYPAL if "paypal" in tokens else PaymentOptions.COIN ctx[prop_payment_option_detection] = DetectionStates.IN return rs.Emit(wipe=True) ctx[prop_payment_option_detection] = DetectionStates.OUT
def analyse_finish_order_answer(ctx: rs.ContextWrapper): if ctx[nlp.prop_yesno].yes(): flavor_scoop_tuple_list = ctx[prop_flavor_scoop_tuple_list] complete_order, complete_cost = get_complete_order_and_cost( flavor_scoop_tuple_list) ctx[rawio.prop_out] = verbaliser.get_random_phrase( "preparing_order").format(order=complete_order) ctx[prop_price] = complete_cost * 100 # price is in cents return rs.Emit(wipe=True) elif ctx[nlp.prop_yesno].no(): ctx[rawio.prop_out] = verbaliser.get_random_phrase( "continue_order")
def waiting_for_complete_order(ctx: rs.ContextWrapper): asked_order_count = ctx[prop_asked_order_count] if asked_order_count < 3: ctx[prop_asked_order_count] = asked_order_count + 1 return rs.Emit(wipe=True) else: ctx[rawio.prop_out] = verbaliser.get_random_phrase("order_aborted") ctx[prop_flavor_scoop_tuple_list] = [] ctx[prop_flavors] = [] ctx[prop_scoops] = [] ctx[prop_suggested_ice_cream] = False ctx[prop_asked_order_count] = 1
def detect_ice_cream_desire(ctx: rs.ContextWrapper): triples = ctx[nlp.prop_triples] lemmas = ctx[nlp.prop_lemmas] if triples[0].match_either_lemma(subj={"i"}) and \ triples[0].match_either_lemma(pred=DESIRE_SYNONYMS) and \ triples[0].match_either_lemma(obj=ICE_CREAM_SYNONYMS) and \ not NEGATION_SYNONYMS & set(lemmas): # signal is emitted when customer expresses the wish for ice cream using phrases like # "i would like to have ice cream please" # "can i get some ice cream?" # "i want ice cream!" ctx[prop_ice_cream_desire_detection] = DetectionStates.IN return rs.Emit(wipe=True) ctx[prop_ice_cream_desire_detection] = DetectionStates.OUT
def start_recording(ctx: rs.ContextWrapper): if ctx.conf(key=USE_EMOTIONS): emo_client(HYPNO_EMOTION) resp = recognition_client() if resp: global count_round count_round = count_round + 1 logger.info(f"Round {count_round}") ctx[rawio.prop_out] = "Beep! Now let me think a little bit" ctx[prop_waiting_for_label] = True ctx[prop_guess_attempt_count] = 0 return rs.Emit() else: ctx[rawio.prop_out] = BROKEN_MESSAGE ctx[prop_stop_game] = True
def after_payment(ctx: rs.ContextWrapper): payment_success = ctx[prop_payment_success] if payment_success: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "luigi_farewell") ctx[prop_flavor_scoop_tuple_list] = [] ctx[prop_flavors] = [] ctx[prop_scoops] = [] ctx[prop_suggested_ice_cream] = False ctx[prop_price] = -1 ctx[prop_payment_option] = -1 ctx[prop_payment_success] = False ctx[prop_asked_order_count] = 1 ctx[prop_asked_payment_count] = 1 else: return rs.Emit(wipe=True)
def ping_activity_choice(ctx: rs.ContextWrapper): if ctx[prop_game_in_progress]: rand = random.random() pinged_times = ctx[prop_ping_choice_count] if pinged_times < 5: if rand < 0.4 and ctx.conf(key=USE_EMOTIONS): emo_client(LOOK_RIGHT_EMOTION) elif (rand > 0.4 and rand < 0.8 and ctx.conf(key=USE_EMOTIONS)): emo_client(LOOK_LEFT_EMOTION) ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_ping_activity_choice") ctx[prop_ping_choice_count] = pinged_times + 1 return rs.Emit() else: ctx[prop_stop_game] = True
def small_talk(ctx: rs.ContextWrapper): sess: Session = mem.get_session() interloc: Node = ctx[interloc_path] if interloc.get_id( ) < 0: # ask for name, if the interlocutor is not (yet) a persistent instance pred = "NAME" else: pred = find_empty_relationship(interloc.get_relationships()) ctx[prop_subject] = interloc_path if not ctx[prop_predicate]: if pred: logger.info(f"Personal question: intent={pred}") ctx[prop_predicate] = pred ctx[rawio.prop_out] = verbaliser.get_random_question(pred) else: unused_fup_preds = PREDICATE_SET.difference( used_follow_up_preds) if not unused_fup_preds: logger.info( f"Ran out of smalltalk predicates for {interloc_path}, committing suicide..." ) return rs.Delete(resign=True) pred = random.sample( PREDICATE_SET.difference(used_follow_up_preds), 1) pred = pred[0] used_follow_up_preds.add(pred) ctx[prop_predicate] = pred relationship_ids: Set[int] = interloc.get_relationships( pred) if len(relationship_ids) > 0: # Just to be safe ... object_node_list = sess.retrieve( node_id=list(relationship_ids)[0]) if len(object_node_list) > 0: ctx[rawio. prop_out] = verbaliser.get_random_followup_question( pred).format( name=interloc.get_name(), obj=object_node_list[0].get_name()) logger.info(f"Follow-up: intent={pred}") return rs.Emit() return rs.Resign() else: # While the predicate is set, repeat the question. Once the predicate is answered, # it will be set to None, such that a new predicate is entered. ctx[rawio.prop_out] = verbaliser.get_random_question( ctx[prop_predicate])
def process_rules_decision(ctx: rs.ContextWrapper): if not ctx[prop_game_in_progress]: rules = "You pick an activity " \ "and show it to me and I try to guess it. " \ "You will have 3 seconds for the demonstration. I will " \ "tell you when to start." if ctx[nlp.prop_yesno].no(): out = "Ok, let's get to the game then" elif ctx[nlp.prop_yesno].yes(): out = rules else: out = "I will count that as no." ctx[rawio.prop_out] = out ctx[prop_game_in_progress] = True return rs.Emit() else: return rs.Resign()
def react_to_choice(ctx: rs.ContextWrapper): if ctx[prop_game_in_progress]: if ("ready" in ctx[nlp.prop_lemmas] or "already" in ctx[nlp.prop_lemmas] or "reading" in ctx[nlp.prop_lemmas] or "reddit" in ctx[nlp.prop_lemmas] or "reggie" in ctx[nlp.prop_lemmas] or "meridian" in ctx[nlp.prop_lemmas] or "radio" in ctx[nlp.prop_lemmas]): ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_countdown") ctx[prop_ping_choice_count] = 0 return rs.Emit() else: ctx[prop_choice_unclear] = True else: return rs.Resign()
def react_to_continue_decision(ctx: rs.ContextWrapper): rand = random.random() if ctx[nlp.prop_yesno].yes(): ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_positive_expressions") + " Let's continue then" if rand < 0.7 and ctx.conf(key=USE_EMOTIONS): emo_client(LUCKY_EMOTION) return rs.Emit() elif ctx[nlp.prop_yesno].no(): ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_no_continuation") if ctx.conf(key=USE_EMOTIONS): emo_client(KISS_EMOTION) ctx[prop_stop_game] = True else: ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_misunderstanding") ctx[prop_continuation_unclear] = True
def process_play_decision(ctx: rs.ContextWrapper): if not ctx[prop_game_in_progress]: if ctx[nlp.prop_yesno].yes(): ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_positive_expressions") + \ " Do you want to hear the rules?" if ctx.conf(key=USE_EMOTIONS): emo_client(SMILEBLINK_EMOTION) return rs.Emit() elif ctx[nlp.prop_yesno].no(): if random.random() < 0.5 and ctx.conf(key=USE_EMOTIONS): emo_client(ROLL_EYES_EMOTION) ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_refuse_offer") ctx[prop_stop_game] = True else: ctx[prop_decision_unclear] = True ctx[rawio.prop_out] = verbaliser.get_random_phrase( "charades_misunderstanding") else: return rs.Resign()
def store_face_and_name(ctx: rs.ContextWrapper): tokens = ctx[nlp.prop_tokens] triples = ctx[nlp.prop_triples] if len(tokens) == 1: name = tokens[0] elif triples[0].get_object().text and triples[0].match_either_lemma( pred={"be"}): name = triples[0].get_object().text else: ctx["rawio:out"] = "Sorry, what was the name?" return rs.Emit() ctx["rawio:out"] = f"Got it, I'm sure I'll remember {name} next time I see that face!" # Create memory entry sess: Session = ravestate_ontology.get_session() onto: Ontology = ravestate_ontology.get_ontology() query = Node(metatype=onto.get_type("Person")) query.set_properties({"name": name}) node_list = sess.retrieve(query) if not node_list: node = sess.create(query) logger.info(f"Created new Node in scientio session: {node}") elif len(node_list) == 1: node = node_list[0] else: logger.error( f'Failed to create or retrieve Scientio Node for {name}!') return logger.info(f"Node ID for {name} in picture is {node.get_id()}!") # Store face vector with node id in redis try: redis_conn = redis.Redis(host=ctx.conf(key=REDIS_HOST_CONF), port=ctx.conf(key=REDIS_PORT_CONF), password=ctx.conf(key=REDIS_PASS_CONF)) redis_conn.set(node.get_id(), ctx["sendpics:face_vec"]) except redis.exceptions.ConnectionError as e: err_msg = "Looks like the redis connection is unavailable :-(" logger.error(err_msg) ctx[rawio.prop_out] = err_msg
def am_i_impatient(ctx: rs.ContextWrapper): return rs.Emit(wipe=True)
def am_i_bored_by_user(ctx: rs.ContextWrapper): """ Emits idle:bored-by-user idle:bored as emitted and there is a present interlocutor. """ if any(ctx.enum(interloc.prop_all)): return rs.Emit(wipe=True)
def am_i_bored(ctx: rs.ContextWrapper): """ Emits idle:bored signal if no states are currently partially fulfilled """ if ctx[rs.prop_activity] == 0: return rs.Emit(wipe=True)