AIML实现智能聊天机器人

最近在做个智能聊天机器人『我知道大家肯定要说对接图灵,小黄鸡啊,但是有时候我们需要根据自己的业务和专业领域内的一些东西做智能聊天,那这里又有人说了,用他们的添加词库啊,那我想说的对应自己业务相关的一些动态数据你咋添加.......自行补脑吧,如果您有啥高见欢迎反馈』,经过google baidu的一番搜索发现了一个aiml的智能聊天机器人,当然也在国内找到了一些教程,都写的非常好^_^

首先给大家提供一些资源的下载地址

首先是AIML网址:http://www.alicebot.org/

以及相关下载地址和文档:

http://www.alicebot.org/downloads/programs.html

http://www.geocities.ws/phelio/chatterbean/

另外附上chatterbean的源码 http://pan.baidu.com/s/1o7QwsFS,下载后解压在Sources目录里面

Sources目录里面的源码可直接加入你自己工程,就两个依赖jar  bsh.jar  junit.jar

maven pom如下

        <dependency>
		    <groupId>bsh</groupId>
		    <artifactId>bsh</artifactId>
		    <version>2.0b4</version>
		</dependency>
		<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.12</version>
		</dependency>

加入源码和配置好jar后你会发现有个类JavascriptInterpreter报错,原因是

import netscape.javascript.JSObject 这个引入找不到,这里有两种解决方法

第一种是直接干掉这个类,对,你没看错就是干掉JavascriptInterpreter这个类

干掉这个类之后你会发现ChatterBean这个类报错那么解决方法就是注释掉

//private final Interpreter javascript = new JavascriptInterpreter(this);  66行

//context.property("javascript.interpreter", javascript);   154行

然后就一切正常咯

154846_k9DS_867520.png最后工程就是这样的。

第二种是你去找对应的jar加入,话说是jdk里面,我是第一种方案,所以就没管了

 

到此我们的准备工作已经完成了

然后开始我们的编码了

一共两个类

public class AliceBotMother {

	private ByteArrayOutputStream gossip;

	public void setUp() {
		gossip = new ByteArrayOutputStream();
	}

	public String gossip() {
		return gossip.toString();
	}

	public AliceBot newInstance() throws Exception {
		Searcher searcher = new Searcher();
		AliceBotParser parser = new AliceBotParser();
		
		System.out.println(ClassLoader.getSystemResource("Bots/Alice/").getPath());
		
		AliceBot bot = parser.parse(
				new FileInputStream(ClassLoader.getSystemResource("Bots/context.xml").getPath()), 
				new FileInputStream(ClassLoader.getSystemResource("Bots/splitters.xml").getPath()),
				new FileInputStream(ClassLoader.getSystemResource("Bots/substitutions.xml").getPath()), 
				searcher.search(ClassLoader.getSystemResource("Bots/Alice/").getPath(), "/*.aiml"));
		
		Context context = bot.getContext();
		context.outputStream(gossip);
		return bot;
	}
}

需要注意的就是这些文件的路径

155153_pwWW_867520.png我这就是这么摆放的,仅供参考。

这些文件在http://pan.baidu.com/s/1o7QwsFS 这个里面下载后都有 您可以下载后瞄一下

第二个类,这个类就是我们的测试类咯,可以说是异常的简单

public class Test {
	public static void main(String[] args) throws Exception {
		AliceBotMother mother = new AliceBotMother();
		mother.setUp();
		AliceBot bot = mother.newInstance();

        //这里你可以自己搞个循环然后输入
		String reMsg = "推荐";
		//reMsg = AnalyzerHelp.spaceStr(reMsg);这里分词大家就自己折腾吧我使用的是lucene
		//System.out.println("分词结果:" + reMsg);
		String result = bot.respond(reMsg);
		System.out.println("AI:" + result); 
	}
}

 

哈哈,看看效果吧

160800_Mtjs_867520.png

 

没太多时间排版,凑合看下吧。。。。。

这里只是个简单的例子后续的自己业务一些相关动态数据包括一些标签扩展(标签类只需扩展TemplateElement即可)感兴趣的同学可以研究一下。

 

容我吟诗一首:

写字楼里写字间,写字间里程序员。

程序人员写程序,又拿程序换酒钱。

 

欢迎讨论交流哈,我邮箱:lwcloud@vip.qq.com

 

转载于:https://my.oschina.net/Thinkeryjgfn/blog/913320

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值