File Coverage

blib/lib/Book/Collate/Report.pm
Criterion Covered Total %
statement 8 67 11.9
branch 0 8 0.0
condition n/a
subroutine 3 13 23.0
pod 10 10 100.0
total 21 98 21.4


line stmt bran cond sub pod time code
1             package Book::Collate::Report;
2              
3 1     1   18 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         19  
5 1     1   5 use warnings;
  1         2  
  1         1043  
6              
7             =head1 NAME
8              
9             Report
10              
11             =head1 VERSION
12              
13             Version 0.0.1
14              
15             =cut
16              
17             our $VERSION = 'v0.0.1';
18              
19              
20             =head1 SYNOPSIS
21              
22             Report Object
23              
24             use Report;
25              
26             my $report = Report->new(
27             headless_data = $section->headless_data
28             );
29              
30             =head1 SUBROUTINES/METHODS
31              
32             =head2 new
33              
34             =cut
35              
36             sub new {
37 0     0 1   my ($class, %data) = @_;
38             my $self = {
39             _data => undef,
40             _string => $data{string},
41 0           _words => undef,
42             };
43 0           bless $self, $class;
44              
45 0           (my $data = $self->{_string}) =~ s/[;:!'"?.,]/ /g;
46 0           $data =~ s/ (d|ll|m|re|s|t|ve) / /g;
47 0           $data =~ s/\s*(.*)\s*/$1/;
48 0           $self->{_data} = $data;
49 0           $self->{_words} = [ split(/\s+/, $self->{_data}) ];
50 0           return $self;
51             }
52              
53             =head2 avg_sentence_length
54              
55             Returns the average sentence length
56              
57             =cut
58              
59             sub avg_sentence_length {
60 0     0 1   my $self = shift;
61 0           return sprintf( "%.2f", $self->word_count / $self->sentence_count );
62             }
63              
64              
65             =head2 avg_word_length
66              
67             Returns the average word length
68              
69             =cut
70              
71             sub avg_word_length {
72 0     0 1   my $self = shift;
73 0           my $character_count = 0;
74 0           foreach my $word ( $self->words() ){
75 0           $character_count += length($word);
76             }
77 0           return sprintf("%.2f", $character_count / $self->word_count );
78             }
79              
80             =head2 grade_level
81              
82             Returns the grade level.
83              
84             =cut
85              
86             sub grade_level {
87 0     0 1   my $self = shift;
88 0           my $sentence_average = $self->word_count / $self->sentence_count;
89 0           my $word_average = $self->syllable_count / $self->word_count;
90 0           my $grade = 0.39 * $sentence_average ;
91 0           $grade += 11.8 * ( $word_average );
92 0           $grade -= 15.59;
93 0           return sprintf("%.2f", $grade);
94             }
95              
96             =head2 sentence_count
97              
98             Returns the number of sentences, based off the count of ".", "?", and "!" marks.
99              
100             =cut
101              
102             sub sentence_count {
103 0     0 1   my $self = shift;
104 0           return $self->{_string} =~ tr/[?!.]//;
105             }
106              
107             =head2 sorted_word_list
108              
109             Returns a hash of lists, based on word frequency.
110              
111             =cut
112              
113             sub sorted_word_list {
114 0     0 1   my $self = shift;
115 0           my %sorted_word_list;
116 0           my %word_hash = $self->word_list();
117 0           while ( my( $word, $count) = each %word_hash ) {
118 0 0         $sorted_word_list{$count} = [] unless defined( $sorted_word_list{$count} );
119 0           push ( @{$sorted_word_list{$count}}, $word );
  0            
120             }
121 0           foreach my $key ( keys %sorted_word_list ) {
122 0           my @array = sort( @{$sorted_word_list{$key}} ) ;
  0            
123 0           $sorted_word_list{$key} = [ sort( @{$sorted_word_list{$key}} ) ];
  0            
124             }
125 0           return %sorted_word_list;
126             }
127              
128             =head2 syllable_count
129              
130             Returns the number of syllables for a word.
131              
132             =cut
133              
134             sub syllable_count {
135 0     0 1   my $self = shift;
136 0           my $syllable_count = 0;
137 0           foreach my $word ( $self->words ){
138 0           $word = lc($word);
139 0           my $sc = $word =~ tr/[aeiou]//;
140 0           $sc -= $word =~ m/(ee|ey$|oi|oo|ou)/;
141 0 0         $sc += 1 if $word =~ m/y$/;
142 0 0         $sc -= 1 if $word =~ m/e$/;
143 0 0         $sc = 1 if $sc < 1;
144              
145 0           $syllable_count += $sc;
146             }
147 0           return $syllable_count;
148             }
149              
150             =head2 words
151              
152             Returns array of words, in order.
153              
154             =cut
155              
156             sub words {
157 0     0 1   return @{$_[0]->{_words}};
  0            
158             }
159              
160             =head2 word_count
161              
162             Returns the number of words.
163              
164             =cut
165              
166             sub word_count {
167 0     0 1   my $self = shift;
168 0           return scalar($self->words);
169             }
170              
171             =head2 word_list
172              
173             Returns hash of lowercase words as keys, count as values.
174              
175             =cut
176              
177             sub word_list {
178 0     0 1   my $self = shift;
179 0           my %word_list;
180 0           foreach my $word ( $self->words ) {
181 0           $word = lc($word);
182 0           $word_list{$word} += 1;
183             }
184 0           return %word_list;
185             }
186              
187              
188              
189             =head1 AUTHOR
190              
191             Leam Hall, C<< >>
192              
193             =head1 BUGS
194              
195             Please report any bugs or feature requests to L.
196              
197              
198              
199             =head1 SUPPORT
200              
201             You can find documentation for this module with the perldoc command.
202              
203             perldoc lib/Book/Collate/Report
204              
205              
206             You can also look for information at:
207              
208             =over 4
209              
210             =item * GitHub Project Page
211              
212             L
213              
214             =back
215              
216              
217             =head1 ACKNOWLEDGEMENTS
218              
219             Besides Larry Wall, you can blame the folks on IRC Libera#perl for this stuff existing.
220              
221              
222             =head1 LICENSE AND COPYRIGHT
223              
224             This software is Copyright (c) 2021 by Leam Hall.
225              
226             This is free software, licensed under:
227              
228             The Artistic License 2.0 (GPL Compatible)
229              
230              
231             =cut
232              
233             1; # End of Report