宮崎大学 >>
工学部 >>
情報システム工学科 >>
伊達 >>
Last modified: Sun Jun 17 23:34:36 JST 2012
スクリプト言語,grep, cut, gnuplot などのツールを使い, amazonに投稿されている書評データを分析する. 分析した結果を考察し,最終日に発表する. 大雑把な流れとしては
はやい段階で次の文献を読んでおくこと. とくに図の読み方がわかなかならければ早めに相談すること.
%chmod +x *.pl
% lv -Ou8 i001.html > i001u.html
% cat i*u.html > all.html作成した例: all.html
% ./html2xml.pl all.html > all.xml作成した例: all.xml
%./calc.pl all.xml > result.datresult.dat ファイルの中身の例
1, 0.000, 317 3, 0.000, 15 3, 0.000, 491 4, 0.000, 1058 5, 0.000, 546 1, 1.000, 142 2, 0.750, 139 3, 1.000, 140 2, 1.000, 870 1, 0.500, 19 1, 0.800, 352 4, 0.000, 122 2, 1.000, 150 ... ...作成した例: result.dat
result.dat は各行にカンマ(,)で区切られた3つの数字が含まれている. 最初の数字は,書評を書いた人がつけた★の数で,必ず 1,2,3,4,5 のどれかです. 2番目の数字は,その書評の支持率,3番目の数字は,その書評の文字数となっている.
% mkdir RESULT1 % egrep ^1 result.dat | cut -d, -f 2-3 > RESULT1/score1.dat意味: result.dat の最初が 1 で始まる行の,2番目と3番目の数字をとりだして, RESULT1/score1.dat というファイルに書き込んでいる. RESULT1 はディレクトリ(フォルダという人もいる)で, これから何冊もの本を分析していくはずなので, 分析する本ごとにディレクトリを RESULT1, RESULT2 ... のように あらかじめ mkdir コマンドでディレクトリを作っておく.
| はパイプといって,egrep コマンドで得られた出力を cut コマンドで解析している. このあたりの意味は,次のようなコマンドを試し試行錯誤し, もとの result.dat ファイルとの違いを調べることで,分かる(はず).
% egrep ^1 result.datもっと知りたければ,各コマンドのマニュアルを読もう.
% man cut % man egrep
どういう書籍を分析したか,たとえば README ファイルをつくり, そこにかならずメモをしておくこと.
% emacs README
--- 以下,README の中身
RESULT1
もし高校野球の女子マネージャーがドラッカーの『マネジメント』を読んだら
岩崎夏海
% egrep ^2 result.dat | cut -d, -f 2-3 > RESULT1/score2.dat % egrep ^3 result.dat | cut -d, -f 2-3 > RESULT1/score3.dat % egrep ^4 result.dat | cut -d, -f 2-3 > RESULT1/score4.dat % egrep ^5 result.dat | cut -d, -f 2-3 > RESULT1/score5.dat
score1.dat ファイルの中身の例(result.dat の,先頭が1ではじまる行が 抽出されている)
0.000, 317 1.000, 142 0.500, 19 0.800, 352 1.000, 130 ...
作成した例: score1.dat, score2.dat, score3.dat, score4.dat, score5.dat
以下のような図を作成できればOK.
% cd RESULT1
% gnuplot
G N U P L O T
Version 4.2 patchlevel 6
last modified Sep 2009
System: Linux 2.6.32-41-generic
...
gnuplot>
そこで
plot "score1.dat" with pointsとしてみると図が描けているはず.
plot "score1.dat" with points,"score2.dat" with points,"score3.dat" with points,"score4.dat" with points,"score5.dat" with pointsとすれば 5 つのファイルのデータが,それぞれ違うマークでプロットされたはずである.
いちいち打ち込むのは面倒くさい. つぎのようなスクリプトを用意しておき gp_script001
% gnuplot gp_script001 > result1.epsとすれば eps ファイルが作成でき,レポートやプレゼンテーションに使える. 端末から
% evince result1.epsと入力すれば, ポストスクリプトファイルの中身が表示される.
gp_script001 の中身:
set terminal postscript "Helvetica" 20 color eps enhanced set xlabel "supported rate" set ylabel "#words" set logscale y plot "score1.dat" with points,"score2.dat" with points,"score3.dat" with points,"score4.dat" with points,"score5.dat" with points
文献:「CGMコンテンツの分析は集合痴に要注意」 を参考に,結果を分析すること. みたことのない新しい分布を発見し, それを解釈してみよう. ここまでできれば上出来.
#!/usr/bin/perl # 元データのHTMLは utf-8 に変換されていること # usage: ./html2xml.pl < hoge.html > hoge.xml $flag = 0; $body = 0; $mesg = 0; $eval = 0; print "\n\n"; while (<>) { if ($_ =~ /\<\!-- BOUNDARY --\>/) { print " \n";- \n"; $flag = 1; next; } if ($_ =~ /crVotingButtons/) { $flag = 0; next; } if ($flag == 0) { next; } if ($pass > 0) { $pass--; next; } if ($_ =~ /([0-9]*,*[0-9]*) 人中、([0-9]*,*[0-9]*)人の方が/) { $tot = $1; $agr = $2; $tot =~ s/,//g; $agr =~ s/,//g; print "
\n"; next; } $_ =~ s/\$tot \n"; print "$agr \n"; $eval = 1; next; } if ($_ =~ /5つ星のうち ([0-9]*)/) { if ($eval == 0) { print "0 \n0 \n"; } print "$1 \n"; $title = 1; next; } if ($title == 1 && $_ =~ /\(.*)\<\/b\>/) { print "$1 \n"; $title = 0; $body = 1; next; } if ($_ =~ /レビュー対象商品:/ && $body == 1 && $mesg == 0 ) { $pass = 2; $mesg = 1; print "\n"; next; } # レビュー対象商品以下2行飛ばして本文開始 if ($_ eq "\n" && $body == 1 && $mesg == 1 ) { $body = 0; $flag = 0; $mesg = 0; $eval = 0; next; } if ($_ =~ /padding-top: 10px; clear: both; width:/ ) { print " \n
//g; $_ =~ s/\/ /g; $_ =~ s/\&/&/g; if ($mesg == 1) { print $_; } } print "
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
{
package Data::Dumper;
sub qquote { return shift; }
}
$Data::Dumper::Useperl = 1;
use encoding 'utf-8';
$xml = $ARGV[0];
$xs = new XML::Simple();
$ref = XML::Simple::XMLin($xml);
foreach $x (@{$ref->{item}}) {
$rate = ($x->{total} != 0) ? $x->{agreed} / $x->{total} : 0.0;
$len = length ($x->{text});
printf "$x->{star},%6.3f, $len\n", $rate;
}
宮崎大学 >> 工学部 >> 情報システム工学科 >> 伊達 >>