CodeIgniterの学習 59 - ヤフーの画像検索apiと形態素解析apiを組み合わせてお遊びアプリを作る-その4(現在のコード完成度45%)

CodeIgniterでお遊びアプリを作る4回目。

今日は日本語形態素解析を使って関連キーワード抽出機能を入れてみる。
だんだん形になってきた。

(日本語形態素解析APIの仕様はここ http://developer.yahoo.co.jp/webapi/jlp/ma/v1/parse.html


関連キーワードの抽出は画像検索APIの戻り値の 'summary' を使って抽出している。
(画像検索APIの仕様はここ http://developer.yahoo.co.jp/webapi/search/imagesearch/v1/imagesearch.html

(ヤフーでAPPIDを取らないと動きません。)

現在の画面

こんなかんじ。(JPG縮小画像)



関連キーワードを押すとそのキーワードで画像検索が走る。
GET渡しさせたくないので、関連キーワードはsubmitボタンにしてPOST渡しで逃げた。


コード

今日も全部貼っておく。




1:コントローラ… application/controllers/yaimg.php

形態素解析結果の取得処理を追加。

<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * Yahooの画像検索APIを使ってみるテスト
 */
/**
 * 
 * @author : dix3 
 * @link http://d.hatena.ne.jp/dix3/
 */

class Yaimg extends Controller {
    // コンストラクタ
    function Yaimg()
    {
        parent :: Controller(); //親のコンストラクタを呼ぶ
        //$this -> load -> helper( array( 'form', 'html', 'text', 'string' ) ); //ヘルパのロード
        $this -> load -> helper( array( 'url','form', 'html', 'text', 'string','date' ) ); //ヘルパのロード
        $this -> load -> library( 'session' ); //セッションライブラリのロード
        $this -> load -> library( 'form_validation' ); //フォームバリデーションライブラリのロード
        $this -> load -> library( 'lib_yaimg' ); //自作ライブラリのロード
        $this -> load -> model( 'Mod_yaimg' ); //モデルのロード
    } 
    // デフォルトインデックス
    function index( $offset_num = 0 )
    {
        $this -> session -> set_userdata( 'yaimg', array() );
        $this -> page( $offset_num );
    }
    function page( $offset_num = 0 ) // オフセットだけ引数で渡す。キーワードはPOSTデータまたはセッションデータに限定
    {
        $data = array(); //ビュー側に渡す配列
        $data = $this -> Mod_yaimg -> set_init_data(); //画面に固定値のデータをセット
        $data['urchin_tag'] = ( $this -> Mod_yaimg -> get( 'uacct_no' ) ) ? $this -> load -> view( 'urchin_tag', array( 'uacct_no' => $this -> Mod_yaimg -> get( 'uacct_no' ) ) , true ) : '' ; //UACCT_NOをurchinタグのビューに渡す
        $data['result'] = array( 'body' => array(), 'msg' => '', 'page_link' => '', 'sentence' => array() ); //検索結果の初期値
         
        // バリデーションのルール設定
        $this -> form_validation -> set_rules( 'keyword', $this -> Mod_yaimg -> get( 'input_keyword_value' ), 'trim|required|min_length[2]|max_length[64]|xss_clean' );

        $keyword = $this -> _get_keyword(); //検索キーワードの取得(POST経由又はセッション経由)
        if ( $keyword ) {
            $data['keyword'] = $keyword;
            $this -> session -> set_userdata( 'yaimg', array( 'keyword' => $keyword ) ); //セッションに検索キーワードを保持 
            // apiに接続して結果を取得する
            if ( $this -> Mod_yaimg -> query_api( array( 'keyword' => $keyword , 'offset_num' => $offset_num ) ) ) {
                $ir = $this -> Mod_yaimg -> get( 'image_result' );
                $data['result']['body'] = $ir['result']; //結果データ
                $data['result']['page_link'] = $this -> _make_page_link( $ir['attributes'] ); //ページリンクタグの生成
                $data['result']['msg'] = $this -> _make_info_label( $ir['attributes'] ); //件数情報表示
                if ( $this -> Mod_yaimg -> get( 'parse_api_status' ) ) {
                    $pr = $this -> Mod_yaimg -> get( 'parse_result' ); //形態素解析結果の取得
                    $data['result']['sentence'] = $pr;
                }
            }else {
                $data['result']['msg'] = $this -> Mod_yaimg -> get( 'msg_no_result' );
            }
        }else {
            $this -> session -> set_userdata( 'yaimg', array() );
        } 
        // ビューにデータを渡す
        $this -> load -> view( 'yaimg_index', $data );
    } 
    // 検索キーワードの取得
    function _get_keyword()
    {
        $keyword = ''; 
        // post時とページネーション時の振り分け
        if ( $this -> input -> post( 'submit' ) ) {
            // バリデーション処理の実行
            if ( ( TRUE === $this -> form_validation -> run() ) &&
                    ( $this -> input -> post( 'keyword', true ) !== $this -> Mod_yaimg -> get( 'default_keyword' ) ) ) {
                $keyword = $this -> input -> post( 'keyword', true );
            }
        }else {
            if ( $sess_data = $this -> session -> userdata( 'yaimg' ) ) {
                if ( isset( $sess_data['keyword'] ) ) { // セッションにキーワードがセットされている
                    $keyword = $this -> input -> xss_clean( $sess_data['keyword'] );
                }
            }
        }
        return $keyword;
    } 
    // ページリンクタグの生成
    function _make_page_link( $attributes )
    { 
        // ページネーションライブラリのロード
        $this -> load -> library( 'pagination' );
        $config['base_url'] = '/yaimg/page/';
        $total_rows = ( int ) $attributes['total']; 
        // 件数制限
        $total_rows = ( ( int ) $this -> Mod_yaimg -> get( 'image_api_max_rows' ) <= $total_rows ) ? ( int ) $this -> Mod_yaimg -> get( 'image_api_max_rows' ) : $total_rows;
        $config['total_rows'] = $total_rows ;
        $config['per_page'] = ( int ) $this -> Mod_yaimg -> get( 'image_api_results_count' );
        $config['num_links'] = $this -> Mod_yaimg -> get( 'num_links' );
        $config['first_link'] = $this -> Mod_yaimg -> get( 'first_link' );
        $config['last_link'] = $this -> Mod_yaimg -> get( 'last_link' );
        $config['prev_link'] = $this -> Mod_yaimg -> get( 'prev_link' );
        $config['next_link'] = $this -> Mod_yaimg -> get( 'next_link' );
        $this -> pagination -> initialize( $config );
        return $this -> pagination -> create_links();
    } 
    // 件数情報表示
    function _make_info_label( $attributes )
    {
        $ret = '';
        $ilbl = $this -> Mod_yaimg -> get( 'info_label' );
        $total = number_format( $attributes['total'] );
        $position_h = number_format( $attributes['position'] );
        $position_f = number_format( ( int )$attributes['position'] + ( int ) $attributes['count'] -1 ) ;

        $ret = sprintf( $ilbl, $total, $position_h, $position_f ) ; //総件数情報
        if ( ( int )$this -> Mod_yaimg -> get( 'image_api_max_rows' ) <= ( int ) $attributes['total'] ) {
            $ret .= $this -> Mod_yaimg -> get( 'info_label_notice' ); //件数制限のメッセージ
        }
        return $ret;
    }
}

/**
 * End of file yaimg.php
 */
/**
 * Location: ./application/controllers/yaimg.php
 */

 ?>



2:ビュー… application/views/yaimg_index.php

関連キーワード検索のsentence_link部分を追加。

関連キーワードは、アンカーでpost用javascript生成ではなくて、submitボタンで逃げてます。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<title><?= $svs_title ?></title>
    <?= meta('Content-Type','text/html; charset=utf-8','http-equiv') ?>
    <?= meta('Content-language',$header_lang,'http-equiv') ?>
    <?= meta('Pragma','no-cache','http-equiv') ?>
    <?= meta('Cache-Control','no-cache','http-equiv') ?>
    <?= meta('Expires','Thu, 01 Dec 1994 23:59:59 GMT','http-equiv') ?>
    <?= meta('Distribution','Global') ?>
    <?= meta('Author','dix3') ?>
    <?= meta('Robots','index,nofollow') ?>
    <?= $header_js ?>
    <?= $header_css ?>
</head>
<body>
    <div>
        <?= heading($svs_description,3) ?>
        <?= form_open('yaimg') ?>
            <?= form_input(array('name'=>'keyword','style'=>'width:250px;','onclick'=>"javascript:this.value='';"),set_value('keyword',$keyword)) ?>
            <?= form_submit('submit',$submit_button_value) ?>
            <?= validation_errors('<div class="error">','</div>'); ?>
            <?php if($result['msg']){ ?><div class='result_msg'><?= $result['msg'] ?></div><?php } ?>
        <?= form_close() ?>
    </div>
    <hr>
    <div class='sentence_link'>
        <div class='bold'>
        <?php if($result['sentence']){ echo $sentence_link_label; } ?>
        </div>
        <div>
        <?= form_open('yaimg') ?>
        <?php 
          $cnt = 0;
          foreach($result['sentence'] as $k => $v){
            echo form_submit('keyword',$v['sentence'],"class=\"keyword\" title=\"{$v['count']}\"") . "\n";
            $cnt++;
          }
          if($cnt){
            echo form_hidden('submit','submit');
          }
        ?>
        <?= form_close() ?>
        </div>
    </div>
    <div class='page_link'><?= $result['page_link'] ?></div>
    <div class='result_body'>
     <?php
        foreach($result['body'] as $k => $v){
            $img_tag = img(array('src'=>$v['thumbnail']['url'],
                       'style'=>"width:{$v['thumbnail']['width']};height:{$v['thumbnail']['height']};",
                       'title'=>form_prep($v['summary'].' '.$v['refererurl'].' '.$v['filesize']),
                       'alt'=>form_prep($v['title']),
                       ));
            echo anchor($v['refererurl'],$img_tag,array('target'=>'_blank'))."\n";
        }
     ?>
    </div>
    <div class='page_link'><?= $result['page_link'] ?></div>
    <hr>
<?= $urchin_tag ?>
</body>
</html>



3:モデル… application/models/mod_yaimg.php

形態素解析部分のソースを変更。

<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * 
 * @author : dix3 
 * @link http://d.hatena.ne.jp/dix3/
 */

class Mod_yaimg extends Model {
    var $mod_config;
    var $image_api_arr = array();
    var $parse_api_arr = array();
    var $image_result = array();
    var $parse_result = array();
    var $image_api_status = false;
    var $parse_api_status = false;
    var $sentence = ''; 
    // コンストラクタ
    function Mod_yaimg()
    {
        parent :: Model();
        $this -> _init(); //初期化
    } 
    // 初期化
    function _init()
    {
        $this -> load -> helper( 'html' );
        $this -> load -> helper( 'yaimg' ); 
        // $this -> load -> library( 'curl' ); //curlライブラリのロード
        $this -> load -> config( 'yaimg_config', true );
        $this -> mod_config = $this -> config -> item( 'yaimg_config' );
    } 
    // 画面に固定値のデータをセット
    function set_init_data()
    {
        $arr = array( 'svs_title' ,
            'svs_description' ,
            'header_lang' ,
            'header_js' ,
            'header_css' ,
            'input_keyword_value',
            'keyword' ,
            'submit_button_value' , 
            'sentence_link_label' , 
            );
        $data = array();
        foreach( $arr as $v ) {
            $data[$v] = $this -> get( $v );
        }
        return $data;
    } 
    // 値取得
    function get( $v )
    {
        switch ( $v ) {
            case 'header_js':
                return header_js( $this -> _get_mod_config( $v ) ); //yaimgヘルパのheader_jsタグ
                break;
            case 'header_css':
                return link_tag( $this -> _get_mod_config( $v ) ); //htmlヘルパのlink_tag
                break;
            case 'image_api_arr':
                return $this -> image_api_arr; //画像取得結果の配列
                break;
            case 'parse_api_arr':
                return $this -> parse_api_arr; //形態素解析取得結果の配列
                break;
            case 'image_result':
                return $this -> image_result; //画像取得結果を扱いやすい形に変換した配列
                break;
            case 'parse_result':
                return $this -> parse_result; //形態素解析結果を扱いやすい形に変換した配列
                break;
            case 'image_api_status':
                return $this -> image_api_status; //画像検索結果の取得件数有り真偽
                break;
            case 'parse_api_status':
                return $this -> parse_api_status; // 形態素解析検索結果の取得件数有り真偽
                break;
            case 'sentence':
                return $this -> sentence; // 形態素解析用文字列
                break;
            default:
                return $this -> _get_mod_config( $v ); //configファイルの設定値
                break;
        }
    } 
    // configファイルから設定値を読みとる
    function _get_mod_config( $v )
    {
        $v = strtoupper( $v );
        return isset( $this -> mod_config[$v] ) ? $this -> mod_config[$v] : '';
    } 
    // 画像取得結果の配列をセット
    function _set_image_api_arr( $arr = array() )
    {
        if ( is_array( $arr ) ) {
            $this -> image_api_arr = $arr;
        }
    } 
    // 形態素解析取得結果の配列をセット
    function _set_parse_api_arr( $arr = array() )
    {
        if ( is_array( $arr ) ) {
            $this -> parse_api_arr = $arr;
        }
    } 
    // 画像検索結果の戻り値を扱いやすい形に加工してセットする
    function _set_image_result()
    {
        $ret = false;
        $arr = $this -> get( 'image_api_arr' );
        if ( isset( $arr['@attributes'] ) ) {
            $data['attributes']['total'] = $arr['@attributes']['totalresultsavailable'];
            $data['attributes']['count'] = $arr['@attributes']['totalresultsreturned'];
            $data['attributes']['position'] = $arr['@attributes']['firstresultposition'];
            $data['result'] = isset( $arr['result'] ) ? $arr['result'] : array();
            if ( ( int ) $data['attributes']['total'] > 0 ) {
                $this -> _set_sentence(); //形態素解析用文字列のセット
                $this -> _set_image_api_status( TRUE ); //取得件数有り真偽
                $ret = true;
            }else {
                $this -> _set_image_api_status( FALSE ); //取得件数有り真偽
            }
        }else {
            $data['attributes']['total'] = '';
            $data['attributes']['count'] = '';
            $data['attributes']['position'] = '';
            $data['result'] = array();
            $this -> _set_image_api_status( FALSE ); //取得件数有り真偽
        }
        $this -> image_result = $data;
        return $ret;
    } 
    // 形態素解析結果の戻り値を扱いやすい形に加工してセットする
    function _set_parse_result()
    {
        $ret = false;
        $arr = $this -> get( 'parse_api_arr' );
        $data = array();
        if ( isset( $arr['uniq_result']['word_list']['word'] ) ) {
            $regexp_ng1 = '#' . $this -> get( 'ng_parse_word1' ) . '#iu';
            $w =  str_replace( ',', '|', rtrim( trim( $this -> get( 'ng_parse_word2' ), ',' ) ) ) ;
            $regexp_ng2 = "#^({$w})$#iu";
            foreach( ( array ) $arr['uniq_result']['word_list']['word'] as $v ) {
                if(mb_strlen($v['surface']) < 2 ){
                    continue; //1文字単語を除外
                }
                if ( preg_match( $regexp_ng1, $v['surface'] ) ) {
                    continue; //不要な単語を除外
                }
                if ( preg_match( $regexp_ng2, $v['surface'] ) ) {
                    continue; //不要な単語を除外
                }
                $data[] = array( "sentence" => strip_tags($v['surface']), "count" => $v['count'], "pos" => $v['pos'] );
                $ret = true;
            }
            if ( $ret ) {
                $this -> _set_parse_api_status( TRUE ); //取得件数有り真偽
            }
        }
        $this -> parse_result = $data;
        return $ret;
    } 
    // 形態素解析用文字列のセット
    function _set_sentence()
    {
        $arr = $this -> get( 'image_api_arr' );
        $s_arr = array(); 
        // var_dump($arr);
        if ( isset( $arr['result'] ) ) {
            foreach( ( array ) $arr['result'] as $k => $v ) {
                if ( isset( $v['summary'] ) && $v['summary'] ) {
                    $s_arr[] = $v['summary'];
                }
            } 
            // var_dump($s_arr);
            $str = implode( ',', $s_arr );
        }
        $this -> sentence = $str;
    } 
    // 画像検索結果の取得件数有り真偽をセット
    function _set_image_api_status( $flg )
    {
        $this -> image_api_status = ( $flg ) ? TRUE : FALSE ;
    } 
    // 形態素解析検索結果の取得件数有り真偽をセット
    function _set_parse_api_status( $flg )
    {
        $this -> parse_api_status = ( $flg ) ? TRUE : FALSE ;
    } 
    // apiに接続して結果を取得する
    function query_api( $params = array() )
    {
        $keyword = isset( $params['keyword'] ) ? $this -> input -> xss_clean( strip_tags( $params['keyword'] ) ) : '';
        $offset_num = isset( $params['offset_num'] ) ? $this -> input -> xss_clean( strip_tags( $params['offset_num'] ) ) : '';

        if ( $keyword ) {
            $this -> _query_image_api( $keyword , $offset_num ); //画像検索apiに接続
        }
        $sentence = $this -> get( 'sentence' ); //形態素解析用文字列の取得
        if ( $sentence ) {
            $this -> _query_parse_api( $sentence ); //形態素解析apiに接続
        }
        return $this -> get( 'image_api_status' ); //画像が見つかったらtrue
    } 
    // 画像検索apiに接続して結果を取得する
    function _query_image_api( $keyword = '', $offset_num = 0 )
    {
        if ( !( $url = $this -> _get_ya_url( 'image_api', $keyword , $offset_num ) ) ) {
            return false;
        }
        usleep( 5000 ); //連続してつながないようにウエイトが必要 
        // $data = $this -> curl -> get( $url ); //curlライブラリ経由でリクエストを投げレスポンスデータを取得
        if ( $data = @file_get_contents( $url ) ) { // 本当はステータスを取りたいけど
            if ( $data ) {
                $obj = @simplexml_load_string( $data ); //xmlのパース 
                // 取り回しが面倒なので配列に変換
                $this -> _set_image_api_arr( $this -> _convert_obj_to_arr( $obj ) );
            }
            return $this -> _set_image_result(); // 画像検索結果の戻り値を扱いやすい形に加工してセットする
        }else {
            return false;
        }
    } 
    // 形態素解析apiに接続して単語をばらした結果を配列で取得する
    function _query_parse_api( $sentence = '' )
    {
        if ( !( $url = $this -> _get_ya_url( 'parse_api', $sentence ) ) ) {
            return false;
        }
        usleep( 5000 ); //連続してつながないようにウエイトが必要 
        // $data = $this -> curl -> get( $url ); //curlライブラリ経由でリクエストを投げレスポンスデータを取得
        $data = @file_get_contents( $url );
        if ( $data ) {
            $obj = @simplexml_load_string( $data ); //xmlのパース 
            // 取り回しが面倒なので配列に変換
            $this -> _set_parse_api_arr( $this -> _convert_obj_to_arr( $obj ) );
            return $this -> _set_parse_result(); // 形態素解析結果の戻り値を扱いやすい形に加工してセットする
        }else {
            return false;
        }
    } 
    // 面倒なのでオブジェクトを再帰的に配列に変換
    function _convert_obj_to_arr( $p )
    {
        if ( is_object( $p ) ) {
            $ret = array();
            $arr = get_object_vars( $p );
            foreach( $arr as $k => $v ) {
                $key = strtolower( $k ); //キーは小文字で統一
                if ( empty( $v ) ) {
                    $ret[$key] = '';
                }else {
                    $ret[$key] = $this -> _convert_obj_to_arr( $v );
                }
            }
        }elseif ( is_array( $p ) ) {
            $ret = array();
            foreach( $p as $k => $v ) {
                $key = strtolower( $k ); //キーは小文字で統一
                if ( empty( $v ) ) {
                    $ret[$key] = '';
                }else {
                    $ret[$key] = $this -> _convert_obj_to_arr( $v );
                }
            }
        }else {
            $ret = $p;
        }
        return $ret;
    } 
    // URLの組み立て
    function _get_ya_url( $ptn, $str , $offset_num = 0 )
    {
        if ( !$str ) {
            return '';
        }
        $str = $this -> input -> xss_clean( strip_tags( $str ) );
        $appid = $this -> get( 'appid' );
        switch ( $ptn ) {
            case 'parse_api':// 形態素解析用URLの組み立て
                //アルファベットと数字のみの文字列をあらかじめ除外
                $str = preg_replace('#[a-zA-Z0-9]+#iu',' ',$str);
                $enc_str = urlencode( mb_substr( $str , 0, 960 ) );//適当に短くしないとリクエストが長すぎて蹴られる
                $url = $this -> get( 'parse_api_url' );
                $url = str_replace( '%appid%', $appid, $url );
                $url = str_replace( '%sentence%', $enc_str, $url );
                return $url;
                break;
            case 'image_api':// 画像検索用URLの組み立て
                $enc_str = urlencode( $str );
                $url = $this -> get( 'image_api_url' );
                $url = str_replace( '%appid%', $appid, $url );
                $url = str_replace( '%keyword%', $enc_str, $url );
                $site_lock_url_list = $this -> get( 'site_lock_url_list' ); //検索対象のURLを限定
                if ( $site_lock_url_list ) {
                    $s_arr = explode( ',', trim( str_replace( ' ', '', $site_lock_url_list ) ) );
                    $image_api_site_lock_url = '&site=' . implode( '&site=', $s_arr );
                    $url = str_replace( '%image_api_site_lock_url%', $image_api_site_lock_url, $url );
                }
                $arr = array( 'image_api_adult_ok',
                    'image_api_similar_ok',
                    'image_api_language',
                    'image_api_country',
                    'image_api_results_count', 
                    );
                foreach( $arr as $v ) {
                    $url = str_replace( "%{$v}%", $this -> get( $v ), $url );
                } 
                // 取得開始カウントは正の整数のみ
                if ( !preg_match( '#^\d+$#', $offset_num ) || ( ( int ) $offset_num < 1 ) ) {
                    $start_num = ( int ) $this -> get( 'image_api_start_num' );
                }else {
                    $start_num = ( int ) $offset_num; //パラメータstartの調整
                }
                $url = str_replace( "%image_api_startcount_num%", $start_num, $url ); 
                // echo($url);
                return $url;
                break;
            default:
                return '';
                break;
        }
    }
} //endofclass
/**
 * End of file mod_yaimg.php
 */
/**
 * Location: ./application/models/mod_yaimg.php
 */

 ?>



4:ライブラリ… application/libraries/lib_yaimg.php

前回と変わらず。

<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/*
* @author: dix3
* @link : http://d.hatena.ne.jp/dix3/
*/
class Lib_yaimg {
    function Lib_yaimg()
    {
    }
} //endofclass
/**
 * End of file lib_yaimg.php
 */
/**
 * Location: ./application/libraries/lib_yaimg.php
 */

 ?>



5:ヘルパー… application/helpers/yaimg_helper.php

前回と変わらず。ヘッダのjavascriptを読みこむ用。

<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/*
* @author: dix3
* @link : http://d.hatena.ne.jp/dix3/
*/
if ( ! function_exists( 'header_js' ) ) {
    // scriptの読み込みタグ生成
    // 例: header_js('js/hogehoge.js');で
    // <script src="js/hogehoge.js" type="text/javascript"></script>
    // を作る
    function header_js( $src_str = '', $charset = '' )
    {
        $ret = '';
        if ( $src_str ) {
            if ( $charset ) {
                $cstr = ' charset="' . $charset . '" ' ;
            }else {
                $cstr = '' ;
            }
            $ret .= '<script language="javascript" type="text/javascript" src="' . $src_str . '"' . $cstr . "></script>\n";
        }
        return $ret;
    }
}

/* End of file yaimg_helper.php */
/* Location: ./application/helpers/yaimg_helper.php */

 ?>



6:設定ファイル… application/config/yaimg.php

いくつか変更有り。

<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 *
 * @author : dix3
 * @link : http://d.hatena.ne.jp/dix3/
 */

$config['SVS_TITLE'] = 'ヤフー画像検索 WEBAPIテスト'; //タイトル
$config['SVS_NAME'] = 'yaimg_search'; //サービス名
$config['SVS_DESCRIPTION'] = 'ヤフー画像検索と形態素解析の実験'; //ページタイトル
$config['HEADER_LANG'] = 'ja'; //Content-language
$config['HEADER_JS'] = 'js/yaimg.js'; //javascriptの相対パス
$config['HEADER_CSS'] = 'css/yaimg.css'; //cssの相対パス
$config['INPUT_KEYWORD_VALUE'] = 'キーワード'; //キーワード欄の名称
$config['SUBMIT_BUTTON_VALUE'] = 'ぽちっとな!画像検索'; //サブミットボタン名
$config['KEYWORD'] = 'なんかいれて'; //キーワード欄のデフォルト値
$config['SENTENCE_LINK_LABEL'] = '関連キーワード:';
$config['INFO_LABEL'] = '総件数:%s件 %s件目 〜 %s件目を表示'; //情報用ラベル
$config['INFO_LABEL_NOTICE'] = ' (検索結果の表示は999件が上限です。)'; //情報用ラベル
$config['FIRST_LINK'] = '最初のページ'; //ページネーション用
$config['LAST_LINK'] = '最後のページ'; //ページネーション用
$config['PREV_LINK'] = '&lt;前へ '; //ページネーション用
$config['NEXT_LINK'] = '&gt;次へ'; //ページネーション用
$config['NUM_LINKS'] = '5'; //ページネーション用

$config['UACCT_NO'] = ''; //google-analyticsのurchinのタグ

// yahooのアプリケーションID appid(自分で取得する)
$config['APPID'] = ''; //yahooのappid(自分でとる)
// API接続URLのテンプレート

// yahooの形態素解析apiのURL
#$config['PARSE_API_URL'] = 'http://api.jlp.yahoo.co.jp/MAService/V1/parse?appid=%appid%&results=uniq&uniq_filter=9&sentence=%sentence%';
$config['PARSE_API_URL'] = 'http://jlp.yahooapis.jp/MAService/V1/parse?appid=%appid%&results=uniq&uniq_filter=9&sentence=%sentence%';
// yahooの画像検索apiのURL
#$config['IMAGE_API_URL'] = 'http://api.search.yahoo.co.jp/ImageSearchService/V1/imageSearch?appid=%appid%&query=%keyword%&adult_ok=%image_api_adult_ok%&similar_ok=%image_api_similar_ok%&language=%image_api_language%&country=%image_api_country%%image_api_site_lock_url%&results=%image_api_results_count%&start=%image_api_startcount_num%';
$config['IMAGE_API_URL'] = 'http://search.yahooapis.jp/ImageSearchService/V1/imageSearch?appid=%appid%&query=%keyword%&adult_ok=%image_api_adult_ok%&similar_ok=%image_api_similar_ok%&language=%image_api_language%&country=%image_api_country%%image_api_site_lock_url%&results=%image_api_results_count%&start=%image_api_startcount_num%';


// 画像検索APIのパラメータ
$config['IMAGE_API_ADULT_OK'] = '1'; //アダルト OK:1,NG:0
$config['IMAGE_API_SIMILAR_OK'] = '1'; //SIMILAR OK:1,NG:0
$config['IMAGE_API_LANGUAGE'] = 'ja'; //言語 ja
$config['IMAGE_API_COUNTRY'] = 'ja'; //国 ja
$config['IMAGE_API_RESULTS_COUNT'] = '50'; //一回の問い合わせのデータ取得件数
$config['IMAGE_API_START_NUM'] = '1'; //検索結果のn番目から表示
$config['IMAGE_API_MAX_ROWS'] = '1000'; //1000件を超えられないAPIの仕様
$config['SITE_LOCK_URL_LIST'] = ''; //画像検索を特定のサイト内に限定するときに指定
// $config['SITE_LOCK_URL_LIST'] = 'hatena.ne.jp,example.com';//画像検索を特定のサイト内に限定するときに指定。カンマ区切りで複数指定可
// 形態素解析での抽出対象外ワード、正規表現で指定
$config['NG_PARSE_WORD1'] = '[-+_0-9a-zA-Z\s0123456789]+';//数字のみアルファベットのみを除外
// 形態素解析での抽出対象外ワード、カンマ区切りで指定
$config['NG_PARSE_WORD2'] = '上記,名前,名無し,投稿,片手,クリック,サイト,ジャンプ,リンク,これ,それ,あれ,こと,どれ,ここ,そこ,こちら,先週,今週,来週,版,先月,今月,来月';
// 合致したデータが無いときのメッセージ
$config['MSG_NO_RESULT'] = '該当するデータは見つかりませんでした。';
/**
 * End of file yaimg.php
 */
/**
 * Location: ./application/config/yaimg.php
 */

 ?>


7:javascriptファイル… ドキュメントルート/js/yaimg.js

前回と変わらず。(hogeは動作確認用ダミー。)

//@author : dix3
//@url : http://d.hatena.ne.jp/dix3

var hoge=function(){
  try{
    window.alert('hoge');
  }catch(e){
    window.alert(e);
  }
}



8:cssファイル… ドキュメントルート/css/yaimg.css

関連キーワードの追加に関連した変更あり。

/* author : dix3 */
/* link : http://d.hatena.ne.jp/dix3/ */

body{ 
 color:#fff;
 background:#111;
}
hr { 
 color:#eeeeee;
 height:1px; 
}
h1,h2,h3{
 font-size:1em;
}
a{ 
  text-decoration: none;
}

.error{
 background:#ff0000;
 color:#fff;
 padding:3px;
 font-weight:bold;
 display:inline;
}
.result_msg{
 background:#ffd700;
 color:#111;
 padding:3px;
 font-weight:bold;
 display:inline;
}

.result_body{
 text-decoration: none; 
 margin:20px 0 0 0;
 padding:5px; 
}
.result_body a:link,
.result_body a:visited,
.result_body a:active{
  color: #111;
}
.result_body a:hover{
  color: #ffd700;
}

.page_link{
 font-size:1.2em;
 text-align:center;
 margin:10px 0 10px 0;
}

.page_link b{
 color:#111;
 background:#ffd700;
}
.page_link a{
 color:#fff;
}

.page_link a:hover{
 color:#ffd700;
}

.sentence_link input.keyword{
 margin:0px;
 padding:0px;
 color:#fff;
 background:#111;
 font-size:0.875em;
 border:0;
 cursor:pointer;
}

.sentence_link input.keyword:hover{
 color:#ffd700;
 background:#777;
}

.bold{
 font-weight:bold;
}

img{
 border:2px solid #111;
}
img:hover{
 border:2px solid #ffd700;
}


9:google-analytics(urchin)用ビューファイル… application/modules/yaimg/views/urchin_tag.php

前回と変わらず。

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("<?= $uacct_no ?>");
pageTracker._trackPageview();
</script>



次は

  • DB(またはファイル)への検索キャッシュ保存
  • 検索ログの保存
  • 最新の検索内容の画面表示(全利用者と共用する検索語句一覧)

にとりかかる。


今日はここまで。



09/02/22 一部修正

  • ヘルパのロードに 'url','date'を追加(autoloadで読みこんでいたため)
  • cssに img , img : hover 追加