图灵机器人API可以用来做机器人的智能交互,上传一段话,该云机器人可以以json格式返回一个比较智能的回答,它支持云机器人的知识库管理和调教。
以下是一个Java实现的简单例子,可以直接运行,更改question的问题可以得到相应的回答:
package com.ultrasoft;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* 图灵机器人测试程序
* @author Sword
*
*/
public class Tuling {
public static void main(String[] args) throws IOException {
//申请的APIKey
String APIKEY = "e7a1447ed2182d57758ca845e5a0f36e";
// 这是上传给云机器人的问题
String question = "什么是人工智能?";
tulingQA(APIKEY,question);
}
public static void tulingQA(String APIKEY,String question) throws UnsupportedEncodingException,
MalformedURLException, IOException {
String INFO = URLEncoder.encode(question, "utf-8");
String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY
+ "&info=" + INFO;
URL getUrl = new URL(getURL);
HttpURLConnection connection = (HttpURLConnection) getUrl
.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
// 断开连接
connection.disconnect();
System.out.println(sb);
}
}
运行结果:
{"code":100000,"text":"人工智能(Artificial Intelligence),英文缩写为AI。它是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学。 人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器,该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等"}
由结果可以看出图灵机器人的回答还是比较智能的。