Example #1
0
def test():
    nlp = spacy.load("en")

    root_dir = sys.argv[1]
    sets = ["set1"]
    set_dict = preprocess_docs(root_dir, sets, nlp)
    #doc1 = set_dict[sets[0]][0]

    A = Answerer(set_dict, nlp)
    if use_terminal:
        questions = [unicode(raw_input("Question: "))]
    else:
        '''
        questions =\
            [
                u"What does the dog like to chase?",
                u"Is London a famous city?",
                u"Is Clint Dempsey a famous city?",
                u"Who was Clint Dempsey?",
                u"What does Clint Dempsey think about Communism?",
                u"Is Clint Dempsey in a good mood today?"
            ]
        '''
        questions =\
            [
                u"Who is Lionel Messi?",
                u"Where was Clint Dempsey born?",
                u"Is David Beckham American?",
                u"Which team won the league title in 2000?"
            ]

    for question in questions:
        answer = A.get_answer(question, 3)
        print "Q: {0} | A: {1}".format(question, answer)
Example #2
0
def answer_questions(doc_text, questions, format_answer):
    nlp = spacy.load("en")
    doc = preprocess(doc_text, nlp)
    A = Answerer(doc, nlp)
    for q in questions:
        if len(q) == 0:
            print " "
        else:
            answer = A.get_answer(q, 0, format_answer)
            print answer
Example #3
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é !")
Example #4
0
 def test_send_answer(self):
     ans = Answerer(1)
     test_ans_list = pd.DataFrame([{"text": "hello"}, {"text": "hello"}])
     ans.answer_list = test_ans_list
     answer = ans.get_answer()
     assert answer == "hello"