2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

言語解析からネガ・ポジをとって、DBにおさめる(エクシング社言語解析API利用)

Last updated at Posted at 2015-08-07

言語解析APIを利用して、DBに保存していけばユーザー分析に使えるのではないか?と思い作ってみた。

今回使う言語解析APIはエクシング社を活用、ネガポジくらいなら取れる。
DBにはニフティクラウドmobilebackendを活用
また、APIを叩く基盤としてmonacaを利用した.

やることは以下
・エクシング社 言語解析APIをJavaScriptで叩く
・上記の結果をニフティクラウドmobile backendに保存

以下コード

index.html
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no">
    <script src="components/loader.js"></script>
    <link rel="stylesheet" href="components/loader.css">
    <link rel="stylesheet" href="css/styles.css">
    <script src="js/ncmb-latest.min.js"></script>
    <script src="js/cssua.min.js"></script>
    <script src="js/app.js"></script>
    
</head>
<body>
    <div data-role="page" id="list-page">
        <div data-role="header" data-position="fixed" data-theme="c">
            <h1>XING!</h1>
            <input id="search-button" type="button" value="Search" onclick="XINGApi()">
        </div>
        <div data-role="content">
            <ul data-role="listview" data-inset="true" id="x-list"></ul>
        </div>
    </div>
</body>
</html>
app.js
var xhr = new XMLHttpRequest();
var current;
var YOUR_APP_KEY = "YOUR_APP_KEY";
var YOUR_CLIENT_KEY = "YOUR_CLIENT_KEY";

$(function(){
    //起動時にmobile backend APIキーを設定
    NCMB.initialize(YOUR_APP_KEY,YOUR_CLIENT_KEY);
});

function XINGApi(){
//エクシング言語解析API
var XINGApi =
   "https://lr.capio.jp/webapis/iminos/synana_k/1_1/";
//アクセスキー
  var XINGAccessKey =
   "XINGAccessKey";
  var sent = "ばかやろう"; 
  //クエリを作る
  var queri = XINGApi + "?acckey=" + XINGAccessKey + "&sent=" + sent;

  // XMLHttpRequest オブジェクトを作成
  xhr.open("GET",queri,false);
  //レスポンス処理の指定
  xhr.onreadystatechange = processResponse;
  //APIを叩く
  xhr.send();
  console.log("quit");

  return queri;
}

function processResponse() {
    console.log("xhr.readyState:" +xhr.readyState);
    if(xhr.readyState == 4) {
        if(xhr.status == 200 || xhr.status == 201) {
            // リクエストの処理
            var XING = JSON.parse(xhr.responseText);
             for(var i in XING.results){
                //文章がポジティブかネガティブかを判定
                var spn = JSON.stringify(XING.results[i].spn[0]);
                console.log("spn:" +spn);
                
                var SPNClass = NCMB.Object.extend("spn");
                var spnClass = new SPNClass();    
                spnClass.set("NUM", spn);
                spnClass.save(null, {
                    success: function(object) {// 保存完了後に実行される
                    },
                    error: function(object, error) {// エラー時に実行される
                    }
                });
                
                $("#x-list").append("<li>" + "<p><h3>" + spn + "</h3>"); 
                clickInput = "#" + clickInput ;
                $(clickInput).click({name : spnl},reserve);
                $("#x-list").append("</p>" );
                $("#x-list").append("</li>" );
                $("#x-list").listview('refresh');
             
            }
           
        } else {
            // エラー処理
           
        }
    }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?