Archive for the ‘php’ Category

 

PHP namespace autoloader

10月 20th, 2011

サービスごとのコードを切り分けるのにnamespaceを使おうかと
http://www.php.net/manual/ja/language.namespaces.php
http://www.php.net/manual/ja/language.namespaces.faq.php#language.namespaces.faq.shouldicare
さて構造は。
http://d.hatena.ne.jp/sotarok/20101208/1291739722
普通に使ったら問題なさそうだけど。
今から作る部分だけ切り替えていくか悩もうかと思ったけど、
悩むまでもなく変えるべきみたいなのでとりあえず入れてみる。
autoload非推奨でspl_autoload_registerを使うと。
http://php.net/manual/ja/language.oop5.autoload.php
http://php.net/manual/ja/function.spl-autoload-register.php

Read full article | コメントは受け付けていません。

set timeout Zend_XmlRpc_Client

9月 4th, 2011

PHP Fatal error: Uncaught exception ‘Zend_Http_Client_Adapter_Exception’ with message ‘Read timed out after 10 seconds’

default timeout is 10 seconds.
i set timeout to 60secs with zend_httd_client

$httpClient = new Zend_Http_Client();
$httpClient->setConfig(array(’timeout’ => ‘60′));
$xmlrpcClient = new Zend_XmlRpc_Client(’http://blog.bbtune/’,$httpClient);

Read full article | コメントは受け付けていません。

PHP Notice: iconv(): Detected an illegal character in input string | zend Lucene

8月 18th, 2011

PHP Notice: iconv(): Detected an illegal character in input string | zend Lucene
というエラーが。

$doc = new Zend_Search_Lucene_Document();
foreach( $fields as $f ){
switch($f['type']){
case Lib_Lucene::FTYPE_KEYWORD:
$doc->addField(Zend_Search_Lucene_Field::Keyword($f['key'],$f['val'],’UTF-8′));
[...]

Read full article | コメントは受け付けていません。

in_arrayは配列も使える。

7月 20th, 2011

メモ
http://php.net/manual/ja/function.in-array.php

例3 needleが配列の場合の in_array()

$a = array(array(’p', ‘h’), array(’p', ‘r’), ‘o’);

if (in_array(array(’p', ‘h’), $a)) {
echo “‘ph’ was found\n”;
}

if (in_array(array(’f', ‘i’), $a)) {
echo “‘fi’ was found\n”;
}

if (in_array(’o', $a)) {
echo “‘o’ was found\n”;
}

Read full article | コメントは受け付けていません。

クラス名を変数に入れて使う。

7月 12th, 2011

class Test{
public static function e(){echo ‘test’;}
public function f(){echo ‘test’;}
}
$c = ‘Test’;
$c::e();
$x = new $c();
$x->f();

こういう使い方もできるんですね。

Read full article | コメントは受け付けていません。

mecab ユーザー辞書登録

5月 24th, 2011

http://mecab.sourceforge.net/dic.html
表層形,左文脈ID,右文脈ID,コスト,品詞,品詞細分類1,品詞細分類2,品詞細分類3,活用形,活用型,原形,読み,発音
の形式で
music.csv
を作成

perfume,1361,1361,20,名詞,固有名詞,auction,music,*,*,perfume,パフューム,パヒューム
少女時代,1361,1361,20,名詞,固有名詞,auction,music,*,*,少女時代,ショウジョジダイ,ショウジョジダイ
KARA,1361,1361,20,名詞,固有名詞,auction,music,*,*,KARA,カラ,カラ

/usr/local/lib/mecab/dic/naist-jdic/left-id.defを確認して1361を付けてみる。
コンパイル、csvも辞書も utf8のみ利用なので

$ /usr/local/libexec/mecab/mecab-dict-index -d/usr/local/lib/mecab/dic/naist-jdic -u music.dic -f utf8 -t utf8 music.csv
reading music.csv … 3
emitting double-array: 100% |###########################################|

done!

/usr/local/etc/mecabrcを ~/.mecabrcと自分のところにコピー
ユーザー辞書の場所を追記

userdic = /home/myuser/music.dic

実行してみる。

$ echo ‘来月perfumeの新曲が出るよ’ | mecab
来月 名詞,副詞可能,*,*,*,*,来月,ライゲツ,ライゲツ,,
perfume 名詞,固有名詞,auction,music,*,*,perfume,パフューム,パヒューム
の 助詞,連体化,*,*,*,*,の,ノ,ノ,,
新曲 名詞,一般,*,*,*,*,新曲,シンキョク,シンキョク,,
が 助詞,格助詞,一般,*,*,*,が,ガ,ガ,,
出る 動詞,自立,*,*,一段,基本形,出る,デル,デル,でる/出る,
よ 助詞,終助詞,*,*,*,*,よ,ヨ,ヨ,,
EOS

な感じです。
phpからのapacheから利用する場合は、php.iniかini_setで

<?php
ini_set(’mecab.default_userdic’, [...]

Read full article | コメントは受け付けていません。

mecab install | cent os 5.3

5月 20th, 2011

mecab + phpをやってみます。
http://mecab.sourceforge.net/
http://sourceforge.jp/projects/naist-jdic/
mecab install

% tar zxfv mecab-X.X.tar.gz
% cd mecab-X.X
% ./configure --enable-utf8-only
% make
% make check
% su
# make install

make checkでエラーが出ますが、良いみたいです。

runtests faild in
FAIL: run-cost-train.sh
===================
1 of 3 tests failed
===================
make[2]: *** [check-TESTS] エラー 1
make[2]: ディレクトリ `/opt/downloads/mecab-0.98/tests’ から出ます
make[1]: *** [check-am] エラー 2
make[1]: ディレクトリ `/opt/downloads/mecab-0.98/tests’ から出ます
make: *** [check-recursive] エラー 1

mecab-naist-jdic-0.6.3-20100801.tar.gz
辞書:naist-jdic for mecabをインストール

# ./configure --with-charset=utf8
# make
done!
To enable [...]

Read full article | コメントは受け付けていません。

Lucene+PHPでoptimize時のエラー: failed to open stream: Too many open files

5月 18th, 2011

optimizeしてもエラーになってしまうので、lukeというツールを使えと。
http://www.getopt.org/luke/
とりあえず、使おうと思ったけどGUIなので無理っ
lucene部分は、javaで書こうかな、とりあえずsolrを入れてみてから検討しよ。
http://framework.zend.com/manual/en/zend.search.lucene.index-creation.html

MergeFactor auto-optimization option
MergeFactor determines how often segment indices are merged by addDocument(). With smaller values, less RAM is used while indexing, and searches on unoptimized indices are faster, but indexing speed is slower. With larger values, more RAM is used during indexing, and while searches on unoptimized indices are slower, indexing is faster. Thus larger [...]

Read full article | コメントは受け付けていません。

bi-gram解析器でindex & query | Lucene PHP

5月 12th, 2011

英数字連続 + bi-gram解析器のアナライザーを作ってやっと動きました^-^。
☆以下サイトを参考にさせてもらいました。
Mecab解析器 : http://nonn-et-twk.net/twk/php-lucene-japanese-search
CJKIndexを利用して生成した索引の検索 : http://codezine.jp/article/detail/3947?p=3
UTF-8 互換のテキスト解析器 : http://framework.zend.com/manual/ja/zend.search.lucene.charset.html
=== sample ===
インデックスに追加! Lib_Bigramが自分のアナライザーです。

Zend_Search_Lucene_Analysis_Analyzer::setDefault( new Lib_Bigram() ) ;
//Zend_Search_Lucene_Analysis_Analyzer::setDefault( new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());

$indexFile = ‘/tmp/myIndex’;
$index = null;
if( !is_dir($indexFile) ){
$index = Zend_Search_Lucene::create($indexFile);
}else{
$index = Zend_Search_Lucene::open($indexFile);
}

setIndex( ‘9xyz’,'888『賭博黙示録カイジ』今日は少女時代。999′,$index);
setIndex(’snsd’,'SNSD Live in TOKYO’, $index );
setIndex( ‘123′,’Zend_Search_Lucene は、完全に PHP 545 で書かれている汎用的なテキスト検索エンジンです。’, $index );
setIndex( ‘ddd’,'『賭博黙示録カイジ』(とばくもくしろくカイジ)は、福本伸行による日本の漫画作品。’, $index );

// たまに最適化
$index->optimize();

function setIndex( $myid, $title, [...]

Read full article | コメントは受け付けていません。

インデックスの構築 | Lucene PHP

5月 11th, 2011

http://framework.zend.com/manual/ja/zend.search.lucene.overview.html

Zend_Search_Lucene_Field の型
フィールド型 保存 インデックス化 トークン化 バイナリ
Keyword Yes Yes No No
UnIndexed Yes No No No
Binary Yes No No Yes
Text Yes Yes Yes No
UnStored No Yes Yes No

使い方によって保存方法を選べますね。自分の場合は、検索だけでデータは、別に保存するので検索して独自のIDだけ取得出来れば大丈夫なので。

$indexFile = ‘/tmp/myIndex’;
$index = null;
if( !is_dir($indexFile) ){
$index = Zend_Search_Lucene::create($indexFile);
}else{
$index = Zend_Search_Lucene::open($indexFile);
}

// add to index
$doc = new Zend_Search_Lucene_Document();
// for datastore key
$doc->addField(Zend_Search_Lucene_Field::UnIndexed(’myid’,'abcd1235467890′));
// search index
$doc->addField(Zend_Search_Lucene_Field::UnStored(’contents’,'逆境無頼カイジ 破戒録篇’,'utf-8′));
// add index
$index->addDocument($doc);

$doc2 = new Zend_Search_Lucene_Document();
$doc2->addField(Zend_Search_Lucene_Field::UnIndexed(’myid’,'xyzd1235467890′));
$doc2->addField(Zend_Search_Lucene_Field::UnStored(’contents’,'Nayutawave Recordsよりリリースする少女時代の公式ペ>ージ。リリース情報やニュース、視聴・試聴コンテンツをご覧いただけます。’,'utf-8′));
$index->addDocument($doc2);

// たまに最適化
$index->optimize();

これを実行すると以下のようなファイルが生成されます。

$ tree -L 3 /tmp/myIndex
/tmp/myIndex/
|-- _1.cfs
|-- _1.sti
|-- optimization.lock.file
|-- read-lock-processing.lock.file
|-- read.lock.file
|-- segments.gen
|-- segments_d
`-- write.lock.file

Read full article | コメントは受け付けていません。