LoginSignup
11
18

More than 5 years have passed since last update.

Androidに使える機械学習用フレームワーク

Last updated at Posted at 2016-12-20

有名どころですが、使い易いAPIといえばGoogleCloudPlatformですね。

自然言語処理ができる、CLOUD NATURAL LANGUAGE APIを利用してみたいと思います。
ドキュメントを見ながらAndroid用のサンプルを動かします。
※credental.jsonを作っても403が返ってくる場合は、ダッシュボードの方でAPIが有効になっているか確認してください。

alt

エンティティというのが恐らく特徴的な単語や固有名詞のことで、sentimentが感情値を表しています。
使ってみた感じは単純に単語の頻出度でネガポジ判定している感じではなさそうです。もう少し高度な判定はしていそうという感想です。
係り受けなどもしているようなので、基本的なことをカバーしています。これができたからどうとかいう問題ではなくて、この機能を使って何ができそうかってことを考えて使うものみたいですね

感情の判定のソースコードは以下の通りです。

SentimentFragment.java
package com.google.cloud.android.language;

import com.google.cloud.android.language.model.SentimentInfo;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.res.ResourcesCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class SentimentFragment extends Fragment {

    private static final String ARG_SENTIMENT = "sentiment";

    public static SentimentFragment newInstance() {
        final SentimentFragment fragment = new SentimentFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    private int mColorPositive;
    private int mColorNeutral;
    private int mColorNegative;

    private TextView mPolarity;
    private TextView mMagnitude;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Resources resources = getResources();
        final Resources.Theme theme = getActivity().getTheme();
        mColorPositive = ResourcesCompat.getColor(resources, R.color.polarity_positive, theme);
        mColorNeutral = ResourcesCompat.getColor(resources, R.color.polarity_neutral, theme);
        mColorNegative = ResourcesCompat.getColor(resources, R.color.polarity_negative, theme);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sentiment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mPolarity = (TextView) view.findViewById(R.id.polarity);
        mMagnitude = (TextView) view.findViewById(R.id.magnitude);
        final Bundle args = getArguments();
        if (args.containsKey(ARG_SENTIMENT)) {
            showSentiment((SentimentInfo) args.getParcelable(ARG_SENTIMENT));
        }
    }

    public void setSentiment(SentimentInfo sentiment) {
        showSentiment(sentiment);
        getArguments().putParcelable(ARG_SENTIMENT, sentiment);
    }

     //ここで感情の閾値を設定している
   //数値が高ければ高いほどポジティブである
    private void showSentiment(SentimentInfo sentiment) {
        mPolarity.setText(String.valueOf(sentiment.polarity));
        if (sentiment.polarity > 0.25) {
            mPolarity.setBackgroundColor(mColorPositive);
        } else if (sentiment.polarity > -0.75) {
            mPolarity.setBackgroundColor(mColorNeutral);
        } else {
            mPolarity.setBackgroundColor(mColorNegative);
        }
        mMagnitude.setText(String.valueOf(sentiment.magnitude));
    }

}

他にもGCPの機械学習系のサービスには以下の画像のようにたくさんあります。
製品・サービス_ _ _Google_Cloud_Platform_🔊.png

応答は若干遅いので、リアルタイムが要求されるサービスには少しキツイかなという印象です。
画像系は特にその傾向が強いですが、今回のような自然言語処理系のAPIはあまりリアルタイム性は要求されないので様々な活用方法があると思います。研究やハッカソンにも使えると思います。

他にはDeepBeliefSDKという通信を行わないで物体を識別することができる学習器を作るものがありますが、メンテナンスがされておらず一年以上経っているためあまりお勧めできません。

いろいろ探しているとQittaに以下のような記事がありました。

TensorFlowの画像認識をモバイルで動かす&その仕組み
tomoima525 2016年09月26日に投稿

Tensorflowを使いPCで先に学習器を作りAndroidで動かすような内容でした。
使いやすそうで、良さそうなのでTensorflowを使うのがリアルタイム性が重要なシステムでは主流になりそうですね。

手みじかでしたが、これからもAWSなどいろいろ出てくるだろうと思うのでウォッチが必要そうな分野ですね。

11
18
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
11
18