Location : Home > Languages > Perl > Package Title : Algorithm::HowSimilar |
![]() |
Algorithm::HowSimilar - 類似度の計算
use Algorithm::HowSimilar qw(compare); @res = compare( $str1, $str2, sub { s/\s+//g; [split //] } ); @res = compare( \@ary1, \@ary2 );
本モジュールは配列または文字列の同一性の度合いを比較できるように Algorithm::Diff を拡張する。それらがどれくらい類似しているかを定める結果集合を返す。
compare( ARG1, ARG2, OPTIONAL_CALLBACK )
2つの文字列 ( $str1, $str2 ) を比較するために呼び出す。
my ( $av_similarity, $sim_str1_to_str2, $sim_str2_to_str1, $matches, $in_str1_but_not_str2, $in_str2_but_not_str1 ) = compare( 'this is a string-a', 'this is a string bbb' );
文字列長が同じでなければある文字列と他の文字列との数学的な類似度は異なる。最初の結果は平均的な類似度を返す。まったく異なっていれば 0 を返す。まったく同じであれば 1 を返す。それゆえに類似度は 0 - 1 の間の数値であり、OS/Perl が管理できる最大の精度で表示する。
2つの配列参照 ( \@ary1, \@ary2 ) の場合は以下のように比較することができる。
my ( $av_similarity, $sim_ary1_to_ary2, $sim_ary2_to_ary1, $ref_ary_matches, $ref_ary_in_ary1_but_not_ary2, $ref_ary_in_ary2_but_not_ary1 ) = compare( [ 1,2,3,4 ], [ 3,4,5,6,7 ] );
2つの文字列とともに呼び出した場合、必要ならば文字列のデフォルトのトークン化(単純な分割または null)を変更するコールバックを指定することができる。文字列は $_ におけるコールバックに渡され、 sub は配列参照を返すことが期待されている。以下の例では空白をもししている。
@res = compare( 'this is a string', 'this is a string ', sub { s/\s+//g; [split //] } );
文字列または配列の共通部分を取得することができる。同様に結合も以下のように取得することができる。
@res = compare( $str1, $str2 ); $intersection = $res[3]; $union = $res[3].$res[4].$res[5]; @res = compare( \@ary1, \@ary2 ); @intersection = @{$res[3]}; @union = ( @{$res[3]}, @{$res[4]}, @{$res[5]} );
デフォルトではなし。
Dr. James Freeman, <james.freeman@id3.org.uk>
perl
![]() |
Updated : 2007/02/02 |