NLP(十五) 聊天机器人

原文链接:http://www.one2know.cn/nlp15/

  • 对话引擎
    1.了解目标用户
    2.理解用于沟通得语言
    3.了解用户的意图
    4.应答用户,并给出进一步线索
  • NLTK中的引擎
    eliza,iesha,rude,suntsu,zen
import nltk

def builtinEngines(whichOne): # 测试自带的聊天机器人
    if whichOne == 'eliza':
        nltk.chat.eliza.demo()
    elif whichOne == 'iesha':
        nltk.chat.iesha.demo()
    elif whichOne == 'rude':
        nltk.chat.rude.demo()
    elif whichOne == 'suntsu':
        nltk.chat.suntsu.demo()
    elif whichOne == 'zen':
        nltk.chat.zen.demo()
    else:
        print('unknown built-in chat engine {}'.format(whichOne))

def myEngine(): # 自己的简单对话机器人
    chatpairs = (
        (r"(.*?)Stock price(.*)",
         ("Today stock price is 100",
          "I am unable to find out the stock price.")),
        (r"(.*?)not well(.*)",
         ("Oh, take care. May be you should cisit a doctor",
          "Did you take some medicine ?")),
        (r"(.*?)raining(.*)",
         ("It's monsoon season, what more do you expect ?",
          "Yes, it's good for farmers")),
        (r"How(.*?)health(.*)",
         ("I am always healthy.",
          "I am a program, super healthy!")),
        (r".*",
         ("I am good. How are you today ?",
          "What brings you here ?"))
    )
    def chat():
        print('!'*60)
        print(' >> my Engine << ')
        print('Talk to the program using normal english')
        print('='*60)
        print("Enter 'quit' when done")
        chatbot = nltk.chat.util.Chat(chatpairs,nltk.chat.util.reflections)
        chatbot.converse()
    chat()

if __name__ == "__main__":
    myEngine()
    for engine in ['eliza','iesha','rude','suntsu','zen']:
        print("=== demo of {} ===".format(engine))
        builtinEngines(engine)
        print()
posted @ 2019-07-05 16:34  鹏懿如斯  阅读(388)  评论(0编辑  收藏  举报