当前位置:   article > 正文

人工智能初体验(一):使用图灵机器人智能获取问题回答

利用百度、图灵接口实现人工智能问答

一 简单介绍以及apikey获取

就我个人而言,目前有两个API是比较不错的,一个是百度的接口,另一个是图灵机器人(http://www.tuling123.com/)的接口。前者调用简单,而且没有使用次数限制(PS:据说还是有限制?);后者需要进行一系列身份认证,而且每天次数限制是5000(PS:貌似可以免费增加次数),但是它的优势是可以进行个性化设置,这点比较好。

wKiom1Z0uQuAZTVLAACTxl68oz0253.png

在这里为了方便演示,我使用百度的接口进行测试,申请地址是:http://apistore.baidu.com/apiworks/servicedetail/736.html

wKiom1Z0uTWgn90DAAE-vFOFEUY082.png

可以看到,请求参数有三个,分别是:key,info,userid,其中key和userid用默认值就可以了。当然最重要的是要在请求的header里添加上apikey这一项,点击这里就可以免费获取了:

wKiom1Z0uVnCbYVkAAB6EI65mFQ786.png

注:要是对Java网络编程不是很熟悉的话,可以参考下方的Demo

二 一个简单的Demo

通过HttpURLConnection对指定的API发起GET请求,然后对返回的JSON数据进行简单的匹配,然后获取我们需要的回答,测试代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package  action;
 
import  java.io.BufferedReader;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.net.HttpURLConnection;
import  java.net.MalformedURLException;
import  java.net.URL;
import  java.util.regex.Matcher;
import  java.util.regex.Pattern;
 
public  class  TuringRobot {
 
     public  static  void  main(String[] args) {
         TuringRobot turing =  new  TuringRobot();
         String question =  "北京天气" ;
         
         String temp = turing.getResponse( "879a6cb3afb84dbf4fc84a1df2ab7319" , "您自己的apikey" , question,  "eb2edb736" );
         System.out.println( "小图:"  + temp);
         
         String temp2 = turing.getResponse( "879a6cb3afb84dbf4fc84a1df2ab7319" , "您自己的apikey" "你这么可爱,一定是个男孩子" "eb2edb736" );
         System.out.println( "小图:"  + temp2);
     }
     
     /**
      * 使用百度图灵机器人,获取回答
     
      * @param key 默认值:879a6cb3afb84dbf4fc84a1df2ab7319
      * @param ApiKey 在APIStore调用服务所需要的API密钥,申请地址:http://apistore.baidu.com
      * @param info 想要请求的问题
      * @param userid 用户id 默认值:eb2edb736
     
      * @return 获取的回复
      * */
     public  String getResponse(String key,String ApiKey,String info,String userid){
         String httpUrl =  "http://apis.baidu.com/turing/turing/turing?" ;
//      try {
//          info = URLEncoder.encode(info,"UTF-8");  //URL编码,可以不加
//      } catch (UnsupportedEncodingException e1) {
//          e1.printStackTrace();
//      }
         String httpArg =  "key="  + key +  "&info="  + info +  "&userid="  + userid;
         try  {
             URL url =  new  URL(httpUrl + httpArg);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod( "GET" );
             connection.setRequestProperty( "apikey" , ApiKey);
             
             InputStream inputStream = connection.getInputStream();
             BufferedReader reader =  new  BufferedReader( new  InputStreamReader(inputStream, "UTF-8" ));
             String line =  "" ;
             String reg =  "\"text\":\"(.*)?\",\"code\"" ;
             Pattern pattern = Pattern.compile(reg);
             Matcher matcher;
             while ((line = reader.readLine()) !=  null ){
                 matcher = pattern.matcher(line);
                 if (matcher.find())
                     return  matcher.group( 1 );
             }      
         catch  (MalformedURLException e) {
             e.printStackTrace();
         catch  (IOException e) {
             e.printStackTrace();
         }
         return  "" ;
         
     }
 
}

三 测试结果:

wKiom1Z0uZbD5HcdAAA1Drdl0WA176.png



本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1726260,如需转载请自行联系原作者

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/715276
推荐阅读
相关标签
  

闽ICP备14008679号