Exemple #1
0
def frame_conversation(utterance=None, user_id=None):
    last_system_utterance_info = db_linker.getLatestUtterance(user_id=user_id,
                                                              speaker='system')
    now_user_utterance_info = db_linker.getLatestUtterance(user_id=user_id,
                                                           speaker='user')
    last_user_utterance_info = db_linker.getLatestUtterance(
        user_id=user_id, speaker='last_user')
    #print(last_utterance_info)
    print(last_user_utterance_info)

    if last_system_utterance_info['intent_req'] == "frame_question":
        return react_frame_answer(utterance,
                                  last_user_utterance_info['utterance_id'],
                                  now_user_utterance_info['utterance_id'])
    else:
        frames = sentence_parser.Frame_Interpreter(utterance, target='v')
        print(frames)

        if len(frames) > 0:
            frame_log_id = save_frame_info(
                frames, now_user_utterance_info['utterance_id'])

            lu = frames[-1]['lu']
            empty_argument_list = get_frame_core_empty_argument(frames[-1])

            ## frame이 잡혔고, 비어있는 core element가 존재하는 경우
            if len(empty_argument_list) > 0:
                dialog_act = "frame_question"
                return frame_argument_question(
                    lu, empty_argument_list,
                    now_user_utterance_info['utterance_id'],
                    frame_log_id), dialog_act

    return None, None
Exemple #2
0
def user_access(user_name=None, user_id=None):
    result = db_linker.LookUpUsers()

    user_info = None

    for user in result['user_list']:
        if user_name:
            if user['user_name'] == user_name:
                user_info = user

        if user_id:
            if user['user_id'] == user_id:
                user_info = user

    if user_info:
        print(user_info)
    else:
        print('user_not_found')
        user_info = db_linker.AddNewUser(user_name)

    session_info = db_linker.AddNewSession(user_info['user_id'])

    result = {
        'session_info': session_info,
        'user_info': user_info
    }

    return result
Exemple #3
0
def save_frame_answer(frames, frame_question, utterance_id):
    print(frame_question)
    for denotation in frames[-1]['denotations']:
        datalist = []
        if denotation['role'] == 'TARGET':
            datadict = {
                'frame_log_id': frame_question['frame_log_id'],
                'utterance_id': utterance_id,
                'object': frame_question['question_argument'],
                'role': 'ARGUMENT'
            }
            datalist.append(datadict)
            frame_answered_denotation_id = db_linker.InsertDataToTable(
                'FRAME_ANSWERED_DENOTATION', datalist)
            for span in denotation['token_span']:
                datalist = []
                datadict = {
                    'frame_answered_denotation_id':
                    frame_answered_denotation_id,
                    'token_span': span
                }
                datalist.append(datadict)
                span_id = db_linker.InsertDataToTable(
                    'FRAME_ANSWERED_DENOTATION_SPAN', datalist)

        return denotation['text']
Exemple #4
0
def save_frame_info(frames, utterance_id):
    for frame in frames:

        datalist = []
        datadict = {
            'utterance_id': utterance_id,
            'lu': frame['lu'],
            'frame': frame['frame']
        }
        datalist.append(datadict)
        frame_log_id = db_linker.InsertDataToTable('FRAME_LOG', datalist)

        for denotation in frame['denotations']:
            datalist = []
            datadict = {
                'frame_log_id': frame_log_id,
                'object': denotation['obj'],
                'role': denotation['role']
            }
            datalist.append(datadict)
            denotation_log_id = db_linker.InsertDataToTable(
                'FRAME_DENOTATION', datalist)
            for token_span in denotation['token_span']:
                datalist = []
                datadict = {
                    'denotation_log_id': denotation_log_id,
                    'token_span': token_span
                }
                datalist.append(datadict)
                span_id = db_linker.InsertDataToTable('FRAME_DENOTATION_SPAN',
                                                      datalist)

    return frame_log_id
