Skip to content

Instantly share code, notes, and snippets.

@kiyotakagoto
Created November 21, 2011 03:44
Show Gist options
  • Select an option

  • Save kiyotakagoto/1381552 to your computer and use it in GitHub Desktop.

Select an option

Save kiyotakagoto/1381552 to your computer and use it in GitHub Desktop.
Text::MeCabとTermExtract::MeCabでキーワード抽出
sub morphological_analysis_by_mecab {
my $decoded_str = shift;
my @array_terms;
my $mecab = Text::MeCab->new({
userdic => 'hoge.dic',
}
);
my $str = encode('euc-jp', $decoded_str);
my $node = $mecab->parse($str);
my $text = '';
for( ; $node; $node = $node->next ) {
if ( defined $node->surface && is_ideal_word($node->feature) ) {
$text .= $node->surface . "\t";
$text .= $node->feature;
$text .= "\n";
push @array_terms, decode('euc-jp', $node->surface);
}
}
$text .= "EOS\n";
return { mecab_terms => \@array_terms, mecab_output_string => $text };
}
sub get_terms_by_term_extract {
my @array_terms;
my $mecab_output_string = shift;
my $term_extract = new TermExtract::MeCab;
my @noun_list = $term_extract->get_imp_word($mecab_output_string, 'var'); # 入力が変数
foreach (@noun_list) {
# 日付・時刻は表示しない
next if $_->[0] =~ /^(昭和)*(平成)*(\d+年)*(\d+月)*(\d+日)*(午前)*(午後)*(\d+時)*(\d+分)*(\d+秒)*$/;
# 数値のみは表示しない
next if $_->[0] =~ /^\d+$/;
# 結果表示
push @array_terms, decode('euc-jp', $_->[0]);
}
return \@array_terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment