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

本文档将指导你如何创建一个基于Java的AIML聊天机器人。内容包括AIML简介,详细介绍了Java版聊天程序的实现,提供了源码地址以及关键类如AliceBotMother和AskServlet。启动机器人只需配置Tomcat并支持UTF-8编码。
摘要由CSDN通过智能技术生成

文章列表

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

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


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

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

源码入口:

AIML工厂:AliceBotMother

/*
Copyleft (C) 2005 Hélio Perroni Filho
xperroni@yahoo.com
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);
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值