コード例 #1
0
 def test_update_conversation_sentences(self):
     ans = Answerer(1)
     message = "test message 1. test message 2"
     test_df = pd.DataFrame([{"message": s} for s in message.split(".")])
     ans.update_conversation(message)
     assert test_df.equals(ans.conversation_data)
コード例 #2
0
async def on_message(message):
    bot_name = str(client.user).split("#")[0]
    target_name = str(message.author.name)
    str_author = str(message.author)
    prefix = "user_data"
    if message.author == client.user:
        return
    if message.channel.type == discord.ChannelType.private:
        print(target_name)
        print(active_sessions.keys())
        if target_name not in active_sessions.keys():
            # print("session not active")
            active_sessions[target_name] = False
            session_count[target_name] = 0
            msg_count[target_name] = 0

        if not active_sessions[target_name]:
            if f"Bonjour {bot_name}" in message.content:
                active_sessions[target_name] = True
                sentence_buffer[target_name] = ""
                if target_name not in target_modelers.keys():
                    target_modelers[target_name] = Modeler(target_name)
                    if not os.path.exists(f"{prefix}/{str_author}"):
                        os.makedirs(f"{prefix}/{str_author}")
                    target_modelers[target_name].save_profile(
                        f"{prefix}/{str_author}/{str_author}_profile.json")
                else:
                    target_modelers[target_name].load_profile(
                        f"{prefix}/{str_author}/{str_author}_profile.json")
                time.sleep(1)
                await message.channel.send(f"Bonjour {target_name} !")
                time.sleep(.7)
                await message.channel.send(
                    f"Je suis {bot_name}, le robot qui écoute les problèmes ! Mon rôle est de déchiffrer tes 'méta-programmes' afin d'identifier les meilleurs vecteurs d'amélioration selon ta personnalité."
                )
                time.sleep(1)
                await message.channel.send(
                    "Ainsi, j'aimerais que tu me parles d'un élément de ta vie que tu souhaiterais améliorer afin que l'on puisse ensemble l'analyser en profondeur. Cela peut être lié aux hobbies, au travail, aux relations ..."
                )
                time.sleep(1.5)
                await message.channel.send(
                    "Note: je ne réponds que lorsque que ton message sera terminé par un point."
                )
                session_count[target_name] += 1
                session_answerer = Answerer(session_count)
                session_answerer.load_answer_list("templates/meta_answers.csv")
                target_answerers[target_name] = session_answerer
                # TODO: Potentiellement demander si prise en compte des conversations passées si nb session > 1
                time.sleep(1.5)
                await message.channel.send(
                    f"De quoi allons-nous parler aujourd'hui ?")
                time.sleep(.7)
                await message.channel.send(
                    f"(Écrire 'Merci {bot_name}' pour mettre fin à la discussion)"
                )
            else:
                await message.channel.send(
                    f"Vous pouvez écrire 'Bonjour {bot_name}' pour lancer la discussion !"
                )
        else:
            if message.content == f"Merci {bot_name}":
                print("end_message")
                time.sleep(1)
                await message.channel.send(f"Bonne journée {target_name} !")
                active_sessions[target_name] = False
                if not os.path.exists(f"{prefix}/{str_author}"):
                    os.makedirs(f"{prefix}/{str_author}")
                print(target_modelers[target_name].profile)
                target_answerers[target_name].save_conversation_data(
                    f"{prefix}/{str_author}/{str_author}_{datetime.now()}_{session_count[target_name]}.csv"
                )
                target_modelers[target_name].save_profile(
                    f"{prefix}/{str_author}/{str_author}_profile.json")
            else:
                print("normal_message")
                session_answerer = target_answerers[target_name]
                session_modeler = target_modelers[target_name]
                if ("." in message.content) or ("!" in message.content) or (
                        "?" in message.content):
                    sentence_list = [
                        msg.strip()
                        for msg in re.split('[.!?]+', message.content)
                    ]
                    print(sentence_list)
                    if sentence_buffer[target_name] != "":
                        current_sentence = sentence_buffer[
                            target_name] + ' ' + sentence_list[0]
                    else:
                        current_sentence = sentence_list[0]
                    session_answerer.update_conversation(current_sentence)
                    session_modeler = session_modeler.update_profile(
                        current_sentence)
                    session_answerer.update_target_profile(
                        session_modeler.profile)
                    sentence_buffer[target_name] = ""
                    msg_count[target_name] += len(sentence_list[:-1])
                    for current_sentence in sentence_list[1:-1]:
                        session_answerer.update_conversation(current_sentence)
                        session_modeler = session_modeler.update_profile(
                            current_sentence)
                        session_answerer.update_target_profile(
                            session_modeler.profile)
                    if sentence_list[-1] == "":
                        sentence_buffer[target_name] = ""
                        session_answerer.nb_answers = msg_count[target_name]
                        response = session_answerer.get_answer()
                        response_time = max(
                            1.0, 0.2 * len(message.content.split(" ")))
                        time.sleep(response_time)
                        await message.channel.send(response)
                    else:
                        sentence_buffer[target_name] = sentence_list[-1]
                else:
                    if sentence_buffer[target_name] != "":
                        sentence_buffer[target_name] = sentence_buffer[
                            target_name] + ' ' + message.content
                    else:
                        sentence_buffer[target_name] = message.content

    else:
        await message.channel.send(f"Venez discutez par message privé !")
コード例 #3
0
 def test_get_message(self):
     ans = Answerer(1)
     message = "test message"
     test_df = pd.DataFrame([{"message": message}])
     ans.update_conversation(message)
     assert test_df.equals(ans.conversation_data)