├── README.md ├── src └── main │ ├── config │ └── Bots │ │ ├── substitutions.xml │ │ ├── Alice │ │ ├── chinese │ │ │ ├── gossip.aiml │ │ │ ├── Plan.aiml │ │ │ ├── Life.aiml │ │ │ ├── Healthy.aiml │ │ │ ├── Praise.aiml │ │ │ ├── Book.aiml │ │ │ ├── Computer.aiml │ │ │ ├── Photography.aiml │ │ │ ├── Constellation.aiml │ │ │ ├── Film.aiml │ │ │ ├── Master.aiml │ │ │ ├── Inherence.aiml │ │ │ ├── Encourage.aiml │ │ │ ├── External.aiml │ │ │ ├── Music.aiml │ │ │ ├── Food.aiml │ │ │ ├── Travel.aiml │ │ │ ├── main.aiml │ │ │ ├── Internet.aiml │ │ │ ├── Job.aiml │ │ │ ├── Game.aiml │ │ │ ├── Study.aiml │ │ │ ├── Sex.aiml │ │ │ ├── Sentiment .aiml │ │ │ └── MarkResponse.aiml │ │ ├── english │ │ │ ├── id.aiml │ │ │ ├── learn.aiml │ │ │ ├── version.aiml │ │ │ ├── Gender.aiml │ │ │ ├── Person.aiml │ │ │ ├── Person2.aiml │ │ │ ├── random.aiml │ │ │ ├── thatstar.aiml │ │ │ ├── Again.aiml │ │ │ ├── topicstar.aiml │ │ │ ├── empty_wildcard.aiml │ │ │ ├── thattopic.aiml │ │ │ ├── alice.txt │ │ │ ├── condition.aiml │ │ │ ├── Astrology.aiml │ │ │ └── Alice.aiml │ │ └── Learn │ │ │ └── Learned.aiml │ │ ├── properties.xml │ │ ├── splitters.xml │ │ └── context.xml │ ├── java │ ├── bitoflife │ │ └── chatterbean │ │ │ ├── aiml │ │ │ ├── Sr.java │ │ │ ├── AIMLStack.java │ │ │ ├── Template.java │ │ │ ├── AIMLElement.java │ │ │ ├── AIMLParserException.java │ │ │ ├── TemplateElementTest.java │ │ │ ├── AIMLParserConfigurationException.java │ │ │ ├── Br.java │ │ │ ├── A.java │ │ │ ├── B.java │ │ │ ├── Em.java │ │ │ ├── Think.java │ │ │ ├── Size.java │ │ │ ├── Lowercase.java │ │ │ ├── Uppercase.java │ │ │ ├── Version.java │ │ │ ├── Person.java │ │ │ ├── Gender.java │ │ │ ├── Person2.java │ │ │ ├── Text.java │ │ │ ├── CategoryTest.java │ │ │ ├── Random.java │ │ │ ├── Gossip.java │ │ │ ├── Id.java │ │ │ ├── Bot.java │ │ │ ├── Learn.java │ │ │ ├── Formal.java │ │ │ ├── Sentence.java │ │ │ ├── Srai.java │ │ │ ├── Get.java │ │ │ ├── AIMLParser.java │ │ │ ├── SystemTest.java │ │ │ ├── Topicstar.java │ │ │ ├── Thatstar.java │ │ │ ├── Date.java │ │ │ ├── Star.java │ │ │ ├── Javascript.java │ │ │ ├── System.java │ │ │ ├── Li.java │ │ │ ├── Set.java │ │ │ ├── Input.java │ │ │ ├── Aiml.java │ │ │ ├── Topic.java │ │ │ ├── TemplateElement.java │ │ │ ├── Pattern.java │ │ │ └── That.java │ │ │ ├── util │ │ │ ├── Escaper.java │ │ │ ├── Translate.java │ │ │ ├── SequenceMother.java │ │ │ ├── SearcherTest.java │ │ │ ├── SequenceTest.java │ │ │ ├── Sequence.java │ │ │ └── Searcher.java │ │ │ ├── config │ │ │ ├── ConfigException.java │ │ │ ├── TokenizerConfig.java │ │ │ └── TokenizerConfigStream.java │ │ │ ├── parser │ │ │ ├── ReflectionBuilder.java │ │ │ ├── AliceBotParserException.java │ │ │ ├── AliceBotParserConfigurationException.java │ │ │ ├── SplitterHandler.java │ │ │ ├── ContextParserTest.java │ │ │ ├── ContextParser.java │ │ │ ├── ReflectionHandler.java │ │ │ ├── SubstitutionBuilder.java │ │ │ ├── TransformationsParser.java │ │ │ └── AliceBotParser.java │ │ │ ├── script │ │ │ ├── InterpretingException.java │ │ │ ├── Interpreter.java │ │ │ └── BeanshellInterpreter.java │ │ │ ├── text │ │ │ ├── TransformationsMother.java │ │ │ ├── TokenizerMother.java │ │ │ ├── Response.java │ │ │ ├── SentenceTest.java │ │ │ ├── TokenizerTest.java │ │ │ ├── SentenceSplitterTest.java │ │ │ ├── Request.java │ │ │ └── SentenceSplitter.java │ │ │ ├── ChatterBeanException.java │ │ │ ├── MatchTest.java │ │ │ ├── GraphmasterMother.java │ │ │ ├── ContextPropertyChangeListener.java │ │ │ ├── Logger.java │ │ │ ├── ContextRandomSeedChangeListener.java │ │ │ ├── ContextTopicChangeListener.java │ │ │ ├── LoggerTest.java │ │ │ ├── AliceBotMother.java │ │ │ └── GraphmasterTest.java │ └── com │ │ └── xiaomi │ │ └── alice │ │ └── Main.java │ └── resources │ └── my.properties └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | 支持aiml的alice聊天机器人 2 | chatterbean实现了aiml,但不支持中文,需要做些改动,该代码是改动后的 3 | 另外提供了一些中文aiml 4 | -------------------------------------------------------------------------------- /src/main/config/Bots/substitutions.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/config/Bots/substitutions.xml -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Sr.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/aiml/Sr.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLStack.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/aiml/AIMLStack.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Template.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/aiml/Template.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/Escaper.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/util/Escaper.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLElement.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/aiml/AIMLElement.java -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/gossip.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 你的主人帅吗 4 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLParserException.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/aiml/AIMLParserException.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/TemplateElementTest.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/aiml/TemplateElementTest.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/config/ConfigException.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/config/ConfigException.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/config/TokenizerConfig.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/config/TokenizerConfig.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/ReflectionBuilder.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/parser/ReflectionBuilder.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/script/InterpretingException.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/script/InterpretingException.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/AliceBotParserException.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/parser/AliceBotParserException.java -------------------------------------------------------------------------------- /src/main/resources/my.properties: -------------------------------------------------------------------------------- 1 | alice.context.path=${alice.context.path} 2 | alice.splitters.path=${alice.splitters.path} 3 | alice.substitutions.path=${alice.substitutions.path} 4 | alice.aiml=${alice.aiml} -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/AIMLParserConfigurationException.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/aiml/AIMLParserConfigurationException.java -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/AliceBotParserConfigurationException.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/whuqin/alice-chatterbean-cn/HEAD/src/main/java/bitoflife/chatterbean/parser/AliceBotParserConfigurationException.java -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/id.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | WHAT IS YOUR ID 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Plan.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | * 你 * 梦 想 * 6 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Life.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 今天天气挺好* 5 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Healthy.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | * 晨 练 * 6 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/learn.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | LEARN THIS RESOURCE * 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/version.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | WHAT IS YOUR VERSION 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/Learn/Learned.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HAVE YOU LEARNED ANYTHING 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Praise.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 夸奖* 6 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/Gender.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DO THE GENDER TEST 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/Person.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DO THE PERSON TEST 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/Person2.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DO THE PERSON2 TEST 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Book.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Computer.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Photography.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Constellation.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | * 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Film.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *看影片* 5 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Master.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 你 *主人是谁 5 | 6 | 7 | 你主人是谁 8 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Inherence.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *梦想* 5 | 8 | 9 | 10 | *品位* 11 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/random.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DO THE RANDOM EXAMPLE 6 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Encourage.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 我要努力* 5 | 6 | 7 | 8 | 没有人是* 9 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/thatstar.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DO THE THATSTAR EXAMPLE 6 | 7 | 8 | 9 | WHY SO 10 | IT WILL BE * THOUGH 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/config/Bots/properties.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ChatterBean default configuration properties 5 | context.xml 6 | splitters.xml 7 | substitutions.xml 8 | * 9 | ../Logs/ 10 | 11 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/External.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *靓照* 5 | 8 | 9 | 10 | 11 | *身材* 12 | 15 | 16 | 17 | 18 | * 整 容 * 19 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/config/Bots/splitters.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Br.java: -------------------------------------------------------------------------------- 1 | package bitoflife.chatterbean.aiml; 2 | 3 | import bitoflife.chatterbean.Match; 4 | import org.xml.sax.Attributes; 5 | 6 | public class Br extends TemplateElement 7 | { 8 | /* 9 | Constructors 10 | */ 11 | 12 | public Br(Attributes attributes) 13 | { 14 | } 15 | 16 | public Br(Object... children) 17 | { 18 | super(children); 19 | } 20 | 21 | /* 22 | Methods 23 | */ 24 | 25 | public String process(Match match) 26 | { 27 | return ""; 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/Again.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | _ ALICE 6 | 7 | 8 | 9 | _ AGAIN 10 | 11 | 12 | 13 | YOU MAY * 14 | 15 | 16 | 17 | SAY * 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/A.java: -------------------------------------------------------------------------------- 1 | package bitoflife.chatterbean.aiml; 2 | 3 | 4 | import bitoflife.chatterbean.Match; 5 | import org.xml.sax.Attributes; 6 | 7 | 8 | public class A extends TemplateElement 9 | { 10 | /* 11 | Constructors 12 | */ 13 | 14 | public A(Attributes attributes) 15 | { 16 | } 17 | 18 | public A(Object... children) 19 | { 20 | super(children); 21 | } 22 | 23 | /* 24 | Methods 25 | */ 26 | 27 | public String process(Match match) 28 | { 29 | return ""; 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/B.java: -------------------------------------------------------------------------------- 1 | package bitoflife.chatterbean.aiml; 2 | 3 | 4 | import bitoflife.chatterbean.Match; 5 | import org.xml.sax.Attributes; 6 | 7 | 8 | public class B extends TemplateElement 9 | { 10 | /* 11 | Constructors 12 | */ 13 | 14 | public B(Attributes attributes) 15 | { 16 | } 17 | 18 | public B(Object... children) 19 | { 20 | super(children); 21 | } 22 | 23 | /* 24 | Methods 25 | */ 26 | 27 | public String process(Match match) 28 | { 29 | return ""; 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Em.java: -------------------------------------------------------------------------------- 1 | package bitoflife.chatterbean.aiml; 2 | 3 | 4 | import bitoflife.chatterbean.Match; 5 | import org.xml.sax.Attributes; 6 | 7 | 8 | public class Em extends TemplateElement 9 | { 10 | /* 11 | Constructors 12 | */ 13 | 14 | public Em(Attributes attributes) 15 | { 16 | } 17 | 18 | public Em(Object... children) 19 | { 20 | super(children); 21 | } 22 | 23 | /* 24 | Methods 25 | */ 26 | 27 | public String process(Match match) 28 | { 29 | return ""; 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/topicstar.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DO THE TOPICSTAR EXAMPLE 6 | 7 | 8 | 9 | THE WEATHER IS * 10 | 11 | 12 | 13 | 14 | I THINK SO 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Music.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *要听* 5 | 13 | 14 | 15 | 16 | *想听* 17 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/empty_wildcard.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | THANK YOU * 9 | 10 | 11 | 12 | I AM HAPPY * IS * 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Food.aiml: -------------------------------------------------------------------------------- 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/config/Bots/Alice/english/thattopic.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | YES 7 | DO YOU LIKE CHEESE 8 | 9 | 10 | 11 | 12 | WHAT DO YOU WANT TO KNOW 13 | 14 | 15 | 16 | YES 17 | 18 | 19 | 20 | YES 21 | DO YOU LIKE CAKE 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/config/Bots/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Travel.aiml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/util/Translate.java: -------------------------------------------------------------------------------- 1 | package bitoflife.chatterbean.util; 2 | 3 | import java.util.regex.Matcher; 4 | 5 | /** 6 | * 当遇到非字母数字的时候,就在该字符之间加上空格。 7 | * 8 | * @author xiaolong 9 | * 10 | */ 11 | public class Translate { 12 | public static String translateString(String input) { 13 | StringBuffer newStr = new StringBuffer(""); 14 | String strTemp = ""; 15 | java.util.regex.Pattern p = java.util.regex.Pattern 16 | .compile("[A-Za-z0-9\\s]"); 17 | char[] chars = new char[1]; 18 | Matcher m; 19 | for (int i = 0; i < input.length(); i++) { 20 | chars[0] = input.charAt(i); 21 | m = p.matcher(new String(chars)); 22 | if (!m.matches()) 23 | newStr.append(" ").append(input.charAt(i)).append(" "); 24 | else 25 | newStr.append(input.charAt(i)); 26 | } 27 | // java.lang.System.out.println("#" + newStr.toString()); 28 | strTemp = newStr.toString().replaceAll("\\s{2,}", " ");// 把2个连续的空格替换成一个空格。 29 | strTemp = strTemp.replaceAll("^\\s*|\\s*$", ""); // 把头和尾的空格去掉。 30 | return strTemp; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /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 | public Object evaluate(String script) throws InterpretingException; 19 | 20 | public Object variable(String name) throws InterpretingException; 21 | 22 | public void variable(String name, Object value) 23 | throws InterpretingException; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/main.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WELCOME 5 | 28 | 29 | 30 | 31 | 32 | 36 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/alice.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HELLO 6 | 7 | 8 | 9 | MY NAME IS * 10 | 11 | 12 | 13 | NAME IS * 14 | 15 | 16 | 17 | I AM * 18 | 19 | 20 | 21 | GREETING * 22 | 23 | 24 | 25 | WHAT IS * 26 | 27 | 28 | 29 | _ BOT 30 | 31 | 32 | 33 | _ NAME 34 | 35 | 36 | 37 | TELL ME ABOUT * 38 | 39 | 40 | 41 | * 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /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 | * Constructors 23 | */ 24 | 25 | public Think(Attributes attributes) { 26 | } 27 | 28 | public Think(Object... children) { 29 | super(children); 30 | } 31 | 32 | /* 33 | * Methods 34 | */ 35 | 36 | public String process(Match match) { 37 | super.process(match); 38 | return ""; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /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/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/main/java/com/xiaomi/alice/Main.java: -------------------------------------------------------------------------------- 1 | package com.xiaomi.alice; 2 | 3 | import bitoflife.chatterbean.AliceBot; 4 | import bitoflife.chatterbean.AliceBotMother; 5 | 6 | import java.io.BufferedReader; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | 10 | /** 11 | * Created by qinbin on 2016/6/21. 12 | */ 13 | public class Main { 14 | public static final String END = "bye"; 15 | 16 | public static String input() 17 | { 18 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 19 | String input = ""; 20 | try 21 | { 22 | input = in.readLine(); 23 | } catch (IOException e) { 24 | e.printStackTrace(); 25 | } 26 | return input; 27 | 28 | } 29 | public static void main(String[] args) throws Exception { 30 | System.getProperties().load(Main.class.getClassLoader().getResourceAsStream("my.properties")); 31 | 32 | AliceBotMother mother = new AliceBotMother(); 33 | mother.setUp(); 34 | AliceBot bot = mother.newInstance(); 35 | System.out.println(bot.respond("今天星期几")); 36 | System.out.println(bot.respond("好烦呢")); 37 | /*System.err.println("Alice>" + bot.respond("welcome")); 38 | while (true) { 39 | String input = input(); 40 | if (END.equalsIgnoreCase(input)) { 41 | break; 42 | } 43 | System.err.println("Alice>" + bot.respond(input)); 44 | }*/ 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Internet.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 你会上网吗 5 | 6 | 7 | 8 | 你*电子信箱* 9 | 10 | 11 | 12 | 你有email吗 13 | 14 | 15 | 16 | 17 | * 黑 客 * 18 | 20 | 21 | 22 | 23 | * 服 务 器 * 大 * 24 | 26 | 27 | 28 | 29 | * 用 * 无 线 网 * 30 | 32 | 33 | 34 | 35 | * 网 名 * 有 趣 * 36 | 38 | 39 | 40 | 41 | * 网 络 时 代 * 42 | 44 | 45 | 46 | 47 | * 电 脑 * 防 火 墙 * 48 | 50 | 51 | 52 | 53 | * 去 * 网 站 * 54 | 56 | 57 | 58 | 59 | * 下 * 次 * 战 争 * 60 | 62 | 63 | -------------------------------------------------------------------------------- /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/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 23 | * of the last revision where the class was created / modified. 24 | */ 25 | // private static final long serialVersionUID = 8L;//这到底是什么东西,干什么用的???? 26 | 27 | public ChatterBeanException(String message) { 28 | super(message); 29 | } 30 | 31 | public ChatterBeanException(Exception cause) { 32 | super(cause); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/config/Bots/Alice/chinese/Job.aiml: -------------------------------------------------------------------------------- 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/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 | * Attributes 22 | */ 23 | 24 | private final String value; 25 | 26 | /* 27 | * Constructor 28 | */ 29 | 30 | public Text(String value) { 31 | this.value = value; 32 | } 33 | 34 | /* 35 | * Methods 36 | */ 37 | 38 | public boolean equals(Object obj) { 39 | if (obj == null) 40 | return false; 41 | String text = obj.toString(); 42 | return (text != null ? text.equals(value) : value == null); 43 | } 44 | 45 | public int hashCode() { 46 | return (value == null ? 0 : value.hashCode()); 47 | } 48 | 49 | public String toString() { 50 | return value; 51 | } 52 | 53 | // 这方法是否被修改过了? 54 | public String process(Match match) { 55 | return value; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /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/config/Bots/Alice/chinese/Game.aiml: -------------------------------------------------------------------------------- 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/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 = getChildByIndex(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/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/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 | //fixed by lcl for StringIndexOutOfBoundsException. 47 | if (original != null && original.length() > 1) 48 | { 49 | builder.append(original); 50 | int index = builder.length() - 1; 51 | if (builder.charAt(index) != ' ') 52 | builder.append(' '); 53 | } 54 | 55 | builder.append(output); 56 | setOriginal(builder.toString()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /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 | * Attributes 23 | */ 24 | 25 | private String name; 26 | 27 | /* 28 | * Constructors 29 | */ 30 | 31 | public Bot(Attributes attributes) { 32 | name = attributes.getValue(0); 33 | } 34 | 35 | public Bot(String name) { 36 | this.name = name; 37 | } 38 | 39 | /* 40 | * Methods 41 | */ 42 | 43 | public boolean equals(Object obj) { 44 | return (super.equals(obj) && name.equals(((Bot) obj).name));// 这里super这样调用equals第一次见啊? 45 | } 46 | 47 | public int hashCode() { 48 | return name.hashCode(); 49 | } 50 | 51 | public String process(Match match) { 52 | try { 53 | String value = (String) match.getCallback().getContext() 54 | .property("bot." + name); 55 | return (value != null ? value : ""); 56 | } catch (NullPointerException e) {// 这里是作者主动抓的异常。为什么要这样做呢? 57 | return ""; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /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/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 20 | PropertyChangeListener { 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 36 | * The name of the property whose changes this object listens 37 | * for. 38 | */ 39 | public ContextPropertyChangeListener(String name) { 40 | this.name = name; 41 | } 42 | 43 | /* 44 | * Accessor Section 45 | */ 46 | 47 | /** 48 | * Returns the name of the property whose changes this object listens for. 49 | * 50 | * @return The name of the property whose changes this object listens for. 51 | */ 52 | public String name() { 53 | return name; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /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/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 | * Constructors 27 | */ 28 | 29 | public Learn(Attributes attributes) { 30 | } 31 | 32 | public Learn(Object... children) { 33 | super(children); 34 | } 35 | 36 | /* 37 | * Methods 38 | */ 39 | 40 | public String process(Match match) { 41 | AliceBot bot = null; 42 | try { 43 | bot = match.getCallback(); 44 | Graphmaster graphmaster = bot.getGraphmaster(); 45 | 46 | String address = super.process(match); 47 | URL url = new URL(address); // 比如某个本地的文件夹file:///C:/Users/xiaolong/Desktop/test.txt 48 | 49 | AIMLParser parser = new AIMLParser(); 50 | parser.parse(graphmaster, url.openStream()); 51 | } catch (Exception e) { 52 | throw new RuntimeException(e); 53 | } 54 | 55 | return ""; 56 | } 57 | } -------------------------------------------------------------------------------- /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/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 | // Interpreter是接口吗???? 21 | public class BeanshellInterpreter implements Interpreter { 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 | try { 35 | return interpreter.eval(script); 36 | } catch (Exception e) { 37 | throw new InterpretingException(e); 38 | } 39 | } 40 | 41 | public Object variable(String name) throws InterpretingException { 42 | try { 43 | return interpreter.get(name); 44 | } catch (Exception e) { 45 | throw new InterpretingException(e); 46 | } 47 | } 48 | 49 | public void variable(String name, Object value) 50 | throws InterpretingException { 51 | try { 52 | interpreter.set(name, value); 53 | } catch (Exception e) { 54 | throw new InterpretingException(e); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /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/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/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 | public Srai(int index) { 34 | super(new Star(index)); 35 | } 36 | 37 | /* 38 | * Method Section 39 | */ 40 | 41 | public String process(Match match) { 42 | String request = super.process(match); 43 | // request = Translate.translateString(request); 44 | try { 45 | AliceBot bot = (match != null ? match.getCallback() : null); 46 | // java.lang.System.out.println("*****srai(request):" + request); 47 | return (bot != null ? bot.respond(request) : ""); 48 | } catch (Exception e) { 49 | throw new RuntimeException("While trying to respond \"" + request 50 | + "\"", e); 51 | } 52 | // return ""; 53 | } 54 | 55 | public String toString() { 56 | return "" + super.toString() + ""; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /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/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) 50 | throws AIMLParserException { 51 | try { 52 | for (InputStream aiml : sources) 53 | parser.parse(aiml, handler); 54 | 55 | graphmaster.append(handler.unload()); 56 | 57 | } catch (Exception e) { 58 | throw new AIMLParserException(e); 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /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/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/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/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 | * Attribute Section 27 | */ 28 | 29 | private List splitters; 30 | 31 | /* 32 | * Constructor Section 33 | */ 34 | 35 | public SplitterHandler() { 36 | splitters = new ArrayList(4);// 这里为什么把他的长度定义为4呢??? 37 | } 38 | 39 | public SplitterHandler(List splitters) { 40 | this.splitters = splitters; 41 | } 42 | 43 | /* 44 | * Event Section 45 | */ 46 | 47 | // 这是专门把“句子分隔符”挑选出来了。 48 | public void startElement(String namespace, String name, String qname, 49 | Attributes attributes) throws SAXException { 50 | if (qname.equals("splitter") 51 | && !"word".equals(attributes.getValue("type"))) 52 | splitters.add(attributes.getValue(0)); 53 | } 54 | 55 | /* 56 | * Method Section 57 | */ 58 | 59 | public void clear() { 60 | splitters.clear(); 61 | } 62 | 63 | public List parsed() { 64 | return new ArrayList(splitters);// 这里重新复制了一个列表! 65 | } 66 | } -------------------------------------------------------------------------------- /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 | * Attributes 25 | */ 26 | 27 | private final SimpleDateFormat format = new SimpleDateFormat(); 28 | 29 | /** date tag format value, add by lcl **/ 30 | private String formatStr = ""; 31 | 32 | /* 33 | * Constructors 34 | */ 35 | 36 | public Date() { 37 | } 38 | 39 | public Date(Attributes attributes) { 40 | formatStr = attributes.getValue(0); 41 | } 42 | 43 | /* 44 | * Methods 45 | */ 46 | 47 | public int hashCode() { 48 | return 13;// 直接return一个数字,不是吧??? 49 | } 50 | 51 | public String process(Match match) { 52 | try { 53 | format.applyPattern(formatStr); 54 | return format.format(new java.util.Date()); 55 | } catch (Exception e) { 56 | return defaultDate(match);// 可以自己处理的异常,就不要抛出去。 57 | } 58 | } 59 | 60 | private String defaultDate(Match match) { 61 | try { 62 | format.applyPattern((String) match.getCallback().getContext() 63 | .property("predicate.dateFormat")); 64 | return format.format(new java.util.Date()); 65 | } catch (NullPointerException e) { 66 | return ""; 67 | } 68 | } 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 | /** 23 | * 这一个元素本来不支持中文的,现在支持了。 24 | * 25 | * @author xiaolong 26 | * 27 | */ 28 | public class Star extends TemplateElement { 29 | /* 30 | * Attributes 31 | */ 32 | 33 | private int index; 34 | 35 | /* 36 | * Constructor 37 | */ 38 | 39 | public Star(Attributes attributes) { 40 | String value = attributes.getValue(0); 41 | index = (value != null ? Integer.parseInt(value) : 1); 42 | } 43 | 44 | public Star(int index) { 45 | this.index = index; 46 | } 47 | 48 | /* 49 | * Methods 50 | */ 51 | 52 | public boolean equals(Object obj) { 53 | if (obj == null || !(obj instanceof Star)) 54 | return false; 55 | else { 56 | Star star = (Star) obj; 57 | return (index == star.index); 58 | } 59 | } 60 | 61 | public int hashCode() { 62 | return index; 63 | } 64 | 65 | public String toString() { 66 | return ""; 67 | } 68 | 69 | public String process(Match match) { 70 | String wildcard = match.wildcard(PATTERN, index);// 为什么这里一定就是pattern呢,难道不可能是that和topic吗? 71 | return (wildcard != null ? wildcard.trim() : ""); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /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/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 | * Constructors 26 | */ 27 | 28 | public System(Attributes attributes) { 29 | } 30 | 31 | public System(Object... children) { 32 | super(children); 33 | } 34 | 35 | /* 36 | * Methods 37 | */ 38 | 39 | public String process(Match match) // 这个函数很重要 40 | { 41 | try { 42 | AliceBot bot = match.getCallback(); 43 | Context context = bot.getContext(); 44 | Interpreter interpreter = (Interpreter) context 45 | .property("beanshell.interpreter"); 46 | if (interpreter == null) 47 | return ""; 48 | 49 | String script = super.process(match); 50 | interpreter.variable("result", null); 51 | interpreter.variable("match", match); 52 | 53 | Object evaluated = interpreter.evaluate(script); 54 | Object result = interpreter.variable("result"); 55 | if (result == null) 56 | result = evaluated; 57 | 58 | interpreter.variable("match", null); 59 | 60 | return (result != null ? result.toString() : ""); 61 | } catch (Exception e) { 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 | * Attributes 22 | */ 23 | 24 | private String name; 25 | private String value; 26 | 27 | /* 28 | * Constructors 29 | */ 30 | 31 | public Li() { 32 | } 33 | 34 | public Li(Attributes attributes) { 35 | name = attributes.getValue("name"); 36 | value = attributes.getValue("value"); 37 | } 38 | 39 | public Li(String name, String value, Object... children) { 40 | super(children); 41 | this.name = name; 42 | this.value = value; 43 | } 44 | 45 | /* 46 | * Methods 47 | */ 48 | 49 | private boolean isEquals(Object comparing, Object compared) { 50 | return (comparing == null ? compared == null : comparing 51 | .equals(compared)); 52 | } 53 | 54 | public boolean equals(Object obj) // 不就是比较2个字符串是否先等吗?有必要在写一个isEquals方法吗? 55 | { 56 | if (!super.equals(obj)) 57 | return false; 58 | Li compared = (Li) obj; 59 | return (isEquals(name, compared.name) && isEquals(value, compared.value)); 60 | } 61 | 62 | /* 63 | * Properties 64 | */ 65 | 66 | public String getName() { 67 | return name; 68 | } 69 | 70 | public void setName(String name) { 71 | this.name = name; 72 | } 73 | 74 | public String getValue() { 75 | return value; 76 | } 77 | 78 | public void setValue(String value) { 79 | this.value = value; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/condition.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | FIRST BLOCK CONDITION TEST 6 | 14 | 15 | 16 | SECOND BLOCK CONDITION TEST 17 | 27 | 28 | 29 | THIRD BLOCK CONDITION TEST 30 | 41 | 42 | 43 | FOURTH BLOCK CONDITION TEST 44 | 55 | 56 | 57 | FIFTH BLOCK CONDITION TEST 58 | 69 | 70 | 71 | SIXTH BLOCK CONDITION TEST 72 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /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/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 | * Attributes 25 | */ 26 | 27 | private String name; 28 | 29 | /* 30 | * Constructors 31 | */ 32 | 33 | public Set(Attributes attributes) { 34 | name = attributes.getValue(0); 35 | } 36 | 37 | public Set(String name, Object... children) { 38 | super(children); 39 | this.name = name; 40 | } 41 | 42 | /* 43 | * Methods 44 | */ 45 | 46 | public boolean equals(Object obj) { 47 | if (obj == null) 48 | return false; 49 | Set compared = (Set) obj; 50 | if (!name.equals(compared.name)) 51 | return false; 52 | return super.equals(compared); 53 | } 54 | 55 | public String process(Match match) { 56 | // for (String str : match.getMatchPath()) { 57 | // java.lang.System.out.println(str); 58 | // } 59 | String output = super.process(match); 60 | if (match == null) // 这里往往会做一些参数的检查。 61 | output = "" + output + ""; 62 | else { 63 | AliceBot bot = match.getCallback(); 64 | Context context = (bot != null ? bot.getContext() : null);// 时时刻刻都在检查变量的合法性。 65 | if (context != null) 66 | context.property("predicate." + name, output);// 这里应该会触发propertyListener 67 | } 68 | // java.lang.System.out.println("#name:" + name); 69 | // java.lang.System.out.println("#output:" + output); 70 | return output; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.alice 8 | test-alice 9 | 1.0-SNAPSHOT 10 | 11 | 12 | junit 13 | junit 14 | 4.12.2 15 | 16 | 17 | bsh 18 | bsh 19 | 1.3.0 20 | 21 | 22 | com.chenlb.mmseg4j 23 | mmseg4j-core 24 | 1.10.0 25 | 26 | 27 | 28 | 29 | ${project.basedir}/src/main/config/Bots/context.xml 30 | ${project.basedir}/src/main/config/Bots/splitters.xml 31 | ${project.basedir}/src/main/config/Bots/substitutions.xml 32 | ${project.basedir}/src/main/config/Bots/Alice 33 | 34 | 35 | 36 | 37 | 38 | ${project.basedir}/src/main/resources 39 | true 40 | 41 | 42 | 43 | 44 | org.apache.maven.plugins 45 | maven-compiler-plugin 46 | 3.2 47 | 48 | 1.7 49 | 1.7 50 | true 51 | true 52 | UTF-8 53 | 54 | ${project.basedir}/src/main/java 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /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/config/Bots/Alice/english/Astrology.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | WHAT IS A CANCER 13 | 14 | 15 | 16 | AQUARIUS 17 | 18 | 19 | 20 | CAPRICORN 21 | 22 | 23 | 24 | ARIES 25 | 26 | 27 | 28 | TAURUS 29 | 30 | 31 | 32 | CANCER 33 | 34 | 35 | 36 | GEMINI 37 | 38 | 39 | 40 | LIBRA 41 | 42 | 43 | 44 | PISCES 45 | 46 | 47 | 48 | VIRGO 49 | 50 | 51 | 52 | SCORPIO 53 | 54 | 55 | 56 | SAGGITARIUS 57 | 58 | 59 | 60 | WHAT IS A CAPRICORN 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /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/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 | * Inner Classes 31 | */ 32 | // 对内部类不熟悉???? 33 | private class ContextHandler extends DefaultHandler { 34 | public void startElement(String namespace, String name, String qname, 35 | Attributes attributes) throws SAXException { 36 | if (qname.equals("set")) 37 | context.property("predicate." + attributes.getValue("name"), 38 | attributes.getValue("value")); 39 | else if (qname.equals("bot")) 40 | context.property("bot." + attributes.getValue("name"), 41 | 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 | parser = SAXParserFactory.newInstance().newSAXParser(); 60 | } 61 | 62 | /* 63 | * Methods 64 | */ 65 | 66 | public Context parse(InputStream input) throws IOException, SAXException { 67 | parse(new Context(), input); 68 | return context; 69 | } 70 | 71 | public void parse(Context context, InputStream input) throws IOException, 72 | SAXException { 73 | this.context = context; 74 | parser.parse(input, handler); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /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.parser.AliceBotParserConfigurationException; 19 | import bitoflife.chatterbean.parser.AliceBotParserException; 20 | import bitoflife.chatterbean.util.Searcher; 21 | 22 | import java.io.ByteArrayOutputStream; 23 | import java.io.FileInputStream; 24 | import java.io.FileNotFoundException; 25 | import java.io.IOException; 26 | 27 | public class AliceBotMother { 28 | /* 29 | * Attribute Section 30 | */ 31 | 32 | private ByteArrayOutputStream gossip = null; 33 | 34 | /* 35 | * Event Section 36 | */ 37 | 38 | public void setUp() { 39 | // gossip = new ByteArrayOutputStream(); 40 | } 41 | 42 | /* 43 | * Method Section 44 | */ 45 | 46 | public String gossip() { 47 | return gossip.toString(); 48 | } 49 | 50 | public AliceBot newInstance() { 51 | Searcher searcher = new Searcher(); 52 | AliceBotParser parser; 53 | AliceBot bot = null; 54 | try { 55 | parser = new AliceBotParser(); 56 | bot = parser.parse(new FileInputStream(System.getProperty("alice.context.path")), 57 | new FileInputStream(System.getProperty("alice.splitters.path")), 58 | new FileInputStream(System.getProperty("alice.substitutions.path")), 59 | searcher.search(System.getProperty("alice.aiml"), ".*\\.aiml")); 60 | } catch (AliceBotParserConfigurationException e) { 61 | throw new RuntimeException(e); 62 | } catch (FileNotFoundException e) { 63 | throw new RuntimeException("[ExceptionInfo]相关文件没有找到。", e); 64 | } catch (AliceBotParserException e) { 65 | throw new RuntimeException(e); 66 | } catch (IOException e) { 67 | throw new RuntimeException(e); 68 | } 69 | 70 | // Context context = bot.getContext(); 71 | // context.outputStream(gossip); 72 | return bot; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /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( 30 | Arrays.asList(defaultTopic)); 31 | 32 | private final List categories = new LinkedList(); 33 | 34 | private String version; 35 | 36 | /* 37 | * Constructors 38 | */ 39 | 40 | public Aiml(Attributes attributes) { 41 | version = attributes.getValue(0); 42 | } 43 | 44 | public Aiml(Category... categories) { 45 | this.categories.addAll(Arrays.asList(categories)); 46 | } 47 | 48 | /* 49 | * Method Section 50 | */ 51 | 52 | public void appendChild(AIMLElement child) { 53 | if (child instanceof Category) { 54 | Category category = (Category) child; 55 | category.setTopic(defaultTopic); 56 | defaultTopic.appendChild(category); 57 | categories.add(category); 58 | } else { 59 | Topic topic = (Topic) child; // 如果child不是topic的类型怎么办? 60 | topics.add(topic); 61 | categories.addAll(topic.getCategories()); 62 | } 63 | } 64 | 65 | public void appendChildren(List children) { 66 | for (AIMLElement child : children) 67 | appendChild(child); 68 | } 69 | 70 | public List children() { 71 | return categories; 72 | } 73 | 74 | public boolean equals(Object obj) { 75 | if (obj == null || !(obj instanceof Aiml)) 76 | return false; 77 | else 78 | return categories.equals(((Aiml) obj).categories); 79 | } 80 | 81 | public String toString() { 82 | StringBuilder result = new StringBuilder(); 83 | for (Category i : categories) { 84 | result.append(i); 85 | result.append('\n'); 86 | } 87 | 88 | return result.toString(); 89 | } 90 | 91 | /* 92 | * Properties 93 | */ 94 | 95 | public String getVersion() { 96 | return version; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /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/config/Bots/Alice/chinese/Study.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 28 | 29 | 30 | T 31 | 32 | 44 | 45 | 46 | 47 | *我教你* 48 | 49 | 50 | 51 | 52 | 53 | Q*A* 54 | 60 | 61 | 62 | 76 | 77 | 78 | *自学* 79 | 81 | 82 | 83 | 84 | * 思 维 * 85 | 87 | 88 | 89 | 90 | * 给 你 * 范 文 * 91 | 93 | 94 | 95 | 96 | * 及 格 * 97 | 99 | 100 | 101 | 102 | * 深 造 * 103 | 105 | 106 | 107 | 108 | * 毕 业 * 109 | 111 | 112 | 113 | 114 | * 考 * 傻 * 115 | 117 | 118 | 119 | 120 | * 答 辩 * 121 | 122 | 123 | -------------------------------------------------------------------------------- /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 | * Attribute Section 26 | */ 27 | 28 | private List categories = new LinkedList(); 29 | 30 | private String name; 31 | 32 | /* 33 | * Constructor Section 34 | */ 35 | 36 | public Topic(Attributes attributes) // Attributes在这里是怎么用的??? 37 | { 38 | name(attributes.getValue(0)); 39 | } 40 | 41 | public Topic(String name, Category... children) { 42 | name(name); 43 | categories.addAll(Arrays.asList(children)); 44 | } 45 | 46 | /* 47 | * Method Section 48 | */ 49 | 50 | public void appendChild(AIMLElement child) { 51 | Category category = (Category) child; 52 | category.setTopic(this); 53 | categories.add(category); 54 | } 55 | 56 | public void appendChildren(List children) { 57 | for (AIMLElement child : children) 58 | appendChild(child); 59 | } 60 | 61 | public List getCategories() { 62 | return categories; 63 | } 64 | 65 | public String[] elements() { 66 | return name.split(" "); 67 | } 68 | 69 | public boolean equals(Object obj) { 70 | if (obj == null || !(obj instanceof Topic)) 71 | return false; 72 | else { 73 | Topic compared = (Topic) obj; 74 | return (name.equals(compared.name) && categories 75 | .equals(compared.categories)); 76 | } 77 | } 78 | 79 | public String toString() { 80 | StringBuilder result = new StringBuilder(); 81 | for (Category i : categories) { 82 | result.append(i); 83 | result.append('\n'); 84 | } 85 | 86 | return result.toString(); 87 | } 88 | 89 | /* 90 | * Acessor Section 91 | */ 92 | 93 | private void name(String name) { 94 | this.name = name.trim(); 95 | } 96 | 97 | /* 98 | * Property Section 99 | */ 100 | 101 | public String getName() { 102 | return name; 103 | } 104 | 105 | public void setName(String name) { 106 | this.name = name; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /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 | /* 23 | * 这个控制器其实根本就没有对xml配置文件做什么。 24 | */ 25 | public class ReflectionHandler extends DefaultHandler { 26 | /* 27 | * Attributes 28 | */ 29 | 30 | private ReflectionBuilder builder = null; 31 | 32 | /* 33 | * Constructor 34 | */ 35 | 36 | public ReflectionHandler() { 37 | } 38 | 39 | public ReflectionHandler(ReflectionBuilder builder) { 40 | this.builder = builder; 41 | } 42 | 43 | /* 44 | * Methods 45 | */ 46 | 47 | /* 48 | * 这个函数写的厉害,这里用到了反射机制。如果不用反射机制,那么就要用分支语句把所有可能的qname列举出来 49 | * 分别处理,但是现在通过反射机制,想要调用什么,只要构造函数名就能调用了。 50 | */ 51 | 52 | public void startElement(String namespace, String name, String qname, 53 | Attributes attributes) { 54 | try { 55 | String methodName = "start" + qname.substring(0, 1).toUpperCase() 56 | + qname.substring(1); 57 | Method event = builder.getClass().getMethod(methodName, 58 | Attributes.class); 59 | event.invoke(builder, attributes); 60 | } catch (NoSuchMethodException e) { 61 | } catch (Exception e) { 62 | e.printStackTrace(); 63 | } 64 | } 65 | 66 | // substitutions.xml中没有文本内容,所以可以没有这个方法。 67 | public void characters(char[] chars, int start, int length) { 68 | builder.characters(chars, start, length); 69 | } 70 | 71 | public void endElement(String namespace, String name, String qname) { 72 | try { 73 | String methodName = "end" + qname.substring(0, 1).toUpperCase() 74 | + qname.substring(1); 75 | // System.out.println("methodname:" + methodName); 76 | Method event = builder.getClass().getMethod(methodName); 77 | event.invoke(builder); 78 | } catch (NoSuchMethodException e) {// 实际上根本就没有这个end的方法,这里会出现异常的。但是作者把NoSuchMethodException这个异常给隐藏起来了,有没有搞错啊。 79 | } catch (Exception e) { 80 | e.printStackTrace(); 81 | } 82 | } 83 | 84 | /* 85 | * Properties 86 | */ 87 | 88 | public ReflectionBuilder getReflectionBuilder() { 89 | return builder; 90 | } 91 | 92 | public void setReflectionBuilder(ReflectionBuilder builder) { 93 | this.builder = builder; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Sex.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | *亲一* 5 | 6 | 7 | 8 | 9 | 啵一个 10 | 11 | 12 | 13 | 14 | *亲亲* 15 | 23 | 24 | 25 | 调戏* 26 | *调戏* 27 | 喵* 28 | 29 | 笑一个 30 | 31 | 调戏 32 | 40 | 41 | 42 | 43 | *笑一个* 44 | 50 | 51 | 52 | 53 | *靓照* 54 | 57 | 58 | 59 | 60 | *身材* 61 | 64 | 65 | 66 | 67 | 我 喜 欢 你 68 | 73 | 74 | 75 | 76 | 我 爱 你 77 | 78 | 79 | 80 | 81 | _ 帅 哥 * 美 女 * 82 | 84 | 85 | 86 | 87 | _ 帅 哥 * 美 女 88 | 89 | 90 | 91 | 92 | _ 花 痴 * 93 | 95 | 96 | 97 | 98 | _ 花 痴 99 | 100 | 101 | 102 | 103 | _ 我 * 追 求 你 * 104 | 106 | 107 | 108 | 109 | 你 好 贫 嘴 * 110 | 112 | 113 | 114 | 115 | * 你 * 好 坏 * 116 | 118 | 119 | 120 | 121 | * 你 * 煽 情 * 122 | 124 | 125 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/Sentiment .aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | _ 唯 一 * 6 | 8 | 9 | 10 | 11 | _ 故 地 重 游 * 12 | 14 | 15 | 16 | 17 | _ 遗 憾 * 18 | 20 | 21 | 22 | 23 | _ 遗 憾 24 | 25 | 26 | 27 | 28 | _ 没 有 爱 情 * 29 | 31 | 32 | 33 | 34 | _ 没 有 爱 情 35 | 36 | 37 | 38 | 39 | _ 爱 * 理 由 * 40 | 42 | 43 | 44 | 45 | _ 爱 * 理 由 46 | 49 | 50 | 51 | 52 | 你 _ 做 家 务 * 53 | 55 | 56 | 57 | 58 | * 交 代 * 59 | 61 | 62 | 63 | 64 | _ 爱 情 * 65 | 69 | 70 | 71 | 72 | 真 诚 * 爱 情 * 73 | 76 | 77 | 78 | 79 | _ 真 诚 * 爱 情 80 | 83 | 84 | 85 | 86 | 真 诚 * 爱 情 87 | 90 | 91 | 92 | 93 | * 两 人 * 粘 * 94 | 96 | 97 | 98 | 99 | 100 | * 装 * 成 熟 * 101 | 103 | 104 | 105 | 106 | * 同 学 * 聚 会 * 107 | 109 | 110 | 111 | 112 | * 温 暖 * 113 | 115 | 116 | 117 | 118 | 119 | * 不 喜 欢 他 * 120 | 122 | 123 | 124 | 125 | * 有 * 朋 友 * 126 | 128 | 129 | 130 | 131 | * 你 * 好 绝 情 * 132 | 134 | 135 | -------------------------------------------------------------------------------- /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 | * Attribute Section 26 | */ 27 | 28 | private static final TemplateElement[] TEMPLATE_ELEMENT_ARRAY = {}; 29 | 30 | private final List children = new LinkedList(); 31 | 32 | /* 33 | * Constructor Section 34 | */ 35 | 36 | public TemplateElement(Object... elements) { 37 | for (Object child : elements) { 38 | if (child instanceof AIMLElement) 39 | children.add((TemplateElement) child); 40 | else 41 | children.add(new Text(child.toString())); // 这里有一个特殊处理。 42 | } 43 | } 44 | 45 | /* 46 | * Method Section 47 | */ 48 | 49 | public void appendChild(AIMLElement element) { 50 | children.add((TemplateElement) element); 51 | } 52 | 53 | public void appendChildren(List elements) { 54 | for (AIMLElement element : elements) 55 | children.add((TemplateElement) element); 56 | } 57 | 58 | public List children() { 59 | return children; 60 | } 61 | 62 | public boolean equals(Object object) { 63 | if (object == null || !(object instanceof TemplateElement)) 64 | return false; 65 | 66 | TemplateElement that = (TemplateElement) object; 67 | return children.equals(that.children); 68 | } 69 | 70 | public int hashCode() { 71 | return children.hashCode(); // hashcode和equal的联系???? 72 | } 73 | 74 | public String process(Match match) { 75 | 76 | StringBuilder value = new StringBuilder(); 77 | for (TemplateElement i : children) { 78 | value.append(i.process(match)); 79 | } 80 | // java.lang.System.out.println(value.toString()); 81 | return value.toString(); 82 | } 83 | 84 | /* 85 | * Property Section 86 | */ 87 | 88 | public TemplateElement[] getChildren() { 89 | return children.toArray(TEMPLATE_ELEMENT_ARRAY); // TEMPLATE_ELEMENT_ARRAY是为了指定运行时类型,这样children.toArra就会返回TemplateElement类型的数组了。 90 | } 91 | 92 | public TemplateElement getChildByIndex(int index) { 93 | return children.get(index); 94 | } 95 | 96 | public void setChildren(TemplateElement[] elements) { 97 | children.clear(); 98 | children.addAll(Arrays.asList(elements)); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/config/TokenizerConfigStream.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 | import org.xml.sax.Attributes; 19 | import org.xml.sax.SAXException; 20 | import org.xml.sax.helpers.DefaultHandler; 21 | 22 | import javax.xml.parsers.SAXParser; 23 | import javax.xml.parsers.SAXParserFactory; 24 | import java.io.InputStream; 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | 28 | public class TokenizerConfigStream extends DefaultHandler implements 29 | TokenizerConfig { 30 | /* 31 | * Attribute Section 32 | */ 33 | 34 | private static final String[] STRING_ARRAY = new String[0]; 35 | 36 | private final SAXParser parser; 37 | 38 | // 这里把splitters列表长度设置成6,那如果splitters.xml中超过6个分割符不就无效了吗??? 39 | private final List splitters = new ArrayList(6); 40 | 41 | private boolean ignoreWhitespace; 42 | 43 | /* 44 | * Constructor Section 45 | */ 46 | 47 | public TokenizerConfigStream() throws ConfigException { 48 | try { 49 | SAXParserFactory factory = SAXParserFactory.newInstance(); 50 | parser = factory.newSAXParser(); 51 | } catch (Exception e) { 52 | throw new ConfigException(e); 53 | } 54 | } 55 | 56 | public TokenizerConfigStream(InputStream input) throws ConfigException { 57 | try { 58 | SAXParserFactory factory = SAXParserFactory.newInstance(); 59 | parser = factory.newSAXParser(); 60 | parse(input); 61 | } catch (ConfigException e) { 62 | throw e; 63 | } catch (Exception e) { 64 | throw new ConfigException(e); 65 | } 66 | } 67 | 68 | /* 69 | * Event Section 70 | */ 71 | 72 | public void startElement(String namespace, String name, String qname, 73 | Attributes attributes) throws SAXException { 74 | if ("splitter".equals(qname)) 75 | splitters.add(attributes.getValue(0)); // 这里只负责把分隔符记录下来,却忽略了分隔符的类型(sentence,word) 76 | } 77 | 78 | /* 79 | * Method Section 80 | */ 81 | 82 | public Tokenizer newInstance() { 83 | return new Tokenizer(splitters()); 84 | } 85 | 86 | public void parse(InputStream input) throws ConfigException { 87 | try { 88 | splitters.clear(); 89 | ignoreWhitespace = true; 90 | parser.parse(input, this); 91 | } catch (Exception e) { 92 | throw new ConfigException(e); 93 | } 94 | } 95 | 96 | /* 97 | * Accessor Section 98 | */ 99 | 100 | public String[] splitters() { 101 | return splitters.toArray(STRING_ARRAY); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /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 | public class Request { 20 | /* 21 | * Attributes 22 | */ 23 | // 这2个变量都是被规范化过的。 24 | private Sentence[] sentences;// 用户可能输入很多的句子,所以这是一个句子数组。 25 | private String original; 26 | 27 | /* 28 | * Constructor 29 | */ 30 | 31 | public Request() { 32 | } 33 | 34 | public Request(String original) { 35 | this.original = original; 36 | } 37 | 38 | public Request(String original, Sentence... sentences) { 39 | this.original = original; 40 | this.sentences = sentences; 41 | } 42 | 43 | /* 44 | * Methods 45 | */ 46 | 47 | public boolean empty() { 48 | return (sentences == null || sentences.length == 0); 49 | } 50 | 51 | public boolean equals(Object obj) { 52 | if (obj == null || !(obj instanceof Request)) 53 | return false; 54 | 55 | Request compared = (Request) obj; 56 | return original.equals(compared.original) 57 | && Arrays.equals(sentences, compared.sentences); 58 | } 59 | 60 | /** 61 | * 取倒数第 index 个句子。 62 | * 63 | * @param index 64 | * @return 65 | */ 66 | public Sentence lastSentence(int index) { 67 | return sentences[sentences.length - (1 + index)]; 68 | } 69 | 70 | public String toString() { 71 | String tempOriginal = ""; 72 | if (original == null) 73 | tempOriginal = "null"; 74 | else 75 | tempOriginal = original.replace(" ", "#"); 76 | StringBuffer output = new StringBuffer(""); 77 | output.append("-----------Request-----------\n").append( 78 | "[original]:" + tempOriginal + "\n"); 79 | if (sentences != null) 80 | for (int i = 0; i < sentences.length; i++) { 81 | output.append("[sentence " + i + "]:" + sentences[i].toString() 82 | + "\n"); 83 | } 84 | else 85 | output.append("[sentence]:null"); 86 | return output.toString(); 87 | } 88 | 89 | public String trimOriginal() { 90 | return original.trim(); 91 | } 92 | 93 | /* 94 | * Properties 95 | */ 96 | 97 | public String getOriginal() { 98 | return original; 99 | } 100 | 101 | public void setOriginal(String original) { 102 | this.original = original; 103 | } 104 | 105 | public Sentence[] getSentences() { 106 | return sentences; 107 | } 108 | 109 | public Sentence getSentences(int index) { 110 | return sentences[index]; 111 | } 112 | 113 | public void setSentences(Sentence[] sentences) { 114 | this.sentences = sentences; 115 | } 116 | 117 | } -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/GraphmasterTest.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.Pattern; 19 | import bitoflife.chatterbean.aiml.Template; 20 | import bitoflife.chatterbean.aiml.That; 21 | import bitoflife.chatterbean.text.Sentence; 22 | import junit.framework.TestCase; 23 | 24 | import static bitoflife.chatterbean.text.Sentence.ASTERISK; 25 | 26 | public class GraphmasterTest extends TestCase 27 | { 28 | /* 29 | Attributes 30 | */ 31 | 32 | private final GraphmasterMother mother = new GraphmasterMother(); 33 | 34 | private Graphmaster root; 35 | 36 | /* 37 | Events 38 | */ 39 | 40 | protected void setUp() 41 | { 42 | root = mother.newInstance(); 43 | } 44 | 45 | protected void tearDown() 46 | { 47 | root = null; 48 | } 49 | 50 | /* 51 | Methods 52 | */ 53 | 54 | public void testMatch() 55 | { 56 | Category category; 57 | Match match; 58 | 59 | match = new Match(new Sentence(" Say goodbye again. ", new Integer[] {0, 4, 12, 19}, " SAY GOODBYE AGAIN ")); 60 | category = root.match(match); 61 | assertNotNull(category); 62 | assertEquals("What, again? \"goodbye\".", category.process(match)); 63 | 64 | match = new Match(new Sentence(" Say it now. ", new Integer[] {0, 4, 7, 12}, " SAY IT NOW ")); 65 | category = root.match(match); 66 | assertNotNull(category); 67 | assertEquals("Whatever you want...", category.process(match)); 68 | 69 | match = new Match(new Sentence(" Say goodbye. ", new Integer[] {0, 4, 13}, " SAY GOODBYE ")); 70 | category = root.match(match); 71 | assertNotNull(category); 72 | assertEquals("goodbye!", category.process(match)); 73 | 74 | match = new Match( 75 | new Sentence(" Do you see the fire in my eyes? ", 76 | new Integer[] {0, 3, 7, 11, 15, 20, 23, 26, 32}, 77 | " DO YOU SEE THE FIRE IN MY EYES ")); 78 | 79 | category = root.match(match); 80 | assertNotNull(category); 81 | assertEquals("Yes, I see the fire in your eyes.", category.process(match)); 82 | } 83 | 84 | public void testThatMatch() 85 | { 86 | Sentence input = new Sentence(" Do you like it? ", new Integer[] {0, 3, 12, 16}, " DO YOU LIKE IT "); 87 | Sentence that = new Sentence(" CHEESE ", new Integer[] {0, 7}, " CHEESE "); 88 | Match match = new Match(null, input, that, ASTERISK); 89 | 90 | Category expected = new Category(new Pattern("DO YOU LIKE IT"), new That("CHEESE"), new Template("Yes.")); 91 | root.append(expected); 92 | Category actual = root.match(match); 93 | 94 | assertEquals(expected, actual); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/chinese/MarkResponse.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | *你知道* 7 | 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 | 39 | 40 | 41 | 42 | *为什么* 43 | 51 | 52 | 53 | 54 | 你 能 说 出 * 55 | 65 | 66 | 67 | *好不好_ 68 | *行不行_ 69 | 71 | 72 | 73 | *好不好 74 | 83 | 84 | 85 | 86 | 87 | *行不行 88 | 101 | 102 | 103 | 104 | 105 | 106 | 我*想知道* 107 | 108 | 109 | 110 | 111 | 我希望* 112 | 119 | 120 | 121 | 122 | 你 认 为 * 123 | 126 | 127 | 128 | 129 | 你 以 为 * 130 | 132 | 133 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/Pattern.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.util.Translate; 18 | import org.xml.sax.Attributes; 19 | 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | 24 | public class Pattern implements AIMLElement { 25 | /* 26 | * Attribute Section 27 | */ 28 | 29 | private String[] pattern; 30 | 31 | private int hashCode; 32 | 33 | /* 34 | * Constructor Section 35 | */ 36 | 37 | public Pattern() { 38 | } 39 | 40 | public Pattern(String pattern) { 41 | this.pattern = pattern.trim().split(" "); 42 | hashCode = Arrays.hashCode(this.pattern); 43 | } 44 | 45 | public Pattern(Attributes attributes) { 46 | } 47 | 48 | /* 49 | * Method Section 50 | */ 51 | 52 | public void appendChild(AIMLElement child) { 53 | String text = child.toString(); 54 | if (pattern == null) 55 | pattern = new String[] { text }; 56 | else { 57 | int length = pattern.length; 58 | String[] larger = new String[length + 1]; 59 | java.lang.System.arraycopy(pattern, 0, larger, 0, length); 60 | //System.arraycopy(pattern, 0, larger, 0, length); 61 | larger[length] = text; 62 | pattern = larger; 63 | } 64 | } 65 | 66 | public void appendChildren(List children) { 67 | StringBuilder builder = new StringBuilder(); 68 | for (AIMLElement child : children) 69 | builder.append(child);// 把一个对象加进去都行?? 70 | 71 | String text = builder.toString().trim(); 72 | text = Translate.translateString(text); 73 | 74 | pattern = text.split(" "); 75 | // java.lang.System.out.println("#######"); 76 | // java.lang.System.out.println(pattern.length); 77 | // java.lang.System.out.println("text:" + text); 78 | // for (String str : pattern) { 79 | // java.lang.System.out.println(str); 80 | // } 81 | // java.lang.System.out.println("#######"); 82 | hashCode = Arrays.hashCode(pattern); 83 | } 84 | 85 | public boolean equals(Object obj) { 86 | if (obj == null || !(obj instanceof Pattern)) 87 | return false; 88 | Pattern compared = (Pattern) obj; 89 | return Arrays.equals(pattern, compared.pattern); 90 | } 91 | 92 | public int hashCode() { 93 | return hashCode; 94 | } 95 | 96 | public String toString() { 97 | StringBuilder buffer = new StringBuilder(); 98 | for (int i = 0, n = pattern.length;;) { 99 | buffer.append(pattern[i]); 100 | if (++i >= n) 101 | break; 102 | buffer.append(" "); 103 | } 104 | 105 | return buffer.toString(); 106 | } 107 | 108 | /* 109 | * Property Section 110 | */ 111 | 112 | public String[] getElements() { 113 | return pattern; 114 | } 115 | 116 | public void setElements(String[] pattern) { 117 | this.pattern = pattern; 118 | hashCode = Arrays.hashCode(pattern); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/SubstitutionBuilder.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 | 19 | import java.util.HashMap; 20 | import java.util.LinkedHashMap; 21 | import java.util.Map; 22 | 23 | import static bitoflife.chatterbean.util.Escaper.escapeRegex; 24 | 25 | public class SubstitutionBuilder implements ReflectionBuilder { 26 | /* 27 | * Attributes 28 | */ 29 | 30 | private Map> substitutions; 31 | 32 | private Map section; 33 | 34 | /* 35 | * Constructor 36 | */ 37 | 38 | public SubstitutionBuilder() { 39 | substitutions = new HashMap>(); 40 | } 41 | 42 | public SubstitutionBuilder(Map> substitutions) { 43 | this.substitutions = substitutions; 44 | } 45 | 46 | /* 47 | * Event Handlers 48 | */ 49 | 50 | public void characters(char[] chars, int start, int length) { 51 | } 52 | 53 | public void startAccentuation(Attributes attributes) { 54 | section = substitutions.get("accentuation"); 55 | } 56 | 57 | public void startCorrection(Attributes attributes) { 58 | section = substitutions.get("correction"); 59 | } 60 | 61 | public void startPerson(Attributes attributes) { 62 | section = substitutions.get("person"); 63 | } 64 | 65 | public void startPerson2(Attributes attributes) { 66 | section = substitutions.get("person2"); 67 | } 68 | 69 | public void startGender(Attributes attributes) { 70 | section = substitutions.get("gender"); 71 | } 72 | 73 | public void startProtection(Attributes attributes) { 74 | section = substitutions.get("protection"); 75 | } 76 | 77 | public void startPunctuation(Attributes attributes) { 78 | section = substitutions.get("punctuation"); 79 | } 80 | 81 | public void startSubstitute(Attributes attributes) { 82 | String find = escapeRegex(attributes.getValue(0)); 83 | String replace = attributes.getValue(1); 84 | section.put(find, replace); 85 | } 86 | 87 | /* 88 | * Method Section 89 | */ 90 | // 这些列表里面不会存有数据。 91 | public void clear() { 92 | substitutions.clear(); 93 | substitutions.put("correction", new LinkedHashMap()); 94 | substitutions.put("protection", new LinkedHashMap()); 95 | substitutions.put("accentuation", new LinkedHashMap()); 96 | substitutions.put("punctuation", new LinkedHashMap()); 97 | substitutions.put("person", new LinkedHashMap()); 98 | substitutions.put("person2", new LinkedHashMap()); 99 | substitutions.put("gender", new LinkedHashMap()); 100 | } 101 | 102 | public Map> parsed() { 103 | return new HashMap>(substitutions); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/aiml/That.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.text.Response; 21 | import org.xml.sax.Attributes; 22 | 23 | import java.util.Arrays; 24 | import java.util.LinkedList; 25 | import java.util.List; 26 | 27 | public class That extends TemplateElement { 28 | /* 29 | * Attribute Section 30 | */ 31 | 32 | private static final String[] STRING_ARRAY = {}; 33 | 34 | private int responseIndex = 1, sentenceIndex = 1; 35 | 36 | /* 37 | * Constructor Section 38 | */ 39 | 40 | public That(Attributes attributes) { 41 | String value = attributes.getValue(0); // 关注Attributes是怎么赋值的。 42 | if (value == null) 43 | return; 44 | 45 | String[] indexes = value.split(","); 46 | responseIndex = Integer.parseInt(indexes[0].trim()); 47 | if (indexes.length > 1) 48 | sentenceIndex = Integer.parseInt(indexes[1].trim()); 49 | } 50 | 51 | public That(Object... children) { 52 | super(children); 53 | } 54 | 55 | public That(int responseIndex, int sentenceIndex) { 56 | this.responseIndex = responseIndex; 57 | this.sentenceIndex = sentenceIndex; 58 | } 59 | 60 | /* 61 | * Method Section 62 | */ 63 | 64 | public String[] elements() { 65 | TemplateElement[] children = getChildren(); 66 | List elements = new LinkedList(); 67 | for (int i = 0, n = children.length; i < n; i++) { 68 | String text = children[i].toString(); // toString会返回什么? 69 | text = text.trim(); 70 | elements.addAll(Arrays.asList(text.split(" "))); 71 | } 72 | 73 | return elements.toArray(STRING_ARRAY); 74 | } 75 | 76 | public boolean equals(Object obj) { 77 | if (obj == null || !(obj instanceof That)) 78 | return false; 79 | 80 | That compared = (That) obj; 81 | 82 | return (responseIndex == compared.responseIndex && sentenceIndex == compared.sentenceIndex); 83 | } 84 | 85 | public int hashCode() { 86 | return responseIndex + sentenceIndex; 87 | } 88 | 89 | public String process(Match match) { 90 | if (match == null) 91 | return ""; 92 | 93 | AliceBot bot = match.getCallback(); 94 | Context context = bot.getContext(); 95 | Response response = context.getResponses(responseIndex - 1); 96 | return response.getSentences(sentenceIndex - 1).trimOriginal(); 97 | } 98 | 99 | public String toString() { 100 | if (children().size() == 0) 101 | return ""; 103 | else { 104 | StringBuilder builder = new StringBuilder(""); 105 | for (TemplateElement element : children()) 106 | builder.append(element); 107 | builder.append(""); 108 | 109 | return builder.toString(); 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/TransformationsParser.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.config.ConfigException; 18 | import bitoflife.chatterbean.config.TokenizerConfig; 19 | import bitoflife.chatterbean.config.TokenizerConfigStream; 20 | import bitoflife.chatterbean.text.Tokenizer; 21 | import bitoflife.chatterbean.text.Transformations; 22 | import org.xml.sax.SAXException; 23 | 24 | import javax.xml.parsers.ParserConfigurationException; 25 | import javax.xml.parsers.SAXParser; 26 | import javax.xml.parsers.SAXParserFactory; 27 | import java.io.ByteArrayInputStream; 28 | import java.io.IOException; 29 | import java.io.InputStream; 30 | import java.util.LinkedList; 31 | import java.util.List; 32 | import java.util.Map; 33 | 34 | public class TransformationsParser { 35 | /* 36 | * Attribute Section 37 | */ 38 | 39 | private final SubstitutionBuilder substBuilder = new SubstitutionBuilder(); 40 | private final ReflectionHandler substHandler = new ReflectionHandler( 41 | substBuilder); 42 | private final SplitterHandler splitHandler = new SplitterHandler(); 43 | 44 | private SAXParser parser; 45 | 46 | /* 47 | * Constructor Section 48 | */ 49 | 50 | public TransformationsParser() throws ParserConfigurationException, 51 | SAXException { 52 | parser = SAXParserFactory.newInstance().newSAXParser(); 53 | } 54 | 55 | /* 56 | * Method Section 57 | */ 58 | 59 | private List parseSplitters(InputStream splitters) 60 | throws IOException, SAXException { 61 | splitHandler.clear(); 62 | parser.parse(splitters, splitHandler); 63 | return splitHandler.parsed(); 64 | } 65 | 66 | private Map> parseSubstitutions( 67 | InputStream substitutions) throws IOException, SAXException { 68 | substBuilder.clear(); 69 | parser.parse(substitutions, substHandler); 70 | return substBuilder.parsed(); 71 | } 72 | 73 | // 把inputstream转换成byte数组,不过不知道为什么非要这样做。 74 | private byte[] toByteArray(InputStream input) throws IOException { 75 | List list = new LinkedList(); 76 | for (int i = 0; (i = input.read()) > -1;) 77 | list.add((byte) i); 78 | 79 | int i = 0; 80 | byte[] bytes = new byte[list.size()]; 81 | for (byte b : list) 82 | bytes[i++] = b; 83 | return bytes; 84 | } 85 | 86 | public Transformations parse(InputStream splitters, 87 | InputStream substitutions) throws ConfigException, IOException, 88 | SAXException { 89 | byte[] bytes = toByteArray(splitters); 90 | 91 | TokenizerConfig config = new TokenizerConfigStream( 92 | new ByteArrayInputStream(bytes)); 93 | Tokenizer tokenizer = new Tokenizer(config); 94 | 95 | // 这里列表里面放的是“句子分隔符” 96 | List splitChars = parseSplitters(new ByteArrayInputStream(bytes)); 97 | // 这个map里面不会存有数据 98 | Map> maps = parseSubstitutions(substitutions); 99 | return new Transformations(splitChars, maps, tokenizer); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/text/SentenceSplitter.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 java.util.Iterator; 18 | import java.util.LinkedList; 19 | import java.util.List; 20 | import java.util.Map; 21 | import java.util.regex.Matcher; 22 | import java.util.regex.Pattern; 23 | 24 | import static bitoflife.chatterbean.util.Escaper.escapeRegex; 25 | import static java.util.regex.Pattern.CASE_INSENSITIVE; 26 | import static java.util.regex.Pattern.UNICODE_CASE; 27 | 28 | public class SentenceSplitter { 29 | /* 30 | * Attribute Section 31 | */ 32 | 33 | /** Map of sentence-protection substitution patterns. */ 34 | private final Map protection; 35 | 36 | /** List of sentence-spliting patterns. */ 37 | private final List splitters; 38 | 39 | /** The regular expression which will split entries by sentence splitters. */ 40 | private final Pattern pattern; 41 | 42 | /* 43 | * Constructor Section 44 | */ 45 | 46 | public SentenceSplitter(Map protection, 47 | List splitters) { 48 | this.protection = protection; 49 | this.splitters = splitters; 50 | 51 | String splitPattern = "\\s*("; 52 | for (Iterator i = splitters.iterator();;) { 53 | splitPattern += escapeRegex(i.next()); // 这一部分不是之前已经处理过了吗,为什么还有来一遍? 54 | if (!i.hasNext()) 55 | break; 56 | splitPattern += "|"; 57 | } 58 | splitPattern += ")\\s*"; 59 | 60 | this.pattern = Pattern.compile(splitPattern); 61 | } 62 | 63 | /* 64 | * Method Section 65 | */ 66 | 67 | private String protect(String input) { 68 | for (String find : protection.keySet()) { 69 | Pattern pattern = Pattern.compile(find, CASE_INSENSITIVE 70 | | UNICODE_CASE); 71 | Matcher matcher = pattern.matcher(input); 72 | String replace = protection.get(find); 73 | input = matcher.replaceAll(replace); 74 | } 75 | 76 | return input; 77 | } 78 | 79 | private String[] split(String original, String prepared) {// 这个函数细节还不懂???? 80 | /* 81 | * See the description of java.util.regex.Matcher.appendReplacement() in 82 | * the Javadocs to understand this code. 83 | */ 84 | Matcher matcher = pattern.matcher(prepared); 85 | List sentences = new LinkedList(); 86 | int beginIndex = 0; 87 | 88 | while (matcher.find()) { 89 | int endIndex = matcher.start(); 90 | String sentence = original.substring(beginIndex, endIndex) 91 | + matcher.group(1); 92 | if (!splitters.contains(sentence.trim())) 93 | sentences.add(sentence); 94 | beginIndex = endIndex + matcher.group().length(); 95 | } 96 | 97 | String[] splitted; 98 | if (sentences.size() > 0) { 99 | splitted = new String[sentences.size()]; 100 | sentences.toArray(splitted); 101 | } else { 102 | splitted = new String[] { original }; 103 | } 104 | 105 | return splitted; 106 | } 107 | 108 | public String[] split(String original) { 109 | return split(original, protect(original));// 不知道protect这个函数是干什么用的? 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/main/config/Bots/Alice/english/Alice.aiml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 秦秋 6 | 12 | 13 | 14 | WELCOME 15 | 16 | 17 | 18 | WHO ARE YOU 19 | 20 | 21 | 22 | WHAT DID I JUST SAY 23 | 24 | 25 | 26 | WHAT SERIES YOUR ENGINE IS 27 | 28 | 29 | 30 | WHAT TIME IS NOW 31 | 32 | 33 | 34 | WHAT SIZE ARE YOU 35 | 36 | 37 | 38 | WHAT DID YOU JUST SAY 39 | 40 | 41 | 42 | IF YOU ARE * THEN I AM _ 43 | 44 | 45 | 46 | DO YOU SEE THE * 47 | 48 | 49 | 50 | DO YOU SEE THE * IN MY EYES 51 | 52 | 53 | 54 | HI YA 55 | 56 | 57 | 58 | HELLO 59 | 63 | 64 | 65 | GREETINGS FROM * 66 | 67 | 68 | 69 | MY NAME IS * 70 | 71 | 72 | 73 | NAME IS * 74 | 75 | 76 | 77 | I AM * 78 | 79 | 80 | 81 | I AM CALLED * 82 | 83 | 84 | 85 | GREETING * 86 | 90 | 91 | 92 | NICE TO MEET YOU TOO 93 | 94 | 95 | 96 | WHAT IS * 97 | 98 | 99 | 100 | _ BOT 101 | 102 | 103 | 104 | _ NAME 105 | 106 | 107 | 108 | TELL ME ABOUT * 109 | 110 | 111 | 112 | PRINT THIS * 113 | 114 | 115 | 116 | * 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /src/main/java/bitoflife/chatterbean/parser/AliceBotParser.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.AliceBot; 18 | import bitoflife.chatterbean.Context; 19 | import bitoflife.chatterbean.Graphmaster; 20 | import bitoflife.chatterbean.aiml.AIMLParser; 21 | import bitoflife.chatterbean.text.Transformations; 22 | 23 | import java.io.InputStream; 24 | 25 | public class AliceBotParser { 26 | /* 27 | * Attributes 28 | */ 29 | 30 | private Class contextClass = Context.class; 31 | 32 | private Class graphmasterClass = Graphmaster.class; 33 | 34 | private AIMLParser aimlParser; 35 | 36 | private ContextParser contParser; 37 | 38 | private TransformationsParser normParser; 39 | 40 | /* 41 | * Constructor 42 | */ 43 | 44 | public AliceBotParser() throws AliceBotParserConfigurationException { 45 | try { 46 | aimlParser = new AIMLParser(); 47 | contParser = new ContextParser(); 48 | normParser = new TransformationsParser(); 49 | } catch (Exception e) { 50 | throw new AliceBotParserConfigurationException(e); 51 | } 52 | } 53 | 54 | /* 55 | * Methods 56 | */ 57 | 58 | private Context newContext(InputStream defaults, InputStream splitters, 59 | InputStream substitutions) throws Exception { 60 | Context context = (Context) contextClass.newInstance(); 61 | contParser.parse(context, defaults); 62 | Transformations transformations = normParser.parse(splitters, 63 | substitutions); 64 | context.setTransformations(transformations); 65 | return context; 66 | } 67 | 68 | private Graphmaster newGraphmaster(InputStream... aiml) throws Exception { 69 | Graphmaster graphmaster = (Graphmaster) graphmasterClass.newInstance();// 这个是第一个对象。 70 | aimlParser.parse(graphmaster, aiml); 71 | return graphmaster; 72 | } 73 | 74 | public void parse(AliceBot bot, InputStream defaults, 75 | InputStream splitters, InputStream substitutions, 76 | InputStream... aiml) throws AliceBotParserException { 77 | try { 78 | Context context = newContext(defaults, splitters, substitutions); 79 | Graphmaster graphmaster = newGraphmaster(aiml); 80 | 81 | bot.setContext(context); 82 | bot.setGraphmaster(graphmaster); 83 | } catch (Exception e) { 84 | throw new AliceBotParserException(e); 85 | } 86 | } 87 | 88 | public AliceBot parse(InputStream defaults, InputStream splitters, 89 | InputStream substitutions, InputStream... aiml)// 这里的省略号还有疑问。 90 | throws AliceBotParserException { 91 | try { 92 | AliceBot bot = new AliceBot(); 93 | parse(bot, defaults, splitters, substitutions, aiml); 94 | return bot; 95 | } catch (AliceBotParserException e) { 96 | throw e; 97 | } catch (Exception e) { 98 | throw new AliceBotParserException(e); 99 | } 100 | } 101 | 102 | /* 103 | * Accessor Section 104 | */ 105 | 106 | public void contextClass(Class contextClass) { 107 | this.contextClass = contextClass; 108 | } 109 | 110 | public void graphmasterClass( 111 | Class graphmasterClass) { 112 | this.graphmasterClass = graphmasterClass; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /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.ArrayList; 20 | import java.util.Arrays; 21 | import java.util.LinkedList; 22 | import java.util.List; 23 | 24 | public class Searcher implements FilenameFilter { 25 | /* 26 | * Attribute Section 27 | */ 28 | private LinkedList fileQueue = new LinkedList(); 29 | private static final String[] STRING_ARRAY = {}; 30 | 31 | private String expression; 32 | 33 | /* 34 | * Method Section 35 | */ 36 | 37 | /** 38 | * 该方法被我修改,支持文件遍历。 39 | * 40 | * @param path 41 | * @param expression 42 | * @return 43 | */ 44 | protected String[] dir(String path, String expression) { 45 | // this.expression = expression; 46 | // if (path.charAt(path.length() - 1) != '/') 47 | // path += "/"; 48 | // File dir = new File(path); 49 | // String[] names = dir.list(this); 50 | // Arrays.sort(names); 51 | // for (int i = 0, n = names.length; i < n; i++) 52 | // names[i] = path + names[i]; 53 | // return names; 54 | if (path.charAt(path.length() - 1) != '/') 55 | path += "/"; 56 | File file = new File(path); 57 | 58 | List filenames = new ArrayList(); 59 | 60 | addFilesToQueue(file.listFiles()); 61 | 62 | while (!fileQueue.isEmpty()) { 63 | File f = fileQueue.poll(); 64 | if (f.isFile() && f.getName().matches(expression)) { 65 | filenames.add(f.getPath()); 66 | } else if (f.isDirectory()) { 67 | addFilesToQueue(f.listFiles()); 68 | } 69 | } 70 | String[] names = filenames.toArray(new String[0]); 71 | Arrays.sort(names); 72 | return names; 73 | } 74 | 75 | private void addFilesToQueue(File[] files) { 76 | for (int i = 0; i < files.length; i++) { 77 | fileQueue.add(files[i]); 78 | } 79 | } 80 | 81 | protected String[] dir(URL base, String path, String expression) 82 | throws IOException { 83 | if (path.charAt(path.length() - 1) != '/') 84 | path += "/"; 85 | 86 | URL url = new URL(base, path); 87 | BufferedReader dir = new BufferedReader(new InputStreamReader( 88 | url.openStream())); 89 | 90 | List files = new LinkedList(); 91 | for (String file = ""; (file = dir.readLine()) != null;) 92 | if (file.matches(expression)) 93 | files.add(path + file); 94 | 95 | return files.toArray(STRING_ARRAY); 96 | } 97 | 98 | public boolean accept(File dir, String name) { 99 | return name.matches(expression); 100 | } 101 | 102 | public InputStream[] search(String path, String expression) 103 | throws IOException { 104 | String[] names = dir(path, expression); 105 | InputStream[] files = new InputStream[names.length]; 106 | for (int i = 0, n = names.length; i < n; i++) { 107 | files[i] = new FileInputStream(names[i]); // 这里有一个问题,这么多的数据流,是在哪里关闭的,我没有发现。 108 | } 109 | return files; 110 | } 111 | 112 | public InputStream[] search(URL base, String path, String expression) 113 | throws IOException { 114 | String[] files = dir(base, path, expression); 115 | InputStream[] streams = new InputStream[files.length]; 116 | for (int i = 0, n = files.length; i < n; i++) { 117 | URL aiml = new URL(base, files[i]); 118 | streams[i] = aiml.openStream(); 119 | } 120 | 121 | return streams; 122 | } 123 | } 124 | --------------------------------------------------------------------------------