博客
关于我
Python模拟简易版淘宝客服机器人
阅读量:405 次
发布时间:2019-03-06

本文共 2551 字,大约阅读时间需要 8 分钟。

用Python制作简易版淘宝客服机器人

货物信息储存到MySQL数据库中

在实际场景中,购物信息通常存储在数据库中以便在问答时访问。使用Python的sqlalchemy包可以将表格导入到MySQL数据库中。以下是实现步骤:

import pymysqlimport pandas as pdimport sqlalchemy as sqlafrom sqlalchemy.orm import sessionmakerimport timeimport re# 导入数据data = pd.read_excel(r"E:/1/Study/大三下/自然语言处理/作业表.xlsx")data = data.head(1)# 创建数据库连接db = sqla.create_engine('mysql+pymysql://root:******@localhost/lsq?charset=utf8')# 导入数据到数据库data.to_sql('shopping_informations', db, index=False, if_exists='append')

在问答过程中访问数据库获取结果

使用pymysql包连接数据库,通过cursor()方法获取操作游标。定义函数分别查询发货时间、发货地、商品单号和商品状态,并处理异常:

conn = pymysql.connect("localhost","root","******","lsq")cursor = conn.cursor()def start_time():    start_time_sql = "SELECT START_TIME FROM shopping_informations"    try:        cursor.execute(start_time_sql)        result1 = cursor.fetchall()        print("亲您所购买的宝贝计划在%s进行发货~预计将会在%s不要着急哟" % result1[0])    except:        print("哎呀!机器客服这边暂时找不到相关数据呜呜呜~亲可以联系一下人工客服")        conn.close()def start_local():    start_local_sql = "SELECT START_LOCAL FROM shopping_informations"    try:        cursor.execute(start_local_sql)        result2 = cursor.fetchone()        print("亲您所购买的宝贝计划从%s发出~不要着急哟" % result2[0])    except:        print("哎呀!机器客服这边暂时找不到相关数据呜呜呜~亲可以联系一下人工客服")        conn.close()def ID():    id_sql = "SELECT ID FROM shopping_informations"    try:        cursor.execute(id_sql)        result3 = cursor.fetchone()        print("亲您所购买的宝贝单号是%s" % result3[0])    except:        print("哎呀!机器客服这边暂时找不到相关数据呜呜呜~亲可以联系一下人工客服")        conn.close()def state():    state_sql = "SELECT STATE FROM shopping_informations"    try:        cursor.execute(state_sql)        result4 = cursor.fetchone()        print("亲您所购买的宝贝现在在%s中~不要着急哟" % result4[0])    except:        print("哎呀!机器客服这边暂时找不到相关数据呜呜呜~亲可以联系一下人工客服")    conn.close()

利用正则表达式对问句进行识别

使用正则表达式分析客户输入,识别问题类型并选择相应答案:

str1 = ""def answer_robot(str1):    if re.search(r'.*快递(.*)?', str1):        return "亲!我们店统一默认发百世汇通,按仓择优分配快递,不能指定哟~请谅解!"    elif re.search(r'.*状态(.*)?', str1):        return state()    elif re.search(r'(\w)?[编号|单号|货号]\w', str1):        return ID()    elif re.search(r'(\w)?[哪里|发货地]\w', str1):        return start_local()    elif re.search(r'(\w)?[时间|时候]\w', str1):        return start_time()    else:        return "呜呜呜问题太复杂啦!建议亲找人工姐姐哟~"

主体函数实现

定义主体函数,处理客户输入并每次回答后休眠1秒:

def main():    while True:        str1 = input("输入对话:")        str3 = answer_robot(str1)        time.sleep(1)        print(str3)

优化方向

  • 并发处理:进一步利用1秒间隔时间,模拟多人同时使用客服机器人。
  • 正则表达式优化:从结果中可以看出,回答有时不够精确,需要优化正则表达式。
  • 数据库查询优化:直接获取整个数据库表,使用pandas的dataframe提取信息,减少多次查询。
  • 转载地址:http://ttdkz.baihongyu.com/

    你可能感兴趣的文章
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘html-webpack-plugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>