wit.ai聊天机器人在python中创建

时间:2017-01-11 12:13:27

标签: python-2.7 python-3.x

wit.ai聊天机器人在python中创建

   def first_entity_value(entities, entity):
        if entity not in entities:
            return None
        val = entities[entity][0]['value']
        if not val:
            return None
        return val['value'] if isinstance(val, dict) else val

    def second_entity_value(entities, entity):
        if entity not in entities:
            return None
        val = entities[entity][1]['value']
        if not val:
            return None
        return val['value'] if isinstance(val, dict) else val

    def send(request, response):
        print(response['text'])

    def get_contact(request):
        context = request['context']
        entities = request['entities']
        print "*"*100
        loc = first_entity_value(entities, 'person')
        if loc:
            context['Merge'] = ''

    else:
        context['Merge'] = True
        if context.get('merge') is not None:
            del context['merge']

    return context

def start_quiz(request):
    context = request['context']
    entities = request['entities']
    print "*"*100
    loc = second_entity_value(entities, 'Reply')
    if loc:
        context['quiz'] = ''

    else:
        context['quiz'] = True
        if context.get('startQuiz') is not None:
            del context['startQuiz']

    return context

def check_answer(request):
    context = request['context']
    entities = request['entities']
    print "*"*100
    loc = second_entity_value(entities, 'answer')
    if loc:
        context['verify'] = ''

    else:
        context['verify'] = True
        if context.get('checkAnswer') is not None:
            del context['checkAnswer']

    return context


actions = {
    'send': send,
    'merge': get_contact,
    'startQuiz':start_quiz,
    'checkAnswer':check_answer

}

client = Wit(access_token='XXXXXXXXXXXXXXXXXXXXXXXXX', actions=actions)
client.interactive()

任何帮助都会得到赞赏

0 个答案:

没有答案