Exemple #1
0
 def inference(ctx: rs.ContextWrapper):
     """
     recognizes name in sentences like:
      - i am jj
      - my name toseban
      - dino
     """
     triple = ctx[nlp.prop_triples][0]
     if triple.is_question():
         return rs.Resign()
     pred = ctx[prop_predicate]
     answer_str = None
     if pred == "NAME" or pred in PREDICATE_SET:
         # TODO City, Country -> NLP NER also only recognizes locations...
         if triple.has_object():
             answer_str = triple.get_object().text
         elif len(ctx[nlp.prop_tokens]) == 1:
             answer_str = ctx[nlp.prop_tokens][0]
         elif len(ctx[nlp.prop_tokens]) == 2:
             answer_str = "%s %s" % (ctx[nlp.prop_tokens][0],
                                     ctx[nlp.prop_tokens][1])
     if answer_str:
         logger.debug(
             f"Inference: extracted answer '{answer_str}' for predicate {pred}"
         )
         ctx[prop_answer] = answer_str
Exemple #2
0
 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()
Exemple #3
0
 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()
Exemple #4
0
 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)
Exemple #5
0
 def analyse_ice_cream_suggestion_answer(ctx: rs.ContextWrapper):
     if ctx[prop_scoops] or ctx[prop_flavors]:
         return rs.Resign()
     elif ctx[nlp.prop_yesno].yes():
         ctx[rawio.prop_out] = verbaliser.get_random_successful_answer(
             "greet_general")
     elif ctx[nlp.prop_yesno].no():
         ctx[rawio.prop_out] = verbaliser.get_random_failure_answer(
             "greet_general")
Exemple #6
0
 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()
Exemple #7
0
 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()
Exemple #8
0
        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])
Exemple #9
0
 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()
Exemple #10
0
 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()
Exemple #11
0
 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()
Exemple #12
0
    def react(ctx: rs.ContextWrapper):
        """
        Retrieves memory node with the name, or creates a new one
        outputs a polite response.
        """
        onto: Ontology = mem.get_ontology()
        sess: Session = mem.get_session()
        inferred_answer = ctx[prop_answer]
        pred = ctx[prop_predicate]
        subject_path: str = ctx[prop_subject]
        if not subject_path:
            return rs.Resign()
        subject_node: Node = ctx[subject_path]
        assert inferred_answer

        if pred == "NAME":
            # If name was asked, it must be because the node is not yet persistent
            assert subject_node.get_id() < 0
            subject_node.set_name(inferred_answer)
            persistent_subject_node = retrieve_or_create_node(subject_node)
            # TODO: Workaround - see #83 - if this state would write to interloc:all,
            #  it would promise an interloc:all:pushed signal. This would be
            #  picked up by persqa:new_interloc.
            subject_node.set_node(persistent_subject_node)
            sess.update(subject_node)
            ctx[interloc.prop_persisted] = subject_node
        elif pred in PREDICATE_SET:
            relationship_type = onto.get_type(ONTOLOGY_TYPE_FOR_PRED[pred])
            relationship_node = Node(metatype=relationship_type)
            relationship_node.set_name(inferred_answer)
            relationship_node = retrieve_or_create_node(relationship_node)
            if relationship_node is not None:
                subject_node.add_relationships(
                    {pred: {relationship_node.get_id()}})
                sess.update(subject_node)

        ctx[rawio.prop_out] = verbaliser.get_random_successful_answer(
            pred).format(name=subject_node.get_name(), obj=inferred_answer)
        ctx[prop_predicate] = None
