File Coverage

blib/lib/Benchmark/Featureset/StopwordLists.pm
Criterion Covered Total %
statement 25 27 92.5
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 34 36 94.4


line stmt bran cond sub pod time code
1             package Benchmark::Featureset::StopwordLists;
2              
3 1     1   24638 use strict;
  1         3  
  1         31  
4 1     1   4 use warnings;
  1         2  
  1         28  
5              
6 1     1   521 use Benchmark::Featureset::StopwordLists::Config;
  1         3  
  1         30  
7              
8 1     1   9 use Config;
  1         1  
  1         32  
9 1     1   4 use Config::Tiny;
  1         2  
  1         25  
10              
11 1     1   930 use Date::Simple;
  1         5805  
  1         29  
12              
13 1     1   7 use Hash::FieldHash ':all';
  1         3  
  1         143  
14              
15 1     1   787 use Lingua::EN::StopWordList;
  1         568  
  1         23  
16 1     1   385 use Lingua::EN::StopWords;
  0            
  0            
17             use Lingua::StopWords;
18              
19             use Text::Xslate 'mark_raw';
20              
21             fieldhash my %html_config => 'html_config';
22             fieldhash my %module_config => 'module_config';
23              
24             our $VERSION = '1.01';
25              
26             # ------------------------------------------------
27              
28             sub _build_environment
29             {
30             my($self) = @_;
31              
32             my(@environment);
33              
34             # mark_raw() is needed because of the HTML tag .
35              
36             push @environment,
37             {left => 'Author', right => mark_raw(qq|Ron Savage|)},
38             {left => 'Date', right => Date::Simple -> today},
39             {left => 'OS', right => 'Debian V 6.0.4'},
40             {left => 'Perl', right => $Config{version} };
41              
42             return \@environment;
43             }
44             # End of _build_environment.
45              
46             # ------------------------------------------------
47              
48             sub _build_excluded_list
49             {
50             my($self, $module_config) = @_;
51             my($count) = 0;
52              
53             my($href);
54             my(@tr);
55              
56             push @tr, [{td => 'Name'}, {td => 'Package'}, {td => 'Notes'}];
57              
58             for my $module (sort keys %$module_config)
59             {
60             next if ($$module_config{$module}{include} eq 'Yes');
61              
62             $count++;
63              
64             ($href = $$module_config{$module}{package}) =~ s/::/-/g;
65              
66             # mark_raw() is needed because notes contain the HTML tag
.
67              
68             push @tr,
69             [
70             {td => mark_raw("$count: $module")},
71             {td => mark_raw(qq|$$module_config{$module}{package}|)},
72             {td => mark_raw($$module_config{$module}{notes} || '')},
73             ];
74             }
75              
76             push @tr, [{td => 'Name'}, {td => 'Package'}, {td => 'Notes'}];
77              
78             return [@tr];
79             }
80             # End of _build_excluded_list.
81              
82             # ------------------------------------------------
83              
84             sub _build_module_list
85             {
86             my($self, $module_config) = @_;
87             my($count) = 0;
88              
89             my($href);
90             my(@tr);
91             my($version);
92             my($word_list, %word_list);
93              
94             push @tr, [{td => 'Name'}, {td => 'Package'}, {td => 'Version'}, {td => 'Word count'}];
95              
96             for my $module (sort keys %$module_config)
97             {
98             next if ($$module_config{$module}{include} eq 'No');
99              
100             $count++;
101              
102             ($href = $$module_config{$module}{package}) =~ s/::/-/g;
103             $version = `mversion $module`;
104             $word_list = [];
105              
106             if ($module eq 'Lingua::EN::StopWordList')
107             {
108             $word_list = Lingua::EN::StopWordList -> new -> words;
109             }
110             elsif ($module eq 'Lingua::EN::StopWords')
111             {
112             $word_list = [sort keys %Lingua::EN::StopWords::StopWords];
113             }
114             elsif ($module eq 'Lingua::StopWords')
115             {
116             $word_list = [sort keys Lingua::StopWords::getStopWords('en')];
117             }
118              
119             $word_list{$module} = [@$word_list];
120              
121             push @tr,
122             [
123             {td => "$count: $module"},
124             {td => mark_raw(qq|$$module_config{$module}{package}|)},
125             {td => $version},
126             {td => scalar @$word_list},
127             ];
128             }
129              
130             push @tr, [{td => 'Name'}, {td => 'Package'}, {td => 'Version'}, {td => 'Word count'}];
131              
132             return (\@tr, \%word_list);
133             }
134             # End of _build_module_list.
135              
136             # ------------------------------------------------
137              
138             sub _build_report_generator
139             {
140             my($self) = @_;
141             my($module) = __PACKAGE__;
142             my($href) = $module;
143             $href =~ s/::/-/g;
144              
145             my(@report);
146              
147             push @report,
148             {left => 'Module', right => 'Version'},
149             {left => mark_raw(qq|$module|), right => $VERSION};
150              
151             return \@report;
152              
153             } # End of _build_report_generator;
154              
155             # ------------------------------------------------
156              
157             sub _build_templater
158             {
159             my($self, $html_config) = @_;
160              
161             return Text::Xslate -> new
162             (
163             input_layer => '',
164             path => $$html_config{template_path},
165             );
166              
167             } # End of _build_templater.
168              
169             # --------------------------------------------------
170              
171             sub _build_word_lists
172             {
173             my($self, $module_list) = @_;
174              
175             # 1: Build a list of all words.
176              
177             my(%word, %word_list);
178              
179             for my $module (keys %$module_list)
180             {
181             $word{$_} = 1 for @{$$module_list{$module} };
182             $word_list{$module} = {};
183             @{$word_list{$module} }{@{$$module_list{$module} } } = (1) x @{$$module_list{$module} };
184             }
185              
186             # 2: Determine which module has which words.
187              
188             my(@temp);
189              
190             push @temp, {td => 'Id'};
191              
192             for my $module (sort keys %$module_list)
193             {
194             push @temp, {td => $module};
195             }
196              
197             my(@tr) = [@temp];
198             my($count) = 0;
199              
200             for my $word (sort keys %word)
201             {
202             @temp = ();
203              
204             push @temp, {td => ++$count};
205              
206             for my $module (sort keys %$module_list)
207             {
208             push @temp, {td => $word_list{$module}{$word} ? $word : ''};
209             }
210              
211             push @tr, [@temp];
212             }
213              
214             @temp = ();
215              
216             push @temp, {td => 'Id'};
217              
218             for my $module (sort keys %$module_list)
219             {
220             push @temp, {td => $module};
221             }
222              
223             push @tr, [@temp];
224              
225             return \@tr;
226              
227             } # End of _build_word_lists.
228              
229             # --------------------------------------------------
230              
231             sub _init
232             {
233             my($self, $arg) = @_;
234             $$arg{html_config} = Benchmark::Featureset::StopwordLists::Config -> new -> config;
235             $$arg{module_config} = Config::Tiny -> read('config/module.list.ini');
236             $self = from_hash($self, $arg);
237              
238             return $self;
239              
240             } # End of _init.
241              
242             # --------------------------------------------------
243              
244             sub log
245             {
246             my($self, $level, $s) = @_;
247             $level ||= 'debug';
248             $s ||= '';
249              
250             print "$level: $s\n";
251              
252             } # End of log.
253              
254             # --------------------------------------------------
255              
256             sub new
257             {
258             my($class, %arg) = @_;
259             my($self) = bless {}, $class;
260             $self = $self -> _init(\%arg);
261              
262             return $self;
263              
264             } # End of new.
265              
266             # ------------------------------------------------
267              
268             sub run
269             {
270             my($self) = @_;
271             my($html_config) = $self -> html_config;
272             my($module_config) = $self -> module_config;
273             my(@module_list) = $self -> _build_module_list($module_config);
274             my($templater) = $self -> _build_templater($html_config);
275             my($excluded_modules) = $self -> _build_excluded_list($module_config);
276              
277             print $templater -> render
278             (
279             'stopwordlists.report.tx',
280             {
281             default_css => "$$html_config{css_url}/default.css",
282             environment => $self -> _build_environment,
283             fancy_table_css => "$$html_config{css_url}/fancy.table.css",
284             modules_excluded => $#$excluded_modules > 1 ? $excluded_modules : [],
285             modules_included => $module_list[0],
286             report_generator => $self -> _build_report_generator,
287             word_data => $self -> _build_word_lists($module_list[1]),
288             }
289             );
290              
291             } # End of run.
292              
293             # ------------------------------------------------
294              
295             1;
296              
297             =pod
298              
299             =head1 NAME
300              
301             Benchmark::Featureset::StopwordLists - Compare various stopword list modules
302              
303             =head1 Synopsis
304              
305             #!/usr/bin/env perl
306              
307             use Benchmark::Featureset::StopwordLists;
308              
309             Benchmark::Featureset::StopwordLists -> new -> run;
310              
311             See scripts/stopwordlists.report.pl. This outputs HTML to STDOUT.
312              
313             Hint: Redirect the output of that script to your $doc_root/stopwordlists.report.html.
314              
315             A copy of the report ships in html/stopwordlists.report.html.
316              
317             L.
318              
319             =head1 Description
320              
321             L compares various stopword list modules.
322              
323             The list of modules processed is shipped in data/module.list.ini, and can easily be edited before re-running:
324              
325             shell> scripts/copy.config.pl
326             shell> scripts/stopwordlists.report.pl
327              
328             The config stuff is explained below.
329              
330             =head1 Distributions
331              
332             This module is available as a Unix-style distro (*.tgz).
333              
334             See L
335             for help on unpacking and installing distros.
336              
337             =head1 Installation
338              
339             =head2 The Module Itself
340              
341             Install L as you would for any C module:
342              
343             Run:
344              
345             cpanm Benchmark::Featureset::StopwordLists
346              
347             or run:
348              
349             sudo cpan Benchmark::Featureset::StopwordLists
350              
351             or unpack the distro, and then either:
352              
353             perl Build.PL
354             ./Build
355             ./Build test
356             sudo ./Build install
357              
358             or:
359              
360             perl Makefile.PL
361             make (or dmake or nmake)
362             make test
363             make install
364              
365             =head2 The Configuration File
366              
367             All that remains is to tell L your values for some options.
368              
369             For that, see config/.htbenchmark.featureset.stopwordlists.conf.
370              
371             If you are using Build.PL, running Build (without parameters) will run scripts/copy.config.pl,
372             as explained next.
373              
374             If you are using Makefile.PL, running make (without parameters) will also run scripts/copy.config.pl.
375              
376             Either way, before editing the config file, ensure you run scripts/copy.config.pl. It will copy
377             the config file using L, to a directory where the run-time code in
378             L will look for it.
379              
380             shell>cd Benchmark-Featureset-StopwordLists-1.00
381             shell>perl scripts/copy.config.pl
382              
383             Under Debian, this directory will be $HOME/.perl/Benchmark-Featureset-StopwordLists/. When you
384             run copy.config.pl, it will report where it has copied the config file to.
385              
386             Check the docs for L to see what your operating system returns for a
387             call to my_dist_config().
388              
389             The point of this is that after the module is installed, the config file will be
390             easily accessible and editable without needing permission to write to the directory
391             structure in which modules are stored.
392              
393             That's why L and L are pre-requisites for this module.
394              
395             Although this is a good mechanism for modules which ship with their own config files, be advised that some
396             CPAN tester machines run tests as users who don't have home directories, resulting in test failures.
397              
398             =head1 Constructor and Initialization
399              
400             C is called as C<< my($builder) = Benchmark::Featureset::StopwordLists -> new(k1 => v1, k2 => v2, ...) >>.
401              
402             It returns a new object of type C.
403              
404             Key-value pairs in accepted in the parameter list (see corresponding methods for details):
405              
406             =over 4
407              
408             =item o (None as yet)
409              
410             =back
411              
412             =head1 Methods
413              
414             =head2 new()
415              
416             For use by subclasses.
417              
418             =head2 run()
419              
420             Does the real work.
421              
422             See scripts/stopwordlists.report.pl and its output html/stopwordlists.report.html.
423              
424             Hint: Redirect the output of that script to $doc_root/stopwordlists.report.html.
425              
426             =head1 FAQ
427              
428             =head2 Where is the HTML template for the report?
429              
430             Templates ship in htdocs/assets/templates/benchmark/featureset/stopwordlists/.
431              
432             See also htdocs/assets/css/benchmark/featureset/stopwordlists/.
433              
434             =head2 How did you choose the modules to review?
435              
436             By searching MetaCPAN.org for phrases like 'stopword' and 'stop word'.
437              
438             =head1 See Also
439              
440             The other modules in this series are L and
441             L.
442              
443             One set of module comparison reviews, by Neil Bowers, is L.
444              
445             And another set of module comparison reviews, by Ron Savage, is L.
446              
447             =head1 Machine-Readable Change Log
448              
449             The file CHANGES was converted into Changelog.ini by L.
450              
451             =head1 Version Numbers
452              
453             Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
454              
455             =head1 Support
456              
457             Email the author, or log a bug on RT:
458              
459             L.
460              
461             =head1 Author
462              
463             L was written by Ron Savage Iron@savage.net.auE> in 2012.
464              
465             Home page: L.
466              
467             =head1 Copyright
468              
469             Australian copyright (c) 2012, Ron Savage.
470              
471             All Programs of mine are 'OSI Certified Open Source Software';
472             you can redistribute them and/or modify them under the terms of
473             The Artistic License, a copy of which is available at:
474             http://www.opensource.org/licenses/index.html
475              
476             =cut