php source code search with php

40
柄沢聡太郎 (sotarok) http://nequal.jp/ twitter.com/sotarok PHP Source Code Search with PHP

Upload: sotaro-karasawa

Post on 05-Dec-2014

4.086 views

Category:

Technology


7 download

DESCRIPTION

@PHP勉強会#46 ソース検索をPHPでつくる

TRANSCRIPT

Page 1: PHP Source Code Search with PHP

柄沢聡太郎 (sotarok)

http://nequal.jp/

twitter.com/sotarok

PHP Source Code Search

with

PHP

Page 2: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

始める前に

Page 3: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

今⽇はPHP勉強会

Page 4: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

なぜ 9/29 ではないのか

Page 5: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

懇親会はピザだって?

Page 6: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⾁はどうしたんですか

Page 7: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

本来のPHP勉強会

Page 8: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

PHPerPHPerはは⾁の精神を忘れ⾁の精神を忘れてしまったかてしまったか

Page 9: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

ごめんなさい冗談ですgusagiさん幹事おつかれさまです

Page 10: PHP Source Code Search with PHP

じこしょうかい

Page 11: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

sotarok

• そうたろう(けー)• プリン• プリン• プリン• pudding

• プリン• ⾁• ごはん• 自転⾞• 写真 – Sony α 300• Ethna

• nequal

Page 12: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

宣伝• WikiHub

– http://wikihub.org/

– Gitで⽂書管理して,それをWiki 形式で整形して表⽰

– GitHub の Service Hook で簡単同期• 制限つきOpenBeta– 100⼈まで

Page 13: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

宣伝2• プリンが好きです

なぜか誰も買ってこない

Page 14: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

宣伝2• プリンが好きです

yuchimiriさんにRTまでされたのに

Page 15: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

Preferred Infrastructure (PFI)

というところでインターンやってます

検索エンジンとかつくってるとこ!

だからか だからです

Page 16: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

Agenda

• 検索エンジン• 転置インデックス• クロールとキーワード抽出• スコアリングと並び替え• デモ• 技術的な課題とか

Page 17: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

検索エンジン• キーワードを⼊⼒– →そのキーワードが含まれる(そして探したいと思われる)⽂書が探せる

• ソースコード検索– grep

– gtags (GNU GLOBAL)

– trac + Hyper Estraier (BTS + Plugin)

– Google Code Search

Page 18: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

grep

• 与えられたキーワードをファイルをたどり,⽂字列⼀致で探す

• 前処理が必要ない• どこにでも⼊ってる

• 大量の⽂書から探すのは時間がかかる– 同じキーワードで再検索するときもまた同じ処理

• ランキングづけができない

grepのデメリット

Page 19: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

検索エンジンを作る• 既存のいろんなソフトウェアを使うのもいいけど• 「検索エンジン」– そんな難しくない– PHPでもできる– カンタンだよ!