Exemple #5
0
def react_frame_answer(sentence, last_user_utterance_id, user_utterance_id):
    frames = sentence_parser.Frame_Interpreter(sentence, target='n')
    print(frames)
    print(user_utterance_id)
    ## 대답에서 frame을 잡은 경우
    if len(frames) > 0:
        frame_question = db_linker.getFrameQuestionByUtteranceID(
            last_user_utterance_id)

        obj = save_frame_answer(frames, frame_question, user_utterance_id)

        if frame_question:
            answer = frame_question['question_argument'] + '는 ' + obj + ' 이군요 '
            answer = answer + '감사합니다.'
        else:
            answer = "그렇군요."
        ## 질문이 더 남은 경우
        # if len(empty_argument_list) > 0:
        #     pre_system_dialog_act = 'frame_question'
        #     answer = answer + ' ' + frame_argument_question(frames)

        ## 질문이 더 남지 않은 경우
        # else:
        #    pre_system_dialog_act = None

    ## 대답에서 frame을 잡지 못한 경우
    else:
        answer = '죄송한데, 잘 이해를 못했어요.'
        return answer, "none"
    return answer, "frame_answer"
Exemple #6
0
def _lookUpSessionOfUser():
    print('_saveUtterance')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.LookUpSessionOfUser(user_id=myjson['user_id'],
                                           user_name=myjson['user_name'])
    return result
Exemple #7
0
def _getUtterances():
    print('_getUtterances')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.GetUtterances(user_id=myjson['user_id'],
                                     session_id=myjson['session_id'])
    return result
Exemple #8
0
def _addNewUser():
    print('_addNewUser')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    user_name = myjson['user_name']
    result = DB_Linker.AddNewUser(user_name)
    return result
Exemple #9
0
def _getUserInfo():
    print('_getUserInfo')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.GetUserInfo(user_id=myjson['user_id'],
                                   user_name=myjson['user_name'])
    return result
Exemple #10
0
def kb_agent(user_id=None, user_utterance=None, modules=[]):
    answer = '어떤 응답을 해야할지 모르겠어요.'
    last_system_utterance_info = db_linker.getLatestUtterance(user_id=user_id, speaker='system')

    if 'sparql_qa' in modules and last_system_utterance_info['intent_req'] not in ['frame_question', 'entity_question']:
        print('sparql_qa')
        sparql_answer, dialog_act = SPARQL_QA.sparql_conversation(user_utterance)
        if sparql_answer is not None:
            answer = sparql_answer
            return answer, 'none'

    if 'frame_qa' in modules and last_system_utterance_info['intent_req'] not in ['entity_question']:
        print('frame_qa')
        frame_answer, dialog_act = frame_QA.frame_conversation(user_id=user_id, utterance=user_utterance)
        if frame_answer is not None:
            answer = frame_answer
            return answer, dialog_act

    if 'knowledge_acquire' in modules:
        print('knowledge_qa')
        knowledge_answer, dialog_act = knowledge_question.knowledge_conversation(user_id=user_id,
                                                                                 user_utterance=user_utterance)
        if knowledge_answer is not None:
            answer = knowledge_answer
            return answer, dialog_act

    return answer, 'none'
Exemple #11
0
def respond_to_user_utterance(user_id=None, user_name=None, user_utterance=None, session_id=None, modules=[]):
    if user_id is None and user_name is None:
        return False
    user_info = db_linker.GetUserInfo(user_id=user_id, user_name=user_name)

    db_linker.SaveUtterance(user_id=user_info['user_id'], speaker='user', utterance=user_utterance,
                            session_id=session_id)

    answer, dialog_act = kb_agent(user_id=user_info['user_id'], user_utterance=user_utterance, modules=modules)
    print('answer', answer)
    db_linker.SaveUtterance(user_id=user_info['user_id'], speaker='system', utterance=answer, session_id=session_id,
                            intent_req=dialog_act)

    response = {
        'answer': answer
    }
    return response
Exemple #12
0
def _addNewSession():
    print('_addNewSession')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.AddNewSession(user_id=myjson['user_id'],
                                     model_id=myjson['model_id'],
                                     mission_id=myjson['mission_id'],
                                     feedback=myjson['feedback'])
    return result
