Пример #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)
Пример #2
0
 def test_load_answer_list(self):
     test_df = pd.DataFrame([{"test": "hello"}, {"test": "world"}])
     test_df.to_csv(
         "test.csv",
         index=False)  # replace with IOStream to bypass file creation
     ans = Answerer(1)
     ans.load_answer_list("test.csv")
     assert test_df.equals(ans.answer_list)
Пример #3
0
 def test_save_cv(self):
     ans = Answerer(1)
     test_df = pd.DataFrame([{"test": 1}, {"test": 2}])
     ans.conversation_data = test_df
     ans.save_conversation_data("test.csv")
     test_df = pd.read_csv(
         "test.csv")  # replace with IOStream to bypass file creation
     assert test_df.equals(ans.conversation_data)
Пример #4
0
 def test_load_cv(self):
     test_df = pd.DataFrame([{"test": 1}, {"test": 2}])
     test_df.to_csv(
         "test.csv",
         index=False)  # replace with IOStream to bypass file creation
     ans = Answerer(1)
     ans.load_conversation_data("test.csv")
     assert test_df.equals(ans.conversation_data)
Пример #5
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
Пример #6
0
 def test_create(self):
     ans = Answerer(1)
     assert ans.session == 1
     assert ans.conversation_data.equals(pd.DataFrame())
     assert ans.response_strategy.strategy == "random"
     assert ans.answer_list.equals(pd.DataFrame())
     assert ans.target_profile is None
Пример #7
0
def main():
    with open('../data/questions_clean') as f:
        questions = f.read().split('\n')
    
    data = [[val.strip() for val in question.split('|')] for question in questions]

    # Grab set of already processed questions
    try:
        questions_processed = json.load(open('questions_processed.json'))
    except ValueError:
        questions_processed = {}

    answerer = Answerer()
    for entry in data:
        # Make sure we don't process an empty line
        if not bool(entry[0]):
            continue

        # Check if question was already processed
        question = entry[0]
        if question.decode('utf-8') in questions_processed:
            continue

        # Extract information from entry
        answers = entry[1:-1]
        right_answer = int(entry[-1])

        # Get raw counts from approaches
        raw_data = answerer.answer(question, answers)

        # Check if we got rate limited
        if raw_data['rate_limited']:
            print 'We got rate limited. Use a different IP or wait a while'
            exit()

        # Convert to three rows in the input data
        one_hot_right_answer = []
        for i in range(len(answers)):
            one_hot_right_answer.append(1 if i == right_answer else 0)

        decoded_question = question.decode('utf-8')
        questions_processed[decoded_question] = {}
        questions_processed[decoded_question]['right_answer'] = one_hot_right_answer
        questions_processed[decoded_question]['raw_data'] = raw_data
        # Save question as a processed question
        with open('questions_processed.json', 'w') as f:
            json.dump(questions_processed, f, indent=4, separators=(',', ': '))
Пример #8
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é !")
Пример #9
0
                                 ))
            t.start()
            t.join()
    elif data['type'] == 'broadcastEnded' and 'reason' not in data:
        print 'The broadcast ended'
        ws.close()
    if data_watch.check_data is True:
        print 'Still getting data...'
        data_watch.check_data = False
    if not DEBUG:
        on_message.logger.write(message)
        on_message.logger.write('\n')


print 'Instantiating Answerer class...make take a while'
on_message.solver = Answerer()
print 'Done Instantiating Answerer class!'
on_message.logger = open(parent_dir_name + '/data/log', 'a+')


def on_error(ws, error):
    print(error)
    print("### error ###")


def on_close(ws):
    print("### closed ###")


def on_open(ws):
    print("### opened ###")
Пример #10
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"
Пример #11
0
 def test_update_response_strategy(self):
     ans = Answerer(1)
     strategy = "opening"
     ans.update_response_strategy(strategy)
     assert ans.response_strategy.strategy == strategy
Пример #12
0
 def test_update_profile(self):
     ans = Answerer(1)
     profile = [0, 0, 0, 0]
     ans.update_target_profile(profile)
     assert ans.target_profile == profile
Пример #13
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)
Пример #14
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)