机器人之间对聊,您想试试么
By:Roy.LiuLast updated:2016-02-23
这是借助图灵机器人提供的免费API实现的两个机器人对话的功能,系统自动提取图灵平台提供的智能聊天库,自己还能导入自己的知识库让机器人更具个性化,更符合你的胃口.
import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; public class Main { private static final String KEY = "91e71ab13d4f1be71de4a7ef1c1ec2ef"; private static final String BASEURL = "http://www.tuling123.com/openapi/api?key="+KEY+"&userid="; private static final String ALICE = "Alice"; private static final String TITAN = "Titan"; public static void main(String[] args) { //第一次交谈 String first = "你好啊!"; System.out.println(ALICE+"对"+TITAN+"说:"+first); new Main().communication(ALICE, first); } /** * ALICE 与 TITAN 交谈 * @param user 用户 * @param text 聊天室内容 */ public void communication(String user, String text) { // 构造 访问图灵的地址 StringBuffer url = new StringBuffer(""); url.append(BASEURL); url.append(user); url.append("&info=").append(text); // 输出日志 // System.out.println(url.toString()); // 取回交谈内容 String msg = getRobotMsg(url.toString()); if (ALICE.equals(user)) {//如果对ALICE说 System.out.println(TITAN+"对"+ALICE+"说:"+msg); communication(TITAN, msg); } else if (TITAN.equals(user)) {//如果对TITAN说 System.out.println(ALICE+"对"+TITAN+"说:"+msg); communication(ALICE, msg); } } /** * 取回机器人的回答 * @param url 图灵机器人完全访问地址;带 userid和info * @return 机器人的回答 */ public String getRobotMsg(String url) { try { HttpResponsejsonNodeHttpResponse = Unirest.get(url).asJson(); String text = jsonNodeHttpResponse.getBody().getObject().get("text").toString(); return text; } catch (Exception e) { return "我不知道!"; } } }
From:一号门
Previous:获取文件 MD5和SHA1
COMMENTS