• 今回の目標– ローカルのPHPソースファイルからメソッドや関数や変数を検索できるようにする– ディレクトリを指定して,再帰的にPHPファイルを検索– grep -rn 'substr' **/*.php

Page 20: PHP Source Code Search with PHP

転置インデックス

Page 21: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

転置インデックス• キーワードから⽂書を逆引きできるようにあらかじめつくっておくインデックス

• 焼⾁ =>

– ⽜角– Wikipedia – 焼⾁– YAKINIQUEST - ヤキニクエスト– ...

Page 22: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⽂書1I like Sushi, Yakiniku and pudding !

⽂書2PHPer like Yakiniku. Maybe not like Sushi.

⽂書3Dankogai is an enemy of PHPer.

※⽂書はフィクションです

Page 23: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⽂書1I like Sushi , Yakiniku and pudding !

⽂書2PHPer like Yakiniku. Maybe not like Sushi.

⽂書3Dankogai is an enemy of PHPer. He also like Yakiniku.

※⽂書はフィクションです

Page 24: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⽂書3Dankogai is an enemy of PHPer. He also like Yakiniku.

⽂書2PHPer like Yakiniku. Maybe not like Sushi.

⽂書1I like Sushi, Yakiniku and pudding !

1Sushi

Page 25: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⽂書3Dankogai is an enemy of PHPer. He also like Yakiniku.

⽂書2PHPer like Yakiniku. Maybe not like Sushi.

⽂書1I like Sushi, Yakiniku and pudding !

1Yakiniku

1Sushi

Page 26: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⽂書3Dankogai is an enemy of PHPer. He also like Yakiniku.

⽂書2PHPer like Yakiniku. Maybe not like Sushi.

⽂書1I like Sushi, Yakiniku and pudding !

1Yakiniku

1pudding

1Sushi

Page 27: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⽂書3Dankogai is an enemy of PHPer. He also like Yakiniku.

⽂書2PHPer like Yakiniku. Maybe not like Sushi.

⽂書1I like Sushi, Yakiniku and pudding !

21Yakiniku

1pudding

1Sushi

Page 28: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

⽂書3Dankogai is an enemy of PHPer. He also like Yakiniku.

⽂書2PHPer like Yakiniku. Maybe not like Sushi.

⽂書1I like Sushi, Yakiniku and pudding !

21Yakiniku

2

1pudding

1Sushi

Page 29: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

転置インデックスをPHPで• array でしょJK

$inverted_index = array(

'miyazaki' => array(1, 2, 6, 4 ...),

'aoi' => array(2, 6, 7, ...),

'sotarok' => array(...),

...

);

配列のキー:単語

配列の要素:⽂書IDのリスト

Page 30: PHP Source Code Search with PHP

クロールとキーワード抽出

Page 31: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

クロールとキーワード抽出• 対象の⽂書からキーワードを拾い集める• クローラー -> Google なら Google Botとか

• キーワード ->

– 空⽩区切りの⽂字列(英語圏のひと)– 形態素解析(明⽇ は あめ だ)– n-gram

• n⽂字区切りでキーワードにする• 2-gram(bi-gram)

• 明⽇ ⽇は はあ あめ めだ

Page 32: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

クロールとキーワード抽出をPHPで(1)

•function crawl ($dirname)

• foreach(glob($dirname . "/*") as $file)

• if is_dir($file):

• $files += crawl ($file)

• else:

• if $file ~= /.+¥.php$/

• $files[] = $file

• return $files

Page 33: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

クロールとキーワード抽出をPHPで(2)

• token_get_all ��������

• PHP �������������������������� ��� ��� ��� �

• ������������ tokenize ����������������������������

• http://php.net/tokenizer

• T_STRING

• T_VARIABLE

• キーワードの抽出– 関数名/メソッド名/クラス名/変数名– tokenizer を使う

Page 34: PHP Source Code Search with PHP

スコアリングと並び替え

Page 35: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

スコアリングと並び替え• なにかの基準で検索結果を並び替える• あてはまりの良い⽂書を上に出す

• 普通の検索なら– ページランク (Google のウェブ検索)– TF-IDF

• ⽂書中の単語の出現頻度• その単語の⽂書全体での珍しさ

– ...

Page 36: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

スコアリングと並び替えをPHPで• スコアはソースコード中のそのキーワードの出現数

• 多い順に並び替え

uasort(

$scored,

create_function(

'$v1, $v2',

'return $v1[¥'count¥'] < $v2[¥'count¥'];'

)

);

PHP 5.3 ならfunction($v1, $v2)

でいける!

Page 37: PHP Source Code Search with PHP

デモ

Hyper Pudding

http://gist.github.com/192879

Page 38: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

技術的な課題• デモったやつは全部 on memory

– 大規模⽂書はイケない• symfonyで 140MB くらい⾷うかな

– ということで,転置インデックスを外部ファイル(Key-Value Store... まぁ,SQLiteとかでもいいけど) に保持するといい

• 並び替えの基準は出現頻度でいいの?– これはまあ難しいですね– 属性情報とかいろいろとればさらに

• And/Or 検索

Page 39: PHP Source Code Search with PHP

Copyright © nequal, Creative Commons Attribution-Noncommercial 2.1 @ PHP Study #46.

まとめ• 簡単な検索エンジンはPHPでもつくれる– それもお⼿軽に

• インデックスをあらかじめつくっておけば検索は瞬時にできる

• 検索技術とか楽しい

Page 40: PHP Source Code Search with PHP

ありがとうございました

Question?