Exemple #13
0
def _deleteUserListInfo():
    print('_deleteUserListInfo')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.DeleteUserListInfo(myjson['user_id'],
                                          myjson['user_interest_celeb'],
                                          myjson['user_interest_hobby'],
                                          myjson['user_interest_location'],
                                          myjson['user_topic'])
    return result
Exemple #14
0
def _queryToMasterKB():
    print('QueryToMasterKB')

    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    query = myjson['query']

    result = DB_Linker.QueryToMasterKB(query)
    print(result)
    return result
Exemple #15
0
def _updateUserInfo():
    print('_updateUserInfo')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.UpdateUserInfo(
        myjson['user_id'], myjson['user_name'], myjson['user_age'],
        myjson['user_birth'], myjson['user_gender'],
        myjson['user_current_city'], myjson['user_hometown'],
        myjson['user_professional'], myjson['user_job_title'])

    return result
Exemple #16
0
def _insertKnowledgeToUserKB():
    print('InsertKnowledgeToUserKB')

    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    user_name = myjson['user_name']
    triple = myjson['triple']
    print(triple)
    result = DB_Linker.InsertKnowledgeToUserKB(user_name, triple)

    return 'okay'
Exemple #17
0
def _saveUtterance():
    print('_saveUtterance')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.SaveUtterance(user_id=myjson['user_id'],
                                     utterance=myjson['utterance'],
                                     session_id=myjson['session_id'],
                                     speaker=myjson['speaker'],
                                     emotion=myjson['emotion'],
                                     intent_req=myjson['intent_req'],
                                     intent_emp=myjson['intent_emp'])
    return result
Exemple #18
0
def frame_argument_question(lu, empty_argument_list, utterance_id,
                            frame_log_id):
    question_argument = empty_argument_list.pop()
    frame_question = lu + '의 ' + question_argument + '는 무엇인가요?'
    datalist = []
    datadict = {
        'utterance_id': utterance_id,
        'frame_log_id': frame_log_id,
        'question_argument': question_argument
    }
    datalist.append(datadict)
    frame_question_id = db_linker.InsertDataToTable('FRAME_QUESTION', datalist)
    return frame_question
Exemple #19
0
def get_entity_question_list(user_name, entities, entity_type):
    question_property_list = prior_property[entity_type]
    question_num = 0
    question_list = []
    for candidate_property in question_property_list:
        if question_num == 3:
            break
        userdb_query = Knowledge_check(
            [entities[0]['uri'], candidate_property, '?o'], user_name)
        masterdb_query = Knowledge_check(
            [entities[0]['uri'], candidate_property, '?o'])

        masterdb_result = db_linker.QueryToMasterKB(masterdb_query)
        userdb_result = db_linker.QueryToUserKB(userdb_query)

        if masterdb_result['boolean'] == False and userdb_result[
                'boolean'] == False:
            question_list.append(
                [entities[0]['uri'], candidate_property, '?o'])
            question_num += 1

    return question_list
Exemple #20
0
def save_knowledge_to_database(triple, utterance_id):
    s, p, o = triple
    # s = s.split('/')[-1].rstrip('>')
    # p = p.split('/')[-1].rstrip('>')
    # o = o.split('/')[-1].rstrip('>')
    datalist = []
    datadict = {
        'utterance_id': utterance_id,
        'subject': s,
        'property': p,
        'object': o
    }
    datalist.append(datadict)
    db_linker.InsertDataToTable('USERKB_LOG', datalist)
Exemple #21
0
def sparql_conversation(sentence):
    sentence_template, replaced_word_dict = sentence_to_template(sentence)
    top_score, sparql_query = get_highest_similar_sparql(sentence_template)
    print(top_score)
    if top_score > 0.9:
        sparql_query = get_complete_sparql(replaced_word_dict, sparql_query)
        masterdb_result = db_linker.QueryToMasterKB(sparql_query)
        print(masterdb_result)
        if 'results' in masterdb_result:
            if len(masterdb_result['results']['bindings']) == 0:
                return '잘 모르겠어요'
            answer = json.dumps(masterdb_result['results']['bindings'], indent=4)
            return answer
        elif 'boolean' in masterdb_result:
            if masterdb_result['boolean'] == True:
                return '네 맞아요'
            else:
                return '아닌것 같아요'
        else:
            return '질문이 어려워요'
    return None, None
