ドコモのデベロッパーサービスの雑談対話APIを使ってみた。

20160528

Logging

ドコモのデベロッパーサービスの雑談対話APIを使ってみた。
結果から言うと機械学習アルゴリズムではないぽっいので
会話がいまいち成り立たないという結果かな。
ドコモが提供しているAIのrepl-aiという物もあるのですが
文字をいまのAPIサービスだと投げれないという
事なので…。ボットを楽しむには良いかもしれませんが、
APIとしては使えないので
雑談対話APIで作ってみた感じです。
やはり機械学習でないとスムーズな会話は
出来ないのかも・・・・そろそろりんなAPI RINNAとして
提供されそうなのでソチラを試してみたいなと思っています。
今回作ったAPIのソースを載せときます。ご自由にお使いください(´Д`)。

<!doctype html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>zip358.com:雑談API{ドコモ}</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=10.0, user-scalable=yes">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <style>
    .balloon {
      position: relative;
      width: 200px;
      height: 30px;
      background-color: #ccc;
      text-align: center;
    }
    .balloon:before {
      content: "";
      position: absolute;
      bottom: -10px;
      left: 50%;
      margin-left: -10px;
      width: 0;
      height: 0;
      border-top: 10px solid #ccc;
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
    }
  </style>
   <script>
  $(function() {
    $( "#msg_set" ).click(function() {
       $("#msg_ai").html("");
       $.ajax({
            type: "POST",
            url: "json.php",
            data:{
                msg:$("#msg_human").val()
            },
            dataType: 'json',
            success: function(obj){
              //alert(obj.txt);
              $("#msg_ai").html(obj.aiman);
            }
        });
    });
  });
  </script>
  <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-71682075-1', 'auto');
  ga('send', 'pageview');
</script>
  <style>
      table, td, th { border: 2px #808080 solid; }
  </style>
</head>
<body>
<h2>雑談APIです。なんでも語りかけてください。</h2>
<div  class="balloon">
  <p id="msg_ai">......</p>
</div>
<p><br><input type="text" id="msg_human"><input id="msg_set" type="button" value="送信"></p>
</body>
</html>
<?php
include './aiman.php';
$text = urldecode($_POST["msg"]);
$aiman = new aiman();
$msg["aiman"] = $aiman->ai_msg($text);
$msg["txt"] = $text;
$json = json_encode($msg);
echo $json;
<?php
//ドコモ:雑談対話API
class aiman{
    var $apikey ="ドコモで取得したAPIKEY";
    function ai_msg($text="おはよう"){
        $url = 'https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY='.$this->apikey;
        $headers = array(
            'Content-Type: application/json; charset=UTF-8',
        );
        $data = array(
            'utt' => $text
        );
        $options = array('http' => array(
            'method' => 'POST',
            'header' => implode("\r\n", $headers),
            'content' => json_encode($data)
        ));
        $context = stream_context_create($options);
        $response = file_get_contents($url, false, $context);
        $res = json_decode($response);
        return $res->utt;
    }
}

 

タグ

AM,