Yahoo Web API の返り値を XML で受け取って Perl で処理をする

Yahoo Web API の返り値を JSON で受け取って Perl で処理をする」の改良版。

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Encode;
use LWP::UserAgent;
use Data::Dumper;
use URI::Escape;
use XML::Simple;

my $yahoo_app_id = 'your_app_id';
my $agent        = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
my $apibaseuri   = 'http://jlp.yahooapis.jp/KeyphraseService/V1/extract';
my $referrer     = 'http://developer.yahoo.co.jp/';

my $text = <<'EOS';
第1条(はじめに)
この利用規約は、株式会社はてな(以下「当社」)が本サイト上で提供する全てのサービス(以下「本サービス」)における利用条件を定めるものです。ユーザーのみなさま(以下「ユーザー」)には、本規約に従い本サービスをご利用いただきます。
本サービス内には、本規約以外に「ヘルプ」や各種ガイドラインにおいて、本サービスの利用方法や注意書きが提示されています。これらも本規約の一部を実質的に構成するものですので、合わせてお読みください。
EOS

# 文字列は URL エンコードしておく
my $escaped = uri_escape(encode('utf-8', $text));
my $url = sprintf('%s?output=xml&appid=%s&sentence=%s', $apibaseuri, $yahoo_app_id, $escaped);

my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new('GET', $url);
$ua->agent($agent);
$req->referer($referrer);
my $response = $ua->request($req);

unless ($response->is_success) {
    print 'Request: ' . $url, "\n";
    warn 'Failed to request to WEB API.', "\n";
} else {
    # see at http://iandeth.dyndns.org/mt/ian/archives/000589.html
    $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
    # see at http://search.cpan.org/dist/XML-Simple/lib/XML/Simple.pm#ForceArray_=%3E_1_#_in_-_important
    my $xmlparser = XML::Simple->new(ForceArray => ['Result']);
    my $results   = $xmlparser->XMLin(decode_utf8($response->content));

    if ($results->{Error}->{Message}) {
        warn 'Error occured: ', $results->{Error}->{Message}, "\n";
    } else {
        # $results->{Result} の中身を確認する
        # print Dumper($results->{Result});
        # 配列のリファレンスをデリファレンスする
        foreach my $result (@{$results->{Result}}) {
            printf("Keyword: %s. Score: %s\n", encode('utf-8', $result->{Keyphrase}), $result->{Score});
        }
    }
}

実行結果

Keyword: 本サービス. Score: 100
Keyword: ユーザー. Score: 98
Keyword: 本規約. Score: 95
Keyword: 各種ガイドライン. Score: 92
Keyword: 注意書き. Score: 81
Keyword: 株式会社はてな. Score: 77
Keyword: 以下. Score: 73
Keyword: ヘルプ. Score: 61
Keyword: お読み. Score: 61
Keyword: みなさま. Score: 59
Keyword: これら. Score: 56
Keyword: 利用規約. Score: 53
Keyword: 提示. Score: 53
Keyword: ご利用. Score: 52
Keyword: 当社. Score: 52
Keyword: 本規約以外. Score: 51
Keyword: 利用条件. Score: 50
Keyword: 本サービス内. Score: 50
Keyword: はじめ. Score: 45
Keyword: 利用方法. Score: 45

Dumper の出力結果

$VAR1 = [
  {
    'Score' => '100',
    'Keyphrase' => "\x{672c}\x{30b5}\x{30fc}\x{30d3}\x{30b9}"
  },
  {
    'Score' => '98',
    'Keyphrase' => "\x{30e6}\x{30fc}\x{30b6}\x{30fc}"
  },
  {
    'Score' => '95',
    'Keyphrase' => "\x{672c}\x{898f}\x{7d04}"
  },
  {
    'Score' => '92',
    'Keyphrase' => "\x{5404}\x{7a2e}\x{30ac}\x{30a4}\x{30c9}\x{30e9}\x{30a4}\x{30f3}"
  },
  {
    'Score' => '81',
    'Keyphrase' => "\x{6ce8}\x{610f}\x{66f8}\x{304d}"
  },
  {
    'Score' => '77',
    'Keyphrase' => "\x{682a}\x{5f0f}\x{4f1a}\x{793e}\x{306f}\x{3066}\x{306a}"
  },
  {
    'Score' => '73',
    'Keyphrase' => "\x{4ee5}\x{4e0b}"
  },
  {
    'Score' => '61',
    'Keyphrase' => "\x{30d8}\x{30eb}\x{30d7}"
  },
  {
    'Score' => '61',
    'Keyphrase' => "\x{304a}\x{8aad}\x{307f}"
  },
  {
    'Score' => '59',
    'Keyphrase' => "\x{307f}\x{306a}\x{3055}\x{307e}"
  },
  {
    'Score' => '56',
    'Keyphrase' => "\x{3053}\x{308c}\x{3089}"
  },
  {
    'Score' => '53',
    'Keyphrase' => "\x{5229}\x{7528}\x{898f}\x{7d04}"
  },
  {
    'Score' => '53',
    'Keyphrase' => "\x{63d0}\x{793a}"
  },
  {
    'Score' => '52',
    'Keyphrase' => "\x{3054}\x{5229}\x{7528}"
  },
  {
    'Score' => '52',
    'Keyphrase' => "\x{5f53}\x{793e}"
  },
  {
    'Score' => '51',
    'Keyphrase' => "\x{672c}\x{898f}\x{7d04}\x{4ee5}\x{5916}"
  },
  {
    'Score' => '50',
    'Keyphrase' => "\x{5229}\x{7528}\x{6761}\x{4ef6}"
  },
  {
    'Score' => '50',
    'Keyphrase' => "\x{672c}\x{30b5}\x{30fc}\x{30d3}\x{30b9}\x{5185}"
  },
  {
    'Score' => '45',
    'Keyphrase' => "\x{306f}\x{3058}\x{3081}"
  },
  {
    'Score' => '45',
    'Keyphrase' => "\x{5229}\x{7528}\x{65b9}\x{6cd5}"
  }
];