File Coverage

blib/lib/Book/Collate/Section.pm
Criterion Covered Total %
statement 8 76 10.5
branch 0 12 0.0
condition 0 2 0.0
subroutine 3 19 15.7
pod 14 14 100.0
total 25 123 20.3


line stmt bran cond sub pod time code
1             package Book::Collate::Section;
2              
3              
4 1     1   17 use 5.006;
  1         4  
5 1     1   6 use strict;
  1         1  
  1         20  
6 1     1   4 use warnings;
  1         2  
  1         1270  
7              
8             =head1 NAME
9              
10             Section
11              
12             =head1 VERSION
13              
14             Version 0.0.1
15              
16             =cut
17              
18             our $VERSION = 'v0.0.1';
19              
20              
21             =head1 SYNOPSIS
22              
23             Section Object
24              
25             use Section;
26              
27             my $section = Section->new(
28             raw_data => "TITLE: An odd event
29             [1429.123.0457] Nowhere
30             Al looked again, and he was there.",
31             has_header => 1,
32             number => 1,
33             file => '1429_123_0457_nowhere',
34             );
35              
36             =head1 SUBROUTINES/METHODS
37              
38             =head2 new
39              
40             new takes a hash of data, to include: the section number ('number') if relevant,
41             and the raw_data from the file.
42              
43             It uses the raw_data, and a "has_header" flag to create the section header.
44              
45             The section's title is set by the first data line beginning with "TITLE:".
46              
47             =cut
48              
49             sub new {
50 0     0 1   my ($class, %data) = @_;
51             my $self = {
52             _has_header => $data{has_header} || 0,
53             _header => undef,
54             _headless_data => undef,
55             _number => $data{number},
56             _raw_data => $data{raw_data},
57             _report => undef,
58             _title => undef,
59             _filename => $data{filename},
60 0   0       };
61 0           bless $self, $class;
62 0           $self->_write_headless_data();
63 0           $self->_write_report();
64 0           return $self;
65             }
66              
67             =head2 avg_sentence_length
68              
69             Returns the average sentence length.
70              
71             =cut
72            
73 0     0 1   sub avg_sentence_length { $_[0]->{_report}->avg_sentence_length };
74              
75             =head2 avg_word_length
76              
77             Returns the average word length.
78              
79             =cut
80              
81             sub avg_word_length {
82 0     0 1   my ( $self ) = @_;
83 0           return $self->{_report}->avg_word_length;
84             }
85              
86             =head2 filename
87              
88             Returns the filename of the section.
89              
90             =cut
91              
92 0     0 1   sub filename { return $_[0]->{_filename}; }
93              
94              
95             =head2 grade_level
96              
97             Returns the Flesch-Kincaid Grade Level score per: https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests
98              
99             =cut
100              
101             sub grade_level {
102 0     0 1   my ($self) = @_;
103 0           my $f = sprintf("%0.2f", $self->{_report}->grade_level());
104 0           return $f;
105             }
106              
107             =head2 header
108              
109             Returns the section header, if appropriate, or undef.
110              
111             =cut
112              
113             sub header {
114 0     0 1   my ($self) = @_;
115 0 0         return undef unless $self->{_has_header};
116 0           my (@lines) = split(/\n/, $self->raw_data() );
117 0           foreach my $line (@lines) {
118 0 0         next if $line =~ m/TITLE:/;
119 0           chomp($line);
120 0           $line =~ s/^\s*//;
121 0 0         next unless length($line); # Skips extra blank lines at start.
122 0           $self->{_header} = $line;
123 0           last;
124             }
125 0           return $self->{_header};
126             }
127              
128             =head2 headless_data
129              
130             Returns the raw_data, minus any header or title.
131              
132             =cut
133              
134 0     0 1   sub headless_data { return $_[0]->{_headless_data}; }
135              
136            
137             =head2 number
138              
139             Returns the section number.
140              
141             =cut
142              
143 0     0 1   sub number { $_[0]->{_number} };
144              
145              
146             =head2 raw_data
147              
148             Returns the raw_data from the file.
149              
150             =cut
151              
152 0     0 1   sub raw_data { $_[0]->{_raw_data} };
153              
154             =head2 sentence_count
155              
156             Returns the number of sentences.
157              
158             =cut
159              
160 0     0 1   sub sentence_count { $_[0]->{_report}->sentence_count };
161              
162              
163             =head2 title
164              
165             Returns the title.
166              
167             =cut
168              
169             sub title {
170 0     0 1   my ($self) = @_;
171 0           my (@lines) = split(/\n/, $self->raw_data() );
172 0           foreach my $line (@lines) {
173 0 0         if ( $line =~ m/TITLE:/ ) {
174 0           chomp($line);
175 0           $line =~ s/^\s*TITLE:\s*//;
176 0           $self->{_title} = $line;
177 0           last;
178             }
179             }
180 0           return $self->{_title};
181             }
182              
183             =head2 word_count
184              
185             Returns the word count of the section, minus title or header.
186              
187             =cut
188              
189 0     0 1   sub word_count { return $_[0]->{_report}->word_count; }
190              
191             =head2 word_list
192              
193             Returns a hash of the words used, in lowercase, with the usage count of that word as the value.
194              
195             =cut
196              
197             sub word_list {
198 0     0 1   my $self = shift;
199 0           my %word_list = $self->{_report}->sorted_word_list();
200 0           return %word_list;
201             }
202              
203             =head2 write_report
204              
205             Writes the report file.
206              
207             =cut
208              
209             sub write_report {
210 0     0 1   my $self = shift;
211 0           my $string = "Grade Level: " . $self->grade_level() . "\n";
212 0           $string .= "Word Frequency List:\n";
213 0           my %word_list = $self->{_report}->sorted_word_list();
214 0           my @unsorted_keys = ( keys %word_list );
215 0           my @sorted_keys = reverse ( sort { $a <=> $b } @unsorted_keys );
  0            
216 0           my $max_keys = 25;
217 0           foreach my $count ( @sorted_keys ){
218 0           $string .= " $count ";
219 0           foreach my $word ( @{$word_list{$count}} ){
  0            
220 0           $string .= " $word";
221             }
222 0           $string .= "\n";
223 0           $max_keys -= 1;
224 0 0         last unless $max_keys;
225             }
226 0           return $string;
227             }
228              
229              
230             =head2 _write_report
231              
232             Writes the report data.
233              
234             =cut
235              
236             sub _write_report {
237 0     0     my ($self) = @_;
238 0           my $text = $self->headless_data;
239 0           $self->{_report} = Book::Collate::Report->new( string => $self->headless_data() );
240             }
241              
242             =head2 _write_headless_data
243              
244             Writes the headless data.
245              
246             =cut
247              
248             sub _write_headless_data {
249 0     0     my ($self) = @_;
250 0           my $data;
251 0           ($data = $self->raw_data) =~ s/^TITLE:.*\n//;
252 0           $data =~ s/^\s*//;
253             # TODO: Not sure this covers multiple empty blank lines.
254 0 0         $data =~ s/^.*\n// if $self->{_has_header};
255 0           $data =~ s/^\s*(.*)\s*$/$1/;
256 0           $self->{_headless_data} = $data;
257             }
258              
259             =head1 AUTHOR
260              
261             Leam Hall, C<< >>
262              
263             =head1 BUGS
264              
265             Please report any bugs or feature requests to L.
266              
267              
268              
269              
270             =head1 SUPPORT
271              
272             You can find documentation for this module with the perldoc command.
273              
274             perldoc lib/Book/Collate/Section
275              
276              
277             You can also look for information at:
278              
279             =over 4
280              
281             =item * GitHub Project Page
282              
283             L
284              
285             =back
286              
287              
288             =head1 ACKNOWLEDGEMENTS
289              
290             Besides Larry Wall, you can blame the folks on IRC Libera#perl for this stuff existing.
291              
292              
293             =head1 LICENSE AND COPYRIGHT
294              
295             This software is Copyright (c) 2021 by Leam Hall.
296              
297             This is free software, licensed under:
298              
299             The Artistic License 2.0 (GPL Compatible)
300              
301              
302             =cut
303              
304             1; # End of Section