tetsunosukeのnotebook

tetsunosukeのメモです

べつやくメソッドなグラフ+Twitter+Yahoo!形態素解析をためしてみた

Twitterのfriendsのupdateから形態素解析して人気ベスト10の名詞をグラフ化するよ。(ソース適当ごめんなさい)

#!/usr/local/bin/php
<?php
define('EXTLIB', dirname(__FILE__) . '/extlib');
set_include_path(get_include_path() . PATH_SEPARATOR . EXTLIB);
require_once 'HTTP/Request.php';
require_once 'Services/Twitter.php';
require_once 'XML/Unserializer.php';
define('YDN_PARSE_API_URL', 'http://api.jlp.yahoo.co.jp/MAService/V1/parse');
define('YDN_PARSE_API_ID' , 'appid');

$twitter = new Services_Twitter('userid', 'password');
$twitter->enableJsonConvert();
$friends = $twitter->getFriendsTimeline();

$sentence = "";
foreach($friends as $friend){
    $text = $friend['text'];
    $sentence .= " " . $text;
}



$ydn =& new HTTP_Request(YDN_PARSE_API_URL);

$ydn->setMethod(HTTP_REQUEST_METHOD_POST);
$ydn->addPostData('appid', YDN_PARSE_API_ID);
$ydn->addPostData('filter',"9");
$ydn->addPostData('ma_filter',"9|10");
$ydn->addPostData('sentence', $sentence);
$ydn->addPostData('results', 'ma,uniq');
$res = $ydn->sendRequest();
if(PEAR::isError($res)){
    die($res->getMessage());
}
$xml =  $ydn->getResponseBody();
$options = array('complexType' => 'array');
$obj = new XML_Unserializer($options);
$result = $obj->unserialize($xml, false);
$res_array = $obj->getUnserializedData();
$words = $res_array['uniq_result']['word_list']['word'];
$i = 0;
$data = array();
foreach($words as $word){
    if( $i < 10){
        $data[] = array('count' => $word['count'], 'word' => $word['surface']);
    }
    $i++;
}

// betsuyaku

define('GRAPH_API_URL', 'http://graph.heartrails.com/api/');
$bet =& new HTTP_Request(GRAPH_API_URL);
$bet->setMethod(HTTP_REQUEST_METHOD_GET);
for($i=0; $i < 10; $i++){
    $bet->addQueryString("value$i", $data[$i]['count']);
    $bet->addQueryString("text$i", $data[$i]['word']);
    $bet->addQueryString("font","hui");
    $bet->addQueryString('title_top','twitter! + Y!');
    $bet->addQueryString('title_bottom', date('Y/m/d H:i:s'));
}
$res = $bet->sendRequest();
if(PEAR::isError($res)){
    die($res->getMessage());
}

$location = $bet->getResponseHeader("location");
echo $bet->getResponseBody();
echo $location;
sleep(5);
$res = $bet->sendRequest();
$location = $bet->getResponseHeader("location");
echo $location;
$twitter->setUpdate($location);
exit;
?>