AIML应答机器人(二)java实现

文章列表

AIML应答机器人(一)aiml简介

AIML应答机器人(二)java实现


AIML应答机器人(二)java实现_第1张图片

想做一款和上图一样的自动应答机器人吗,跟着博客,咱们一步步实现,现在开始第二个内容,开始做一个java版的聊天程序

本文源码地址:https://github.com/xvshu/alice_bot

源码入口:

AIML工厂:AliceBotMother

/*
Copyleft (C) 2005 Hélio Perroni Filho
[email protected]
ICQ: 2490863

This file is part of ChatterBean.

ChatterBean is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

ChatterBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with ChatterBean (look at the Documents/ directory); if not, either write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA, or visit (http://www.gnu.org/licenses/gpl.txt).
*/

package bitoflife.chatterbean;

import bitoflife.chatterbean.parser.AliceBotParser;
import bitoflife.chatterbean.util.Searcher;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class AliceBotMother
{
  /*
  Attribute Section
  */
  
  private ByteArrayOutputStream gossip;
  
  /*
  Event Section
  */
  
  public void setUp()
  {
    gossip = new ByteArrayOutputStream();
  }
  
  /*
  Method Section
  */
  
  public String gossip()
  {
    return gossip.toString();
  }

  public AliceBot newInstance() throws Exception
  {
    Searcher searcher = new Searcher();
    AliceBotParser parser = new AliceBotParser();
    AliceBot bot = parser.parse(getClass().getResourceAsStream("/conf/context.xml"),
            getClass().getResourceAsStream("/conf/splitters.xml"),
            getClass().getResourceAsStream("/conf/substitutions.xml"),
                                searcher.search(getClass().getResource("/Corpus/Chinese").toString().substring(5), ".*\\.xml"));

    Context context = bot.getContext(); 
    context.outputStream(gossip);
    return bot;
  }
}

web入口AskServlet

package com.web;

import com.context.ChartManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AskServlet extends HttpServlet {

    private ChartManager chartManager = null;

    public AskServlet() {
        super();
        chartManager = ChartManager.getInstance();
    }



    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String askWord=request.getParameter("askWord");
        String outWord=chartManager.response(askWord);
        response.setContentType("text/html");
        response.getWriter().println(outWord);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String askWord=request.getParameter("askWord");
        System.out.println("askWord:"+String.valueOf(askWord));
        String outWord=chartManager.response(askWord);
        System.out.println("outWord:"+String.valueOf(outWord));
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(outWord);
    }
}

html页面




    
    
    
    客服小A
    
    
    
    


    
AIML应答机器人--小A
您好,很高兴为您服务,请问有什么可以帮助您?

实现效果:

AIML应答机器人(二)java实现_第2张图片

AIML应答机器人(二)java实现_第3张图片


是不是非常简单,只需要将git代码clone到本地,初始化mysql(备用,扩展以后人工录入),编写自己公司的xml应答文件,我这里给公司大米时代(北京)编写了一份,请大家参考:





    
        *米老师*
        
    

    
        *米新江*
        
    

    
        *新江*
        
    

    
        *大米时代*创*人*
        
    

    
        *提高班*创*人*
        
    


    
        *米老师*语录*
        
    

    
        *米新江*语录*
        
    

    
        *新江*语录*
        
    

    
        *新江*话*
        
    

    
        *米老师*话*
        
    

    
        *米教授*话*
        
    

    
        *米教授*语录*
        
    


    
        *入*大米时代*
        
    


    
        *进*大米时代*
        
    

    
        *加*大米时代*
        
    

    
        *入*提高班*
        
    

    
        *进*提高班*
        
    

    
        *加*提高班*
        
    

    
        *大米时代*
        
    
    
        *提高班*
        
    

    
        大米时代
        
    

    
        报名大米时代
        
    

    
        米老师
        
    

    
        米老师语录
        
    




效果:

AIML应答机器人(二)java实现_第4张图片

使用tomcat启动,设置utf-8编码支持中文

                 connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />

你可能感兴趣的:(AI,人工神经网络)