23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/main/config/Bots/Alice/english/empty_wildcard.aiml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 | THANK YOU *
9 | Why, you're welcome!
10 |
11 |
12 | I AM HAPPY * IS *
13 | Yes, I am happy for being as well.
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/main/config/Bots/Alice/chinese/Food.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 |
29 | *好吃*
30 | 哦,那我也想吃。
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/main/config/Bots/Alice/english/thattopic.aiml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | YES
7 | DO YOU LIKE CHEESE
8 | Good for you.*
9 |
10 |
11 |
12 | WHAT DO YOU WANT TO KNOW
13 | Do you like cheese?
14 |
15 |
16 | YES
17 | "Yes" what?
18 |
19 |
20 | YES
21 | DO YOU LIKE CAKE
22 | I couldn't care less.
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 | 喜欢。人一辈子有3件事是不用省钱的:学习,锻炼身体。还有一个就是旅游了。你喜欢吗?
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 |
6 | 欢迎!请问我能为你做点什么呢?
7 |
8 | import bitoflife.chatterbean.AliceBot;
9 | import bitoflife.chatterbean.Context;
10 | import bitoflife.chatterbean.Graphmaster;
11 | import bitoflife.chatterbean.aiml.Category;
12 | import bitoflife.chatterbean.text.Transformations;
13 |
14 | void learn(String pattern, String template)
15 | {
16 | /* The "match" variable represents the current matching context. */
17 | AliceBot bot = match.getCallback();
18 | Context context = bot.getContext();
19 | Transformations transformations = context.getTransformations();
20 |
21 | pattern = transformations.normalization(pattern);
22 | Category category = new Category(pattern, new String[] {template});
23 | Graphmaster brain = bot.getGraphmaster();
24 | brain.append(category);
25 | }
26 |
27 |
28 |
29 |
30 |
31 |
32 |
36 |
--------------------------------------------------------------------------------
/src/main/config/Bots/Alice/english/alice.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | HELLO
6 | Hello! My name is Alice, who are you?
7 |
8 |
9 | MY NAME IS *
10 | GREETING
11 |
12 |
13 | NAME IS *
14 | GREETING
15 |
16 |
17 | I AM *
18 | GREETING
19 |
20 |
21 | GREETING *
22 | Nice to meet you, . :-)
23 |
24 |
25 | WHAT IS *
26 | Sorry, I don't know what is.
27 |
28 |
29 | _ BOT
30 | Yes, I am an ALICE Bot.
31 |
32 |
33 | _ NAME
34 | My name is Alice, nice to meet you!
35 |
36 |
37 | TELL ME ABOUT *
38 |
39 |
40 |
41 | *
42 | I am sorry, my answers are limited -- you must provide the right questions.
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 | 他不像水管修理工人,只有坏了才需要他上门。
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 |
44 |
45 |
46 |
47 | * 电 脑 * 防 火 墙 *
48 | 装了也没用,电脑病毒天天都在更新。
49 |
50 |
51 |
52 |
53 | * 去 * 网 站 *
54 | 相信不?我的电脑没装防火墙,只好去一些正规网站。
55 |
56 |
57 |
58 |
59 | * 下 * 次 * 战 争 *
60 | 拉开序幕的肯定是不冒烟的电脑网络大战。
61 |
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 | 三流的公司卖劳力,二流的公司卖产品,一流的公司卖
6 | 技术,超一流的公司卖标准。
7 |
8 |
9 |
10 |
11 |
12 | * 出 色 * 男 人 *
13 | 无论在什么地方,都像漆黑中的萤火虫一样,那样的鲜明,那样的出众。
14 |
15 |
16 |
17 |
18 | * 女 人 *
19 | 香水是女人的第二层皮
20 |
21 |
22 |
23 |
24 | * 被 * 解 雇 *
25 | 一切正常呀。
26 |
27 |
28 |
29 |
30 | * 经 济 *
31 | 不管什么经济,能赚钱的经济才是好经济。
32 |
33 |
34 |
35 |
36 | * 新 经 济 *
37 | 一场由风险资本推动的全社会的造钱运动。
38 |
39 |
40 |
41 |
42 | * 公 司 * 招 人 *
43 | 我好久都没面试了
44 |
45 |
46 |
47 |
48 | * 城 市 *
49 | 我喜欢农村,有新鲜的空气:)人会长命百岁的
50 |
51 |
52 |
53 |
54 | * 过 关 *
55 | 开玩笑,对你来说还不是小CASE呀
56 |
57 |
58 |
59 |
60 | * 赚 钱 * 难 *
61 | 是哦,我的梦想就是哪天可以坐在办公室里数钞票,呵呵。
62 |
63 |
64 |
65 |
66 | * 女 博 士 *
67 | 真想见识见识这些灭绝师太:)
68 |
69 |
70 |
71 |
72 | * 拍 * 马 屁 *
73 | 呵呵,那你要拍好了哇
74 |
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 |
7 |
8 |
49 |
50 |
51 |
52 |
53 | * 喜 欢 * 超 级 玛 莉 *
54 | 我一玩就死的,相比而言,大富翁比较好玩一点。
55 |
56 |
57 |
58 |
59 | *玩什么游戏*
60 | 我喜欢玩英雄联盟,把对面杀穿。你呢?
61 |
62 |
63 |
64 |
65 | 我喜欢*
66 | 嗯,听起来不错。
67 |
68 |
69 |
70 |
71 | 我玩*
72 | 是吗?没有尝试过,不过听起来好像很好玩。
73 |
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 |
7 |
8 | on
9 |
10 |
11 | The block condition is working.
12 |
13 |
14 |
15 |
16 | SECOND BLOCK CONDITION TEST
17 |
18 |
19 | on
20 |
21 | The block condition is
22 |
23 | not
24 |
25 | working.
26 |
27 |
28 |
29 | THIRD BLOCK CONDITION TEST
30 |
31 |
32 | on
33 |
34 | The block condition
35 |
36 |