2016年7月6日水曜日

形態素解析の方法

YahooデベロッパーネットワークのAPIで日本語形態素解析をやってみました。
http://developer.yahoo.co.jp/webapi/jlp/ma/v1/parse.html

1.アプリケーションIDの準備
https://e.developer.yahoo.co.jp/register
からアプリケーション情報の入力をして、アプリケーションIDを取得します。
取得したIDで試しに動かしてみます。
http://jlp.yahooapis.jp/MAService/V1/parse?appid=取得したID&results=uniq&sentence=解析したい文章
これをブラウザで動かしてみると
のようになります。

2.格納用テーブルの用意
今回は、下記を使います。
・claims(文章が入っているテーブル)
・morphems(解析結果が入るテーブル)
・categories(品詞が入っているテーブル)
morphemsテーブルの構造は図のようになっています。





3.コードの追加
今回はController/ClaimsController.phpのviewアクションを利用してやりました。



public function view2($id = null) {
                if (!$this->Claim->exists($id)) {
                        throw new NotFoundException(__('Invalid claim'));
                }
                $options = array('conditions' => array('Claim.' . $this->Claim->primaryKey => $id));
                $this->set('claim', $this->Claim->find('first', $options));

                $claim = $this->Claim->find('first', $options);
                if($claim['Claim']['chk'] == 0 ){

                $appid = '自分のID';
                $url = "http://jlp.yahooapis.jp/MAService/V1/parse?appid=".$appid."&results=uniq";
                $url .= "&sentence=".urlencode($claim['Claim']['naiyou']);
                $xml  = simplexml_load_file($url);

                APP::import('Model', 'Morphem');
                $this->Morphem = new Morphem;
                APP::import('Model', 'Category');
                $this->Category = new Category;

                foreach ($xml->uniq_result->word_list->word as $cur){

                        $category_id = $this->Category->find('first',
                                array(
                                        'fields' => array('id'),
                                        'conditions' => array('Category.name' => (string)$cur->pos),
                                )
                        );

                        $this->Morphem->create();
                        $data = array(
                                'claim_id' => $id,
                                'word' =>(string)$cur->surface,
                                'category_id' => (int)$category_id['Category']['id'],
                                'count' =>(int)$cur->count,
                        );

                        $this->Morphem->save($data);

                 
                }

                        $data1 = array(
                                'id' => $id,
                                'chk' => 1
                        );
                        $this->Claim->save($data1);
                }
        }

図のように作成され、テーブルに保存します。






2016年7月5日火曜日

MySQLの移行

サーバーを移行するにあたって、MySQLの移行をしました。

バックアップ取得
SSHでログイン後、対象ユーザに一応なっておいてやります。
mysqldump -uuser_name -puser_password -r backup.sql --single-transaction DB_name
プロンプトが返ってくるとbackup.sqlができています。
これを(ローカルにダウンロードしてから)対象サーバにアップロードします。

サーバ間でファイルをFTPで送る場合は、クライアントのインストールが必要でした。
 yum -y install ftp

アップロード後、移行先サーバでデータベースをインポートします(ユーザは発行済で)
mysql -uuser_name -puser_password DB_name < backup.sql

参考
http://qiita.com/rato303/items/2e614f23e5feee150ffc
http://d.hatena.ne.jp/pospome/20130604/1370333526