root/PMTagCloud/bin/generate-tagcloud.pl

Revision 2093 (checked in by miyagawa, 14 years ago)

import PMTagCloud

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use FindBin;
5 use File::Basename;
6 use File::Find::Rule;
7 use HTML::TagCloud;
8 use Template;
9 use YAML;
10 use URI::Escape;
11
12 my @order = qw(abe koizumi mori obuti hashimoto murayama);
13 my %pm_order = map { $order[$_] => $_ } 0..$#order;
14
15 my %data = (
16     abe => {
17         name => '安倍晋三 (Shinzo Abe)',
18         img  => 'http://www.kantei.go.jp/top-images2006/abe_souri.jpg',
19     },
20     koizumi => {
21         name => '小泉純一郎 (Junichiro Koizumi)',
22         img  => 'http://www.kantei.go.jp/jp/koizumiprofile/images/sokaku.jpg',
23     },
24     mori => {
25         name => '森喜朗 (Yoshiro Mori)',
26         img  => 'http://www.kantei.go.jp/top-images2001/mori.gif',
27     },
28     obuti => {
29         name => '小渕恵三 (Keizo Obuchi)',
30         img  => 'http://www.kantei.go.jp/top-images2001/obuchi.gif',
31     },
32     hashimoto => {
33         name => '橋本龍太郎 (Ryutaro Hashimoto)',
34         img  => 'http://www.kantei.go.jp/top-images2001/hashimoto.gif',
35     },
36     murayama => {
37         name => '村山富市 (Tomiichi Murayama)',
38         img  => 'http://www.kantei.go.jp/top-images2001/murayama.gif',
39     },
40 );
41
42 my $dump  = "$FindBin::Bin/../data/dump";
43 my @files = File::Find::Rule->file->in($dump);
44
45 my $out   = "$FindBin::Bin/../output";
46
47 my $stash;
48 $stash->{data} = \%data;
49 $stash->{css} = HTML::TagCloud->new->css;
50
51 for my $file (@files) {
52     my $base = basename($file);
53     my $data = eval { YAML::LoadFile($file) } or next;
54
55     my $cloud = HTML::TagCloud->new;
56     for my $term (keys %$data) {
57         my $uri = '#' . uri_escape($term);
58         $cloud->add($term, $uri, $data->{$term});
59     }
60
61     my($pm, $year) = $base =~ /(\w+)-(\d+)\.yml/;
62     push @{$stash->{clouds}}, { pm => $pm, year => $year, cloud => $cloud };
63 }
64
65 $stash->{clouds} = [
66     sort { $a->{year} <=> $b->{year} || $pm_order{$b->{pm}} <=> $pm_order{$a->{pm}} }
67     @{$stash->{clouds}}
68 ];
69
70 my $tt = Template->new;
71 my $tmpl = join '', <DATA>;
72 $tt->process(\$tmpl, $stash, "output/index.html")
73     or die $tt->error;
74
75 __DATA__
76 <html>
77 <head><title>Japanese Prime Minister Speeches Tag Cloud - 日本の首相演説のタグクラウド</title>
78 <script type="text/javascript" src="js/range.js"></script>
79 <script type="text/javascript" src="js/timer.js"></script>
80 <script type="text/javascript" src="js/slider.js"></script>
81 <link type="text/css" rel="StyleSheet" href="css/winclassic.css" />
82 <style>
83 .tl { display: none; line-height: 26px }
84 body { font-size: 12px }
85 .footer { font-size: 10px }
86 [% css %]
87 </style>
88 </head>
89 <body>
90 <h1>Japanese Prime Minister Speeches Tag Cloud</h1>
91 <p class="description">
92     Inspired by <a href="http://chir.ag/phernalia/preztags/">US Presidential Speeches Tag Cloud</a>.
93 </p>
94
95 <div class="slider" id="slider-1" tabIndex="1">
96    <input class="slider-input" id="slider-input-1"
97     name="slider-input-1"/>
98 </div>
99
100 [% FOREACH c = clouds %]
101 [% SET pm = c.pm -%]
102 <div class=tl id=tl_[% loop.count %]>
103 <h3>[% data.$pm.name | html %] ([% c.year %])</h3>
104 [% c.cloud.html(250) %]
105 </div>
106 [% END %]
107
108 <script type="text/javascript">
109 var s = new Slider(document.getElementById("slider-1"), document.getElementById("slider-input-1"));
110 s.setMinimum(1);
111 s.setMaximum([% clouds.size %]);
112 s.setValue(s.getMaximum());
113 s.onchange = function () { sv = s.getValue(); if(sv != last_tl) { document.getElementById('tl_'+sv).style.display = 'block'; document.getElementById('tl_'+last_tl).style.display = 'none'; last_tl = sv; } };
114
115 var last_tl = s.getValue();
116 document.getElementById('tl_'+last_tl).style.display = 'block';
117 </script>
118
119 <p class="footer">Data collected from <a href="http://www.kantei.go.jp/jp/abespeech/index.html">kantei.go.jp</a>, analyzed using <a href="http://www.perl.org/">Perl</a> with <a href="http://search.cpan.org/dist/HTML-TreeBuilder/">HTML::TreeBuilder</a>, <a href="http://mecab.sourcrforge.net/">mecab</a> and <a href="http://search.cpan.org/dist/Text-MeCab/">Text::MeCab</a>.
120 </p>
121
122 </body>
123 </html>
Note: See TracBrowser for help on using the browser.