#!/usr/bin/perl use strict; use CGI::Carp qw(fatalsToBrowser); use CGI; use URI; use Template; use HTML::TagCloud; use MeCab; sub make_cloud { my $text = shift; my $cloud = HTML::TagCloud->new; my $m = new MeCab::Tagger; my $n = $m->parseToNode($text); my %tag; while ($n = $n->{next}) { my ($word, $type) = ($n->{surface}, split ',', $n->{feature}); next if $type ne '名詞'; next if length $word <= 2; $tag{$word}++; } for my $word (sort keys %tag) { my $uri = new URI('http://www.google.co.jp/search'); $uri->query_form(q => $word, ie => 'euc-jp'); $cloud->add($word, $uri, $tag{$word}); } return $cloud; } my $cgi = new CGI; my $tt = new Template; $tt->process(\*DATA, { title => 'MeCab - HTML::TagCloud', cgi => $cgi, make_cloud => \&make_cloud, }); __DATA__ [% cgi.header('-charset' => 'EUC-JP') -%] [% title | html %]

[% title | html %]

MeCab で 形態素解析を行い、HTML::TagCloud で タグクラウド化させてみるサンプルです。 ソースを見る
下の枠の中に適当な文章(できるだけ長いもの)をコピペし、 Make Cloud ボタンを押してみてください。その文章を元にタグクラウドを作成します。



[% IF cgi.param('text'); cloud = make_cloud( cgi.param('text') ) %] [% cloud.html_and_css(50) %] [% END # IF %]
© 2006 Tomi see http://e8y.net/blog/2006/04/11/p116.html