美文网首页
聊天机器人

聊天机器人

作者: 思想践行 | 来源:发表于2019-12-21 08:23 被阅读0次

编程讲究模块化责任分离,目的是提高代码的复用性。通过编写简单对话机器人了解代码从无到有,从糙到精的过程。

1、对话机器人的基本功能

  • 问名字
  • 问心情
  • 问喜欢的颜色

2、拍脑袋想代码

#问名字
print("What's your name?")
name = input()
print(f"Hello,{name}!")
#问心情
print("How are you today?")
feeling = input()
if "good" in feeling.lower():
  print("I'm feeling good too!")
else:
  print("sorry to hear that.")
#问颜色
import random
print("What's your favorite color?")
FavoriteColor = input()
Color = ['red','yellow','oringe','blue','purple','black','white']
print(f"You like {FavoriteColor}, that's a great color. My favorite color is {random.choice(Color)}!")

3、找共性

  • 流程是统一的:输出询问-输入回答-通过输入,输出回应。
  • 每个问题中都有重复性的代码比如input() ,在编程过程中,要尽量把重复的代码整理到同一个基类中复用。

想要整理出基类的框架,就要先将每一个问题进行封装,把它变成一个类。

#问名字
class Hello:                   #定义名为Hello的类
  def run():                   #定义函数run()
    print("What's your name?")   #run()的函数体
    name = input()             
    print(f"Hello,{name}!")
    return                       #run()函数的返回值,这里为空,所以不返回内容,也可以替换为``return None`或者省略掉不写。
Hello.run()                    #调用并运行Hello内的run()函数_如"问颜色"中调用的random.choice()函数是一样的。

在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回。
—— 廖雪峰的官方网站

#问心情
class Feeling:
  def run():
    print("How are you today?")
    feeling = input()
    if "good" in feeling.lower():
      print("I'm feeling good too!")
    else:
      print("sorry to hear that.")
Feeling.run()
#问颜色
import random
class Color:
  def run():
    print("What's your favorite color?")
    FavoriteColor = input()
    Color = ['red','yellow','oringe','blue','purple','black','white']
    print(f"You like {FavoriteColor}, that's a great color. My favorite color is {random.choice(Color)}!")
Color.run()

4、泛化公共基类

封装成一个个类之后,我们看看哪些是可以复用的,从而泛化出一个公共的基类。
主框架是

class XXX:
  def run():
    pass        #如果想定义一个什么事也不做的空函数,可以用pass语句
XXX.run()

这部分除了类名称,规则是一样的。定义的函数run()的共性上文有提到。

class Bot:
  def __init__(self):
    self.q = ""
    self.a = ""
  def _think(self,s):
    return s
  def run(self):
    print(self.q)
    self.a = input()
    print(self._think(self.a))

修改子类格式

class Feeling(Bot):
    def __init__(self):
        self.q = "How are you today?"
    def _think(self,s):
        if "good" in s.lower():
            print("I'm feeling good too!")
        else:
            print("sorry to hear that.")
Feeling().run()

运行结果多了一个None:


image.png

任何函数都是有返回值的,不管写不写return。所以要把`Feeling()._think()中的print改为return


image.png
image.png
写出来变成了这个样子:
#基类
class Bot:
  def __init__(self):#Python中,定义函数def 函数名(参数): 缩进写函数主体,return输出返回值。
    self.q = "" #这里有点懵,为什么可以突然出现q和a?
    self.a = ""#方法可以在 self 之后带任意的输入参数,这些输入参数由方法自行使用;self.a 定义了一个实例变量(instance variable),前面说了 self 代表未来被实例化的对象自身,. 表示属于对象的,a 则是这个实例变量的名字,这个方法用传进来的参数 a 来初始化实例变量 self.a,要注意这两个 a 是不一样的;
  def _think(self,s):#函数定义在类里,通常我们称之为方法(method)
    return s
  def run(self):
    print(self.q)
    self.a = input()
    print(self._think(self.a))
#子类——问名字
class Hello(Bot): 
  def __init__(self):
    self.q = "What's your name?"
  def _think(self,s):       #有下划线的方法在类外部最好不要调用,可能会更改。
    return f"Hello,{s}!"
Hello().run()   
#子类——问心情
class Feeling(Bot):
    def __init__(self):
        self.q = "How are you today?"
    def _think(self,s):
        if "good" in s.lower():
            return "I'm feeling good too!"
        else:
            return "sorry to hear that."
Feeling().run()
#子类——问颜色
import random
class Color(Bot):
  def __init__(self):
    self.q = "What's your favorite color?"
    self.a = ""
  def _think(self,s):
    Color = ['red','yellow','oringe','blue','purple','black','white']
    return f"You like {s}, that's a great color. My favorite color is {random.choice(Color)}!"
Color().run()

关于类变量与实例变量的参考文献

相关文章

  • 聊天机器人分类

    从聊天机器人是否智能来分的话,可以分为脚本型聊天机器人和智能聊天机器人。 脚本型聊天机器人 它们其实也被称为快速回...

  • 使用TensorFlow实现Sequence to Sequen

    聊天机器人 我们说的聊天机器人是指智能聊天机器人。现在主要用在对话系统(Dialogue Systems),问答系...

  • 可汗精读《自然语言处理实践》01聊天机器人概述

    聊天机器人概述 聊天机器人的发展历史 通过自然语言模拟人类,进而与人进行对话的程序 聊天机器人溯源及发展 1950...

  • 什么是聊天机器人

    什么是聊天机器人?What is ChatBoot? 聊天机器人的应用 目前机器人还是存在于固定领域,业务型的机器...

  • 学习笔记:《如何打造你自己的聊天机器人》系列课程1/2讲

    以下笔记整理自《如何打造你自己的聊天机器人》系列课程之 第一讲《聊天机器人的前世今生》 第二讲《聊天机器人的关键技...

  • 七种功能强大的聊天机器人平台

    摘要:本文主要介绍了七种功能强大的聊天机器人开发平台的特点与优点。 聊天机器人发布平台是用户访问和使用聊天机器人的...

  • 聊天机器人入门篇

    分享几个介绍聊天机器人的相关文章,链接如下: 1.干货篇:一文看懂聊天机器人所有猫腻 2.多轮对话聊天机器人开发也...

  • 聊天机器人概述

    1. 聊天机器人的发展历史 聊天机器人,是一种通过自然语言模拟人类,进而与人进行对话的程序。 1.1 聊天机器人溯...

  • 全球哪些银行在用聊天机器人?

    聊天机器人和机器人投资顾问的出现,正在迅速改变这人们的储蓄、转账和资金管理方式。 金融银行领域聊天机器人(Chat...

  • (微信)聊天机器人--实用案例

    关于微信聊天机器人或者聊天机器人,大家所熟知的就是图灵机器人,聊天比较灵活,可以承担一部分客服的工作。除了图灵机器...

网友评论

      本文标题:聊天机器人

      本文链接:https://www.haomeiwen.com/subject/yibwnctx.html