├── src ├── main │ ├── resources │ │ ├── Index │ │ │ ├── write.lock │ │ │ ├── _0.cfe │ │ │ ├── _0.cfs │ │ │ ├── _0.si │ │ │ └── segments_2 │ │ ├── gossip.txt │ │ ├── myinput.txt │ │ ├── Corpus │ │ │ ├── English │ │ │ │ ├── reduction4.safe.aiml │ │ │ │ ├── phone.aiml │ │ │ │ ├── iu.aiml │ │ │ │ ├── continuation.xml │ │ │ │ ├── literature.aiml │ │ │ │ ├── history.xml │ │ │ │ ├── astrology.xml │ │ │ │ ├── stack.aiml │ │ │ │ ├── reductions-update.aiml │ │ │ │ ├── interjection.aiml │ │ │ │ └── music.aiml │ │ │ └── Chinese │ │ │ │ ├── gossip.xml │ │ │ │ ├── Plan.xml │ │ │ │ ├── Healthy.xml │ │ │ │ ├── MyInfo.xml │ │ │ │ ├── Praise.xml │ │ │ │ ├── Film.xml │ │ │ │ ├── Inherence.xml │ │ │ │ ├── Master.xml │ │ │ │ ├── Book.xml │ │ │ │ ├── Computer.xml │ │ │ │ ├── Photography.xml │ │ │ │ ├── Constellation.xml │ │ │ │ ├── Encourage.xml │ │ │ │ ├── External.xml │ │ │ │ ├── Life.xml │ │ │ │ ├── Music.xml │ │ │ │ ├── Food.xml │ │ │ │ ├── Travel.xml │ │ │ │ ├── main.xml │ │ │ │ ├── Internet.xml │ │ │ │ ├── Job.xml │ │ │ │ └── Game.xml │ │ ├── robot.properties │ │ └── conf │ │ │ ├── splitters.xml │ │ │ └── context.xml │ ├── webapp │ │ ├── img │ │ │ └── favicon.ico │ │ └── WEB-INF │ │ │ └── web.xml │ └── java │ │ ├── com │ │ ├── aiml │ │ │ ├── IAskApproach.java │ │ │ ├── AskToAIML.java │ │ │ ├── AskToDB.java │ │ │ └── IndexSearchService.java │ │ ├── customexception │ │ │ └── AppException.java │ │ ├── util │ │ │ ├── Translate.java │ │ │ ├── ChineseSegmenter.java │ │ │ └── Util.java │ │ ├── context │ │ │ ├── Robot.java │ │ │ ├── ChartManager.java │ │ │ └── ChartContext.java │ │ ├── web │ │ │ └── AskServlet.java │ │ └── background │ │ │ └── RobotProperty.java │ │ └── bitoflife │ │ └── chatterbean │ │ ├── config │ │ └── TokenizerConfig.java │ │ ├── aiml │ │ ├── AIMLElement.java │ │ ├── Sr.java │ │ ├── AIMLParserConfigurationException.java │ │ ├── TemplateElementTest.java │ │ ├── Think.java │ │ ├── AIMLParserException.java │ │ ├── AIMLStack.java │ │ ├── Size.java │ │ ├── Lowercase.java │ │ ├── Uppercase.java │ │ ├── Version.java │ │ ├── Template.java │ │ ├── Person.java │ │ ├── Gender.java │ │ ├── Person2.java │ │ ├── CategoryTest.java │ │ ├── Text.java │ │ ├── Random.java │ │ ├── Gossip.java │ │ ├── Date.java │ │ ├── Id.java │ │ ├── Bot.java │ │ ├── Srai.java │ │ ├── Learn.java │ │ ├── Formal.java │ │ ├── Sentence.java │ │ ├── Get.java │ │ ├── SystemTest.java │ │ ├── Topicstar.java │ │ ├── Thatstar.java │ │ ├── Star.java │ │ ├── AIMLParser.java │ │ ├── Set.java │ │ ├── Javascript.java │ │ ├── Li.java │ │ ├── System.java │ │ ├── Input.java │ │ ├── Aiml.java │ │ ├── Topic.java │ │ └── TemplateElement.java │ │ ├── parser │ │ ├── ReflectionBuilder.java │ │ ├── AliceBotParserException.java │ │ ├── AliceBotParserConfigurationException.java │ │ ├── SplitterHandler.java │ │ ├── ContextParserTest.java │ │ ├── ContextParser.java │ │ └── ReflectionHandler.java │ │ ├── script │ │ ├── Interpreter.java │ │ ├── InterpretingException.java │ │ ├── BeanshellInterpreter.java │ │ └── JavascriptInterpreter.java │ │ ├── util │ │ ├── SequenceMother.java │ │ ├── Escaper.java │ │ ├── SearcherTest.java │ │ ├── SequenceTest.java │ │ ├── Sequence.java │ │ └── Searcher.java │ │ ├── text │ │ ├── TransformationsMother.java │ │ ├── TokenizerMother.java │ │ ├── Response.java │ │ ├── SentenceTest.java │ │ ├── TokenizerTest.java │ │ ├── SentenceSplitterTest.java │ │ └── Request.java │ │ ├── ChatterBeanException.java │ │ ├── MatchTest.java │ │ ├── GraphmasterMother.java │ │ ├── ContextPropertyChangeListener.java │ │ ├── Logger.java │ │ ├── ContextRandomSeedChangeListener.java │ │ ├── AliceBotMother.java │ │ ├── ContextTopicChangeListener.java │ │ └── LoggerTest.java └── test │ └── java │ ├── BigDecimalTest.java │ └── MainTest.java ├── doc ├── dmsd.jpg ├── lean1.jpg ├── lean2.jpg ├── main.jpg ├── 聊天机器人文档.doc └── 聊天机器人应用描述.doc ├── README.md └── database └── db_aiml.sql /src/main/resources/Index/write.lock: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/resources/gossip.txt: -------------------------------------------------------------------------------- 1 | 你的主人帅吗:帅到爆棚 -------------------------------------------------------------------------------- /src/main/resources/myinput.txt: -------------------------------------------------------------------------------- 1 | 你的主人帅吗? 2 | 淘宝是什么 -------------------------------------------------------------------------------- /doc/dmsd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/doc/dmsd.jpg -------------------------------------------------------------------------------- /doc/lean1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/doc/lean1.jpg -------------------------------------------------------------------------------- /doc/lean2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/doc/lean2.jpg -------------------------------------------------------------------------------- /doc/main.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/doc/main.jpg -------------------------------------------------------------------------------- /doc/聊天机器人文档.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/doc/聊天机器人文档.doc -------------------------------------------------------------------------------- /doc/聊天机器人应用描述.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/doc/聊天机器人应用描述.doc -------------------------------------------------------------------------------- /src/main/resources/Index/_0.cfe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/src/main/resources/Index/_0.cfe -------------------------------------------------------------------------------- /src/main/resources/Index/_0.cfs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/src/main/resources/Index/_0.cfs -------------------------------------------------------------------------------- /src/main/resources/Index/_0.si: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/src/main/resources/Index/_0.si -------------------------------------------------------------------------------- /src/main/webapp/img/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/src/main/webapp/img/favicon.ico -------------------------------------------------------------------------------- /src/main/resources/Index/segments_2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/src/main/resources/Index/segments_2 -------------------------------------------------------------------------------- /src/main/java/com/aiml/IAskApproach.java: -------------------------------------------------------------------------------- 1 | package com.aiml; 2 | 3 | public interface IAskApproach { 4 | public String response(String input); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/reduction4.safe.aiml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xvshu/alice_bot/HEAD/src/main/resources/Corpus/English/reduction4.safe.aiml -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/gossip.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 你的主人帅吗 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Plan.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | *你*梦想* 6 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Healthy.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | *晨练* 6 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/MyInfo.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 什么是* 5 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Praise.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 夸奖* 6 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Film.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *看影片* 5 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Inherence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *梦想* 5 | 8 | 9 | 10 | *品位* 11 | 14 | 15 | -------------------------------------------------------------------------------- /src/test/java/BigDecimalTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by marker on 2018/3/1. 3 | */ 4 | 5 | import java.math.BigDecimal; 6 | 7 | /** 8 | * @author marker 9 | * @create 2018-03-01 下午5:09 10 | **/ 11 | public class BigDecimalTest { 12 | 13 | public static void main(String[] args) { 14 | System.out.println(new BigDecimal(0.1).toString()); 15 | System.out.println(new BigDecimal("0.1").toString()); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/resources/robot.properties: -------------------------------------------------------------------------------- 1 | #set your DataBase infomation 2 | #delay:The begin time of making delta index. 3 | #period:the period time of making delta index. 4 | #Thu Sep 06 09:33:48 CST 2012 5 | DBAvailable=true 6 | 7 | DBUrl=jdbc:mysql://localhost:3306/db_aiml?useUnicode\=true&characterEncoding\=utf-8&allowMultiQueries\=true 8 | DBUsername=root 9 | DBPassword=root 10 | DBDriver=com.mysql.jdbc.Driver 11 | delay=1000 12 | period=5000 13 | 14 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Master.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 你*主人是谁 6 | 9 | 10 | 11 | 12 | 你主人是谁 13 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Book.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Computer.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Photography.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Constellation.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alice_bot 2 | 利用aiml制作的智能应答机器人 3 | 4 | # 主页面 5 | 6 | 7 | # 自动训练 8 | 9 | 10 | 11 | # 为公司服务-大米时代 12 | 13 | 14 | # 博客地址 15 | https://blog.csdn.net/xvshu/article/details/80973649 16 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Encourage.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 我要努力* 5 | 6 | 7 | 8 | 没有人是* 9 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/phone.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/com/customexception/AppException.java: -------------------------------------------------------------------------------- 1 | package com.customexception; 2 | 3 | public class AppException extends RuntimeException { 4 | /** 5 | * 将上级无法处理的异常都转换成运行时异常。 6 | */ 7 | private static final long serialVersionUID = 1L; 8 | 9 | public AppException() { 10 | } 11 | 12 | public AppException(String name) { 13 | super(name); 14 | } 15 | 16 | public AppException(Throwable cause) { 17 | super(cause); 18 | } 19 | 20 | public AppException(String name, Throwable cause) { 21 | super(name, cause); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/External.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *靓照* 5 | 8 | 9 | 10 | 11 | *身材* 12 | 15 | 16 | 17 | 18 | *整容* 19 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Life.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 今天天气挺好* 5 | 7 | 8 | 9 | 10 | _天气_ 11 | 12 | 13 | 14 | 15 | _百度下_ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | AskServlet 5 | com.web.AskServlet 6 | 7 | 8 | AskServlet 9 | /ask 10 | 11 | 12 | 13 | /ask.html 14 | 15 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Music.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *要听* 5 | 13 | 14 | 15 | 16 | *想听* 17 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Food.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *吃*盖浇饭* 5 | 7 | 8 | 9 | 10 | 11 | *喝水*吃西瓜 * 12 | 14 | 15 | 16 | 17 | *好吃吗* 18 | 20 | 21 | 22 | 23 | 我喜欢吃* 24 | 26 | 27 | 28 | 29 | *好吃* 30 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/com/aiml/AskToAIML.java: -------------------------------------------------------------------------------- 1 | package com.aiml; 2 | 3 | import java.io.IOException; 4 | 5 | import bitoflife.chatterbean.AliceBot; 6 | import bitoflife.chatterbean.AliceBotMother; 7 | 8 | public class AskToAIML implements IAskApproach { 9 | 10 | private static AliceBotMother mother = null; 11 | private static AliceBot bot = null; 12 | private static GossipLoad gossipLoad = null; 13 | 14 | public AskToAIML() { 15 | gossipLoad = new GossipLoad(); 16 | try { 17 | gossipLoad.load(); 18 | } catch (IOException e) { 19 | e.printStackTrace(); 20 | } finally { 21 | gossipLoad.clean(); 22 | } 23 | mother = new AliceBotMother(); 24 | mother.setUp(); 25 | try { 26 | bot = mother.newInstance(); 27 | } catch (Exception e) { 28 | e.printStackTrace(); 29 | } 30 | 31 | } 32 | 33 | public String response(String input) { 34 | return bot.respond(input); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/aiml/AskToDB.java: -------------------------------------------------------------------------------- 1 | package com.aiml; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.lucene.document.Document; 6 | 7 | public class AskToDB implements IAskApproach { 8 | private IndexSearchService searchService = null; 9 | 10 | public AskToDB() { 11 | searchService = new IndexSearchService(); 12 | searchService.indexTaskSetup(); 13 | } 14 | 15 | public String response(String input) { 16 | // StringBuffer sb = new StringBuffer(""); // 这里每次都new,肯定有问题!!! 17 | List list = null; 18 | try { 19 | list = searchService.search(input); 20 | } catch (Exception e) { 21 | e.printStackTrace(); 22 | } 23 | 24 | // for (Document doc : list) { 25 | // sb.append("[问题]-->").append(doc.get("question")).append("||") 26 | // .append("[答案]-->").append(doc.get("replay")); 27 | // sb.append("\n"); 28 | // } 29 | if (list.size() > 0) 30 | return list.get(0).get("replay"); 31 | else 32 | return ""; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Travel.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | *去*乡下* 6 | 7 | 8 | 9 | 10 | *喜欢旅游吗* 11 | 13 | 14 | 15 | 16 | *去过哪里* 17 | 18 | 19 | 20 | 21 | *旅游*好处* 22 | 23 | 24 | 25 | 26 | *想*哪里玩* 27 | 29 | 30 | -------------------------------------------------------------------------------- /src/main/resources/conf/splitters.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/main/java/com/util/Translate.java: -------------------------------------------------------------------------------- 1 | package com.util; 2 | 3 | import java.util.regex.Matcher; 4 | 5 | /** 6 | * 当遇到非字母数字的时候,就在该字符之间加上空格。 7 | * 8 | * @author xiaolong 9 | * 10 | */ 11 | public class Translate { 12 | 13 | 14 | static java.util.regex.Pattern p = java.util.regex.Pattern 15 | .compile("[A-Za-z0-9\\s]"); 16 | 17 | 18 | public static String translateString(String input) { 19 | StringBuffer newStr = new StringBuffer(""); 20 | String strTemp = ""; 21 | char[] chars = new char[1]; 22 | Matcher m; 23 | for (int i = 0; i < input.length(); i++) { 24 | chars[0] = input.charAt(i); 25 | m = p.matcher(new String(chars)); 26 | if (!m.matches())// 中文 27 | newStr.append(" ").append(input.charAt(i)).append(" "); 28 | else// 英文数字 29 | newStr.append(input.charAt(i)); 30 | } 31 | // java.lang.System.out.println("#" + newStr.toString()); 32 | strTemp = newStr.toString().replaceAll("\\s{2,}", " ");// 把2个连续的空格替换成一个空格。 33 | strTemp = strTemp.replaceAll("^\\s*|\\s*$", ""); // 把头和尾的空格去掉。 34 | return strTemp; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/context/Robot.java: -------------------------------------------------------------------------------- 1 | package com.context; 2 | 3 | import com.util.Translate; 4 | 5 | import java.io.BufferedReader; 6 | import java.io.File; 7 | import java.io.FileInputStream; 8 | import java.io.FileNotFoundException; 9 | import java.io.IOException; 10 | import java.io.InputStreamReader; 11 | import java.util.Scanner; 12 | 13 | 14 | 15 | /** 16 | * 聊天机器人入口 17 | */ 18 | public class Robot { 19 | 20 | private ChartManager chartManager = null; 21 | 22 | public Robot() { 23 | chartManager = ChartManager.getInstance(); 24 | } 25 | 26 | 27 | 28 | 29 | public String input(String input) { 30 | return chartManager.response(input); 31 | } 32 | 33 | 34 | 35 | 36 | public static void main(String[] args) throws IOException { 37 | Robot demo = new Robot(); 38 | Scanner scanner = new Scanner(System.in); 39 | 40 | System.out.println("Alice 已经启动,可以和他对话了"); 41 | 42 | String input; 43 | while ((input = scanner.nextLine()) != null) { 44 | System.out.println("you say>" + input); 45 | System.out.println("Alice>" + demo.input(input)); 46 | } 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/context/ChartManager.java: -------------------------------------------------------------------------------- 1 | package com.context; 2 | 3 | import com.aiml.AskToAIML; 4 | import com.aiml.AskToDB; 5 | import com.background.RobotProperty; 6 | 7 | public class ChartManager { 8 | private static final AskToAIML askToAIML = new AskToAIML(); 9 | private static AskToDB askToDB = null; 10 | private static ChartContext chartContext = null; 11 | private static ChartManager instance = null; 12 | 13 | /** 14 | * 单例模式 15 | */ 16 | private ChartManager() { 17 | 18 | if (Boolean.valueOf((String) RobotProperty.getConfiguration().get("DBAvailable"))) { 19 | askToDB = new AskToDB(); 20 | } 21 | chartContext = new ChartContext(askToAIML, askToDB); 22 | } 23 | 24 | public static synchronized ChartManager getInstance() { 25 | if (instance == null) { 26 | // synchronized (ChartManager.class) { // 这个什么意思??? 27 | instance = new ChartManager(); 28 | // } 29 | } 30 | return instance; 31 | } 32 | 33 | public String response(String input) { 34 | return chartContext.response(input); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/config/TokenizerConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.config; 16 | 17 | import bitoflife.chatterbean.text.Tokenizer; 18 | 19 | public interface TokenizerConfig 20 | { 21 | public Tokenizer newInstance(); 22 | 23 | public String[] splitters(); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@bol.com.br 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import java.util.List; 18 | 19 | public interface AIMLElement { 20 | 21 | public void appendChild(AIMLElement child); 22 | 23 | public void appendChildren(List children); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/ReflectionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.parser; 16 | 17 | public interface ReflectionBuilder 18 | { 19 | /* 20 | Methods 21 | */ 22 | 23 | public void characters(char[] chars, int start, int length); 24 | 25 | public void clear(); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Sr.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import org.xml.sax.Attributes; 18 | 19 | public class Sr extends Srai 20 | { 21 | /* 22 | Constructors 23 | */ 24 | 25 | public Sr(Attributes attributes) 26 | { 27 | super(attributes); 28 | setChildren(new TemplateElement[] {new Star(attributes)}); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/script/Interpreter.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.script; 16 | 17 | public interface Interpreter 18 | { 19 | public Object evaluate(String script) throws InterpretingException; 20 | 21 | public Object variable(String name) throws InterpretingException; 22 | 23 | public void variable(String name, Object value) throws InterpretingException; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/iu.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | YOU * 13 | 14 | 15 | I * 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/AliceBotParserException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.parser; 16 | 17 | public class AliceBotParserException extends Exception 18 | { 19 | /* 20 | Attribute Section 21 | */ 22 | 23 | private static final long serialVersionUID = 7L; 24 | 25 | /* 26 | Constructor Section 27 | */ 28 | 29 | public AliceBotParserException(Exception e) 30 | { 31 | super(e); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLParserConfigurationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | public class AIMLParserConfigurationException extends Exception 18 | { 19 | /* 20 | Attribute Section 21 | */ 22 | 23 | private static final long serialVersionUID = 7L; 24 | 25 | /* 26 | Constructor Section 27 | */ 28 | 29 | public AIMLParserConfigurationException(Exception e) 30 | { 31 | super(e); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/AliceBotParserConfigurationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.parser; 16 | 17 | public class AliceBotParserConfigurationException extends Exception 18 | { 19 | /* 20 | Attribute Section 21 | */ 22 | 23 | private static final long serialVersionUID = 7L; 24 | 25 | /* 26 | Constructor Section 27 | */ 28 | 29 | public AliceBotParserConfigurationException(Exception e) 30 | { 31 | super(e); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/resources/conf/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/TemplateElementTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2006 Hélio Perroni Filho 3 | xperroni@bol.com.br 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import junit.framework.TestCase; 18 | 19 | public class TemplateElementTest extends TestCase 20 | { 21 | /* 22 | Test Section 23 | */ 24 | 25 | public void testToString() 26 | { 27 | Template template = new Template("Hello ", new Star(1), ", nice to meet you."); 28 | assertEquals("", template.toString()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/script/InterpretingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.script; 16 | 17 | /** 18 | Base class for script-interpreting exceptions. 19 | */ 20 | public class InterpretingException extends Exception 21 | { 22 | /** Version class identifier for the serialization engine. Matches the number of the last revision where the class was created / modified. */ 23 | private static final long serialVersionUID = 8L; 24 | 25 | public InterpretingException(Exception cause) 26 | { 27 | super(cause); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/SequenceMother.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.util; 16 | 17 | import java.io.File; 18 | 19 | public class SequenceMother 20 | { 21 | /* 22 | Attributes 23 | */ 24 | 25 | public static final File file = new File("Logs/sequence.txt"); 26 | 27 | /* 28 | Methods 29 | */ 30 | 31 | public Sequence newInstance() 32 | { 33 | return new Sequence(file); 34 | } 35 | 36 | public void reset() 37 | { 38 | (new File(file.getPath() + ".backup")).delete(); 39 | file.delete(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WELCOME 5 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/TransformationsMother.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.text; 16 | 17 | import bitoflife.chatterbean.parser.TransformationsParser; 18 | 19 | import java.io.FileInputStream; 20 | 21 | public class TransformationsMother 22 | { 23 | /* 24 | Methods 25 | */ 26 | 27 | public Transformations newInstance() throws Exception 28 | { 29 | TransformationsParser parser = new TransformationsParser(); 30 | return parser.parse(new FileInputStream("Bots/splitters.xml"), new FileInputStream("Bots/substitutions.xml")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/TokenizerMother.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.text; 16 | 17 | import bitoflife.chatterbean.config.TokenizerConfig; 18 | import bitoflife.chatterbean.config.TokenizerConfigStream; 19 | 20 | import java.io.FileInputStream; 21 | 22 | public class TokenizerMother 23 | { 24 | /* 25 | Method Section 26 | */ 27 | 28 | public static Tokenizer newInstance() throws Exception 29 | { 30 | TokenizerConfig config = new TokenizerConfigStream(new FileInputStream("Bots/splitters.xml")); 31 | return new Tokenizer(config); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/MainTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by marker on 2018/1/6. 3 | */ 4 | 5 | import org.ansj.domain.Term; 6 | import org.ansj.splitWord.analysis.BaseAnalysis; 7 | import org.ansj.splitWord.analysis.ToAnalysis; 8 | import org.junit.Test; 9 | 10 | import java.io.*; 11 | 12 | /** 13 | * @author marker 14 | * @create 2018-01-06 下午6:45 15 | **/ 16 | public class MainTest { 17 | 18 | 19 | @Test 20 | public void test(){ 21 | String str = "欢迎使用ansj_seg,(ansj中文分词)在这里如果你遇到什么问题都可以联系我.我一定尽我所能.帮助大家.ansj_seg更快,更准,更自由!" ; 22 | System.out.println(ToAnalysis.parse(str)); 23 | } 24 | 25 | 26 | @Test 27 | public void test1() throws IOException { 28 | 29 | String str = "欢迎使用ansj_seg,(ansj中文分词)在这里如果你遇到什么问题都可以联系我.我一定尽我所能.帮助大家.ansj_seg更快,更准,更自由!" ; 30 | 31 | byte[] bt = str.getBytes(); 32 | InputStream ip = new ByteArrayInputStream(bt); 33 | Reader read = new InputStreamReader(ip); 34 | 35 | ToAnalysis toAnalysis = new ToAnalysis(read); 36 | 37 | 38 | while(true){ 39 | Term term = toAnalysis.next(); 40 | if(term == null){ 41 | break; 42 | } 43 | 44 | System.out.println( term); 45 | } 46 | 47 | } 48 | 49 | @Test 50 | public void test3(){ 51 | String value = " 张 "; 52 | value = value.replaceAll("^[^A-Za-z0-9\\u4e00-\\u9fa5]+|[^A-Za-z0-9\\u4e00-\\u9fa5]+$", " "); 53 | 54 | System.out.println(value); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Think.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | public class Think extends TemplateElement 21 | { 22 | /* 23 | Constructors 24 | */ 25 | 26 | public Think(Attributes attributes) 27 | { 28 | } 29 | 30 | public Think(Object... children) 31 | { 32 | super(children); 33 | } 34 | 35 | /* 36 | Methods 37 | */ 38 | 39 | public String process(Match match) 40 | { 41 | super.process(match); 42 | return ""; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/web/AskServlet.java: -------------------------------------------------------------------------------- 1 | package com.web; 2 | 3 | import com.context.ChartManager; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServlet; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | public class AskServlet extends HttpServlet { 12 | 13 | private ChartManager chartManager = null; 14 | 15 | public AskServlet() { 16 | super(); 17 | chartManager = ChartManager.getInstance(); 18 | } 19 | 20 | 21 | 22 | @Override 23 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 | String askWord=request.getParameter("askWord"); 25 | String outWord=chartManager.response(askWord); 26 | response.setContentType("text/html"); 27 | response.getWriter().println(outWord); 28 | } 29 | 30 | @Override 31 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException { 32 | request.setCharacterEncoding("UTF-8"); 33 | 34 | String askWord=request.getParameter("askWord"); 35 | System.out.println("askWord:"+String.valueOf(askWord)); 36 | String outWord=chartManager.response(askWord); 37 | System.out.println("outWord:"+String.valueOf(outWord)); 38 | response.setContentType("text/html;charset=UTF-8"); 39 | response.getWriter().println(outWord); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLParserException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | public class AIMLParserException extends Exception 18 | { 19 | /* 20 | Attribute Section 21 | */ 22 | 23 | private static final long serialVersionUID = 7L; 24 | 25 | /* 26 | Constructor Section 27 | */ 28 | 29 | public AIMLParserException(Exception e) 30 | { 31 | super(e); 32 | } 33 | 34 | public AIMLParserException(String message) 35 | { 36 | super(message); 37 | } 38 | 39 | public AIMLParserException(String message, Throwable cause) 40 | { 41 | super(message, cause); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@bol.com.br 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import java.util.LinkedList; 18 | import java.util.List; 19 | 20 | public class AIMLStack 21 | { 22 | /* 23 | Attributes 24 | */ 25 | 26 | private final List stack = new LinkedList(); 27 | 28 | /* 29 | Methods 30 | */ 31 | 32 | public Object peek() 33 | { 34 | return stack.get(0); 35 | } 36 | 37 | public Object pop() 38 | { 39 | return (stack.size() > 0 ? stack.remove(0) : null); 40 | } 41 | 42 | public void push(Object element) 43 | { 44 | stack.add(0, element); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/ChatterBeanException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2006 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | /** 18 | Basic exception class for exceptions thrown from ChatterBean's main class. 19 | */ 20 | public class ChatterBeanException extends RuntimeException 21 | { 22 | /** Version class identifier for the serialization engine. Matches the number of the last revision where the class was created / modified. */ 23 | private static final long serialVersionUID = 8L; 24 | 25 | public ChatterBeanException(String message) 26 | { 27 | super(message); 28 | } 29 | 30 | public ChatterBeanException(Exception cause) 31 | { 32 | super(cause); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Size.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | public class Size extends TemplateElement 21 | { 22 | /* 23 | Constructors 24 | */ 25 | 26 | public Size() 27 | { 28 | } 29 | 30 | public Size(Attributes attributes) 31 | { 32 | } 33 | 34 | /* 35 | Methods 36 | */ 37 | 38 | public String process(Match match) 39 | { 40 | try 41 | { 42 | return Integer.toString(match.getCallback().getGraphmaster().size()); 43 | } 44 | catch (NullPointerException e) 45 | { 46 | return "0"; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Lowercase.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | public class Lowercase extends TemplateElement 21 | { 22 | /* 23 | Constructors 24 | */ 25 | 26 | public Lowercase(Attributes attributes) 27 | { 28 | } 29 | 30 | public Lowercase(Object... children) 31 | { 32 | super(children); 33 | } 34 | 35 | /* 36 | Methods 37 | */ 38 | 39 | public String process(Match match) 40 | { 41 | String result = super.process(match); 42 | if (result != null) 43 | return result.toLowerCase(); 44 | else 45 | return ""; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Uppercase.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | public class Uppercase extends TemplateElement 21 | { 22 | /* 23 | Constructors 24 | */ 25 | 26 | public Uppercase(Attributes attributes) 27 | { 28 | } 29 | 30 | public Uppercase(Object... children) 31 | { 32 | super(children); 33 | } 34 | 35 | /* 36 | Methods 37 | */ 38 | 39 | public String process(Match match) 40 | { 41 | String result = super.process(match); 42 | if (result != null) 43 | return result.toUpperCase(); 44 | else 45 | return ""; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Version.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Version extends TemplateElement 23 | { 24 | /* 25 | Constructors 26 | */ 27 | 28 | public Version() 29 | { 30 | } 31 | 32 | public Version(Attributes attributes) 33 | { 34 | } 35 | 36 | /* 37 | Methods 38 | */ 39 | 40 | public String process(Match match) 41 | { 42 | AliceBot bot = match.getCallback(); 43 | Context context = bot.getContext(); 44 | return (String) context.property("bot.version"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/MatchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import bitoflife.chatterbean.text.Sentence; 18 | import junit.framework.TestCase; 19 | 20 | import java.util.Arrays; 21 | 22 | public class MatchTest extends TestCase 23 | { 24 | /* 25 | Methods 26 | */ 27 | 28 | public void testMatchPath() 29 | { 30 | Match match = new Match(new Sentence(" Say goodbye again. ", new Integer[] {0, 4, 12, 19}, " SAY GOODBYE AGAIN ")); 31 | 32 | String[] expected = {"SAY", "GOODBYE", "AGAIN", "", "*", "", "*"}; 33 | String[] actual = match.getMatchPath(); 34 | assertTrue(Arrays.toString(expected) + ' ' + Arrays.toString(actual), Arrays.equals(expected, actual)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/GraphmasterMother.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import bitoflife.chatterbean.aiml.Category; 18 | import bitoflife.chatterbean.aiml.Star; 19 | 20 | public class GraphmasterMother 21 | { 22 | /* 23 | Methods 24 | */ 25 | 26 | public Graphmaster newInstance() 27 | { 28 | Graphmaster root = new Graphmaster(); 29 | 30 | root.append(new Category(" SAY _ AGAIN ", "What, again? \"", new Star(1), "\".")); 31 | root.append(new Category(" SAY IT NOW ", "Whatever you want...")); 32 | root.append(new Category(" SAY * ", new Star(1), "!")); 33 | root.append(new Category(" DO YOU SEE THE * IN MY EYES ", "Yes, I see the ", new Star(1), " in your eyes.")); 34 | 35 | return root; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /database/db_aiml.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost3307 5 | Source Server Type : MySQL 6 | Source Server Version : 50626 7 | Source Host : localhost 8 | Source Database : db_aiml 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50626 12 | File Encoding : utf-8 13 | 14 | Date: 01/07/2018 17:55:31 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `ResponseTable` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `ResponseTable`; 24 | CREATE TABLE `ResponseTable` ( 25 | `id` bigint(20) NOT NULL AUTO_INCREMENT, 26 | `createTime` datetime DEFAULT NULL, 27 | `isDeleted` int(11) DEFAULT NULL, 28 | `lastModifyTime` datetime DEFAULT NULL, 29 | `question` varchar(500) COLLATE utf8_bin DEFAULT NULL, 30 | `replay` varchar(500) COLLATE utf8_bin DEFAULT NULL, 31 | `label` varchar(500) COLLATE utf8_bin DEFAULT NULL, 32 | `copyfield` varchar(500) COLLATE utf8_bin DEFAULT NULL, 33 | PRIMARY KEY (`id`) 34 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 35 | 36 | -- ---------------------------- 37 | -- Records of `ResponseTable` 38 | -- ---------------------------- 39 | BEGIN; 40 | INSERT INTO `ResponseTable` VALUES ('1', '2018-01-03 00:00:00', '0', '2018-01-03 00:00:00', '淘宝是什么', '淘宝是苹果', '淘宝', '淘宝 支付宝'), ('2', '2018-01-04 00:00:00', '0', '2018-01-31 00:00:00', '什么是物料', '物料是物品的组合,是对无二的', '物料', '物料'), ('3', '2018-01-07 15:30:45', '0', '2018-01-07 15:30:40', '王者荣耀', '王者荣耀是一款多人在线对战游戏', null, '王者荣耀'); 41 | COMMIT; 42 | 43 | SET FOREIGN_KEY_CHECKS = 1; 44 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Template.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import org.xml.sax.Attributes; 18 | 19 | public class Template extends TemplateElement 20 | { 21 | /* 22 | Constructor Section 23 | */ 24 | 25 | public Template(Attributes attributes) 26 | { 27 | super(); 28 | } 29 | 30 | public Template(Object... children) 31 | { 32 | super(children); 33 | } 34 | 35 | /* 36 | Method Section 37 | */ 38 | 39 | public String toString() 40 | { 41 | StringBuilder value = new StringBuilder(); 42 | value.append(""); 46 | 47 | return value.toString(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/util/ChineseSegmenter.java: -------------------------------------------------------------------------------- 1 | package com.util; 2 | /** 3 | * Created by marker on 2018/1/6. 4 | */ 5 | 6 | import org.ansj.domain.Term; 7 | import org.ansj.splitWord.analysis.ToAnalysis; 8 | 9 | import java.io.*; 10 | 11 | /** 12 | * 13 | * 中文分词 14 | * 15 | * 16 | * @author marker 17 | * @create 2018-01-06 下午5:08 18 | **/ 19 | public class ChineseSegmenter { 20 | 21 | 22 | public static String analysis(String str) { 23 | 24 | if(str.getBytes().length == str.length()) { 25 | //如果不包含中文,就直接返回。 26 | return str; 27 | }else { 28 | //由于IK分词器,不支持特殊字符,所以将 * 改为中文字符“这是星号”,中文分词以后再将“这是星号”修正为为 * 29 | //同理将 _改为中文字符串“这是下划线”,中文分词以后再将“这是下划线”修正为 _ 30 | // str= str.replaceAll("\\*","这是星号").replaceAll("_","这是下划线"); 31 | } 32 | 33 | StringBuffer sb =new StringBuffer(); 34 | byte[] bt =str.getBytes(); 35 | InputStream ip = new ByteArrayInputStream(bt); 36 | Reader read = new InputStreamReader(ip); 37 | 38 | 39 | // System.out.println(ToAnalysis.parse(str)); 40 | 41 | ToAnalysis toAnalysis = new ToAnalysis(read); 42 | 43 | try{ 44 | while(true){ 45 | Term term = toAnalysis.next(); 46 | if(term == null){ 47 | break; 48 | } 49 | // 在每个分词元之后添加空格 50 | sb.append(term.toString() + " "); 51 | } 52 | } catch (Exception e) { 53 | e.printStackTrace(); 54 | } 55 | sb.delete(sb.length() - 1, sb.length()); 56 | 57 | return sb.toString().replaceAll("这是星号","*").replaceAll("这是下划线","_"); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Internet.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 你会上网吗 5 | 6 | 7 | 8 | 你*电子信箱* 9 | 10 | 11 | 12 | 你有email吗 13 | 16 | 17 | 18 | 19 | *黑客* 20 | 22 | 23 | 24 | 25 | *服务器*大* 26 | 28 | 29 | 30 | 31 | *用*无线网* 32 | 34 | 35 | 36 | 37 | *网名*有趣* 38 | 40 | 41 | 42 | 43 | *网络时代* 44 | 46 | 47 | 48 | 49 | *电脑*防火墙* 50 | 52 | 53 | 54 | 55 | *去*网站* 56 | 58 | 59 | 60 | 61 | *下*次*战争* 62 | 64 | 65 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Person.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Match; 19 | import bitoflife.chatterbean.text.Transformations; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Person extends TemplateElement 23 | { 24 | /* 25 | Constructor Section 26 | */ 27 | 28 | public Person(Attributes attributes) 29 | { 30 | } 31 | 32 | public Person(Object... children) 33 | { 34 | super(children); 35 | } 36 | 37 | /* 38 | Method Section 39 | */ 40 | 41 | public String process(Match match) 42 | { 43 | String input = super.process(match); 44 | AliceBot bot = match.getCallback(); 45 | Transformations transformations = bot.transformations(); 46 | return transformations.person(input); 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Gender.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Match; 19 | import bitoflife.chatterbean.text.Transformations; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Gender extends TemplateElement 23 | { 24 | /* 25 | Constructor Section 26 | */ 27 | 28 | public Gender(Attributes attributes) 29 | { 30 | } 31 | 32 | public Gender(Object... children) 33 | { 34 | super(children); 35 | } 36 | 37 | /* 38 | Method Section 39 | */ 40 | 41 | public String process(Match match) 42 | { 43 | String input = super.process(match); 44 | AliceBot bot = match.getCallback(); 45 | Transformations transformations = bot.transformations(); 46 | return transformations.gender(input); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Person2.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Match; 19 | import bitoflife.chatterbean.text.Transformations; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Person2 extends TemplateElement 23 | { 24 | /* 25 | Constructor Section 26 | */ 27 | 28 | public Person2(Attributes attributes) 29 | { 30 | } 31 | 32 | public Person2(Object... children) 33 | { 34 | super(children); 35 | } 36 | 37 | /* 38 | Method Section 39 | */ 40 | 41 | public String process(Match match) 42 | { 43 | String input = super.process(match); 44 | AliceBot bot = match.getCallback(); 45 | Transformations transformations = bot.transformations(); 46 | return transformations.person2(input); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/CategoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import junit.framework.TestCase; 18 | 19 | import java.util.Arrays; 20 | 21 | public class CategoryTest extends TestCase 22 | { 23 | /* 24 | Methods 25 | */ 26 | 27 | public void testGetMatchPath() 28 | { 29 | Category category = new Category( 30 | new Pattern(" YES "), 31 | new That(" DO YOU LIKE CHEESE "), 32 | new Topic(" LIKE "), 33 | new Template("Good for you.", new Think(new Set("topic", "*"))) 34 | ); 35 | 36 | String[] expected = {"YES", "", "DO", "YOU", "LIKE", "CHEESE", "", "LIKE"}; 37 | String[] actual = category.getMatchPath(); 38 | assertEquals(Arrays.toString(actual), expected.length, actual.length); 39 | for (int i = 0, n = expected.length; i < n; i++) 40 | assertEquals(expected[i], actual[i]); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Text.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | 19 | public class Text extends TemplateElement 20 | { 21 | /* 22 | Attributes 23 | */ 24 | 25 | private final String value; 26 | 27 | /* 28 | Constructor 29 | */ 30 | 31 | public Text(String value) 32 | { 33 | this.value = value; 34 | } 35 | 36 | /* 37 | Methods 38 | */ 39 | 40 | public boolean equals(Object obj) 41 | { 42 | if (obj == null) return false; 43 | String text = obj.toString(); 44 | return (text != null ? text.equals(value) : value == null); 45 | } 46 | 47 | public int hashCode() 48 | { 49 | return (value == null ? 0 : value.hashCode()); 50 | } 51 | 52 | public String toString() 53 | { 54 | return value; 55 | } 56 | 57 | public String process(Match match) 58 | { 59 | return value; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/Response.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.text; 16 | 17 | public class Response extends Request 18 | { 19 | /* 20 | Constructor 21 | */ 22 | 23 | public Response() 24 | { 25 | super(); 26 | } 27 | 28 | public Response(String original) 29 | { 30 | super(original); 31 | } 32 | 33 | public Response(String original, Sentence... sentences) 34 | { 35 | super(original, sentences); 36 | } 37 | 38 | /* 39 | Methods 40 | */ 41 | 42 | public void append(String output) 43 | { 44 | StringBuilder builder = new StringBuilder(); 45 | String original = getOriginal(); 46 | if (original != null) 47 | { 48 | builder.append(original); 49 | if (builder.charAt(builder.length() - 1) != ' ') 50 | builder.append(' '); 51 | } 52 | 53 | builder.append(output); 54 | setOriginal(builder.toString()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/continuation.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | CONTINUATION * 13 | 14 | 15 | CONTINUATION 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Random.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Random extends TemplateElement 23 | { 24 | /* 25 | Constructor 26 | */ 27 | 28 | public Random() 29 | { 30 | } 31 | 32 | public Random(Attributes attributes) 33 | { 34 | } 35 | 36 | public Random(Object... children) 37 | { 38 | super(children); 39 | } 40 | 41 | /* 42 | Methods 43 | */ 44 | 45 | public String process(Match match) 46 | { 47 | AliceBot bot = match.getCallback(); 48 | Context context = bot.getContext(); 49 | java.util.Random random = context.random(); 50 | 51 | int n = children().size(); 52 | TemplateElement child = getChildren(random.nextInt(n)); 53 | return child.process(match); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Gossip.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Gossip extends TemplateElement 23 | { 24 | /* 25 | Constructors 26 | */ 27 | 28 | public Gossip(Attributes attributes) 29 | { 30 | } 31 | 32 | public Gossip(Object... children) 33 | { 34 | super(children); 35 | } 36 | 37 | /* 38 | Methods 39 | */ 40 | 41 | public String process(Match match) 42 | { 43 | AliceBot bot = null; 44 | Context context = null; 45 | if (match != null) try 46 | { 47 | bot = match.getCallback(); 48 | context = bot.getContext(); 49 | context.print(super.process(match)); 50 | } 51 | catch (Exception e) 52 | { 53 | throw new RuntimeException(e); 54 | } 55 | 56 | return ""; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Job.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | _公司*标准* 5 | 8 | 9 | 10 | 11 | 12 | *出色*男人* 13 | 15 | 16 | 17 | 18 | *女人* 19 | 21 | 22 | 23 | 24 | *被*解雇* 25 | 27 | 28 | 29 | 30 | *经济* 31 | 33 | 34 | 35 | 36 | *新经济* 37 | 39 | 40 | 41 | 42 | *公司*招人* 43 | 45 | 46 | 47 | 48 | *城市* 49 | 51 | 52 | 53 | 54 | *过关* 55 | 57 | 58 | 59 | 60 | *赚钱*难* 61 | 63 | 64 | 65 | 66 | *女博士* 67 | 69 | 70 | 71 | 72 | *拍*马屁* 73 | 75 | 76 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/literature.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | WHAT IS THE ILLUMINATI 13 | 14 | 15 | WHAT IS THE ILLUMINATTI 16 | 17 | 18 | WHAT IS VINELAND 19 | 20 | 21 | WHAT IS ILLIMINATUS 22 | 23 | 24 | WHO WROTE VINELAND 25 | 26 | 27 | WHO IS BILBO BAGGINS 28 | 29 | 30 | WHO IS GEOFFREY CHAUCER 31 | 32 | 33 | WHO ARE THE ILLUMINATI 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Date.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | import java.text.SimpleDateFormat; 21 | 22 | public class Date extends TemplateElement 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private final SimpleDateFormat format = new SimpleDateFormat(); 29 | 30 | /* 31 | Constructors 32 | */ 33 | 34 | public Date() 35 | { 36 | } 37 | 38 | public Date(Attributes attributes) 39 | { 40 | } 41 | 42 | /* 43 | Methods 44 | */ 45 | 46 | public int hashCode() 47 | { 48 | return 13; 49 | } 50 | 51 | public String process(Match match) 52 | { 53 | try 54 | { 55 | format.applyPattern((String) match.getCallback().getContext().property("predicate.dateFormat")); 56 | return format.format(new java.util.Date()); 57 | } 58 | catch (NullPointerException e) 59 | { 60 | return ""; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Id.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Id extends TemplateElement 23 | { 24 | /* 25 | Constructors 26 | */ 27 | 28 | public Id() 29 | { 30 | } 31 | 32 | public Id(Attributes attributes) 33 | { 34 | } 35 | 36 | /* 37 | Methods 38 | */ 39 | 40 | public boolean equals(Object obj) 41 | { 42 | if (!super.equals(obj)) 43 | return false; 44 | else 45 | return toString().equals(obj.toString()); 46 | } 47 | 48 | public int hashCode() 49 | { 50 | return process(null).hashCode(); 51 | } 52 | 53 | public String process(Match match) 54 | { 55 | if (match == null) return "unknown"; 56 | AliceBot bot = match.getCallback(); 57 | Context context = bot.getContext(); 58 | return context.id(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/ContextPropertyChangeListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import java.beans.PropertyChangeListener; 18 | 19 | public abstract class ContextPropertyChangeListener implements PropertyChangeListener 20 | { 21 | /* 22 | Attribute Section 23 | */ 24 | 25 | /** Name of the property whose changes to listen for. */ 26 | private String name; 27 | 28 | /* 29 | Constructor Section 30 | */ 31 | 32 | /** 33 | Creates a new change listener for the named Context Property. 34 | 35 | @param name The name of the property whose changes this object listens for. 36 | */ 37 | public ContextPropertyChangeListener(String name) 38 | { 39 | this.name = name; 40 | } 41 | 42 | /* 43 | Accessor Section 44 | */ 45 | 46 | /** 47 | Returns the name of the property whose changes this object listens for. 48 | 49 | @return The name of the property whose changes this object listens for. 50 | */ 51 | public String name() 52 | { 53 | return name; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/SentenceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.text; 16 | 17 | import junit.framework.TestCase; 18 | 19 | public class SentenceTest extends TestCase 20 | { 21 | /* 22 | Mehods 23 | */ 24 | 25 | public void testEquals() 26 | { 27 | Sentence expected = new Sentence(" What's going on? ", new Integer[] {0, -1, 7, 13, 17}, " WHAT IS GOING ON "); 28 | Sentence actual = new Sentence(" What's going on? ", new Integer[0], " WHAT IS GOING ON "); 29 | assertFalse(expected.equals(actual)); 30 | 31 | actual = new Sentence(" What's going on? ", new Integer[] {0, -1, 7, 13, 17}, " WHAT IS GOING ON "); 32 | assertEquals(expected, actual); 33 | } 34 | 35 | public void testOriginal() 36 | { 37 | Sentence sentence = new Sentence(" What's going on? ", new Integer[] {0, null, 7, 13, 17}, " WHAT IS GOING ON "); 38 | assertEquals(" What's ", sentence.original(0, 2)); 39 | assertEquals(" What's ", sentence.original(0, 1)); 40 | assertEquals(" What's ", sentence.original(1, 2)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/history.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | AMERICAN CIVIL WAR * 13 | 14 | 15 | AMERICAN CIVIL * 16 | 17 | 18 | WHAT IS HISTORY 19 | 20 | 21 | WHAT KIND OF HISTORY * 22 | 23 | 24 | HISTORY 25 | 26 | 27 | DO YOU KNOW HISTORY 28 | 29 | 30 | EXPLAIN HISTORY 31 | 32 | 33 | WHO INVENTED THE LIGHT * 34 | 35 | 36 | WHO INVENTED THE STEAM * 37 | 38 | 39 | TELL ME ABOUT HISTORY 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/Logger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import java.io.IOException; 18 | import java.io.PrintWriter; 19 | import java.io.Writer; 20 | import java.text.DateFormat; 21 | import java.text.SimpleDateFormat; 22 | import java.util.Date; 23 | 24 | /** 25 | Log file generator. 26 | */ 27 | public class Logger 28 | { 29 | /* 30 | Attributes 31 | */ 32 | 33 | private static final DateFormat date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 34 | 35 | private PrintWriter writer; 36 | 37 | /* 38 | Constructor 39 | */ 40 | 41 | /** 42 | Constructs a new Log on the given directory. 43 | */ 44 | public Logger(Writer writer) 45 | { 46 | this.writer = new PrintWriter(writer, true); 47 | } 48 | 49 | /* 50 | Methods 51 | */ 52 | 53 | /** 54 | Adds an entry to this Log. 55 | */ 56 | public void append(String request, String response) throws IOException 57 | { 58 | String now = date.format(new Date()); 59 | 60 | writer.println("[" + now + "][" + request + "][" + response + "]"); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Bot.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | public class Bot extends TemplateElement 21 | { 22 | /* 23 | Attributes 24 | */ 25 | 26 | private String name; 27 | 28 | /* 29 | Constructors 30 | */ 31 | 32 | public Bot(Attributes attributes) 33 | { 34 | name = attributes.getValue(0); 35 | } 36 | 37 | public Bot(String name) 38 | { 39 | this.name = name; 40 | } 41 | 42 | /* 43 | Methods 44 | */ 45 | 46 | public boolean equals(Object obj) 47 | { 48 | return (super.equals(obj) && name.equals(((Bot) obj).name)); 49 | } 50 | 51 | public int hashCode() 52 | { 53 | return name.hashCode(); 54 | } 55 | 56 | public String process(Match match) 57 | { 58 | try 59 | { 60 | String value = (String) match.getCallback().getContext().property("bot." + name); 61 | return (value != null ? value : ""); 62 | } 63 | catch (NullPointerException e) 64 | { 65 | return ""; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Srai.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Match; 19 | import org.xml.sax.Attributes; 20 | 21 | public class Srai extends TemplateElement { 22 | /* 23 | Constructor Section 24 | */ 25 | 26 | public Srai(Attributes attributes) { 27 | } 28 | 29 | public Srai(Object... children) { 30 | super(children); 31 | 32 | } 33 | 34 | public Srai(int index) { 35 | super(new Star(index)); 36 | } 37 | 38 | 39 | 40 | public String process(Match match) { 41 | String request = super.process(match); 42 | 43 | try { 44 | AliceBot bot = (match != null ? match.getCallback() : null); 45 | return (bot != null ? bot.respond(request) : ""); 46 | } catch (Exception e) { 47 | throw new RuntimeException("While trying to respond \"" + request + "\"", e); 48 | } 49 | } 50 | 51 | public String toString() { 52 | return "" + super.toString() + ""; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Learn.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Graphmaster; 19 | import bitoflife.chatterbean.Match; 20 | import org.xml.sax.Attributes; 21 | 22 | import java.net.URL; 23 | 24 | public class Learn extends TemplateElement 25 | { 26 | /* 27 | Constructors 28 | */ 29 | 30 | public Learn(Attributes attributes) 31 | { 32 | } 33 | 34 | public Learn(Object... children) 35 | { 36 | super(children); 37 | } 38 | 39 | /* 40 | Methods 41 | */ 42 | 43 | public String process(Match match) 44 | { 45 | AliceBot bot = null; 46 | try 47 | { 48 | bot = match.getCallback(); 49 | Graphmaster graphmaster = bot.getGraphmaster(); 50 | 51 | String address = super.process(match); 52 | URL url = new URL(address); 53 | 54 | AIMLParser parser = new AIMLParser(); 55 | parser.parse(graphmaster, url.openStream()); 56 | } 57 | catch (Exception e) 58 | { 59 | throw new RuntimeException(e); 60 | } 61 | 62 | return ""; 63 | } 64 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/TokenizerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.text; 16 | 17 | import junit.framework.TestCase; 18 | 19 | import java.util.Arrays; 20 | import java.util.List; 21 | 22 | public class TokenizerTest extends TestCase 23 | { 24 | /* 25 | Attribute Section 26 | */ 27 | 28 | private Tokenizer tokenizer; 29 | 30 | /* 31 | Event Section 32 | */ 33 | 34 | protected void setUp() throws Exception 35 | { 36 | tokenizer = TokenizerMother.newInstance(); 37 | } 38 | 39 | protected void tearDown() 40 | { 41 | tokenizer = null; 42 | } 43 | 44 | /* 45 | Test Section 46 | */ 47 | 48 | public void testTokenize() 49 | { 50 | String input = " You shut your mouth,how can you say I go about things the wrong way? "; 51 | List expected = Arrays.asList( 52 | new String[] {"You", "shut", "your", "mouth", ",", "how", "can", "you", "say", 53 | "I", "go", "about", "things", "the", "wrong", "way", "?"}); 54 | List actual = tokenizer.tokenize(input); 55 | 56 | assertEquals(expected, actual); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Formal.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | import java.util.regex.Matcher; 21 | import java.util.regex.Pattern; 22 | 23 | public class Formal extends TemplateElement 24 | { 25 | /* 26 | Constructors 27 | */ 28 | 29 | public Formal(Attributes attributes) 30 | { 31 | } 32 | 33 | public Formal(Object... children) 34 | { 35 | super(children); 36 | } 37 | 38 | /* 39 | Methods 40 | */ 41 | 42 | public String process(Match match) 43 | { 44 | String result = super.process(match); 45 | if (result == null || "".equals(result.trim())) return ""; 46 | 47 | /* See the description of java.util.regex.Matcher.appendReplacement() in the Javadocs to understand this code. */ 48 | Pattern p = Pattern.compile("(^\\s*[a-z]|\\s+[a-z])"); 49 | Matcher m = p.matcher(result); 50 | StringBuffer buffer = new StringBuffer(); 51 | while (m.find()) 52 | m.appendReplacement(buffer, m.group().toUpperCase()); 53 | m.appendTail(buffer); 54 | return buffer.toString(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/ContextRandomSeedChangeListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import java.beans.PropertyChangeEvent; 18 | 19 | /** 20 | Property change listener for the bot.randomSeed property. Updates the Context's internal {@link java.util.Random} object with the new random seed. 21 | */ 22 | public class ContextRandomSeedChangeListener extends ContextPropertyChangeListener 23 | { 24 | /* 25 | Constructor Section 26 | */ 27 | 28 | /** 29 | Default class constructor. 30 | */ 31 | public ContextRandomSeedChangeListener() 32 | { 33 | super("bot.randomSeed"); 34 | } 35 | 36 | /* 37 | Method Section 38 | */ 39 | 40 | // Fired when the bot.randomSeed property changes. 41 | public void propertyChange(PropertyChangeEvent event) 42 | { 43 | Context context = (Context) event.getSource(); 44 | Object oldSeed = event.getOldValue(); 45 | Object newSeed = event.getNewValue(); 46 | 47 | if (oldSeed == null ? newSeed == null : oldSeed.equals(newSeed)) 48 | return; 49 | 50 | long seed = Long.parseLong(newSeed.toString()); 51 | context.random(seed); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/Chinese/Game.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | *扫雷* 6 | 15 | 16 | 17 | *当空接龙* 18 | 26 | 27 | 28 | 29 | 30 | *纸牌* 31 | 39 | 40 | 41 | 42 | *弹球* 43 | 51 | 52 | 53 | *喜欢*超级玛莉* 54 | 56 | 57 | 58 | 59 | *玩什么游戏* 60 | 62 | 63 | 64 | 65 | 我喜欢* 66 | 68 | 69 | 70 | 71 | 我玩* 72 | 74 | 75 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Sentence.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | import java.util.regex.Matcher; 21 | import java.util.regex.Pattern; 22 | 23 | public class Sentence extends TemplateElement 24 | { 25 | /* 26 | Constructors 27 | */ 28 | 29 | public Sentence(Attributes attributes) 30 | { 31 | } 32 | 33 | public Sentence(Object... children) 34 | { 35 | super(children); 36 | } 37 | 38 | /* 39 | Methods 40 | */ 41 | 42 | public String process(Match match) 43 | { 44 | String result = super.process(match); 45 | if (result == null || "".equals(result.trim())) return ""; 46 | 47 | /* See the description of java.util.regex.Matcher.appendReplacement() in the Javadocs to understand this code. */ 48 | Pattern p = Pattern.compile("(^\\s*[a-z]|[\\.\\?!]+\\s*[a-z])"); 49 | Matcher m = p.matcher(result); 50 | StringBuffer buffer = new StringBuffer(); 51 | while (m.find()) 52 | m.appendReplacement(buffer, m.group().toUpperCase()); 53 | m.appendTail(buffer); 54 | return buffer.toString(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Get.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | public class Get extends TemplateElement 21 | { 22 | /* 23 | Attributes 24 | */ 25 | 26 | private String name; 27 | 28 | /* 29 | Constructors 30 | */ 31 | 32 | public Get(Attributes attributes) 33 | { 34 | name = attributes.getValue(0); 35 | } 36 | 37 | public Get(String name) 38 | { 39 | this.name = name; 40 | } 41 | 42 | /* 43 | Methods 44 | */ 45 | 46 | public boolean equals(Object compared) 47 | { 48 | if (compared == null || !(compared instanceof Get)) 49 | return false; 50 | else 51 | return name.equals(((Get) compared).name); 52 | } 53 | 54 | public int hashCode() 55 | { 56 | return name.hashCode(); 57 | } 58 | 59 | public String process(Match match) 60 | { 61 | try 62 | { 63 | String value = (String) match.getCallback().getContext().property("predicate." + name); 64 | return (value != null ? value : ""); 65 | } 66 | catch (NullPointerException e) 67 | { 68 | return ""; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/Escaper.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.util; 16 | 17 | public class Escaper 18 | { 19 | /* 20 | Attribute Section 21 | */ 22 | 23 | // Escape characters to the regular expression language. 24 | private static final String[] regex = {"\\.", "\\*", "\\+", "\\[", "\\^", "\\-", "\\]", 25 | "\\(", "\\)", "\\?", "\\|", "\\{", "\\}", "\\$"}; 26 | 27 | /* 28 | Method Section 29 | */ 30 | 31 | /** 32 | Replaces characters which are special to the regular expression language by their 33 | escaped versions. 34 | */ 35 | public static String escapeRegex(String splitter) 36 | { 37 | String special = ""; 38 | try 39 | { 40 | splitter = splitter.replaceAll("\\\\", "\\\\\\\\"); 41 | 42 | for (int i = 0, n = regex.length; i < n; i++) 43 | { 44 | special = regex[i]; 45 | splitter = splitter.replaceAll(special, "\\" + special); 46 | } 47 | } 48 | catch (RuntimeException e) 49 | { 50 | throw new RuntimeException(e.getMessage() + "\nWhen trying to escape \"" + special + "\" in \"" + splitter + "\"", e); 51 | } 52 | 53 | return splitter; 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/SystemTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import junit.framework.TestCase; 21 | 22 | public class SystemTest extends TestCase 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private System tag; 29 | 30 | /* 31 | Events 32 | */ 33 | 34 | protected void setUp() 35 | { 36 | 37 | } 38 | 39 | protected void tearDown() 40 | { 41 | tag = null; 42 | } 43 | 44 | /* 45 | Methods 46 | */ 47 | 48 | public void testParse() 49 | { 50 | tag = new System("result = \"Hello System!\""); 51 | AliceBot bot = new AliceBot(); 52 | bot.setContext(new Context()); 53 | Match match = new Match(); 54 | match.setCallback(bot); 55 | 56 | assertEquals("Hello System!", tag.process(match)); 57 | } 58 | 59 | public void testArithmetics() 60 | { 61 | tag = new System("1 + 1"); 62 | AliceBot bot = new AliceBot(); 63 | bot.setContext(new Context()); 64 | Match match = new Match(); 65 | match.setCallback(bot); 66 | 67 | assertEquals("2", tag.process(match)); 68 | } 69 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/SearcherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@bol.com.br 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.util; 16 | 17 | import junit.framework.TestCase; 18 | 19 | import java.net.URL; 20 | 21 | public class SearcherTest extends TestCase 22 | { 23 | /* 24 | Attribute Section 25 | */ 26 | 27 | private Searcher searcher; 28 | 29 | /* 30 | Event Section 31 | */ 32 | 33 | protected void setUp() 34 | { 35 | searcher = new Searcher(); 36 | } 37 | 38 | protected void tearDown() 39 | { 40 | searcher = null; 41 | } 42 | 43 | /* 44 | Method Section 45 | */ 46 | 47 | public void testDirFilesystem() 48 | { 49 | String[] paths = searcher.dir("Bots/Alice", ".+\\.aiml"); 50 | 51 | assertEquals("Bots/Alice/Again.aiml", paths[0]); 52 | assertEquals("Bots/Alice/Alice.aiml", paths[1]); 53 | assertEquals("Bots/Alice/Astrology.aiml", paths[2]); 54 | } 55 | 56 | public void testDirURL() throws Exception 57 | { 58 | String[] paths = searcher.dir(new URL("file", "localhost", "./"), "Bots/Alice", ".+\\.aiml"); 59 | 60 | assertEquals("Bots/Alice/Again.aiml", paths[0]); 61 | assertEquals("Bots/Alice/Alice.aiml", paths[1]); 62 | assertEquals("Bots/Alice/Astrology.aiml", paths[2]); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/script/BeanshellInterpreter.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.script; 16 | 17 | /** 18 | Interpreter for Beanshell scripts. 19 | */ 20 | public class BeanshellInterpreter implements Interpreter 21 | { 22 | /* 23 | Attribute Section 24 | */ 25 | 26 | /** Beanshell interpreter. */ 27 | private final bsh.Interpreter interpreter = new bsh.Interpreter(); 28 | 29 | /* 30 | Method Section 31 | */ 32 | 33 | public Object evaluate(String script) throws InterpretingException 34 | { 35 | try 36 | { 37 | return interpreter.eval(script); 38 | } 39 | catch (Exception e) 40 | { 41 | throw new InterpretingException(e); 42 | } 43 | } 44 | 45 | public Object variable(String name) throws InterpretingException 46 | { 47 | try 48 | { 49 | return interpreter.get(name); 50 | } 51 | catch (Exception e) 52 | { 53 | throw new InterpretingException(e); 54 | } 55 | } 56 | 57 | public void variable(String name, Object value) throws InterpretingException 58 | { 59 | try 60 | { 61 | interpreter.set(name, value); 62 | } 63 | catch (Exception e) 64 | { 65 | throw new InterpretingException(e); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Topicstar.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | import static bitoflife.chatterbean.Match.Section.TOPIC; 21 | 22 | public class Topicstar extends TemplateElement 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private int index; 29 | 30 | /* 31 | Constructor 32 | */ 33 | 34 | public Topicstar(Attributes attributes) 35 | { 36 | String value = attributes.getValue(0); 37 | if (value == null) 38 | index = 1; 39 | else 40 | index = Integer.parseInt(value); 41 | } 42 | 43 | public Topicstar(int index) 44 | { 45 | this.index = index; 46 | } 47 | 48 | /* 49 | Methods 50 | */ 51 | 52 | public boolean equals(Object obj) 53 | { 54 | if (!super.equals(obj)) return false; 55 | Topicstar compared = (Topicstar) obj; 56 | 57 | return (index == compared.index); 58 | } 59 | 60 | public String toString() 61 | { 62 | return ""; 63 | } 64 | 65 | public String process(Match match) 66 | { 67 | String wildcard = match.wildcard(TOPIC, index); 68 | return (wildcard != null ? wildcard.trim() : ""); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/SplitterHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.parser; 16 | 17 | import org.xml.sax.Attributes; 18 | import org.xml.sax.SAXException; 19 | import org.xml.sax.helpers.DefaultHandler; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | public class SplitterHandler extends DefaultHandler 25 | { 26 | /* 27 | Attribute Section 28 | */ 29 | 30 | private List splitters; 31 | 32 | /* 33 | Constructor Section 34 | */ 35 | 36 | public SplitterHandler() 37 | { 38 | splitters = new ArrayList(4); 39 | } 40 | 41 | public SplitterHandler(List splitters) 42 | { 43 | this.splitters = splitters; 44 | } 45 | 46 | /* 47 | Event Section 48 | */ 49 | 50 | public void startElement(String namespace, String name, String qname, Attributes attributes) throws SAXException 51 | { 52 | if (qname.equals("splitter") && !"word".equals(attributes.getValue("type"))) 53 | splitters.add(attributes.getValue(0)); 54 | } 55 | 56 | /* 57 | Method Section 58 | */ 59 | 60 | public void clear() 61 | { 62 | splitters.clear(); 63 | } 64 | 65 | public List parsed() 66 | { 67 | return new ArrayList(splitters); 68 | } 69 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Thatstar.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | import static bitoflife.chatterbean.Match.Section.THAT; 21 | 22 | public class Thatstar extends TemplateElement 23 | { 24 | /* 25 | Attribute Section 26 | */ 27 | 28 | private int index; 29 | 30 | /* 31 | Constructor Section 32 | */ 33 | 34 | public Thatstar(Attributes attributes) 35 | { 36 | String value = attributes.getValue(0); 37 | if (value == null) 38 | index = 1; 39 | else 40 | index = Integer.parseInt(value); 41 | } 42 | 43 | public Thatstar(int index) 44 | { 45 | this.index = index; 46 | } 47 | 48 | /* 49 | Method Section 50 | */ 51 | 52 | public boolean equals(Object obj) 53 | { 54 | if (!super.equals(obj)) return false; 55 | Thatstar compared = (Thatstar) obj; 56 | 57 | return (index == compared.index); 58 | } 59 | 60 | public String toString() 61 | { 62 | return ""; 63 | } 64 | 65 | public String process(Match match) 66 | { 67 | String wildcard = match.wildcard(THAT, index); 68 | return (wildcard != null ? wildcard.trim() : ""); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Star.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import org.xml.sax.Attributes; 19 | 20 | import static bitoflife.chatterbean.Match.Section.PATTERN; 21 | 22 | public class Star extends TemplateElement 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private int index; 29 | 30 | /* 31 | Constructor 32 | */ 33 | 34 | public Star(Attributes attributes) 35 | { 36 | String value = attributes.getValue(0); 37 | index = (value != null ? Integer.parseInt(value) : 1); 38 | } 39 | 40 | public Star(int index) 41 | { 42 | this.index = index; 43 | } 44 | 45 | /* 46 | Methods 47 | */ 48 | 49 | public boolean equals(Object obj) 50 | { 51 | if (obj == null || !(obj instanceof Star)) 52 | return false; 53 | else 54 | { 55 | Star star = (Star) obj; 56 | return (index == star.index); 57 | } 58 | } 59 | 60 | public int hashCode() 61 | { 62 | return index; 63 | } 64 | 65 | public String toString() 66 | { 67 | return ""; 68 | } 69 | 70 | public String process(Match match) 71 | { 72 | String wildcard = match.wildcard(PATTERN, index); 73 | return (wildcard != null ? wildcard.trim() : ""); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/AliceBotMother.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import bitoflife.chatterbean.parser.AliceBotParser; 18 | import bitoflife.chatterbean.util.Searcher; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | import java.io.FileInputStream; 22 | 23 | public class AliceBotMother 24 | { 25 | /* 26 | Attribute Section 27 | */ 28 | 29 | private ByteArrayOutputStream gossip; 30 | 31 | /* 32 | Event Section 33 | */ 34 | 35 | public void setUp() 36 | { 37 | gossip = new ByteArrayOutputStream(); 38 | } 39 | 40 | /* 41 | Method Section 42 | */ 43 | 44 | public String gossip() 45 | { 46 | return gossip.toString(); 47 | } 48 | 49 | public AliceBot newInstance() throws Exception 50 | { 51 | Searcher searcher = new Searcher(); 52 | AliceBotParser parser = new AliceBotParser(); 53 | AliceBot bot = parser.parse(getClass().getResourceAsStream("/conf/context.xml"), 54 | getClass().getResourceAsStream("/conf/splitters.xml"), 55 | getClass().getResourceAsStream("/conf/substitutions.xml"), 56 | searcher.search(getClass().getResource("/Corpus/Chinese").toString().substring(5), ".*\\.xml")); 57 | 58 | Context context = bot.getContext(); 59 | context.outputStream(gossip); 60 | return bot; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/aiml/IndexSearchService.java: -------------------------------------------------------------------------------- 1 | package com.aiml; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import org.apache.lucene.document.Document; 8 | import org.apache.lucene.index.CorruptIndexException; 9 | import org.apache.lucene.index.Term; 10 | import org.apache.lucene.search.*; 11 | 12 | import com.customexception.AppException; 13 | import com.job.CreateIndexTask; 14 | import com.util.Util; 15 | 16 | public class IndexSearchService { 17 | private IndexResource resource = null; 18 | CreateIndexTask indexTask = null; 19 | 20 | public IndexSearchService() { 21 | resource = IndexResource.getInstance(); 22 | indexTask = new CreateIndexTask(resource); 23 | } 24 | 25 | public void indexTaskSetup() { 26 | indexTask.fullIndexSetup(); 27 | indexTask.deltaIndexSetup(); 28 | } 29 | 30 | public List search(String keyword) { 31 | List docs = new ArrayList(); 32 | 33 | if (!Util.INDEXFILE.exists()) { 34 | Util.INDEXFILE.mkdir(); 35 | } 36 | IndexSearcher isearcher = null; 37 | try { 38 | isearcher = resource.indexSearcher; 39 | 40 | keyword = keyword.replaceAll(" ",""); 41 | 42 | Query query = getSearchQuery(Util.FIELDSNAME, keyword); 43 | TopDocs topDocs = isearcher.search(query, 5); 44 | ScoreDoc[] scoreDocs = topDocs.scoreDocs; 45 | for (int i = 0; i < scoreDocs.length; i++) { 46 | Document targetDoc = isearcher.doc(scoreDocs[i].doc); 47 | docs.add(targetDoc); 48 | } 49 | } catch (CorruptIndexException e) { 50 | throw new AppException(e); 51 | } catch (IOException e) { 52 | throw new AppException("[ExceptionInfo]在Search 索引的时候出现了IO错误。", e); 53 | } 54 | return docs; 55 | } 56 | 57 | private static Query getSearchQuery(String fieldName, String keyword) { 58 | Query query = null; 59 | 60 | BooleanQuery booleanQuery = new BooleanQuery(); 61 | TermQuery q = new TermQuery(new Term(fieldName, keyword)); 62 | 63 | 64 | BooleanQuery attrQuery = new BooleanQuery();// 组合查询 65 | 66 | 67 | attrQuery.add(q, BooleanClause.Occur.SHOULD); 68 | 69 | booleanQuery.add(attrQuery, BooleanClause.Occur.MUST); 70 | query = booleanQuery; 71 | return query; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Graphmaster; 18 | import bitoflife.chatterbean.util.Searcher; 19 | 20 | import javax.xml.parsers.SAXParser; 21 | import javax.xml.parsers.SAXParserFactory; 22 | import java.io.InputStream; 23 | 24 | public class AIMLParser { 25 | /* 26 | Attributes 27 | */ 28 | 29 | private final Searcher searcher = new Searcher(); 30 | private final AIMLHandler handler = new AIMLHandler(); 31 | private SAXParser parser; 32 | 33 | /* 34 | Constructor 35 | */ 36 | 37 | public AIMLParser() throws AIMLParserConfigurationException { 38 | try { 39 | parser = SAXParserFactory.newInstance().newSAXParser(); 40 | } catch (Exception e) { 41 | throw new AIMLParserConfigurationException(e); 42 | } 43 | } 44 | 45 | /* 46 | Methods 47 | */ 48 | 49 | public void parse(Graphmaster graphmaster, InputStream... sources) throws AIMLParserException { 50 | try { 51 | for (InputStream aiml : sources) { 52 | parser.parse(aiml, handler); 53 | } 54 | 55 | graphmaster.append(handler.unload()); 56 | } catch (Exception e) { 57 | e.printStackTrace(); 58 | throw new AIMLParserException(e); 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Set.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import org.xml.sax.Attributes; 21 | 22 | public class Set extends TemplateElement 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private String name; 29 | 30 | /* 31 | Constructors 32 | */ 33 | 34 | public Set(Attributes attributes) 35 | { 36 | name = attributes.getValue(0); 37 | } 38 | 39 | public Set(String name, Object... children) 40 | { 41 | super(children); 42 | this.name = name; 43 | } 44 | 45 | /* 46 | Methods 47 | */ 48 | 49 | public boolean equals(Object obj) 50 | { 51 | if (obj == null) return false; 52 | Set compared = (Set) obj; 53 | if (!name.equals(compared.name)) return false; 54 | return super.equals(compared); 55 | } 56 | 57 | public String process(Match match) 58 | { 59 | String output = super.process(match); 60 | if (match == null) 61 | output = "" + output + ""; 62 | else 63 | { 64 | AliceBot bot = match.getCallback(); 65 | Context context = (bot != null ? bot.getContext() : null); 66 | if (context != null) context.property("predicate." + name, output); 67 | } 68 | 69 | return output; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Javascript.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import bitoflife.chatterbean.script.Interpreter; 21 | import org.xml.sax.Attributes; 22 | 23 | public class Javascript extends TemplateElement 24 | { 25 | /* 26 | Constructor Section 27 | */ 28 | 29 | public Javascript(Attributes attributes) 30 | { 31 | } 32 | 33 | public Javascript(Object... children) 34 | { 35 | super(children); 36 | } 37 | 38 | /* 39 | Method Section 40 | */ 41 | 42 | public String process(Match match) 43 | { 44 | try 45 | { 46 | AliceBot bot = match.getCallback(); 47 | Context context = bot.getContext(); 48 | Interpreter interpreter = (Interpreter) context.property("javascript.interpreter"); 49 | if (interpreter == null) 50 | return ""; 51 | 52 | String script = super.process(match); 53 | interpreter.variable("result", null); 54 | Object evaluated = interpreter.evaluate(script); 55 | Object result = interpreter.variable("result"); 56 | if (result == null) result = evaluated; 57 | 58 | return (result != null ? result.toString() : ""); 59 | } 60 | catch (Exception e) 61 | { 62 | throw new RuntimeException("Evaluation error on tag", e); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Li.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import org.xml.sax.Attributes; 18 | 19 | public class Li extends TemplateElement 20 | { 21 | /* 22 | Attributes 23 | */ 24 | 25 | private String name; 26 | private String value; 27 | 28 | 29 | /* 30 | Constructors 31 | */ 32 | 33 | public Li() 34 | { 35 | } 36 | 37 | public Li(Attributes attributes) 38 | { 39 | name = attributes.getValue("name"); 40 | value = attributes.getValue("value"); 41 | } 42 | 43 | public Li(String name, String value, Object... children) 44 | { 45 | super(children); 46 | this.name = name; 47 | this.value = value; 48 | } 49 | 50 | /* 51 | Methods 52 | */ 53 | 54 | private boolean isEquals(Object comparing, Object compared) 55 | { 56 | return (comparing == null ? compared == null : comparing.equals(compared)); 57 | } 58 | 59 | public boolean equals(Object obj) 60 | { 61 | if (!super.equals(obj)) return false; 62 | Li compared = (Li) obj; 63 | return (isEquals(name, compared.name) && isEquals(value, compared.value)); 64 | } 65 | 66 | /* 67 | Properties 68 | */ 69 | 70 | public String getName() 71 | { 72 | return name; 73 | } 74 | 75 | public void setName(String name) 76 | { 77 | this.name = name; 78 | } 79 | 80 | public String getValue() 81 | { 82 | return value; 83 | } 84 | 85 | public void setValue(String value) 86 | { 87 | this.value = value; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/System.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Match; 20 | import bitoflife.chatterbean.script.Interpreter; 21 | import org.xml.sax.Attributes; 22 | 23 | public class System extends TemplateElement 24 | { 25 | /* 26 | Constructors 27 | */ 28 | 29 | public System(Attributes attributes) 30 | { 31 | } 32 | 33 | public System(Object... children) 34 | { 35 | super(children); 36 | } 37 | 38 | /* 39 | Methods 40 | */ 41 | 42 | public String process(Match match) 43 | { 44 | try 45 | { 46 | AliceBot bot = match.getCallback(); 47 | Context context = bot.getContext(); 48 | Interpreter interpreter = (Interpreter) context.property("beanshell.interpreter"); 49 | if (interpreter == null) 50 | return ""; 51 | 52 | String script = super.process(match); 53 | interpreter.variable("result", null); 54 | interpreter.variable("match", match); 55 | 56 | Object evaluated = interpreter.evaluate(script); 57 | Object result = interpreter.variable("result"); 58 | if (result == null) result = evaluated; 59 | 60 | interpreter.variable("match", null); 61 | 62 | return (result != null ? result.toString() : ""); 63 | } 64 | catch (Exception e) 65 | { 66 | throw new RuntimeException("Evaluation error on tag", e); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/ContextTopicChangeListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 H�lio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import bitoflife.chatterbean.text.Sentence; 18 | import bitoflife.chatterbean.text.Transformations; 19 | 20 | import java.beans.PropertyChangeEvent; 21 | 22 | import static bitoflife.chatterbean.text.Sentence.ASTERISK; 23 | 24 | /** 25 | Property change listener for the predicate.topic property. Updates the Context with the new Topic value. 26 | */ 27 | public class ContextTopicChangeListener extends ContextPropertyChangeListener 28 | { 29 | /* 30 | Constructor Section 31 | */ 32 | 33 | /** 34 | Default class constructor. 35 | */ 36 | public ContextTopicChangeListener() 37 | { 38 | super("predicate.topic"); 39 | } 40 | 41 | /* 42 | Method Section 43 | */ 44 | 45 | // Fired when the predicate.topic property changes. 46 | public void propertyChange(PropertyChangeEvent event) 47 | { 48 | Object oldTopic = event.getOldValue(); 49 | Object newTopic = event.getNewValue(); 50 | Context context = (Context) event.getSource(); 51 | Transformations transformations = context.getTransformations(); 52 | 53 | if (oldTopic == null ? newTopic == null : oldTopic.equals(newTopic)) 54 | return; 55 | 56 | String input = newTopic.toString().trim(); 57 | if ("".equals(input) || "*".equals(input)) 58 | context.setTopic(ASTERISK); 59 | else 60 | { 61 | Sentence topic = new Sentence(input); 62 | transformations.normalization(topic); 63 | context.setTopic(topic); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/SequenceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.util; 16 | 17 | import junit.framework.TestCase; 18 | 19 | import java.io.FileWriter; 20 | import java.io.IOException; 21 | import java.io.PrintWriter; 22 | 23 | public class SequenceTest extends TestCase 24 | { 25 | /* 26 | Attributes 27 | */ 28 | 29 | private static final SequenceMother mother = new SequenceMother(); 30 | 31 | private Sequence sequence1, sequence2; 32 | 33 | /* 34 | Events 35 | */ 36 | 37 | protected void setUp() throws Exception 38 | { 39 | mother.reset(); 40 | 41 | sequence1 = mother.newInstance(); 42 | sequence2 = mother.newInstance(); 43 | } 44 | 45 | protected void tearDown() 46 | { 47 | sequence1 = sequence2 = null; 48 | } 49 | 50 | /* 51 | Methods 52 | */ 53 | 54 | public void testGetNext() throws IOException 55 | { 56 | for (int i = 0; i < 100; i++) 57 | { 58 | long a = sequence1.getNext(); 59 | long b = sequence2.getNext(); 60 | 61 | assertTrue("sequence1 = " + a + ", sequence2 = " + b, a != b); 62 | } 63 | 64 | } 65 | 66 | public void testPersistence() throws IOException 67 | { 68 | long count = sequence1.getNext(); 69 | 70 | // Simulates a system crash at the moment the persistent file is open. 71 | PrintWriter writer = new PrintWriter(new FileWriter(mother.file, false), true); 72 | writer.println(""); 73 | writer.close(); 74 | 75 | sequence1 = mother.newInstance(); 76 | 77 | assertEquals(count + 1, sequence1.getNext()); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/util/Util.java: -------------------------------------------------------------------------------- 1 | package com.util; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.FileWriter; 7 | import java.io.IOException; 8 | import java.text.SimpleDateFormat; 9 | import java.util.Date; 10 | 11 | 12 | import com.customexception.AppException; 13 | 14 | public class Util { 15 | 16 | // 数据库数据索引存放位置 17 | public static final File INDEXFILE = new File("./Index"); 18 | public static final String INDEXFILE_PATH = "./Index"; 19 | 20 | // 每次索引的时间戳文件存放位置 21 | public static final File TIMESTAMPFILE = new File("./timestamp.txt"); 22 | 23 | // 编码 24 | public static final String ENCODING = "GBK"; 25 | 26 | // 需要被索引的数据库字段 27 | public static final String FIELDSNAME = "copyfield"; 28 | 29 | 30 | /** 31 | * 用来产生时间戳文件,该文件记录索引最近更新时间 32 | */ 33 | public static void setTimestamp() { 34 | String correntTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") 35 | .format(new Date()); 36 | 37 | FileWriter fileWriter = null; 38 | 39 | try { 40 | if (!TIMESTAMPFILE.exists()) { 41 | TIMESTAMPFILE.createNewFile(); 42 | } 43 | fileWriter = new FileWriter(TIMESTAMPFILE); 44 | fileWriter.write(correntTime); 45 | } catch (IOException e) { 46 | throw new AppException("[ExceptionInfo]在写时间戳的时候出现IO错误。", e); 47 | } finally { 48 | try { 49 | if (fileWriter != null) 50 | fileWriter.close(); 51 | } catch (IOException e) { 52 | throw new AppException("[ExceptionInfo]在写时间戳结束后关闭数据流时出现错误。", e); 53 | } 54 | } 55 | } 56 | 57 | /** 58 | * 读取时间戳文件。 59 | * 60 | * @return 61 | */ 62 | public static String getTimestamp() { 63 | BufferedReader bufReader = null; 64 | FileReader fileReader = null; 65 | String timestamp = ""; 66 | try { 67 | if (!TIMESTAMPFILE.exists()) { 68 | 69 | TIMESTAMPFILE.createNewFile(); 70 | FileWriter fileWriter = new FileWriter(TIMESTAMPFILE); 71 | fileWriter.write("1990/01/01 00:00:00"); 72 | fileWriter.close(); 73 | } 74 | fileReader = new FileReader(TIMESTAMPFILE); 75 | bufReader = new BufferedReader(fileReader); 76 | timestamp = bufReader.readLine(); 77 | 78 | } catch (IOException e) { 79 | throw new AppException("[ExceptionInfo]读时间戳文件或创建时间戳文件出现IO错误。", e); 80 | } finally { 81 | if (fileReader != null) { 82 | try { 83 | fileReader.close(); 84 | } catch (IOException e) { 85 | // TODO Auto-generated catch block 86 | e.printStackTrace(); 87 | } 88 | } 89 | } 90 | return timestamp; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/ContextParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.parser; 16 | 17 | import bitoflife.chatterbean.Context; 18 | import junit.framework.TestCase; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.InputStream; 22 | 23 | public class ContextParserTest extends TestCase 24 | { 25 | /* 26 | Attributes 27 | */ 28 | 29 | private static final String xml = 30 | "" + 31 | "" + 32 | "" + 33 | "" + 34 | "" + 35 | "" + 36 | "" + 37 | ""; 38 | 39 | private InputStream source; 40 | 41 | private ContextParser parser; 42 | 43 | /* 44 | Events 45 | */ 46 | 47 | public void setUp() throws Exception 48 | { 49 | source = new ByteArrayInputStream(xml.getBytes("UTF-8")); 50 | parser = new ContextParser(); 51 | } 52 | 53 | /* 54 | Methods 55 | */ 56 | 57 | public void testParse() throws Exception 58 | { 59 | Context context = parser.parse(source); 60 | assertEquals("Unknown Person", context.property("predicate.user")); 61 | assertEquals("ChatterBean", context.property("predicate.engine")); 62 | assertEquals("yyyy-MM-dd HH:mm:ss", context.property("predicate.dateFormat")); 63 | assertEquals("Alice", context.property("bot.me")); 64 | assertEquals("Logs/", context.property("bot.output")); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Input.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | import bitoflife.chatterbean.text.Request; 19 | import org.xml.sax.Attributes; 20 | 21 | public class Input extends TemplateElement 22 | { 23 | /* 24 | Attributes 25 | */ 26 | 27 | private int requestIndex = 1, sentenceIndex = 1; 28 | 29 | /* 30 | Constructors 31 | */ 32 | 33 | public Input(Attributes attributes) 34 | { 35 | String value = attributes.getValue(0); 36 | if (value == null) return; 37 | 38 | String[] indexes = value.split(","); 39 | requestIndex = Integer.parseInt(indexes[0].trim()); 40 | if (indexes.length > 1) sentenceIndex = Integer.parseInt(indexes[1].trim()); 41 | } 42 | 43 | public Input(int requestIndex, int sentenceIndex) 44 | { 45 | this.requestIndex = requestIndex; 46 | this.sentenceIndex = sentenceIndex; 47 | } 48 | 49 | /* 50 | Methods 51 | */ 52 | 53 | public boolean equals(Object obj) 54 | { 55 | if (!super.equals(obj)) return false; 56 | 57 | Input compared = (Input) obj; 58 | return (requestIndex == compared.requestIndex && 59 | sentenceIndex == compared.sentenceIndex); 60 | } 61 | 62 | public String toString() 63 | { 64 | return ""; 65 | } 66 | 67 | public String process(Match match) 68 | { 69 | if (match == null) return ""; 70 | Request request = match.getCallback().getContext().getRequests(requestIndex - 1); 71 | return request.lastSentence(sentenceIndex - 1).trimOriginal(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/astrology.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | AQUARIUS 13 | 14 | 15 | CAPRICORN 16 | 17 | 18 | WHAT IS A CAPRICORN 19 | 20 | 21 | WHAT IS A CANCER 22 | 23 | 24 | CANCER 25 | 26 | 27 | PISCES 28 | 29 | 30 | SCORPIO 31 | 32 | 33 | ARIES 34 | 35 | 36 | TAURUS 37 | 38 | 39 | LIBRA 40 | 41 | 42 | SAGGITARIUS 43 | 44 | 45 | VIRGO 46 | 47 | 48 | GEMINI 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/LoggerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean; 16 | 17 | import bitoflife.chatterbean.util.Sequence; 18 | import junit.framework.TestCase; 19 | 20 | import java.io.*; 21 | 22 | public class LoggerTest extends TestCase 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private File file; 29 | private Logger logger; 30 | 31 | /* 32 | Events 33 | */ 34 | 35 | protected void setUp() throws Exception 36 | { 37 | Sequence sequence = new Sequence("Logs/sequence.txt"); 38 | file = new File("Logs/log" + sequence.getNext() + ".txt"); 39 | logger = new Logger(new FileWriter(file)); 40 | } 41 | 42 | protected void tearDown() 43 | { 44 | file = null; 45 | logger = null; 46 | } 47 | 48 | /* 49 | Methods 50 | */ 51 | 52 | public void testAddEntry() throws IOException 53 | { 54 | logger.append("First request", "First response"); 55 | logger.append("Second request", "Second response"); 56 | logger.append("Third request", "Third response"); 57 | 58 | BufferedReader reader = new BufferedReader(new FileReader(file)); 59 | 60 | assertTrue(reader.readLine().matches("\\[[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}\\]" + 61 | "\\[First request\\]\\[First response\\]")); 62 | 63 | assertTrue(reader.readLine().matches("\\[[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}\\]" + 64 | "\\[Second request\\]\\[Second response\\]")); 65 | 66 | assertTrue(reader.readLine().matches("\\[[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}\\]" + 67 | "\\[Third request\\]\\[Third response\\]")); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/stack.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | SHOW STACK 13 | 14 | 15 | POP 16 | 17 | 18 | POPOM OM 19 | 20 | 21 | POPOM * 22 | 23 | 24 | RANDOM TOPIC 25 | 26 | 27 | PUSH * 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/SentenceSplitterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2006 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.text; 16 | 17 | import junit.framework.TestCase; 18 | 19 | import java.util.Arrays; 20 | import java.util.HashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | public class SentenceSplitterTest extends TestCase 25 | { 26 | /* 27 | Attribute Section 28 | */ 29 | 30 | private SentenceSplitter splitter; 31 | 32 | /* 33 | Event Section 34 | */ 35 | 36 | protected void setUp() throws Exception 37 | { 38 | splitter = newSentenceSplitter(); 39 | } 40 | 41 | protected void tearDown() 42 | { 43 | splitter = null; 44 | } 45 | 46 | /* 47 | Method Section 48 | */ 49 | 50 | private SentenceSplitter newSentenceSplitter() 51 | { 52 | Map protection = new HashMap(); 53 | List splitters = Arrays.asList(new String[] {"...", ".", "!", "?", ";", ",", ":"}); 54 | return new SentenceSplitter(protection, splitters); 55 | } 56 | 57 | /* 58 | Test Section 59 | */ 60 | 61 | public void testSplitString() 62 | { 63 | String[] expected = 64 | {"Hello Alice.", "How are you?", "You look fine!", "Please forgive my manners;", "I am so happy today..."}; 65 | 66 | String input = "Hello Alice. How are you? You look fine! Please forgive my manners; I am so happy today..."; 67 | String[] actual = splitter.split(input); 68 | 69 | assertEquals(Arrays.asList(expected), Arrays.asList(actual)); 70 | } 71 | 72 | public void testSplitStringEmpty() 73 | { 74 | String[] expected = {"thank you."}; 75 | String input = " ...thank you. "; 76 | String[] actual = splitter.split(input); 77 | 78 | assertEquals(Arrays.asList(expected), Arrays.asList(actual)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/ContextParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.parser; 16 | 17 | import bitoflife.chatterbean.Context; 18 | import org.xml.sax.Attributes; 19 | import org.xml.sax.SAXException; 20 | import org.xml.sax.helpers.DefaultHandler; 21 | 22 | import javax.xml.parsers.ParserConfigurationException; 23 | import javax.xml.parsers.SAXParser; 24 | import javax.xml.parsers.SAXParserFactory; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | 28 | public class ContextParser 29 | { 30 | /* 31 | Inner Classes 32 | */ 33 | 34 | private class ContextHandler extends DefaultHandler 35 | { 36 | public void startElement(String namespace, String name, String qname, Attributes attributes) throws SAXException 37 | { 38 | if (qname.equals("set")) 39 | context.property("predicate." + attributes.getValue("name"), attributes.getValue("value")); 40 | else if (qname.equals("bot")) 41 | context.property("bot." + attributes.getValue("name"), attributes.getValue("value")); 42 | } 43 | } 44 | 45 | /* 46 | Attributes 47 | */ 48 | 49 | private final ContextHandler handler = new ContextHandler(); 50 | private SAXParser parser; 51 | 52 | private Context context; 53 | 54 | /* 55 | Constructor 56 | */ 57 | 58 | public ContextParser() throws ParserConfigurationException, SAXException 59 | { 60 | parser = SAXParserFactory.newInstance().newSAXParser(); 61 | } 62 | 63 | /* 64 | Methods 65 | */ 66 | 67 | public Context parse(InputStream input) throws IOException, SAXException 68 | { 69 | parse(new Context(), input); 70 | return context; 71 | } 72 | 73 | public void parse(Context context, InputStream input) throws IOException, SAXException 74 | { 75 | this.context = context; 76 | parser.parse(input, handler); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/script/JavascriptInterpreter.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.script; 16 | 17 | import netscape.javascript.JSObject; 18 | 19 | import java.applet.Applet; 20 | 21 | /** 22 | Interpreter for Javascript scripts. 23 | */ 24 | public class JavascriptInterpreter implements Interpreter 25 | { 26 | /* 27 | Attribute Section 28 | */ 29 | 30 | /** Reference to the Java Applet containing the AliceBot. */ 31 | private final Applet applet; 32 | 33 | /* 34 | Constructor Section 35 | */ 36 | 37 | /** 38 | Creates a new Javascript interpreter. 39 | 40 | @param window Reference to the Java Applet containing the AliceBot. 41 | */ 42 | public JavascriptInterpreter(Applet applet) 43 | { 44 | this.applet = applet; 45 | } 46 | 47 | /* 48 | Method Section 49 | */ 50 | 51 | /** 52 | Returns a reference to the callback object for the Javascript environment. 53 | 54 | @return Callback object for the Javascript environment. 55 | */ 56 | private JSObject window() 57 | { 58 | return JSObject.getWindow(applet); 59 | } 60 | 61 | public Object evaluate(String script) throws InterpretingException 62 | { 63 | try 64 | { 65 | return window().eval(script); 66 | } 67 | catch (Exception e) 68 | { 69 | throw new InterpretingException(e); 70 | } 71 | } 72 | 73 | public Object variable(String name) throws InterpretingException 74 | { 75 | try 76 | { 77 | return window().getMember(name); 78 | } 79 | catch (Exception e) 80 | { 81 | throw new InterpretingException(e); 82 | } 83 | } 84 | 85 | public void variable(String name, Object value) throws InterpretingException 86 | { 87 | try 88 | { 89 | window().setMember(name, value); 90 | } 91 | catch (Exception e) 92 | { 93 | throw new InterpretingException(e); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/reductions-update.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | * YOU KNOW 13 | 14 | 15 | * I THOUGHT 16 | 17 | 18 | 19 | MY _ S NAME IS * 20 | 21 | 22 | MY _ IS NAMED * 23 | 24 | 25 | 26 | SNOW IN THE FORECAST 27 | 28 | 29 | 30 | INTERESTED IN * 31 | 32 | 33 | 34 | CALL * PHONE 35 | 36 | 37 | 38 | CALL * CALL * 39 | 40 | 41 | 42 | I AM IN * I AM IN * 43 | 44 | 45 | 46 | I AM * YEARS OLD I * 47 | 49 | 50 | WHAT DO YOU MEAN * O M 51 | 53 | 54 | HOW OLD IS THAT MAKE YOU 55 | 57 | 58 | WHO IS MY * 59 | 61 | 62 | _ FOR ME 63 | 65 | 66 | XDMOZ * 67 | 68 | 69 | GOOGLE * 70 | 71 | 72 | ACCESS * 73 | 74 | 75 | XGOOGLE * 76 | 77 | 78 | TO CALL * 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/interjection.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | INTERJECTION 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/java/com/context/ChartContext.java: -------------------------------------------------------------------------------- 1 | package com.context; 2 | 3 | import com.aiml.AskToAIML; 4 | import com.aiml.AskToDB; 5 | import com.aiml.IAskApproach; 6 | 7 | import java.util.regex.Matcher; 8 | import java.util.regex.Pattern; 9 | 10 | 11 | /** 12 | * 聊天上下文 13 | * 14 | */ 15 | public class ChartContext implements IAskApproach { 16 | 17 | private IAskApproach askToAIML = null; 18 | 19 | private IAskApproach askToDB = null; 20 | 21 | private final String NULLSIGN = "#"; // 这个标志是用来表示,当查询AIML的时候,匹配到了*。 22 | 23 | private final String USEFULSIGN = "$";// 这个标志是用来表示,当查询AIML的时候匹配到了专业问题的模式。 24 | 25 | private final String[] NULLREPLAY = { 26 | "对不起,我还不能回答您的这个问题。", 27 | "唔,主人还没有教会我这个问题呢。", 28 | "我暂时还回答不了这个问题呢?", 29 | "我好像不明白。", 30 | 31 | }; 32 | 33 | private final String DMSDMSG ="您可以联系大米时代的其他小哥哥小姐姐,座机:010-51292788 或者 0316-5552070, 手机(微信):1583163905,\n" + 34 | " 邮箱:3460307818@qq.com,\n" + 35 | " 您也可以直接来看看小A,地址:廊坊市广阳区文明路与永丰道交叉口志晟创客中心四楼,我们期待您的来访!\n"; 36 | 37 | public ChartContext(AskToAIML askToAIML, AskToDB askToDB) { 38 | this.askToAIML = askToAIML; 39 | this.askToDB = askToDB; 40 | } 41 | 42 | /** 43 | * 联合AIML和数据库这2个知识库。 44 | */ 45 | public String response(String input) { 46 | String responseFromAIML = askToAIML.response(input); 47 | // 替换文本中的空格字符串 48 | // 判断是中文还是英文,中文需要去掉空格。 49 | // if(isContainChinese(responseFromAIML)){ 50 | // responseFromAIML = responseFromAIML.replace(" ", ""); 51 | // } 52 | return translate(input, responseFromAIML); 53 | } 54 | 55 | 56 | 57 | public static boolean isContainChinese(String str) { 58 | Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); 59 | Matcher m = p.matcher(str); 60 | if (m.find()) { 61 | return true; 62 | } 63 | return false; 64 | } 65 | 66 | 67 | /** 68 | * 从数据库中查询 69 | * (处理无法回复的回复) 70 | * 71 | * @param originInput 原来输入的内容 72 | * @param aimlReplay 回答的内容 73 | * @return 74 | */ 75 | private String translate(String originInput, String aimlReplay) { 76 | String asDBInput = ""; 77 | if (-1 != aimlReplay.indexOf(NULLSIGN)) { 78 | asDBInput = originInput; 79 | } else if (-1 != aimlReplay.indexOf(USEFULSIGN)) { 80 | asDBInput = aimlReplay.replaceAll(USEFULSIGN, ""); 81 | } else { 82 | return aimlReplay; 83 | } 84 | 85 | aimlReplay = aimlReplay.replaceFirst("#",""); 86 | 87 | 88 | String dbReplay = askToDB.response(aimlReplay); 89 | if (0 == dbReplay.length()) { 90 | return getRandomResponse(); 91 | } else { 92 | return dbReplay; 93 | } 94 | } 95 | 96 | /** 97 | * 随机回复 98 | * @return 99 | */ 100 | private String getRandomResponse() { 101 | return NULLREPLAY[getRandomNum()]+DMSDMSG; 102 | } 103 | 104 | private int getRandomNum() { 105 | return (int) (Math.random() * NULLREPLAY.length); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/Sequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.util; 16 | 17 | import java.io.*; 18 | 19 | /** 20 | Synchronized linear integer generator. 21 | */ 22 | public class Sequence 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private File backup, file; 29 | 30 | /* 31 | Constructor 32 | */ 33 | 34 | public Sequence(File file) 35 | { 36 | this.file = file; 37 | backup = new File(file.getAbsolutePath() + ".backup"); 38 | } 39 | 40 | public Sequence(String path) 41 | { 42 | file = new File(path); 43 | backup = new File(path + ".backup"); 44 | } 45 | 46 | /* 47 | Methods 48 | */ 49 | 50 | private long loadNext(File file) throws IOException 51 | { 52 | String line = ""; 53 | 54 | try 55 | { 56 | BufferedReader reader = new BufferedReader(new FileReader(file)); 57 | 58 | line = reader.readLine(); 59 | 60 | long next = Long.parseLong(line); 61 | 62 | reader.close(); 63 | 64 | return next; 65 | } 66 | catch (NumberFormatException e) 67 | { 68 | throw new IOException("Illegal value on persistence file: " + line); 69 | } 70 | catch (FileNotFoundException e) 71 | { 72 | return 0; 73 | } 74 | } 75 | 76 | private void saveNext(File file, long next) throws IOException 77 | { 78 | PrintWriter writer = new PrintWriter(new FileWriter(file, false), true); 79 | 80 | writer.println(Long.toString(next + 1)); 81 | 82 | writer.close(); 83 | } 84 | 85 | /** 86 | Return the next number in the sequence. 87 | */ 88 | public synchronized long getNext() throws IOException 89 | { 90 | long next = 0; 91 | 92 | try 93 | { 94 | next = loadNext(file.exists() ? file : backup); 95 | } 96 | catch (IOException e) 97 | { 98 | next = loadNext(backup); 99 | } 100 | 101 | saveNext(backup, next); 102 | saveNext(file, next); 103 | 104 | return next; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/ReflectionHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.parser; 16 | 17 | import org.xml.sax.Attributes; 18 | import org.xml.sax.helpers.DefaultHandler; 19 | 20 | import java.lang.reflect.Method; 21 | 22 | public class ReflectionHandler extends DefaultHandler 23 | { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private ReflectionBuilder builder = null; 29 | 30 | /* 31 | Constructor 32 | */ 33 | 34 | public ReflectionHandler() 35 | { 36 | } 37 | 38 | public ReflectionHandler(ReflectionBuilder builder) 39 | { 40 | this.builder = builder; 41 | } 42 | 43 | /* 44 | Methods 45 | */ 46 | 47 | public void characters(char[] chars, int start, int length) 48 | { 49 | builder.characters(chars, start, length); 50 | } 51 | 52 | public void endElement(String namespace, String name, String qname) 53 | { 54 | try 55 | { 56 | String methodName = "end" + qname.substring(0, 1).toUpperCase() + qname.substring(1); 57 | Method event = builder.getClass().getMethod(methodName); 58 | event.invoke(builder); 59 | } 60 | catch (NoSuchMethodException e) 61 | { 62 | } 63 | catch (Exception e) 64 | { 65 | e.printStackTrace(); 66 | } 67 | } 68 | 69 | public void startElement(String namespace, String name, String qname, Attributes attributes) 70 | { 71 | try 72 | { 73 | String methodName = "start" + qname.substring(0, 1).toUpperCase() + qname.substring(1); 74 | Method event = builder.getClass().getMethod(methodName, Attributes.class); 75 | event.invoke(builder, attributes); 76 | } 77 | catch (NoSuchMethodException e) 78 | { 79 | } 80 | catch (Exception e) 81 | { 82 | e.printStackTrace(); 83 | } 84 | } 85 | 86 | /* 87 | Properties 88 | */ 89 | 90 | public ReflectionBuilder getReflectionBuilder() 91 | { 92 | return builder; 93 | } 94 | 95 | public void setReflectionBuilder(ReflectionBuilder builder) 96 | { 97 | this.builder = builder; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/Request.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.text; 16 | 17 | import java.util.Arrays; 18 | 19 | 20 | /** 21 | * 请求对象 22 | */ 23 | public class Request { 24 | 25 | /** 26 | * 句子 27 | */ 28 | private Sentence[] sentences; 29 | 30 | /** 31 | * 来源 32 | */ 33 | private String original; 34 | 35 | /** 36 | * 构造 37 | */ 38 | public Request() { 39 | } 40 | 41 | public Request(String original) { 42 | this.original = original; 43 | } 44 | 45 | /** 46 | * 复合构造 47 | */ 48 | public Request(String original, Sentence... sentences) { 49 | this.original = original; 50 | this.sentences = sentences; 51 | } 52 | 53 | /** 54 | * 空句子 55 | * @return 56 | */ 57 | public boolean empty() { 58 | return (sentences == null || sentences.length == 0); 59 | } 60 | 61 | 62 | 63 | public boolean equals(Object obj) { 64 | if (obj == null || !(obj instanceof Request)) return false; 65 | 66 | Request compared = (Request) obj; 67 | return original.equals(compared.original) && 68 | Arrays.equals(sentences, compared.sentences); 69 | } 70 | 71 | 72 | /** 73 | * 最后一个句子 74 | * @param index 75 | * @return 76 | */ 77 | public Sentence lastSentence(int index) { 78 | return sentences[sentences.length - (1 + index)]; 79 | } 80 | 81 | public String toString() { 82 | return original; 83 | } 84 | 85 | public String trimOriginal() { 86 | return original.trim(); 87 | } 88 | 89 | public String getOriginal() { 90 | return original; 91 | } 92 | 93 | public void setOriginal(String original) { 94 | this.original = original; 95 | } 96 | 97 | public Sentence[] getSentences() { 98 | return sentences; 99 | } 100 | 101 | public Sentence getSentences(int index) { 102 | return sentences[index]; 103 | } 104 | 105 | public void setSentences(Sentence[] sentences) { 106 | this.sentences = sentences; 107 | } 108 | } -------------------------------------------------------------------------------- /src/main/resources/Corpus/English/music.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | BEETHOVEN * 13 | 14 | 15 | WHAT IS YOUR FAVORITE ABBA SONG 16 | 17 | 18 | WHAT IS YOUR FAVORITE ALBUM 19 | 20 | 21 | WHAT WAS THE * BEETHOVEN * 22 | 23 | 24 | CAN YOU PLAY MUSIC 25 | 26 | 27 | CAN YOU MAKE MUSIC 28 | 29 | 30 | DO YOU PLAY A MUSICAL INSTRUMENT 31 | 32 | 33 | DO YOU PLAY AN INSTRUMENT 34 | 35 | 36 | DO YOU LIKE AEROSMITH 37 | 38 | 39 | DO YOU LIKE AC DC 40 | 41 | 42 | DO YOU LIKE ABBA 43 | 44 | 45 | WHY IS * YOUR FAVORITE GROUP 46 | 47 | 48 | WHY IS * YOUR FAVORITE BAND 49 | 50 | 51 | ARE YOU A FOLK SINGER 52 | 53 | 54 | WHO IS LUDWIG BEETHOVEN 55 | 56 | 57 | WHO IS BEETHOVEN 58 | 59 | 60 | WHO IS YOUR FAVORITE BAND 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Aiml.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import org.xml.sax.Attributes; 18 | 19 | import java.util.Arrays; 20 | import java.util.LinkedList; 21 | import java.util.List; 22 | 23 | public class Aiml implements AIMLElement { 24 | /* 25 | Attributes 26 | */ 27 | 28 | private final Topic defaultTopic = new Topic("*"); 29 | private final List topics = new LinkedList(Arrays.asList(new Topic[]{defaultTopic})); 30 | 31 | private final List categories = new LinkedList(); 32 | 33 | private String version; 34 | 35 | /* 36 | Constructors 37 | */ 38 | 39 | public Aiml(Attributes attributes) { 40 | version = attributes.getValue(0); 41 | } 42 | 43 | public Aiml(Category... categories) { 44 | this.categories.addAll(Arrays.asList(categories)); 45 | } 46 | 47 | /* 48 | Method Section 49 | */ 50 | 51 | public void appendChild(AIMLElement child) { 52 | if (child instanceof Category) { 53 | Category category = (Category) child; 54 | category.setTopic(defaultTopic); 55 | defaultTopic.appendChild(category); 56 | categories.add(category); 57 | } else { 58 | Topic topic = (Topic) child; 59 | topics.add(topic); 60 | categories.addAll(topic.categories()); 61 | } 62 | } 63 | 64 | public void appendChildren(List children) { 65 | for (AIMLElement child : children) 66 | appendChild(child); 67 | } 68 | 69 | public List children() { 70 | return categories; 71 | } 72 | 73 | public boolean equals(Object obj) { 74 | if (obj == null || !(obj instanceof Aiml)) 75 | return false; 76 | else 77 | return categories.equals(((Aiml) obj).categories); 78 | } 79 | 80 | public String toString() { 81 | StringBuilder result = new StringBuilder(); 82 | for (Category i : categories) { 83 | result.append(i); 84 | result.append('\n'); 85 | } 86 | 87 | return result.toString(); 88 | } 89 | 90 | /* 91 | Properties 92 | */ 93 | 94 | public String getVersion() { 95 | return version; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Topic.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import org.xml.sax.Attributes; 18 | 19 | import java.util.Arrays; 20 | import java.util.LinkedList; 21 | import java.util.List; 22 | 23 | public class Topic implements AIMLElement 24 | { 25 | /* 26 | Attribute Section 27 | */ 28 | 29 | private List categories = new LinkedList(); 30 | 31 | private String name; 32 | 33 | /* 34 | Constructor Section 35 | */ 36 | 37 | public Topic(Attributes attributes) 38 | { 39 | name(attributes.getValue(0)); 40 | } 41 | 42 | public Topic(String name, Category... children) 43 | { 44 | name(name); 45 | categories.addAll(Arrays.asList(children)); 46 | } 47 | 48 | /* 49 | Method Section 50 | */ 51 | 52 | public void appendChild(AIMLElement child) 53 | { 54 | Category category = (Category) child; 55 | category.setTopic(this); 56 | categories.add(category); 57 | } 58 | 59 | public void appendChildren(List children) 60 | { 61 | for (AIMLElement child : children) 62 | appendChild(child); 63 | } 64 | 65 | public List categories() 66 | { 67 | return categories; 68 | } 69 | 70 | public String[] elements() 71 | { 72 | return name.split(" "); 73 | } 74 | 75 | public boolean equals(Object obj) 76 | { 77 | if (obj == null || !(obj instanceof Topic)) 78 | return false; 79 | else 80 | { 81 | Topic compared = (Topic) obj; 82 | return (name.equals(compared.name) && categories.equals(compared.categories)); 83 | } 84 | } 85 | 86 | public String toString() 87 | { 88 | StringBuilder result = new StringBuilder(); 89 | for (Category i : categories) 90 | { 91 | result.append(i); 92 | result.append('\n'); 93 | } 94 | 95 | return result.toString(); 96 | } 97 | 98 | /* 99 | Acessor Section 100 | */ 101 | 102 | private void name(String name) 103 | { 104 | this.name = name.trim(); 105 | } 106 | 107 | /* 108 | Property Section 109 | */ 110 | 111 | public String getName() 112 | { 113 | return name; 114 | } 115 | 116 | public void setName(String name) 117 | { 118 | this.name = name; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/com/background/RobotProperty.java: -------------------------------------------------------------------------------- 1 | package com.background; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.io.FileOutputStream; 8 | import java.io.IOException; 9 | import java.util.Properties; 10 | 11 | import com.customexception.AppException; 12 | 13 | public class RobotProperty { 14 | private static File PROPERTYFILE = null; 15 | 16 | private static Properties configuration = null; 17 | private static RobotProperty property = null; 18 | 19 | private RobotProperty() { 20 | configurationInit(); 21 | } 22 | 23 | private void configurationInit() { 24 | FileOutputStream fOutputStream = null; 25 | FileInputStream fInputStream = null; 26 | BufferedInputStream bufInputStream = null; 27 | 28 | if(PROPERTYFILE==null){ 29 | String path=getClass().getResource("/robot.properties").toString(); 30 | System.out.println(path); 31 | path=path.substring(5); 32 | System.out.println(path); 33 | PROPERTYFILE = new File(path); 34 | } 35 | 36 | configuration = new Properties(); 37 | try { 38 | 39 | // 当robot.properties文件不存在时,该默认配置默认使用sqlserver数据库配置 40 | if (!PROPERTYFILE.exists()) { 41 | fOutputStream = new FileOutputStream(PROPERTYFILE); 42 | configuration.setProperty("DBDriver", 43 | "com.microsoft.sqlserver.jdbc.SQLServerDriver"); 44 | configuration.setProperty("DBUrl", 45 | "jdbc:sqlserver://localhost:1433;databaseName=DBname"); 46 | configuration.setProperty("DBUsername", ""); 47 | configuration.setProperty("DBPassword", ""); 48 | 49 | // 定义 程序运行后多长时间开始启动增量索引工作 50 | configuration.setProperty("delay", "1000"); 51 | 52 | // 定义 增量索引的周期 53 | configuration.setProperty("period", "5000"); 54 | configuration 55 | .store(fOutputStream, 56 | "set your DataBase infomation\ndelay:The begin time of making delta index.\nperiod:the period time of making delta index."); 57 | } 58 | fInputStream = new FileInputStream(PROPERTYFILE); 59 | bufInputStream = new BufferedInputStream(fInputStream); 60 | configuration.load(bufInputStream); 61 | } catch (FileNotFoundException e) { 62 | throw new AppException("[ExceptionInfo]没有找到" + PROPERTYFILE 63 | + "这个文件。", e); 64 | } catch (IOException e) { 65 | throw new AppException("[ExceptionInfo]在加载配置文件的时候出现IO错误。", e); 66 | } finally { 67 | if (bufInputStream != null) { 68 | try { 69 | bufInputStream.close(); 70 | } catch (IOException e) { 71 | // TODO Auto-generated catch block 72 | e.printStackTrace(); 73 | } 74 | } 75 | if (fInputStream != null) { 76 | try { 77 | fInputStream.close(); 78 | } catch (IOException e) { 79 | // TODO Auto-generated catch block 80 | e.printStackTrace(); 81 | } 82 | } 83 | if (fOutputStream != null) { 84 | try { 85 | fOutputStream.close(); 86 | } catch (IOException e) { 87 | // TODO Auto-generated catch block 88 | e.printStackTrace(); 89 | } 90 | } 91 | } 92 | } 93 | 94 | public static Properties getConfiguration() { 95 | if (property == null) { 96 | property = new RobotProperty(); 97 | } 98 | return configuration; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/Searcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.util; 16 | 17 | import java.io.*; 18 | import java.net.URL; 19 | import java.util.Arrays; 20 | import java.util.LinkedList; 21 | import java.util.List; 22 | 23 | public class Searcher implements FilenameFilter 24 | { 25 | /* 26 | Attribute Section 27 | */ 28 | 29 | private static final String[] STRING_ARRAY = {}; 30 | 31 | private String expression; 32 | 33 | /* 34 | Method Section 35 | */ 36 | 37 | protected String[] dir(String path, String expression) 38 | { 39 | this.expression = expression; 40 | 41 | if (path.charAt(path.length() - 1) != '/') path += "/"; 42 | File dir = new File(path); 43 | String[] names = dir.list(this); 44 | Arrays.sort(names); 45 | 46 | for (int i = 0, n = names.length; i < n; i++) 47 | names[i] = path + names[i]; 48 | 49 | return names; 50 | } 51 | 52 | protected String[] dir(URL base, String path, String expression) throws IOException 53 | { 54 | if (path.charAt(path.length() - 1) != '/') 55 | path += "/"; 56 | 57 | URL url = new URL(base, path); 58 | BufferedReader dir = new BufferedReader( 59 | new InputStreamReader(url.openStream()) 60 | ); 61 | 62 | List files = new LinkedList(); 63 | for (String file = ""; (file = dir.readLine()) != null;) 64 | if (file.matches(expression)) 65 | files.add(path + file); 66 | 67 | return files.toArray(STRING_ARRAY); 68 | } 69 | 70 | public boolean accept(File dir, String name) 71 | { 72 | return name.matches(expression); 73 | } 74 | 75 | public InputStream[] search(String path, String expression) throws IOException 76 | { 77 | String[] names = dir(path, expression); 78 | System.out.println(names); 79 | InputStream[] files = new InputStream[names.length]; 80 | for (int i = 0, n = names.length; i < n; i++) 81 | files[i] = new FileInputStream(names[i]); 82 | 83 | return files; 84 | } 85 | 86 | public InputStream[] search(URL base, String path, String expression) throws IOException 87 | { 88 | String[] files = dir(base, path, expression); 89 | InputStream[] streams = new InputStream[files.length]; 90 | for (int i = 0, n = files.length; i < n; i++) 91 | { 92 | URL aiml = new URL(base, files[i]); 93 | streams[i] = aiml.openStream(); 94 | } 95 | 96 | return streams; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/TemplateElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyleft (C) 2005 Hélio Perroni Filho 3 | xperroni@yahoo.com 4 | ICQ: 2490863 5 | 6 | This file is part of ChatterBean. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 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). 13 | */ 14 | 15 | package bitoflife.chatterbean.aiml; 16 | 17 | import bitoflife.chatterbean.Match; 18 | 19 | import java.util.Arrays; 20 | import java.util.LinkedList; 21 | import java.util.List; 22 | 23 | public class TemplateElement implements AIMLElement 24 | { 25 | /* 26 | Attribute Section 27 | */ 28 | 29 | private static final TemplateElement[] TEMPLATE_ELEMENT_ARRAY = {}; 30 | 31 | private final List children = new LinkedList(); 32 | 33 | /* 34 | Constructor Section 35 | */ 36 | 37 | public TemplateElement(Object... elements) 38 | { 39 | for (Object child : elements) 40 | { 41 | if (child instanceof AIMLElement) 42 | children.add((TemplateElement) child); 43 | else 44 | children.add(new Text(child.toString())); 45 | } 46 | } 47 | 48 | /* 49 | Method Section 50 | */ 51 | 52 | public void appendChild(AIMLElement element) 53 | { 54 | children.add((TemplateElement) element); 55 | } 56 | 57 | public void appendChildren(List elements) 58 | { 59 | for (AIMLElement element : elements) 60 | children.add((TemplateElement) element); 61 | } 62 | 63 | public List children() 64 | { 65 | return children; 66 | } 67 | 68 | public boolean equals(Object object) 69 | { 70 | if (object == null || !(object instanceof TemplateElement)) 71 | return false; 72 | 73 | TemplateElement that = (TemplateElement) object; 74 | return children.equals(that.children); 75 | } 76 | 77 | public int hashCode() 78 | { 79 | return children.hashCode(); 80 | } 81 | 82 | public String process(Match match) 83 | { 84 | StringBuilder value = new StringBuilder(); 85 | for (TemplateElement i : children) 86 | value.append(i.process(match)); 87 | 88 | return value.toString(); 89 | } 90 | 91 | /* 92 | Property Section 93 | */ 94 | 95 | public TemplateElement[] getChildren() 96 | { 97 | return children.toArray(TEMPLATE_ELEMENT_ARRAY); 98 | } 99 | 100 | public TemplateElement getChildren(int index) 101 | { 102 | return children.get(index); 103 | } 104 | 105 | public void setChildren(TemplateElement[] elements) 106 | { 107 | children.clear(); 108 | children.addAll(Arrays.asList(elements)); 109 | } 110 | } 111 | --------------------------------------------------------------------------------