图灵机器人平台是一个免费的API开放平台,基于自然语言处理、知识库和云计算等技术,为广大开发者、合作伙伴提供的一系列智能语义处理能力(包括语义理解、智能问答、知识库对接等)的服务平台。api使用地址:http://www.tuling123.com/openapi/record.do?channel=50044
今天 我用图灵机器人开放的api做了一个简单的实时应答程序:)
robot.php:
<?php
error_reporting(0);
$INFO=$_GET['info'];
if(!$INFO)$INFO="Hello!";
$apiKey="aaaaaaaaaaaaaaaaaaaaaaa"; //你的appkey
$apiURL="http://www.tuling123.com/openapi/api?key=$apiKey&info=$INFO";//appkey地址
$cmd=file_get_contents($apiURL);
//echo $cmd."<br>";//输出json格式的信息(本实例中不用)
$tmp=json_decode($cmd,1);
if($tmp[code][0]!="4")//将json解码 如果不出现错误(返回代码以4开头),则显示文本信息
{
print_r($tmp[text]);
}
else
{
echo "Error!error code is:$tmp[code]!";
}
?>
demo.html
<form>
Say:<input type="text" id=“robot” οnkeyup="showHint(this.value)"/> 当键盘敲击时触发事件 发送ajax请求
</form>
<p>Ans: <span id="txtHint"></span></p> 作为回显的区域
<script>
var xmlHttp
function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("您的浏览器不支持AJAX!");
return;
}
var url="robot.php";
url=url+"?info="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>