Book-Collate
view release on metacpan or search on metacpan
lib/Book/Collate/Report.pm view on Meta::CPAN
=head2 new
=cut
sub new {
my ($class, %data) = @_;
my $self = {
_data => undef,
_string => $data{string},
_words => [],
_fry_used => undef,
_custom_words => $data{custom_words},
_weak_words => $data{weak_words},
};
bless $self, $class;
(my $data_set = $self->{_string}) =~ s/[;:!'"?.,]/ /g;
$data_set =~ s/ (d|ll|m|re|s|t|ve) / /g;
$data_set =~ s/\s*(.*)\s*/$1/;
$self->{_data} = $data_set;
$self->{_words} = [ split(/\s+/, $self->{_data}) ];
$self->{_word_list} = $self->word_list($self->{_words});
$self->{_fry_used} = $self->_generate_fry_stats();
$self->{_weak_used} = $self->_generate_weak_used();
return $self;
}
=head2 avg_sentence_length
Returns the average sentence length
=cut
sub avg_sentence_length {
my $self = shift;
return $self->word_count / $self->sentence_count ;
}
=head2 avg_word_length
Returns the average word length
=cut
sub avg_word_length {
my $self = shift;
my $character_count = 0;
foreach my $word ( $self->words() ){
$character_count += length($word);
}
return $character_count / $self->word_count ;
}
=head2 _generate_fry_stats
Gives a percentage of Fry list words used against the total unique words used.
=cut
sub _generate_fry_stats {
my $self = shift;
my %word_list = $self->word_list($self->{_words});
my %custom_words;
if ( defined( $self->{_custom_words} ) ){
%custom_words = %{$self->{_custom_words} };
}
my %fry_words = %Book::Collate::Words::fry;
my %used_words;
my %missed;
my %fry_used = (
fry => 0,
custom => 0,
miss => 0,
);
foreach my $word ( keys %word_list ){
$word = Book::Collate::Utils::scrub_word($word);
$used_words{$word} = 1;
}
foreach my $word ( keys %used_words ){
if ( defined($fry_words{$word}) ){
$fry_used{fry}++;
} elsif ( defined($custom_words{$word}) ){
$fry_used{custom}++;
} else {
$fry_used{miss}++;
$missed{$word} = 1;
}
}
return %fry_used;
}
=head2 _generate_weak_used
Returns a hash of weak words used, with the word as key and the count as value.
=cut
sub _generate_weak_used {
my $self = shift;
my %weak_used;
my %weak_words;
if ( defined( $self->{_weak_words} ) ){
%weak_words = %{$self->{_weak_words} };
}
foreach my $word ( $self->words() ){
$word = lc($word);
if ( exists( $weak_words{$word} ) ) {
$weak_used{$word} += 1;
}
}
return \%weak_used;
}
=head2 grade_level
( run in 2.453 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )