AIML应答机器人(二)java实现
生活随笔
收集整理的這篇文章主要介紹了
AIML应答机器人(二)java实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章列表
AIML應答機器人(一)aiml簡介
AIML應答機器人(二)java實現
想做一款和上圖一樣的自動應答機器人嗎,跟著博客,咱們一步步實現,現在開始第二個內容,開始做一個java版的聊天程序
本文源碼地址:https://github.com/xvshu/alice_bot
源碼入口:
AIML工廠:AliceBotMother
/* Copyleft (C) 2005 Hélio Perroni Filho xperroni@yahoo.com ICQ: 2490863This file is part of ChatterBean.ChatterBean is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.ChatterBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with ChatterBean (look at the Documents/ directory); if not, either write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, or visit (http://www.gnu.org/licenses/gpl.txt). */package bitoflife.chatterbean;import bitoflife.chatterbean.parser.AliceBotParser; import bitoflife.chatterbean.util.Searcher;import java.io.ByteArrayOutputStream; import java.io.FileInputStream;public class AliceBotMother {/*Attribute Section*/private ByteArrayOutputStream gossip;/*Event Section*/public void setUp(){gossip = new ByteArrayOutputStream();}/*Method Section*/public String gossip(){return gossip.toString();}public AliceBot newInstance() throws Exception{Searcher searcher = new Searcher();AliceBotParser parser = new AliceBotParser();AliceBot bot = parser.parse(getClass().getResourceAsStream("/conf/context.xml"),getClass().getResourceAsStream("/conf/splitters.xml"),getClass().getResourceAsStream("/conf/substitutions.xml"),searcher.search(getClass().getResource("/Corpus/Chinese").toString().substring(5), ".*\\.xml"));Context context = bot.getContext(); context.outputStream(gossip);return bot;} }web入口AskServlet
package com.web;import com.context.ChartManager;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;public class AskServlet extends HttpServlet {private ChartManager chartManager = null;public AskServlet() {super();chartManager = ChartManager.getInstance();}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String askWord=request.getParameter("askWord");String outWord=chartManager.response(askWord);response.setContentType("text/html");response.getWriter().println(outWord);}@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {request.setCharacterEncoding("UTF-8");String askWord=request.getParameter("askWord");System.out.println("askWord:"+String.valueOf(askWord));String outWord=chartManager.response(askWord);System.out.println("outWord:"+String.valueOf(outWord));response.setContentType("text/html;charset=UTF-8");response.getWriter().println(outWord);} }html頁面
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>客服小A</title><script src='js/jquery-1.8.3.min.js'></script><script>$(function(){document.onkeydown = function(e){var ev = document.all ? window.event : e;if(ev.keyCode==13) {takeMsg();}}});</script><style type="text/css">.talk_con{width:600px;height:530px;border:1px solid #666;margin:50px auto 0;background:#f9f9f9;border-radius:10px 10px 10px 10px;}.talk_show{width:580px;height:420px;border:1px solid #666;background:#fff;margin:10px auto 0;overflow:auto;}.talk_input{width:580px;margin:10px auto 0;}.whotalk{width:80px;height:30px;float:left;outline:none;}.talk_word{width:500px;height:26px;padding:0px;float:left;margin-left:10px;outline:none;text-indent:10px;} .talk_sub{width:56px;height:30px;float:left;margin-left:10px;}.atalk{margin:10px auto;}.atalk span{display:inline-block;background:#0181cc;border-radius:10px;color:#fff;padding:5px 10px;margin:1px auto;}.btalk{margin:10px auto;text-align:right;}.btalk span{display:inline-block;background:#ef8201;border-radius:10px;color:#fff;padding:5px 10px;margin:1px auto;}iframe html body{zoom: 60%;}</style><script type="text/javascript"> //var count_msg=1;window.onload = function(){var Words = document.getElementById("words");var TalkWords = document.getElementById("talkwords");var TalkSub = document.getElementById("talksub");TalkSub.onclick = function(){takeMsg();}}function takeMsg(){Words = document.getElementById("words");TalkWords = document.getElementById("talkwords");TalkSub = document.getElementById("talksub");count_msg=count_msg+1;//定義空字符串var str = "";if(TalkWords.value == ""){// 消息為空時彈窗alert("消息不能為空");return;}//提問str = '<div id="msgtext_'+count_msg+'" class="btalk"><img style="width: 45px;" src="img/user.png"></img><span>' + TalkWords.value +'</span></div>' ;Words.innerHTML = Words.innerHTML + str;document.getElementById("msgtext_"+count_msg).scrollIntoView();count_msg=count_msg+1;$.get("/ask",{askWord:TalkWords.value},function(data){//ajax應答if(data.indexOf("url:")!=-1){str = '<div id="msgtext_'+count_msg+'" class="atalk"><img style="width: 45px;" src="img/kf.png"></img><span><iframe style="height: 550px;width: 380px;" scrolling="no" src="'+data.substring(4)+'"></iframe></span></div>';}else{str = '<div id="msgtext_'+count_msg+'" class="atalk"><img style="width: 45px;" src="img/kf.png"></img><span>' + data +'</span></div>';}Words.innerHTML = Words.innerHTML + str;document.getElementById("msgtext_"+count_msg).scrollIntoView();count_msg=count_msg+1;});}</script> </head> <body><div class="talk_con"><div class="talk_input"><span >AIML應答機器人--小A</span></div><div class="talk_show" id="words"><div class="atalk"><img style="width: 45px;" src="img/kf.png"></img><span id="asay">您好,很高興為您服務,請問有什么可以幫助您?</span></div></div><div class="talk_input"><input type="text" class="talk_word" id="talkwords"><input type="button" value="發送" class="talk_sub" id="talksub"></div></div> </body> </html>實現效果:
是不是非常簡單,只需要將git代碼clone到本地,初始化mysql(備用,擴展以后人工錄入),編寫自己公司的xml應答文件,我這里給公司大米時代(北京)編寫了一份,請大家參考:
<?xml version="1.0" encoding="UTF-8"?> <aiml><category><pattern>*米老師*</pattern><template><srai>米老師</srai></template></category><category><pattern>*米新江*</pattern><template><srai>米老師</srai></template></category><category><pattern>*新江*</pattern><template><srai>米老師</srai></template></category><category><pattern>*大米時代*創*人*</pattern><template><srai>米老師</srai></template></category><category><pattern>*提高班*創*人*</pattern><template><srai>米老師</srai></template></category><category><pattern>*米老師*語錄*</pattern><template><srai>米老師語錄</srai></template></category><category><pattern>*米新江*語錄*</pattern><template><srai>米老師語錄</srai></template></category><category><pattern>*新江*語錄*</pattern><template><srai>米老師語錄</srai></template></category><category><pattern>*新江*話*</pattern><template><srai>米老師語錄</srai></template></category><category><pattern>*米老師*話*</pattern><template><srai>米老師語錄</srai></template></category><category><pattern>*米教授*話*</pattern><template><srai>米老師語錄</srai></template></category><category><pattern>*米教授*語錄*</pattern><template><srai>米老師語錄</srai></template></category><category><pattern>*入*大米時代*</pattern><template><srai>報名大米時代</srai></template></category><category><pattern>*進*大米時代*</pattern><template><srai>報名大米時代</srai></template></category><category><pattern>*加*大米時代*</pattern><template><srai>報名大米時代</srai></template></category><category><pattern>*入*提高班*</pattern><template><srai>報名大米時代</srai></template></category><category><pattern>*進*提高班*</pattern><template><srai>報名大米時代</srai></template></category><category><pattern>*加*提高班*</pattern><template><srai>報名大米時代</srai></template></category><category><pattern>*大米時代*</pattern><template><srai>大米時代</srai></template></category><category><pattern>*提高班*</pattern><template><srai>大米時代</srai></template></category><category><pattern>大米時代</pattern><template>大米時代(北京)教育科技有限公司,前身為廊坊師范學院信息技術提高班,始于1993年,創建者為米新江教授。大米時代以“授之以欲、受之以愉、漁之于漁”為教育理念,主張“把錯誤放在現在,把成功放在未來”。大米時代總部位于北京市海淀區,現正在北京、廣州、珠海、鄂爾多斯、廊坊、衡水、張家口、三河等地陸續開設分校或運營中心。詳情請訪問大米時代官網:http://www.dmsdbj.com/</template></category><category><pattern>報名大米時代</pattern><template>座機:010-51292788 或者 0316-5552070, 手機(微信):1583163905,郵箱:3460307818@qq.com,地址:廊坊市廣陽區文明路與永豐道交叉口志晟創客中心四樓,我們期待您的來訪!詳情請訪問大米時代官網:http://www.dmsdbj.com/</template></category><category><pattern>米老師</pattern><template>米新江教授。任職于廊坊師范學院,至今從業近三十載。2001年創建了廊坊師范學院信息技術提高班,以獨特的教學方法和教育理念培養出一群“狼”一樣的學生,得到社會的廣泛認可。為教育界和IT界做出了突出貢獻。頗具社會影響力。2011年12月,清華大學計算機雜志社主編和多所名校(北京大學、天津師范大學、清華大學、北京交通大學等)的專家教授來信息技術提高班觀摩指導,對米新江教授的信息技術提高班教學理念和教育模式給予了肯定。其后,眾多知名雜志周刊、新聞網站對米新江教授及信息技術提高班的報道,引發了社會的高度關注。詳情請訪問大米時代官網:http://www.dmsdbj.com/</template></category><category><pattern>米老師語錄</pattern><template>你應該看看這個哦:<random><li>多一個朋友,多一條路;多一個仇人,多一堵墻。 </li><li>對我越是不喜歡的人,我越是客氣。 </li><li>對待生活,合適的編碼很重要。 </li><li>我在乎的不是你心里怎么想我的;我在乎的是你怎么對我的。 </li><li>你有什么理由不積極主動? </li><li>別人心里喜不喜歡你,對你一點也不重要,重要的是別人怎么對你。 </li><li>在你不喜歡一個人的時候,你可以試著對這個人好。如果這樣,慢慢你就會發現那個你不喜歡的人其實也挺好。 </li><li>學習是終生的,你什么時候停止學習,你什么時候就被淘汰。 </li><li>這個時代,賦予我們兩種選擇:要么被信息大潮吞沒;要么站在潮頭做弄潮兒! </li><li>我只不過是一座橋梁,一座橫跨世界的橋梁。 </li><li>郭靖有兩個東西:一個使擁有很多師父;另一個是在實戰中磨練。所以華山論劍,郭靖天下第一。 </li><li>授之以欲,受之以愉,漁之以魚。 </li><li>變是永遠不變的。 </li><li>不怕不知道,就怕不知道。 </li><li>光低頭學習就是一傻帽。 </li><li>不謀全局者不足謀一域,不謀萬世者不足謀一時。 </li><li>還有比不要臉更薄的臉嗎? </li><li>學多少都沒用,不學更沒用! </li><li>困難是進步的階梯 </li><li>多看幫助,少走彎路 </li><li>站在巨人的肩膀上 </li><li>教育是一門藝術 </li><li>學無止境 </li><li>一個講一百個函數的老師是個神經病,但是一個“傻瓜”學生想要質變就必須首先聽這個神經病老師的課,達到他的水平再去聽好老師的課,否則將一團霧水聽不明白最后只等于浪費生命...... </li><li>學習一種思想,邏輯思想,只有有了正確的思想才能準確而有效率的做事,建立一種全面的思考方式,基礎要明白,原理要明白,才會學會學習。合理安排時間,集中精力! </li><li>自己的興趣才是動力,自控能力至關重要!倘若正當學習的時候沒有學,會后悔的! </li><li>一邊體會精彩,一邊學會如何體會。 </li><li>倘若你不會表達,就是說明你思考得不成熟。 </li><li>問題是解決問題的開始。 </li><li>現在就要想:將來做一個怎樣的人。一分耕耘一分收獲。 </li><li>做人要明白,要有很高的業務水平和超強的自我控制能力、學習能力。 </li><li>做人就要做一個:高品德,對事物有正確認識,才會有高素質,才會有高才能 </li><li>學會去愛每一個人,為了國家為了民族的需要而活著! </li><li>要想成功,就要具備超常的耐力、毅力、動力!另外還有創新能力。 </li><li>要有堅定的信念,堅定的信念,沒有抱負哪有成功? </li><li>遇到困難的時候就是進步的時候,但前提是你必須知道或者明白如何把困難變成機會! </li><li>前途是光明的,道路是曲折的,面對堅信怎么辦:要有毅力! </li><li>越早強大起來生存機會就越大! </li><li>美是需要共享的,你的美得不到大家的認同,只有你自己享受,那就不能再叫作美了 </li><li>自信越用越多 </li><li>如果放棄一次,那么碰到下一個挫折時我就會繼續放棄;如果堅持而成功那么碰到下一個挑戰時我會激勵自己再取成功。 </li><li>別著急,別怕剛開始的時候慢…… </li><li>現在的大學教育是大眾教育不是精英教育,你上大學了,最多說明你是一個大眾人,找不到工作或者說畢業了不知道自己學了什么都挺正常的,因為你只是一個大眾人。想做杰出的人,就要做精英,而精英不是上天給的,都是自己努力的,outstanding!為什么?因為你站在巨人的肩上了,可是怎么才能爬到巨人的肩上呢?那就是hardwork。 </li><li>和你有關系嗎? </li><li>辦法總比困難多 </li></random>詳情請訪問大米時代官網:http://www.dmsdbj.com/</template></category></aiml>效果:
使用tomcat啟動,設置utf-8編碼支持中文
? <Connector port="8080" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" URIEncoding="UTF-8" />
總結
以上是生活随笔為你收集整理的AIML应答机器人(二)java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓手机管理软件_超强手机磁力搜索+播放
- 下一篇: 图片随意命名可能被广告拦截插件拦截