python 3 - Clipspy模块使用

一、clipspy安装:

Clipspy底层是基于clips规则引擎开发、支持python3的一个模块,在python3的工程中,可以通过调用clipsy的API接口实现clips规则引擎。

在线安装:

pip install clipspy

当出现Successfully installed字样时,表示安装成功。

离线安装:

手动去pypi官网下载clipspy模块的whl包:

网址:clipspy · PyPI

根据自己的python版本及平台选择对应的文件进行下载:

然后执行以下命令:

后面clipspy-1.0.0-cp39-cp39-win_amd64.whl需根据自己下载的whl包修改。

pip install clipspy-1.0.0-cp39-cp39-win_amd64.whl

二、官方文档:

官方网站:CLIPS Python bindings — clipspy 1.0.0 documentation

clips规则引擎手册:https://clipsrules.sourceforge.io/documentation/v640/apg.pdf

clipspyAPI手册:CLIPS — clipspy 1.0.0 documentation

三、实践:

字符串编写规则,浅试一手:

# !/usr/bin/python3.9
# -*- coding: utf-8 -*-
# @Time: 2022/9/15 10:32
# @Author: [作者名]
# @E-mail: [邮箱]
# @FileName: test.py
# @Software: PyCharm

import clips


def send_msg(msg):
    print("send_msg " + msg)
    pass


myclips = clips.Environment()

# 清除规则
myclips.clear()

# 注册函数  Define the Python function within the CLIPS environment.
myclips.define_function(send_msg)

# 建立规则:通过?实现取参和传参,具体匹配后的操作可在send_msg函数里进行设置
# 也可以将规则都写入一个clp文件中,通过myclips.load("文件名")来加载
conditionstr = """
(defrule my-rule
  (my-fact ?first-slot)
  =>
  (send_msg  ?first-slot))
"""

# 建立规则
myclips.build(conditionstr)

# 清空事实列表
myclips.reset()

# 断言事实
myclips.assert_string("(my-fact test)")
# class clips.facts.Template assert_fact

# 执行引擎,返回激活的次数,int,可以通过比较该返回的int值,来确定匹配到了几条规则,规则是否具有唯一性
myclips.run()

文件编写规则:

rule.clp文件:

(defrule one-rule
  (my-fact 打电话)
  =>
  (send_msg  你想打给谁?)
)

(defrule two-rule
  (my-fact 打给)
  (who ?who)
  =>
  (send_msg  好的,现在拨号给 ?who)
)

(defrule three-rule
  (my-fact 谁最可爱)
  =>
  (send_msg  当然是你呀)
)

(defrule four-rule
  (my-fact 谢谢)
  =>
  (send_msg  不用客气!)
)

主程序:

# !/usr/bin/python3.9
# -*- coding: utf-8 -*-
# @Time: 2022/9/15 10:32
# @Author: [作者名]
# @E-mail: [邮箱]
# @FileName: test.py
# @Software: PyCharm
# 在CLIPS里,以"=>"符号为分界,分别叫Left-hand side (LHS),right-hand side(RHS)。可以叫左端,右端。

import clips

def send_msg(*msgs):
    result = ''
    for msg in msgs:
        result = ''.join([result, msg])
    print("sirin:" + result)
    pass

myclips = clips.Environment()

# 清除规则
myclips.clear()

# 注册函数  Define the Python function within the CLIPS environment.
myclips.define_function(send_msg)
# 也可以将规则都写入一个clp文件中,通过myclips.load("文件名")来加载

# 建立规则
myclips.load("rule.clp")

s = input("siri:请问有什么可以帮到你呢?\n我:")

while True:
    # 清空事实列表
    myclips.reset()
    # 用户输入问题

    if "打给" in s:
        myclips.assert_string("(my-fact 打给)")
        myclips.assert_string("(who %s)" % s[2:])
    else:
        myclips.assert_string("(my-fact %s)" % s)
    # class clips.facts.Template assert_fact

    # 执行引擎,返回激活的次数,int,可以通过比较该返回的int值,来确定匹配到了几条规则,规则是否具有唯一性
    result = myclips.run()

    if result == 0:
        print("siri:抱歉,我听不懂你在说什么。")

    s = input("我:")

执行结果:

clp规则文件中是根据clips要求的规则语法来书写的,如果想要让业务人员能够更加看懂以及自行添加规则,可以开发一套新的规则文件的书写语法,然后在程序中进行转换。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

merlin’s girl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值