1、图灵机器人大脑具备强大的中文语义分析能力,可准确理解中文含义并作出回应,是最擅长聊中文的机器人大脑,赋予软硬件产品自然流畅的人机对话能力。
2、我们熟悉的案例有QQ小冰,通过用户的输入能够达到智能对话,当然,图灵机器人的运用不止于此。
3、作为一个 java 初学者,可以利用图灵的 Api ,搭建一个智能对话系统,玩一点有意思的东西,哈哈。
案例:图灵机器人支持自定义词库,这样的话就可以做一个智能的客服系统,淘宝机器人大家都很熟悉啦!
源码 demo:https://download.csdn.net/download/w2462140956/11150089
1、官网地址:http://www.turingapi.com/
2、如图,首先注册登录后添加自己的机器人,添加完成后,再点击设置机器人,获得机器人的 apiKey,secret,这两个是必须的,用来调用 api 接口。
1、逻辑说明,基于web开发,使用 sevrlet 连接,主要是后台调用图灵api,将获得的数据返回前台,前台根据数据进行页面渲染展示。简单实用,便于读者改造。
2、前台 jsp中使用 Ajax 异步。
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath }/turingServlet",
data: "info="+content,
success: function(data) {
}
});
3、着重把后台代码贴出
3.1、TuringServlet.java
记得把你自己的 apiKey ,secret 数据填入!
package com.turing.util;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
@WebServlet("/turingServlet")
public class TuringServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收数据!
request.setCharacterEncoding("utf-8");
//拿到图灵机器数据
//图灵网站上的secret
String secret = "";//您的secret
//图灵网站上的apiKey
String apiKey = "";//您的apiKey
String cmd = request.getParameter("info");//测试用例
//待加密的json数据
String data = "{\"key\":\""+apiKey+"\",\"info\":\""+cmd+"\"}";
//获取时间戳
String timestamp = String.valueOf(System.currentTimeMillis());
//生成密钥
String keyParam = secret+timestamp+apiKey;
String key = Md5.MD5(keyParam);
//加密
Aes mc = new Aes(key);
data = mc.encrypt(data);
//封装请求参数
JSONObject json = new JSONObject();
json.put("key", apiKey);
json.put("timestamp", timestamp);
json.put("data", data);
//请求图灵api:返回的结果:json的字符串结果!
String result = PostServer.SendPost(json.toString(), "http://www.tuling123.com/openapi/api");
//发送结果!
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(result);
out.flush();
out.close();
}
}
3.2、PostServer.java
package com.turing.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* HTTP工具类
* @author 图灵机器人
*
*/
public class PostServer {
/**
* 向后台发送post请求
* @param param
* @param url
* @return
*/
public static String SendPost(String param, String url) {
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl
.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setConnectTimeout(50000);
conn.setReadTimeout(50000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "token");
conn.setRequestProperty("tag", "htc_new");
conn.connect();
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(param);
out.flush();
out.close();
//
in = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "UTF-8"));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
3.3、Md5.java
package com.turing.util;
import java.security.MessageDigest;
/**
* md5加密
* @author 图灵机器人
*
*/
public class Md5 {
/**
* MD5加密算法
*
* 说明:32位加密算法
*
* @param 待加密的数据
* @return 加密结果,全小写的字符串
*/
public static String MD5(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] btInput = s.getBytes("utf-8");
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
3.4、Aes.java
package com.turing.util;
import java.security.Key;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* aes加密算法
* @author 图灵机器人
*
*/
public class Aes {
private Key key;
/**
* AES CBC模式使用的Initialization Vector
*/
private IvParameterSpec iv;
/**
* Cipher 物件
*/
private Cipher cipher;
/**
* 构造方法
* @param strKet
* 密钥
*/
public Aes(String strKey) {
try {
this.key = new SecretKeySpec(getHash("MD5", strKey), "AES");
this.iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0 });
this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (final Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}
/**
* 加密方法
*
* 说明:采用128位
*
* @return 加密结果
*/
public String encrypt(String strContent) {
try {
byte[] data = strContent.getBytes("UTF-8");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encryptData = cipher.doFinal(data);
String encryptResult = new String(Base64.encodeBase64(
encryptData), "UTF-8");
return encryptResult;
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}
/**
*
* @param algorithm
* @param text
* @return
*/
private static byte[] getHash(String algorithm, String text) {
try {
byte[] bytes = text.getBytes("UTF-8");
final MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(bytes);
return digest.digest();
} catch (final Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}
}
如果各位看官觉得不错,还请文章右上角点个赞哈...