DMOSS/Plugin/SpellChecker.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« DMOSS/File.pm ↑ Summary
1 package DMOSS::Plugin::SpellChecker;
2 $DMOSS::Plugin::SpellChecker::VERSION = '0.01_2';
3 # ABSTRACT: DMOSS spell checker validator plugin
4 1 ▶use parent qw/DMOSS::Plugin/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶use Text::Aspell;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Lingua::Identify qw(:language_identification);
10 use File::Slurp qw/read_file/;
11 use Data::Dumper;
12  
13 our @types = qw/README INSTALL MAN/;
14  
15 my %langs = (
16 en => 'en_US',
17 fr => 'fr_FR',
18 pt => 'pt_PT',
19 );
20  
21 sub name { 'Spell Checker' }
22  
23 sub process {
24 my ($self, $dmoss, $file) = @_;
25 my $content = read_file $file->fullpath;
26 my $lang = langof($content);
27  
28 my $speller = Text::Aspell->new;
29 $speller->set_option('lang',$langs{$lang});
30 my ($total, $valid) = (0, 0);
31 foreach (split /\s+/, $content) {
32 $total++;
33 $valid++ if $speller->check($_);
34 }
35  
36 $dmoss->add_attr('spell_checker', {total=>$total, valid=>$valid});
37 }
38  
39 sub reduce {
40 my ($self, $dmoss, @attrs) = @_;
41  
42 my ($total, $valid) = (0, 0);
43 foreach my $a (@attrs) {
44 $total += $a->value->{total};
45 $valid += $a->value->{valid};
46 }
47  
48 $dmoss->add_attr('spell_checker', {total=>$total, valid=>$valid} );
49 }
50  
51 sub report {
52 my ($self, $dmoss, $attr) = @_;
53 return unless ($attr->value and keys %{$attr->value});
54 return unless $attr->value->{total};
55 my ($valid, $total) = ($attr->value->{valid}, $attr->value->{total});
56  
57 my $p = $total ? int($valid/$total*100) : 0;
58 return [ $attr->file->path, $valid, $total, "$p%" ];
59 }
60  
61 sub report_headers {
62 my ($self, $dmoss) = @_;
63  
64 return [ '', 'Valid Words', 'Total Words', '% Valid' ];
65 }
66  
67 sub grade {
68 my ($self, $dmoss, $attr) = @_;
69 my ($valid, $total) = ($attr->value->{valid}, $attr->value->{total});
70  
71 return ($total ? ($valid/$total) : 0);
72 }
73  
74 1;
75  
76 __END__
« DMOSS/File.pm ↑ Summary