MeCab

管理人が試した限りでは、動作が軽くて一番インストールしやすかった形態素解析エンジンでした。




インストール

ここから必要なものをダウンロードし、サーバの適当なディレクトリに配置してrootでインストールしました。

MeCab本体 mecab-0.98.tar.gz
辞書(IPA辞書) mecab-ipadic-2.7.0-20070801.tar.gz
perlバインディング(perlからMeCabを使うときに必要) mecab-perl-0.98.tar.gz

インストールログ(rootでインストール)
【 本体のインストール 】
#mkdir MeCab
#cd MeCab
#tar xvf mecab-0.98.tar.gz
#cd mecab-0.98
#./configure
#make
#make install
#cd ../

【辞書のインストール】
#tar xvf mecab-ipadic-2.7.0-20070801.tar.gz
#cd mecab-ipadic-2.7.0-20070801
#./configure --with-charset=utf8           (サーバの文字がUTF8だったので辞書もUTF8を指定)
#make
#make install
#cd ../

【perlバインディングをインストール】
#tar xvzf mecab-perl-0.98.tar.gz
#cd mecab-perl-0.98
#perl Makefile.PL
#make
#make test
#make install
#cd $home
#exit



文章から名詞だけ抜き出すスクリプト

標準入力から受け取った文章から、名詞だけを抜き出して表示するスクリプト例です。

スクリプト
#!/usr/bin/perl
use strict;
use MeCab;

my $str = $ARGV[0];

my $mecab = MeCab::Tagger->new();
my $node = $mecab->parseToNode($str);

for( ; $node; $node = $node->{next} ) {
    next unless defined $node->{surface};
    my $midasi = $node->{surface};
    my($hinsi) = (split( /,/, $node->{feature} ))[0];
    if ($hinsi eq "名詞") {
        print $midasi, "\n";
    }
}


実行例
$perl mecab.pl 今日、東京は晴天です。
今日
東京
晴天
$

Googleの検索結果の上位10件から単語の出現頻度をカウントする

Googleの検索結果から、上位10件のWebサイトの内容を取得して、MeCabで名詞だけを抽出して出現頻度をカウントしてみました。
(使用言語:Perl、Web::Scraperモジュール)

2つのスクリプトで上記を実現しました。
・word_count.pl
標準入力で指定されたキーワードのGoogle検索結果を元に出現する名詞をカウントするスクリプト。
・ext_body.pl
標準入力で指定されたWebサイト(URL)の本文(<body>部分)を抜き出すスクリプト。

実行例
$ perl word_count.pl 東日本大震災
県 	434
沖 	323
大震災 	280
市 	238
東北 	220
被災 	200
ID 	188
名前 	185
名無し 	183
編集 	180
地方 	179
宮城 	163
被害 	161
www 	158
発生 	150
福島 	129
放送 	96
(以下略)


word_count.pl
#!/usr/bin/perl
##############################################################
# Googleの検索結果上位10件のサイトの<body>部分から
# 名詞だけを抜き出して出現回数をカウントするスクリプト
#############################################################
use strict;
use warnings;

use Web::Scraper;
use URI;
use Encode;
use MeCab;

my $t_url;
my $sentence_res = "";
my $res;
my $sword = $ARGV[0];
my $Google = 'http://www.google.co.jp/search?&q=' . $sword;

# 検索結果からサイトのタイトルとURLをScrapeするパターンを定義
my $blog_list = scraper {
    process "h3.r", "sites[]" => scraper {
        process "h3 a", link => '@href';
        process "h3 a", name => [
            'TEXT',
            sub {
                (split /\s-/, $_[0])[0];
            }
        ];
    };
};

# 一時ファイル(body_temp.txt)の中身をクリアして初期化
open (OUT, "> body_temp.txt") or die "$!";
   print OUT "";
close(OUT);

# Googleの検索結果をスクレイピング
my $res_infos = $blog_list->scrape( URI->new($Google) );

# スクレイピングの結果を一時ファイルに格納
foreach my $res_info ( @{$res_infos->{'sites'}} ) {
    $t_url = $res_info->{link};
    open (OUT, ">> body_temp.txt") or die "$!";
       $res = `perl ext_body.pl $t_url`;
       print OUT $res;
    close(OUT);
}

# 一時ファイルの中身を変数に展開
open (FILE, "< body_temp.txt");
   while (<FILE>){
     $sentence_res = $_;
   }
close(FILE);

# MeCabで名詞だけ抽出
my $mecab = MeCab::Tagger->new();
my $node = $mecab->parseToNode($sentence_res);
my $ext_res;

# 一時ファイル(ext_none.txt)の中身をクリアして初期化
open (PRE_EXT, "> ext_none.txt");
    print PRE_EXT "";
close(PRE_EXT);

# MeCabで名詞だけ抽出して結果を一時ファイルに出力
open (EXT_OUT, ">>ext_none.txt");
for( ; $node; $node = $node->{next} ) {
    next unless defined $node->{surface};
    my $midasi = $node->{surface};
    my( $hinsi, $yomi ) = (split( /,/, $node->{feature} ))[0,7];
    if ( $hinsi eq "名詞") {
        print EXT_OUT $midasi, "\n";
    }
}
close(EXT_OUT);

# 抽出結果から名詞の出現頻度をカウント
my $textfile = "ext_none.txt";

# 集計
my %words = ();
open(TXT,$textfile);
while(<TXT>){
    chomp;
    if(!$words{$_}){
       $words{$_} = 1;
     }else{
       $words{$_}++;
     }
}
close(TXT);

# 結果表示
foreach(keys %words){
   print "$_ \t $words{$_}\n";
}

exit;


ext_body.pl
#!/usr/bin/perl
##############################################################
#  標準入力で指定されたURLの本文(body)を抜き出すスクリプト
##############################################################

use strict;
use warnings;

use Web::Scraper;
use URI;
use Encode;

my $WebSite = $ARGV[0];

my $blog_list = scraper {
    process "body", 'pres' => 'TEXT' ;
};

my $res = $blog_list->scrape( URI->new($WebSite) );

print encode('utf-8',$res->{pres});
最終更新:2011年05月20日 06:52
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。