3
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.

Perl + Viralheatのサンプル

Last updated at Posted at 2014-04-09

ViralheatのSentiment Analysis APIを使ってセンチメント分析(感情分析)を行う簡単なPerlスクリプトのサンプルを書きました。

"YOUR API KEY HERE"にViralheatのSentiment Analysis APIのAPIキーを入れてください。

Perlモジュールは

JSON

URI::Escape

LWP::Simple

を使っています。

*JSONモジュールはJSON::XSでも大丈夫です。

標準入力から分析したい文章を入れてください。

他にsleepが必要ですが、Viralheatに問い合わせてセンチメント分析をするためのサンプルです。


use JSON;
use URI::Escape;
use LWP::Simple;

my $apikey="YOUR API KEY HERE";

my $input = <>; #標準入力からのテキスト (英語のみ入力してください。)

my $text = uri_escape($input);#標準入力からのテキストをURI エスケープする

my $url = "http://www.viralheat.com/api/sentiment/review.json?text=$text&api_key=$apikey";#ViralheatのSentiment Analysis JSON APIのURL

my $content = get $url;
die "Couldn't get $url" unless defined $content; #エラー処理

$results = decode_json($content);  #JSONをデコード

chomp($results->{text});#chompする
print "======\n";
print $results->{text}, "	", $results->{prob} , "	", $results->{mood}, "\n"; #出力する

入力した文章、確率、ムード(positiveかnegative)が出力されます。

このスクリプトを少し改造すればファイルから一行ずつ感情分析ができるようになります。

Twitterなどの感情分析頑張ってみてください。

重要:ViralheatのSentiment Analysis APIは英語の文章でないと分析できないので注意してください。

3
2
1

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
3
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?