| 1 | package Text::Info::Readability; | ||||||||||
| 2 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 6 | extends 'Text::Info::BASE'; | |||||||||
| 6 | |||||||||||
| 7 | =encoding utf-8 | ||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Text::Info::Readability - Information about a text's readability. | ||||||||||
| 12 | |||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||
| 14 | |||||||||||
| 15 | See Wikipedia's article about L<readability tests|https://en.wikipedia.org/wiki/Readability_test> for more information. | ||||||||||
| 16 | |||||||||||
| 17 | You should never instantiate objects of the class directly, but instead | ||||||||||
| 18 | use L<Text::Info> to access this class' methods. | ||||||||||
| 19 | |||||||||||
| 20 | =head1 METHODS | ||||||||||
| 21 | |||||||||||
| 22 | =over | ||||||||||
| 23 | |||||||||||
| 24 | =item can_be_calculated() | ||||||||||
| 25 | |||||||||||
| 26 | Returns true if the text's readability can be calculated, false otherwise. | ||||||||||
| 27 | |||||||||||
| 28 | =cut | ||||||||||
| 29 | |||||||||||
| 30 | 6 | has 'can_be_calculated' => ( isa => 'Bool', is => 'ro', lazy_build => 1 ); | |||||||||
| 31 | |||||||||||
| 32 | 2 | ▶ | sub _build_can_be_calculated { | ||||||||
|
Subroutine
_build_can_be_calculated: called CC=4 SCAR=14.1
| |||||||||||
| 33 | 2 | my $self = shift; | |||||||||
| 34 | |||||||||||
| 35 | 2 | ▶ | return undef if ( $self->text eq '' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 1 | ▶ | return undef if ( $self->sentence_count == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 1 | ▶ | return undef if ( $self->word_count == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | return 1; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | =item fres() | ||||||||||
| 43 | |||||||||||
| 44 | Returns the text's "Flesch reading ease score" (FRES), a text readability score. | ||||||||||
| 45 | See L<Flesch–Kincaid readability tests|https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests> on Wikipedia for more information. | ||||||||||
| 46 | |||||||||||
| 47 | Returns undef is it's impossible to calculate the score, for example if the | ||||||||||
| 48 | there is no text, no sentences that could be detected etc. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | 6 | has 'fres' => ( isa => 'Maybe[Num]', is => 'ro', lazy_build => 1 ); | |||||||||
| 53 | |||||||||||
| 54 | 2 | ▶ | sub _build_fres { | ||||||||
|
Subroutine
_build_fres: called CC=2 SCAR=7.2
| |||||||||||
| 55 | 2 | my $self = shift; | |||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | return undef unless ( $self->can_be_calculated ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 1 | my $words_per_sentence = $self->word_count / $self->sentence_count; | |||||||||
| 60 | 1 | my $syllables_per_word = $self->syllable_count / $self->word_count; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $score = 206.835 - ( ($words_per_sentence * 1.015) + ($syllables_per_word * 84.6) ); | |||||||||
| 63 | |||||||||||
| 64 | 0 | return sprintf( '%.2f', $score ); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | =item fkrgl() | ||||||||||
| 68 | |||||||||||
| 69 | Returns the text's "Flesch–Kincaid reading grade level", a text readability score. | ||||||||||
| 70 | See L<Flesch–Kincaid readability tests|https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests> on Wikipedia for more information. | ||||||||||
| 71 | |||||||||||
| 72 | Returns undef is it's impossible to calculate the score, for example if the | ||||||||||
| 73 | there is no text, no sentences that could be detected etc. | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | 6 | has 'fkrgl' => ( isa => 'Maybe[Num]', is => 'ro', lazy_build => 1 ); | |||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub _build_fkrgl { | ||||||||
|
Subroutine
_build_fkrgl: not called CC=2 SCAR=17.9
| |||||||||||
| 80 | 0 | my $self = shift; | |||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | return undef unless ( $self->can_be_calculated ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | |||||||||||
| 84 | 0 | my $words_per_sentence = $self->word_count / $self->sentence_count; | |||||||||
| 85 | 0 | my $syllables_per_word = $self->syllable_count / $self->word_count; | |||||||||
| 86 | |||||||||||
| 87 | 0 | my $score = ( ($words_per_sentence * 0.39) + ($syllables_per_word * 11.8) ) - 15.59; | |||||||||
| 88 | |||||||||||
| 89 | 0 | return sprintf( '%.2f', $score ); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||
| 93 | |||||||||||
| 94 | 6 | 1; | |||||||||
| 95 | |||||||||||
| 96 | =back | ||||||||||
| 97 | |||||||||||
| 98 | =head1 SEE ALSO | ||||||||||
| 99 | |||||||||||
| 100 | =over 4 | ||||||||||
| 101 | |||||||||||
| 102 | =item * L<Text::Info> | ||||||||||
| 103 | |||||||||||
| 104 | =back | ||||||||||
| 105 | |||||||||||
| 106 | =head1 AUTHOR | ||||||||||
| 107 | |||||||||||
| 108 | Tore Aursand, C<< <toreau at gmail.com> >> | ||||||||||
| 109 | |||||||||||
| 110 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 111 | |||||||||||
| 112 | Copyright 2015 Tore Aursand. | ||||||||||
| 113 | |||||||||||
| 114 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 115 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 116 | copy of the full license at: | ||||||||||
| 117 | |||||||||||
| 118 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 119 | |||||||||||
| 120 | Any use, modification, and distribution of the Standard or Modified | ||||||||||
| 121 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||
| 122 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||
| 123 | or distribute the Package, if you do not accept this license. | ||||||||||
| 124 | |||||||||||
| 125 | If your Modified Version has been derived from a Modified Version made | ||||||||||
| 126 | by someone other than you, you are nevertheless required to ensure that | ||||||||||
| 127 | your Modified Version complies with the requirements of this license. | ||||||||||
| 128 | |||||||||||
| 129 | This license does not grant you the right to use any trademark, service | ||||||||||
| 130 | mark, tradename, or logo of the Copyright Holder. | ||||||||||
| 131 | |||||||||||
| 132 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||
| 133 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||
| 134 | otherwise transfer the Package with respect to any patent claims | ||||||||||
| 135 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||
| 136 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||
| 137 | counterclaim) against any party alleging that the Package constitutes | ||||||||||
| 138 | direct or contributory patent infringement, then this Artistic License | ||||||||||
| 139 | to you shall terminate on the date that such litigation is filed. | ||||||||||
| 140 | |||||||||||
| 141 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||
| 142 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||
| 143 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||
| 144 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||
| 145 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||
| 146 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||
| 147 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||
| 148 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||