| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Lingua::EN::Fathom - Measure readability of English text | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use Lingua::EN::Fathom; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my $text = Lingua::EN::Fathom->new(); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | $text->analyse_file("sample.txt"); # Analyse contents of a text file | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | $accumulate = 1; | ||||||||||||||||||||||||||||
| 14 | $text->analyse_block($text_string,$accumulate); # Analyse contents of a text string | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Methods to return statistics on the analysed text | ||||||||||||||||||||||||||||
| 17 | $text->num_chars; | ||||||||||||||||||||||||||||
| 18 | $text->num_words; | ||||||||||||||||||||||||||||
| 19 | $text->percent_complex_words; | ||||||||||||||||||||||||||||
| 20 | $text->num_sentences; | ||||||||||||||||||||||||||||
| 21 | $text->num_text_lines; | ||||||||||||||||||||||||||||
| 22 | $text->num_non_text_lines; | ||||||||||||||||||||||||||||
| 23 | $text->num_blank_lines; # trailing EOLs are ignored | ||||||||||||||||||||||||||||
| 24 | $text->num_paragraphs; | ||||||||||||||||||||||||||||
| 25 | $text->syllables_per_word; | ||||||||||||||||||||||||||||
| 26 | $text->words_per_sentence; | ||||||||||||||||||||||||||||
| 27 | $text->unique_words; | ||||||||||||||||||||||||||||
| 28 | $text->fog; | ||||||||||||||||||||||||||||
| 29 | $text->flesch; | ||||||||||||||||||||||||||||
| 30 | $text->kincaid; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Call all of the above methods and present as a formatted report | ||||||||||||||||||||||||||||
| 33 | print($text->report); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # get a hash of unique words, keyed by word and occurrence as the value | ||||||||||||||||||||||||||||
| 36 | $text->unique_words | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Print a list of unique words | ||||||||||||||||||||||||||||
| 39 | %words = $text->unique_words; | ||||||||||||||||||||||||||||
| 40 | foreach $word ( sort keys %words ) | ||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | print("$words{$word} :$word\n"); | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 REQUIRES | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Lingua::EN::Syllable, Lingua::EN::Sentence | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | This module analyses English text in either a string or file. Totals are | ||||||||||||||||||||||||||||
| 54 | then calculated for the number of characters, words, sentences, blank | ||||||||||||||||||||||||||||
| 55 | and non blank (text) lines and paragraphs. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Three common readability statistics are also derived, the Fog, Flesch and | ||||||||||||||||||||||||||||
| 58 | Kincaid indices. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | All of these properties can be accessed through individual methods, or by | ||||||||||||||||||||||||||||
| 61 | generating a text report. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | A hash of all unique words and the number of times they occur is generated. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 METHODS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 new | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | The C<new> method creates an instance of an text object This must be called | ||||||||||||||||||||||||||||
| 71 | before any of the following methods are invoked. Note that the object only | ||||||||||||||||||||||||||||
| 72 | needs to be created once, and can be reused with new input data. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | my $text = Lingua::EN::Fathom->new(); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 analyse_file | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | The C<analyse_file> method takes as input the name of a text file. Various | ||||||||||||||||||||||||||||
| 79 | text based statistics are calculated for the file. This method and | ||||||||||||||||||||||||||||
| 80 | C<analyse_block> are prerequisites for all the following methods. An optional | ||||||||||||||||||||||||||||
| 81 | argument may be supplied to control accumulation of statistics. If set to | ||||||||||||||||||||||||||||
| 82 | a non zero value, all statistics are accumulated with each successive call. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | $text->analyse_file("sample.txt"); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 analyse_block | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | The C<analyse_block> method takes as input a text string. Various | ||||||||||||||||||||||||||||
| 90 | text based statistics are calculated for the file. This method and | ||||||||||||||||||||||||||||
| 91 | C<analyse_file> are prerequisites for all the following methods. An optional | ||||||||||||||||||||||||||||
| 92 | argument may be supplied to control accumulation of statistics. If set to | ||||||||||||||||||||||||||||
| 93 | a non zero value, all statistics are accumulated with each successive call. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | $text->analyse_block($text_str); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 num_chars | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Returns the number of characters in the analysed text file or block. This | ||||||||||||||||||||||||||||
| 100 | includes characters such as spaces, and punctuation marks. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 num_words | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Returns the number of words in the analysed text file or block. A word must | ||||||||||||||||||||||||||||
| 105 | consist of letters a-z with at least one vowel sound, and optionally an | ||||||||||||||||||||||||||||
| 106 | apostrophe or hyphen. Items such as "&, K108, NW" are not counted as words. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 percent_complex_words | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Returns the percentage of complex words in the analysed text file or block. A | ||||||||||||||||||||||||||||
| 111 | complex word must consist of three or more syllables. This statistic is used to | ||||||||||||||||||||||||||||
| 112 | calculate the fog index. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 num_sentences | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Returns the number of sentences in the analysed text file or block. A sentence | ||||||||||||||||||||||||||||
| 117 | is any group of words and non words terminated with a single full stop. Spaces | ||||||||||||||||||||||||||||
| 118 | may occur before and after the full stop. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head2 num_text_lines | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Returns the number of lines containing some text in the analysed | ||||||||||||||||||||||||||||
| 124 | text file or block. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 num_blank_lines | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Returns the number of lines NOT containing any text in the analysed | ||||||||||||||||||||||||||||
| 129 | text file or block. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head2 num_paragraphs | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Returns the number of paragraphs in the analysed text file or block. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 syllables_per_word | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Returns the average number of syllables per word in the analysed | ||||||||||||||||||||||||||||
| 138 | text file or block. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 words_per_sentence | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Returns the average number of words per sentence in the analysed | ||||||||||||||||||||||||||||
| 143 | text file or block. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 READABILITY | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Three indices of text readability are calculated. They all measure complexity as | ||||||||||||||||||||||||||||
| 150 | a function of syllables per word and words per sentence. They assume the text is | ||||||||||||||||||||||||||||
| 151 | well formed and logical. You could analyse a passage of nonsensical English and | ||||||||||||||||||||||||||||
| 152 | find the readability is quite good, provided the words are not too complex and | ||||||||||||||||||||||||||||
| 153 | the sentences not too long. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | For more information see: L<http://www.plainlanguage.com/Resources/readability.html> | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 fog | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Returns the Fog index for the analysed text file or block. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | ( words_per_sentence + percent_complex_words ) * 0.4 | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | The Fog index, developed by Robert Gunning, is a well known and simple | ||||||||||||||||||||||||||||
| 165 | formula for measuring readability. The index indicates the number of years | ||||||||||||||||||||||||||||
| 166 | of formal education a reader of average intelligence would need to read the | ||||||||||||||||||||||||||||
| 167 | text once and understand that piece of writing with its word sentence workload. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 18 unreadable | ||||||||||||||||||||||||||||
| 170 | 14 difficult | ||||||||||||||||||||||||||||
| 171 | 12 ideal | ||||||||||||||||||||||||||||
| 172 | 10 acceptable | ||||||||||||||||||||||||||||
| 173 | 8 childish | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 flesch | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Returns the Flesch reading ease score for the analysed text file or block. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 206.835 - (1.015 * words_per_sentence) - (84.6 * syllables_per_word) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | This score rates text on a 100 point scale. The higher the score, the easier | ||||||||||||||||||||||||||||
| 183 | it is to understand the text. A score of 60 to 70 is considered to be optimal. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 kincaid | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Returns the Flesch-Kincaid grade level score for the analysed text | ||||||||||||||||||||||||||||
| 189 | file or block. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | (11.8 * syllables_per_word) + (0.39 * words_per_sentence) - 15.59; | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | This score rates text on U.S. grade school level. So a score of 8.0 means | ||||||||||||||||||||||||||||
| 194 | that the document can be understood by an eighth grader. A score of 7.0 to | ||||||||||||||||||||||||||||
| 195 | 8.0 is considered to be optimal. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 unique_words | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Returns a hash of unique words. The words (in lower case) are held in | ||||||||||||||||||||||||||||
| 200 | the hash keys while the number of occurrences are held in the hash values. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 report | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | print($text->report); | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Produces a text based report containing all Fathom statistics for | ||||||||||||||||||||||||||||
| 208 | the currently analysed text block or file. For example: | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Number of characters : 813 | ||||||||||||||||||||||||||||
| 211 | Number of words : 135 | ||||||||||||||||||||||||||||
| 212 | Percent of complex words : 20.00 | ||||||||||||||||||||||||||||
| 213 | Average syllables per word : 1.7704 | ||||||||||||||||||||||||||||
| 214 | Number of sentences : 12 | ||||||||||||||||||||||||||||
| 215 | Average words per sentence : 11.2500 | ||||||||||||||||||||||||||||
| 216 | Number of text lines : 13 | ||||||||||||||||||||||||||||
| 217 | Number of blank lines : 8 | ||||||||||||||||||||||||||||
| 218 | Number of paragraphs : 4 | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | READABILITY INDICES | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Fog : 12.5000 | ||||||||||||||||||||||||||||
| 224 | Flesch : 45.6429 | ||||||||||||||||||||||||||||
| 225 | Flesch-Kincaid : 9.6879 | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | The return value is a string containing the report contents | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | L<Lingua::EN::Syllable>,L<Lingua::EN::Sentence>,L<B::Fathom> | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head1 POSSIBLE EXTENSIONS | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Count white space and punctuation characters | ||||||||||||||||||||||||||||
| 238 | Allow user control over what strictly defines a word | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | The syllable count provided in Lingua::EN::Syllable is about 90% accurate | ||||||||||||||||||||||||||||
| 243 | Acronyms that contain vowels, like GPO, will be counted as words. | ||||||||||||||||||||||||||||
| 244 | The fog index should exclude proper names | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Lingua::EN::Fathom was written by Kim Ryan <kimryan at cpan dot org>. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Copyright (c) 2023 Kim Ryan. All rights reserved. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 257 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | package Lingua::EN::Fathom; | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 1 | ▶ | use Lingua::EN::Syllable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 266 | 1 | ▶ | use Lingua::EN::Sentence; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 267 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 268 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 1 | our $VERSION = '1.24'; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 273 | # Create a new instance of a text object. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | sub new | ||||||||||||||||||||||||||||
| 276 | { | ||||||||||||||||||||||||||||
| 277 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 1 | my $text = {}; | |||||||||||||||||||||||||||
| 280 | 1 | bless($text,$class); | |||||||||||||||||||||||||||
| 281 | 1 | $text = &_initialize($text); | |||||||||||||||||||||||||||
| 282 | 1 | return($text); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 285 | # Analyse text stored in a file, reading from the file one line at a time | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | sub analyse_file | ||||||||||||||||||||||||||||
| 288 | { | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
analyse_file: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 290 | 0 | my ($file_name,$accumulate) = @_; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | unless ( $accumulate ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | { | ||||||||||||||||||||||||||||
| 294 | 0 | $text = _initialize($text); | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | $text->{file_name} = $file_name; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Only analyse non-empty text files | ||||||||||||||||||||||||||||
| 300 | 0 | ▶ | unless ( -T $file_name and -s $file_name ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-T $file_name and -s $file_name
MC/DC0%0/2
-T $file_name and -s $file_name
-T $file_name
-s $file_name
Truth table0%0/3
A and B
A= -T $file_nameB= -s $file_name
| |||||||||||||||||||||||||||||
| 301 | { | ||||||||||||||||||||||||||||
| 302 | 0 | return($text); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 0 | open(IN_FH,"<$file_name"); | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 0 | my $in_paragraph = 0; | |||||||||||||||||||||||||||
| 308 | 0 | my $all_text; | |||||||||||||||||||||||||||
| 309 | 0 | ▶ | while ( <IN_FH> ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | { | ||||||||||||||||||||||||||||
| 311 | 0 | my $one_line = $_; | |||||||||||||||||||||||||||
| 312 | 0 | $all_text .= $one_line; | |||||||||||||||||||||||||||
| 313 | 0 | ($in_paragraph,$text) = _analyse_line($text,$one_line,$in_paragraph); | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | 0 | close(IN_FH); | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 0 | my $sentences= Lingua::EN::Sentence::get_sentences($all_text); | |||||||||||||||||||||||||||
| 318 | 0 | $text->{num_sentences} = scalar(@$sentences); | |||||||||||||||||||||||||||
| 319 | 0 | $text->_calculate_readability; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | return($text); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 324 | # Analyse a block of text, stored as a string. The string may contain line | ||||||||||||||||||||||||||||
| 325 | # terminators. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | sub analyse_block | ||||||||||||||||||||||||||||
| 328 | { | ||||||||||||||||||||||||||||
| 329 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
analyse_block: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 330 | 1 | my ($block,$accumulate) = @_; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 1 | ▶ | unless ( $accumulate ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | { | ||||||||||||||||||||||||||||
| 334 | 1 | $text = _initialize($text); | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 1 | ▶ | unless ( $block ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | { | ||||||||||||||||||||||||||||
| 339 | 0 | return($text); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 1 | my $in_paragraph = 0; | |||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # Split on EOL character | ||||||||||||||||||||||||||||
| 345 | # repeating trailing line terminators are stripped | ||||||||||||||||||||||||||||
| 346 | 1 | my @all_lines = split(/\n/,$block); | |||||||||||||||||||||||||||
| 347 | 1 | my $one_line; | |||||||||||||||||||||||||||
| 348 | 1 | foreach $one_line ( @all_lines ) | |||||||||||||||||||||||||||
| 349 | { | ||||||||||||||||||||||||||||
| 350 | 9 | ($in_paragraph,$text) = _analyse_line($text,$one_line,$in_paragraph); | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 1 | my $sentences= Lingua::EN::Sentence::get_sentences($block); | |||||||||||||||||||||||||||
| 354 | 1 | ▶ | if (defined($sentences)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | { | ||||||||||||||||||||||||||||
| 356 | 1 | $text->{num_sentences} = scalar(@$sentences); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 1 | $text->_calculate_readability; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 1 | return($text); | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 365 | sub num_chars | ||||||||||||||||||||||||||||
| 366 | { | ||||||||||||||||||||||||||||
| 367 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
num_chars: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 368 | 1 | return($text->{num_chars}); | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 371 | sub num_words | ||||||||||||||||||||||||||||
| 372 | { | ||||||||||||||||||||||||||||
| 373 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
num_words: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 374 | 1 | return($text->{num_words}); | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 377 | sub percent_complex_words | ||||||||||||||||||||||||||||
| 378 | { | ||||||||||||||||||||||||||||
| 379 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
percent_complex_words: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 380 | 0 | return($text->{percent_complex_words}); | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 383 | sub num_sentences | ||||||||||||||||||||||||||||
| 384 | { | ||||||||||||||||||||||||||||
| 385 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
num_sentences: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 386 | 1 | return($text->{num_sentences}); | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 389 | sub num_text_lines | ||||||||||||||||||||||||||||
| 390 | { | ||||||||||||||||||||||||||||
| 391 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
num_text_lines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 392 | 1 | return($text->{num_text_lines}); | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 395 | sub num_non_text_lines | ||||||||||||||||||||||||||||
| 396 | { | ||||||||||||||||||||||||||||
| 397 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
num_non_text_lines: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 398 | 1 | return($text->{num_non_text_lines}); | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 401 | sub num_blank_lines | ||||||||||||||||||||||||||||
| 402 | { | ||||||||||||||||||||||||||||
| 403 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
num_blank_lines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 404 | 1 | return($text->{num_blank_lines}); | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 407 | sub num_paragraphs | ||||||||||||||||||||||||||||
| 408 | { | ||||||||||||||||||||||||||||
| 409 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
num_paragraphs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 410 | 1 | return($text->{num_paragraphs}); | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 413 | sub syllables_per_word | ||||||||||||||||||||||||||||
| 414 | { | ||||||||||||||||||||||||||||
| 415 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
syllables_per_word: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 416 | 0 | return($text->{syllables_per_word}); | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 419 | sub words_per_sentence | ||||||||||||||||||||||||||||
| 420 | { | ||||||||||||||||||||||||||||
| 421 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
words_per_sentence: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 422 | 0 | return($text->{words_per_sentence}); | |||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 425 | sub fog | ||||||||||||||||||||||||||||
| 426 | { | ||||||||||||||||||||||||||||
| 427 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
fog: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 428 | 0 | return($text->{fog}); | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 431 | sub flesch | ||||||||||||||||||||||||||||
| 432 | { | ||||||||||||||||||||||||||||
| 433 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
flesch: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 434 | 0 | return($text->{flesch}); | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 437 | sub kincaid | ||||||||||||||||||||||||||||
| 438 | { | ||||||||||||||||||||||||||||
| 439 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
kincaid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 440 | 0 | return($text->{kincaid}); | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 443 | # Return anonymous hash of all the unique words in analysed text. The words | ||||||||||||||||||||||||||||
| 444 | # occurrence count is stored in the hash value. | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | sub unique_words | ||||||||||||||||||||||||||||
| 447 | { | ||||||||||||||||||||||||||||
| 448 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
unique_words: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 449 | 0 | ▶ | if ( $text->{unique_words} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 450 | { | ||||||||||||||||||||||||||||
| 451 | 0 | return( %{ $text->{unique_words} } ); | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | else | ||||||||||||||||||||||||||||
| 454 | { | ||||||||||||||||||||||||||||
| 455 | 0 | return(undef); | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 459 | # Provide a formatted text report of all statistics for a text object. | ||||||||||||||||||||||||||||
| 460 | # Return report as a string. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | sub report | ||||||||||||||||||||||||||||
| 463 | { | ||||||||||||||||||||||||||||
| 464 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
report: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 465 | 0 | my $report = ''; | |||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | $text->{file_name} and | ||||||||||||||||||||||||||||
| 469 | 0 | ▶ | $report .= sprintf("File name : %s\n",$text->{file_name} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 0 | $report .= sprintf("Number of characters : %d\n", $text->num_chars); | |||||||||||||||||||||||||||
| 472 | 0 | $report .= sprintf("Number of words : %d\n", $text->num_words); | |||||||||||||||||||||||||||
| 473 | 0 | $report .= sprintf("Percent of complex words : %.2f\n",$text->percent_complex_words); | |||||||||||||||||||||||||||
| 474 | 0 | $report .= sprintf("Average syllables per word : %.4f\n",$text->syllables_per_word); | |||||||||||||||||||||||||||
| 475 | 0 | $report .= sprintf("Number of sentences : %d\n", $text->num_sentences); | |||||||||||||||||||||||||||
| 476 | 0 | $report .= sprintf("Average words per sentence : %.4f\n",$text->words_per_sentence); | |||||||||||||||||||||||||||
| 477 | 0 | $report .= sprintf("Number of text lines : %d\n", $text->num_text_lines); | |||||||||||||||||||||||||||
| 478 | 0 | $report .= sprintf("Number of non-text lines : %d\n", $text->num_non_text_lines); | |||||||||||||||||||||||||||
| 479 | 0 | $report .= sprintf("Number of blank lines : %d\n", $text->num_blank_lines); | |||||||||||||||||||||||||||
| 480 | 0 | $report .= sprintf("Number of paragraphs : %d\n", $text->num_paragraphs); | |||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 0 | $report .= "\n\nREADABILITY INDICES\n\n"; | |||||||||||||||||||||||||||
| 483 | 0 | $report .= sprintf("Fog : %.4f\n",$text->fog); | |||||||||||||||||||||||||||
| 484 | 0 | $report .= sprintf("Flesch : %.4f\n",$text->flesch); | |||||||||||||||||||||||||||
| 485 | 0 | $report .= sprintf("Flesch-Kincaid : %.4f\n",$text->kincaid); | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 0 | return($report); | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 491 | # PRIVATE METHODS | ||||||||||||||||||||||||||||
| 492 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 493 | sub _initialize | ||||||||||||||||||||||||||||
| 494 | { | ||||||||||||||||||||||||||||
| 495 | 2 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 2 | $text->{num_chars} = 0; | |||||||||||||||||||||||||||
| 498 | 2 | $text->{num_syllables} = 0; | |||||||||||||||||||||||||||
| 499 | 2 | $text->{num_words} = 0; | |||||||||||||||||||||||||||
| 500 | 2 | $text->{num_complex_words} = 0; | |||||||||||||||||||||||||||
| 501 | 2 | $text->{syllables_per_word} = 0; | |||||||||||||||||||||||||||
| 502 | 2 | $text->{words_per_sentence} = 0; | |||||||||||||||||||||||||||
| 503 | 2 | $text->{percent_complex_words} = 0; | |||||||||||||||||||||||||||
| 504 | 2 | $text->{num_text_lines} = 0; | |||||||||||||||||||||||||||
| 505 | 2 | $text->{num_non_text_lines} = 0; | |||||||||||||||||||||||||||
| 506 | 2 | $text->{num_blank_lines} = 0; | |||||||||||||||||||||||||||
| 507 | 2 | $text->{num_paragraphs} = 0; | |||||||||||||||||||||||||||
| 508 | 2 | $text->{num_sentences} = 0; | |||||||||||||||||||||||||||
| 509 | 2 | $text->{unique_words} = (); | |||||||||||||||||||||||||||
| 510 | 2 | $text->{file_name} = ''; | |||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 2 | $text->{fog} = 0; | |||||||||||||||||||||||||||
| 513 | 2 | $text->{flesch} = 0; | |||||||||||||||||||||||||||
| 514 | 2 | $text->{kincaid} = 0; | |||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 2 | return($text); | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 519 | # Increment number of text lines, blank lines and paragraphs | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | sub _analyse_line | ||||||||||||||||||||||||||||
| 522 | { | ||||||||||||||||||||||||||||
| 523 | 9 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
_analyse_line: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 9 | my ($one_line,$in_paragraph) = @_; | |||||||||||||||||||||||||||
| 526 | 9 | ▶ | if ( $one_line =~ /\w/ ) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 527 | { | ||||||||||||||||||||||||||||
| 528 | 6 | chomp($one_line); | |||||||||||||||||||||||||||
| 529 | 6 | $text = _analyse_words($text,$one_line); | |||||||||||||||||||||||||||
| 530 | 6 | $text->{num_text_lines}++; | |||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | 6 | ▶ | unless ( $in_paragraph ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 533 | { | ||||||||||||||||||||||||||||
| 534 | 2 | $text->{num_paragraphs}++; | |||||||||||||||||||||||||||
| 535 | 2 | $in_paragraph = 1; | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||
| 538 | elsif ($one_line eq '' ) # empty line | ||||||||||||||||||||||||||||
| 539 | { | ||||||||||||||||||||||||||||
| 540 | 2 | $text->{num_blank_lines}++; | |||||||||||||||||||||||||||
| 541 | 2 | $in_paragraph = 0; | |||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||
| 543 | elsif ($one_line =~ /^\W+$/ ) # non text | ||||||||||||||||||||||||||||
| 544 | { | ||||||||||||||||||||||||||||
| 545 | 1 | $text->{num_non_text_lines}++; | |||||||||||||||||||||||||||
| 546 | 1 | $in_paragraph = 0; | |||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||
| 548 | 9 | return($in_paragraph,$text); | |||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||
| 550 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 551 | # Try to detect real words in line. Increment syllable, word, and complex word counters. | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | sub _analyse_words | ||||||||||||||||||||||||||||
| 554 | { | ||||||||||||||||||||||||||||
| 555 | 6 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
_analyse_words: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 556 | 6 | my ($one_line) = @_; | |||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | 6 | $text->{num_chars} += length($one_line); | |||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | # Word found, such as: twice, BOTH, a, I'd, non-plussed .. | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | # Ignore words like 'Mr.', K12, &, X.Y.Z ... | ||||||||||||||||||||||||||||
| 563 | # It could be argued that Mr. is a word, but this approach should detect most of the non words | ||||||||||||||||||||||||||||
| 564 | # which have punctuation or numbers in them | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | 6 | ▶ | while ( $one_line =~ /\b([a-z][-'a-z]*)\b/ig ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 567 | { | ||||||||||||||||||||||||||||
| 568 | 57 | my $one_word = $1; | |||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | # Try to filter out acronyms and abbreviations by accepting | ||||||||||||||||||||||||||||
| 571 | # words with a vowel sound. This won't work for GPO etc. | ||||||||||||||||||||||||||||
| 572 | 57 | ▶ | next unless $one_word =~ /[aeiouy]/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | # Test for valid hyphenated word like be-bop | ||||||||||||||||||||||||||||
| 575 | 55 | ▶ | if ( $one_word =~ /-/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 576 | { | ||||||||||||||||||||||||||||
| 577 | 1 | ▶ | next unless $one_word =~ /[a-z]{2,}-[a-z]{2,}/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | # word frequency count | ||||||||||||||||||||||||||||
| 581 | 54 | $text->{unique_words}{lc($one_word)}++; | |||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | 54 | $text->{num_words}++; | |||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | # Use subroutine from Lingua::EN::Syllable | ||||||||||||||||||||||||||||
| 586 | 54 | my $num_syllables_current_word = syllable($one_word); | |||||||||||||||||||||||||||
| 587 | 54 | $text->{num_syllables} += $num_syllables_current_word; | |||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | # Required for Fog index, count non hyphenated words of 3 or more | ||||||||||||||||||||||||||||
| 590 | # syllables. Should add check for proper names in here as well | ||||||||||||||||||||||||||||
| 591 | 54 | ▶ | if ( $num_syllables_current_word > 2 and $one_word !~ /-/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$num_syllables_current_word > 2 and not $one_word =~ /-/
MC/DC50%1/2
$num_syllables_current_word > 2 and not $one_word =~ /-/
$num_syllables_current_word > 2
not $one_word =~ /-/
Truth table66%2/3
A and not(B)
A= $num_syllables_current_word > 2B= not $one_word =~ /-/
| |||||||||||||||||||||||||||||
| 592 | { | ||||||||||||||||||||||||||||
| 593 | 4 | $text->{num_complex_words}++; | |||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | 6 | return($text); | |||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||
| 599 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 600 | # Determine the three readability indices | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | sub _calculate_readability | ||||||||||||||||||||||||||||
| 603 | { | ||||||||||||||||||||||||||||
| 604 | 1 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
_calculate_readability: called CC=3 SCAR=13.8
| |||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | 1 | ▶ | if ( $text->{num_sentences} and $text->{num_words} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$text->{"num_sentences"} and $text->{"num_words"}
MC/DC0%0/2
$text->{"num_sentences"} and $text->{"num_words"}
$text->{"num_sentences"}
$text->{"num_words"}
Truth table33%1/3
A and B
A= $text->{"num_sentences"}B= $text->{"num_words"}
| |||||||||||||||||||||||||||||
| 607 | { | ||||||||||||||||||||||||||||
| 608 | 1 | $text->{words_per_sentence} = $text->{num_words} / $text->{num_sentences}; | |||||||||||||||||||||||||||
| 609 | 1 | $text->{syllables_per_word} = $text->{num_syllables} / $text->{num_words}; | |||||||||||||||||||||||||||
| 610 | $text->{percent_complex_words} = | ||||||||||||||||||||||||||||
| 611 | 1 | ( $text->{num_complex_words} / $text->{num_words} ) * 100; | |||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | 1 | $text->{fog} = ( $text->{words_per_sentence} + $text->{percent_complex_words} ) * 0.4; | |||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | $text->{flesch} = 206.835 - (1.015 * $text->{words_per_sentence}) - | ||||||||||||||||||||||||||||
| 616 | 1 | (84.6 * $text->{syllables_per_word}); | |||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | $text->{kincaid} = (11.8 * $text->{syllables_per_word}) + | ||||||||||||||||||||||||||||
| 619 | 1 | (0.39 * $text->{words_per_sentence}) - 15.59; | |||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||
| 621 | else | ||||||||||||||||||||||||||||
| 622 | { | ||||||||||||||||||||||||||||
| 623 | 0 | $text->{words_per_sentence} = 0; | |||||||||||||||||||||||||||
| 624 | 0 | $text->{syllables_per_word} = 0; | |||||||||||||||||||||||||||
| 625 | 0 | $text->{num_complex_words} = 0; | |||||||||||||||||||||||||||
| 626 | 0 | $text->{fog} = 0; | |||||||||||||||||||||||||||
| 627 | 0 | $text->{flesch} = 0; | |||||||||||||||||||||||||||
| 628 | 0 | $text->{kincaid} = 0; | |||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||
| 631 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 632 | 1 | return(1); | |||||||||||||||||||||||||||