Exemple #13
0
    def roboyqa(ctx):
        """
        answers question regarding roboy by retrieving the information out of the neo4j roboy memory graph
        state gets triggered when nlp extracts a new triple: subject, predicate, object
        by analysing the triple the content of the question can be ascertained
        the necessary information is gathered using the neo4j memory session
        if the triple combination is known and the information could be retrieved an answer will be given

        list of questions that can be answered:
        - who are you?
        - what is your name?
        - how old are you?
        - what is your age?
        - what is your hobby?
        - what are your hobbies?
        - what do you like?
        - where are you from?
        - where do you live?
        - who is your father/dad?
        - who is your brother/sibling?
        - who is your friend?
        - what do you want to become?
        - what are you a member of?
        - what can you do?
        - what are your skills?
        - what have you learned?
        - what are your abilities?
        """
        sess: Session = ravestate_ontology.get_session()
        onto: Ontology = ravestate_ontology.get_ontology()

        roboy = Node(metatype=onto.get_type("Robot"))
        roboy.set_properties(ctx.conf(key=ROBOY_NODE_PROP_CONF_KEY))
        node_list = sess.retrieve(request=roboy)
        if node_list:
            roboy = node_list[0]
        else:
            create_default_nodes()
            node_list = sess.retrieve(request=roboy)
            if node_list:
                roboy = node_list[0]
            else:
                logger.error(
                    f"Seems like you do not have my memory running, or no node with properties"
                    f"{ctx.conf(key=ROBOY_NODE_PROP_CONF_KEY)} exists!")
                return rs.Resign()

        triple = ctx[nlp.prop_triples][0]

        category = None
        memory_info = None

        # question word: What?
        if triple.is_question(nlp.QuestionWord.OBJECT):
            if triple.match_either_lemma(pred={"like"}, subj={"hobby"}):
                category = "HAS_HOBBY"
            elif triple.match_either_lemma(pred={"learn"}, subj={"skill"}):
                category = "skills"
            elif triple.match_either_lemma(pred={"can"}, subj={"ability"}):
                category = "abilities"
            elif triple.match_either_lemma(subj={"age"}):
                category = "age"
                memory_info = roboy_age(roboy.get_properties(key="birthdate"))
            elif triple.match_either_lemma(subj={"name"}):
                category = "full_name"
                memory_info = roboy.get_properties(key=category)
            elif triple.match_either_lemma(pred={"become"}):
                category = "future"

        # question word: Where?
        elif triple.is_question(nlp.QuestionWord.PLACE):
            if triple.match_either_lemma(pred={"be"}):
                category = "FROM"
            elif triple.match_either_lemma(pred={"live"}):
                category = "LIVE_IN"

        # question word: Who?
        elif triple.is_question(nlp.QuestionWord.PERSON):
            if triple.match_either_lemma(obj={"father", "dad"}):
                category = "CHILD_OF"
            elif triple.match_either_lemma(obj={"brother", "sibling"}):
                category = "SIBLING_OF"
            elif triple.match_either_lemma(obj={"friend", "girlfriend"}):
                category = "FRIEND_OF"
            else:
                category = "full_name"
                memory_info = roboy.get_properties(key=category)
        elif triple.match_either_lemma(obj={"part", "member"}):
            category = "MEMBER_OF"

        # question word: How?
        elif triple.is_question(nlp.QuestionWord.FORM):
            if triple.match_either_lemma(pred={"old"}):
                category = "age"
                memory_info = roboy_age(roboy.get_properties(key="birthdate"))
            elif triple.match_either_lemma(pred={"be"}):
                category = "well_being"
        elif triple.match_either_lemma(obj={"skill"}):
            category = "skills"
        elif triple.match_either_lemma(obj={"ability"}):
            category = "abilities"

        if category and category.isupper() and not isinstance(
                roboy.get_relationships(key=category), dict):
            node_id = random.sample(roboy.get_relationships(key=category),
                                    1)[0]
            memory_info = sess.retrieve(node_id=int(node_id))[0].get_name()

        elif category and category.islower() and not isinstance(
                roboy.get_properties(key=category), dict):
            property_list = [
                x.strip()
                for x in roboy.get_properties(key=category).split(',')
            ]
            memory_info = random.sample(property_list, 1)[0]

        if memory_info:
            ctx[rawio.prop_out] = verbaliser.get_random_successful_answer(
                "roboy_" + category) % memory_info
        elif category == "well_being":
            ctx[rawio.prop_out] = verbaliser.get_random_successful_answer(
                "roboy_" + category)
        else:
            return rs.Resign()
Exemple #14
0
 def is_affectionate(ctx: rs.ContextWrapper):
     if any(l in ctx[nlp.prop_lemmas] for l in AFFECTIONATE_LIST) and \
             random.random() < ctx.conf(key=AFFECTIONATE_PROB_KEY):
         logger.debug(f"Emitting {sig_affectionate.name}")
         return rs.Emit()
     return rs.Resign()
Exemple #15
0
 def is_happy(ctx: rs.ContextWrapper):
     if random.random() < ctx.conf(key=HAPPY_PROB_KEY):
         logger.debug(f"Emitting {sig_happy.name}")
         return rs.Emit()
     return rs.Resign()
Exemple #16
0
 def is_surprised(ctx: rs.ContextWrapper):
     if random.random() < ctx.conf(key=SURPRISED_PROB_KEY):
         logger.debug(f"Emitting {sig_surprise.name}")
         return rs.Emit()
     return rs.Resign()