Exemple #22
0
def _getSessionInfo():
    print('_getSessionInfo')
    data = request.data.decode('utf-8')
    myjson = json.loads(data)
    result = DB_Linker.GetSessionInfo(myjson['session_id'])
    return result
Exemple #23
0
def _lookUpUsers():
    print('_lookUpUsers')

    result = DB_Linker.LookUpUsers()
    print(result)
    return result
Exemple #24
0
def knowledge_conversation(user_id=None, user_utterance=None):
    entities = sentence_parser.Entity_Linking(user_utterance)
    user_info = db_linker.GetUserInfo(user_id=user_id)
    user_name = user_info['user_name']
    print("entities: ", entities)
    answer = ''
    last_system_utterance_info = db_linker.getLatestUtterance(user_id=user_id,
                                                              speaker='system')
    now_user_utterance_info = db_linker.getLatestUtterance(user_id=user_id,
                                                           speaker='user')
    last_user_utterance_info = db_linker.getLatestUtterance(
        user_id=user_id, speaker='last_user')

    if last_system_utterance_info['intent_req'] == 'entity_question':
        entities = sentence_parser.Entity_Linking(user_utterance)
        answer = ''
        if len(entities) > 0:
            entity = entities[-1]['uri']
            question_info = db_linker.getTripleQuestion(
                last_user_utterance_info['utterance_id'])
            if len(question_info) == 0:
                return '질문이 뭐였는지 못찾았어요, 감사합니다.', 'entity_answer'
            triple = [
                question_info['subject'], question_info['property'], entity
            ]
            save_knowledge_to_database(triple,
                                       now_user_utterance_info['utterance_id'])
            db_linker.InsertKnowledgeToUserKB(user_name, [triple])
            answer += nlg_with_triple([triple], 'Knowledge_inform')
        else:
            answer = '무슨말씀이신지 잘 모르겠어요. 넘어갈게요!\n'

        dialog_act = 'entity_answer'
        answer += '감사합니다.'
        # if len(self.entity_question_triple_list) > 0:
        # 	self.question_triple = self.entity_question_triple_list.pop(0)
        # 	answer = answer + self.triple_question_generation(self.question_triple)
        #
        return answer, dialog_act

    ## entity가 잡힌 경우
    if len(entities) > 0:

        ## Entity summarization을 통해 정보 제공
        print(entities[0]['text'])

        # summarized_triples = entity_summarization.ES(entities[0]['text'])
        #
        # answer = nlg_with_triple(summarized_triples, 'Knowledge_inform')
        # print("summarized_triples: ", summarized_triples)
        if entities[0]['text'] in entity_summarized:
            summarized_triples = entity_summarized[entities[0]['text']]['top5']
            answer = nlg_with_triple(summarized_triples, 'Knowledge_inform')

        entity_type = get_entity_type(entities)

        print("entity_type: ", entity_type)

        ##entity type이 잡혀서 질문 목록 생성
        if entity_type is not None:
            entity_question_triple_list = get_entity_question_list(
                user_name, entities, entity_type)

        print("entity_question_triple_list: ", entity_question_triple_list)

        ## 질문 목록에 대해 질문 시작
        if len(entity_question_triple_list) > 0:
            answer = answer + entities[0]['text'] + '에 대해서 물어보고 싶은게 있어요.\n'
            question_triple = entity_question_triple_list.pop(0)
            print(question_triple)
            save_knowledge_to_database(question_triple,
                                       now_user_utterance_info['utterance_id'])
            answer = answer + triple_question_generation(question_triple)
            dialog_act = 'entity_question'

        return answer, dialog_act

    return None, None