---
title: "JAVA中[人工无脑]系统（chatbot）的实现"
author: "mmdev"
date: "2007-10-17T13:31:00Z"
tags: []
slug: "java-chatbot"
draft: false
lastmod: "2012-04-14T20:15:15Z"
id: 1486388
url: "https://mmdev.iteye.com/blog/1486388"
---

所谓\[人工无脑]，根据本人在百度百科中的解释^^,即：

人工无脑“人工无脳（じんこうむのう）”，是日语中特有的说法，日文引用英文的舶来语，\[人工无脑]在英文中被称为chatterbot、chatbot等，中文通常称其为“聊天机器人”。\[人工无脑]与\[人工智能]等\[强AI]产物相反，是\[弱AI]的代表产品。

世界上第一个\[人工无脑]系统chatterbot, 诞生于1966年．名为ELIZA，由J. Weizenbaum开发。ELIZA会分解问话，再以问话检索事前录入好的答案。从而模拟人机通信，造成计算机与人沟通的\[假象]，这也是所有\[弱 AI]系统的共同特点。初期的\[人工无脑]实现，以ELIZA和PARRY为主流。此后Ractor、Verbot、A.L.I.C.E.、ELLA等陆 续登场，\[弱AI]系统得以长足发展。

\[人工无脑]与\[人工智能]的最大区别在于，它仅是模拟人机数据交换，而根据简单的\[表面现象]使之产生作用的程式，并没有\[强AI]那种模拟生命智能的 需 求。只是应用编码好的程序，把提出的问题再以固定的方式回应，是一种\[弱AI]。再简单地说，这是在暂时无法实现人工智能时，一种模拟人工智能的方式。

如绝大部分游戏编程中，关于\[人机交互]这一部分，都是以\[人工无脑]的方式来实现的。

在这里首先提供一个简单的Java用例：

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/chatbot0.gif)

此用例不过滤用户会话，仅将输入的会话再次返回，但却是\[无脑]系统的基础所在。

代码如下：

ChatbotPanel:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Title:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Description:&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Copyright:Copyright(c)2007&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Company:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.BorderLayout;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Dimension;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Frame;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Panel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.ScrollPane;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.TextArea;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.TextField;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.ActionEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.ActionListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.WindowAdapter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.WindowEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Title:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Description:\[人工无脑]系统的初步实现。&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Copyright:Copyright(c)2007&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Company:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassChatbotPanelextendsPanelimplementsActionListener...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintWIDTH=480;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintHEIGHT=480;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//用于信息显示  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateTextAreadialogueArea;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//用于信息输入  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateTextFieldinputField;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateChatbotchatbot=newChatbot(Chatbot.NAME);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicChatbotPanel()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setPreferredSize(newDimension(WIDTH,HEIGHT));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)init();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*初始显示  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)privatevoidinit()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setLayout(newBorderLayout());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//创建文本框  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dialogueArea=newTextArea();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dialogueArea.setEditable(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dialogueArea.append("人机通信模拟 ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//初始信息  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inputField=newTextField("要与\["+Chatbot.NAME+"]进行的对话。");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inputField.selectAll();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定滚动条  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ScrollPanescrollPane=newScrollPane();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)scrollPane.add(dialogueArea);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)add(scrollPane,BorderLayout.CENTER);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)add(inputField,BorderLayout.SOUTH);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inputField.addActionListener(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidactionPerformed(ActionEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//获得输入的内容  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringinput=inputField.getText();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dialogueArea.append("人类说： "+input+" ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//返回响应  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringresponse=chatbot.getResponse(input);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dialogueArea.append(chatbot.getName()+"： "+response+" ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dialogueArea.setCaretPosition(dialogueArea.getText().length());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inputField.setText("");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Framefrm=newFrame();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setSize(WIDTH,HEIGHT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setResizable(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setTitle(Chatbot.NAME);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.add(newChatbotPanel());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setVisible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)frm.addWindowListener(newWindowAdapter()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidwindowClosing(WindowEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)});  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

Chatbot:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Title:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Description:&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Copyright:Copyright(c)2007&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Company:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassChatbot...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticfinalStringNAME="无脑0号机";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicChatbot(Stringname)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.name=name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetResponse(Stringmessage)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnmessage;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetName()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

在\[人工无脑]系统中，真正关键的技术在词条的检索和回复方式，比如我略微扩充Chatbot类后，将变成这样。  
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/chatbot1.gif)

变更后Chatbot如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.BufferedReader;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileNotFoundException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.InputStreamReader;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.ArrayList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.Random;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Title:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Description:&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Copyright:Copyright(c)2007&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Company:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassChatbot...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticfinalStringNAME="无脑0号机";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateArrayListdic;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateRandomrand;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicChatbot(Stringname)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.name=name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dic=newArrayList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rand=newRandom(System.currentTimeMillis());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//加载字典  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)loadDic();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetResponse(Stringmessage)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringresponse=(String)dic.get(rand.nextInt(dic.size()));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnresponse;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetName()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)privatevoidloadDic()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)BufferedReaderbr=newBufferedReader(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)newInputStreamReader(getClass().getResourceAsStream("dic.txt")));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringline;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while((line=br.readLine())!=null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)dic.add(line);//写入list  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)br.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(FileNotFoundExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

dic.txt内容如下：  
[中国人民是不可战胜的。  
一切派都是纸老虎。  
人民万岁！  
庆祝17大胜利召开。  
共建和谐社会。]

虽然没有经过更多处理，但已经能体现出人机对话的\[假象了]，若通过网络的字典数据库系统，甚至能够完成以假乱真的弱AI聊天机器人呢。