| File: | lib/CheckSpelling/UnknownWordSplitter.pm |
| Coverage: | 87.3% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | #! -*-perl-*- | |||||
| 2 | ||||||
| 3 | # ~/bin/w | |||||
| 4 | # Search for potentially misspelled words | |||||
| 5 | # Output is: | |||||
| 6 | # misspellled | |||||
| 7 | # woord (WOORD, Woord, woord, woord's) | |||||
| 8 | package CheckSpelling::UnknownWordSplitter; | |||||
| 9 | ||||||
| 10 | 1 1 | 118043 3 | use 5.022; | |||
| 11 | 1 1 1 | 2 1 64 | use feature 'unicode_strings'; | |||
| 12 | 1 1 1 | 2 2 10 | use strict; | |||
| 13 | 1 1 1 | 1 1 25 | use warnings; | |||
| 14 | 1 1 1 | 2 0 17 | no warnings qw(experimental::vlb); | |||
| 15 | 1 1 1 | 2 1 35 | use Encode qw/decode_utf8 encode FB_DEFAULT/; | |||
| 16 | 1 1 1 | 2 1 33 | use File::Basename; | |||
| 17 | 1 1 1 | 2 0 22 | use Cwd 'abs_path'; | |||
| 18 | 1 1 1 | 3 1 18 | use File::Temp qw/ tempfile tempdir /; | |||
| 19 | 1 1 1 | 306 1 2878 | use CheckSpelling::Util; | |||
| 20 | our $VERSION='0.1.0'; | |||||
| 21 | ||||||
| 22 | my ($longest_word, $shortest_word, $word_match, $forbidden_re, $patterns_re, $candidates_re, $disable_word_collating, $check_file_names); | |||||
| 23 | my ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||||
| 24 | my ($shortest, $longest) = (255, 0); | |||||
| 25 | my @forbidden_re_list; | |||||
| 26 | my @candidates_re_list; | |||||
| 27 | my $hunspell_dictionary_path; | |||||
| 28 | my @hunspell_dictionaries; | |||||
| 29 | my %dictionary = (); | |||||
| 30 | my $base_dict; | |||||
| 31 | my %unique; | |||||
| 32 | my %unique_unrecognized; | |||||
| 33 | my ($last_file, $words, $unrecognized) = ('', 0, 0); | |||||
| 34 | ||||||
| 35 | my $disable_flags; | |||||
| 36 | ||||||
| 37 | sub test_re { | |||||
| 38 | 19 | 14 | my ($expression) = @_; | |||
| 39 | 19 19 | 11 176 | return eval { qr /$expression/ }; | |||
| 40 | } | |||||
| 41 | ||||||
| 42 | sub quote_re { | |||||
| 43 | 19 | 15 | my ($expression) = @_; | |||
| 44 | 19 | 20 | return $expression if $expression =~ /\?\{/; | |||
| 45 | 19 | 47 | $expression =~ s/ | |||
| 46 | \G | |||||
| 47 | ( | |||||
| 48 | (?:[^\\]|\\[^Q])* | |||||
| 49 | ) | |||||
| 50 | (?: | |||||
| 51 | \\Q | |||||
| 52 | (?:[^\\]|\\[^E])* | |||||
| 53 | (?:\\E)? | |||||
| 54 | )? | |||||
| 55 | / | |||||
| 56 | 38 | 66 | $1 . (defined($2) ? quotemeta($2) : '') | |||
| 57 | /xge; | |||||
| 58 | 19 | 23 | return $expression; | |||
| 59 | } | |||||
| 60 | ||||||
| 61 | sub file_to_list { | |||||
| 62 | 4 | 1641 | my ($re) = @_; | |||
| 63 | 4 | 4 | my @file; | |||
| 64 | 4 | 38 | return @file unless open(FILE, '<:utf8', $re); | |||
| 65 | ||||||
| 66 | 4 | 9 | local $/=undef; | |||
| 67 | 4 | 26 | my $file=<FILE>; | |||
| 68 | 4 | 13 | close FILE; | |||
| 69 | 4 | 4 | my $line_number = 0; | |||
| 70 | 4 | 18 | for (split /\R/, $file) { | |||
| 71 | 17 | 16 | ++$line_number; | |||
| 72 | 17 | 26 | next if /^#/; | |||
| 73 | 12 | 6 | chomp; | |||
| 74 | 12 | 38 | next unless s/^(.+)/(?:$1)/; | |||
| 75 | 9 | 10 | my $quoted = quote_re($1); | |||
| 76 | 9 | 12 | if (test_re $quoted) { | |||
| 77 | 8 | 12 | push @file, $_; | |||
| 78 | } else { | |||||
| 79 | 1 | 4 | my $error = $@; | |||
| 80 | 1 | 47 | my $home = dirname(__FILE__); | |||
| 81 | 1 | 31 | $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/; | |||
| 82 | 1 | 21 | print STDERR $error; | |||
| 83 | 1 | 4 | push @file, '(?:\$^ - skipped because bad-regex)'; | |||
| 84 | } | |||||
| 85 | } | |||||
| 86 | ||||||
| 87 | 4 | 16 | return @file; | |||
| 88 | } | |||||
| 89 | ||||||
| 90 | sub list_to_re { | |||||
| 91 | 3 | 4 | my (@list) = @_; | |||
| 92 | 3 8 8 | 5 5 6 | @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list; | |||
| 93 | 3 8 | 5 7 | @list = grep { $_ ne '' } @list; | |||
| 94 | 3 | 6 | return '$^' unless scalar @list; | |||
| 95 | 3 | 10 | return join "|", (@list); | |||
| 96 | } | |||||
| 97 | ||||||
| 98 | sub file_to_re { | |||||
| 99 | 1 | 594 | my ($re) = @_; | |||
| 100 | 1 | 2 | return list_to_re(file_to_list($re)); | |||
| 101 | } | |||||
| 102 | ||||||
| 103 | sub not_empty { | |||||
| 104 | 51 | 43 | my ($thing) = @_; | |||
| 105 | 51 | 233 | return defined $thing && $thing ne '' | |||
| 106 | } | |||||
| 107 | ||||||
| 108 | sub valid_word { | |||||
| 109 | # shortest_word is an absolute | |||||
| 110 | 22 | 18 | our ($shortest, $longest, $shortest_word, $longest_word); | |||
| 111 | 22 | 24 | $shortest = $shortest_word if $shortest_word; | |||
| 112 | 22 | 23 | if ($longest_word) { | |||
| 113 | # longest_word is an absolute | |||||
| 114 | 20 | 24 | $longest = $longest_word; | |||
| 115 | } elsif (not_empty($longest)) { | |||||
| 116 | # we allow for some sloppiness (a couple of stuck keys per word) | |||||
| 117 | # it's possible that this should scale with word length | |||||
| 118 | 1 | 1 | $longest += 2; | |||
| 119 | } | |||||
| 120 | 22 | 17 | our ($upper_pattern, $lower_pattern, $punctuation_pattern); | |||
| 121 | 22 66 | 18 162 | my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern)); | |||
| 122 | 22 | 21 | $word_pattern = q<\\w|'> unless $word_pattern; | |||
| 123 | 22 | 50 | if ((defined $shortest && not_empty($longest)) && | |||
| 124 | ($shortest > $longest)) { | |||||
| 125 | 0 | 0 | $word_pattern = "(?:$word_pattern){3}"; | |||
| 126 | 0 | 0 | return qr/$word_pattern/; | |||
| 127 | } | |||||
| 128 | 22 | 29 | $shortest = 3 unless defined $shortest; | |||
| 129 | 22 | 26 | $longest = '' unless defined $longest; | |||
| 130 | 22 | 72 | $word_match = "(?:$word_pattern){$shortest,$longest}"; | |||
| 131 | 22 | 184 | return qr/\b$word_match\b/; | |||
| 132 | } | |||||
| 133 | ||||||
| 134 | sub load_dictionary { | |||||
| 135 | 12 | 2154 | my ($dict) = @_; | |||
| 136 | 12 | 8 | our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary); | |||
| 137 | 12 | 16 | $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef); | |||
| 138 | 12 | 14 | $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef); | |||
| 139 | 12 | 8 | our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||
| 140 | 12 | 22 | $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>); | |||
| 141 | 12 | 48 | $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]'); | |||
| 142 | 12 | 30 | $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]'); | |||
| 143 | 12 | 26 | $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]'); | |||
| 144 | 12 | 30 | $not_upper_or_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_UPPER_OR_LOWER_PATTERN', '[^A-Za-z]'); | |||
| 145 | 12 | 27 | $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>); | |||
| 146 | 12 | 31 | %dictionary = (); | |||
| 147 | ||||||
| 148 | 12 | 429 | open(DICT, '<:utf8', $dict); | |||
| 149 | 12 | 66 | while (!eof(DICT)) { | |||
| 150 | 31 | 73 | my $word = <DICT>; | |||
| 151 | 31 | 26 | chomp $word; | |||
| 152 | 31 | 91 | next unless $word =~ $word_match; | |||
| 153 | 28 | 26 | my $l = length $word; | |||
| 154 | 28 | 26 | $longest = -1 unless not_empty($longest); | |||
| 155 | 28 | 33 | $longest = $l if $l > $longest; | |||
| 156 | 28 | 26 | $shortest = $l if $l < $shortest; | |||
| 157 | 28 | 68 | $dictionary{$word}=1; | |||
| 158 | } | |||||
| 159 | 12 | 29 | close DICT; | |||
| 160 | ||||||
| 161 | 12 | 10 | $word_match = valid_word(); | |||
| 162 | } | |||||
| 163 | ||||||
| 164 | sub hunspell_dictionary { | |||||
| 165 | 3 | 5 | my ($dict) = @_; | |||
| 166 | 3 | 4 | my $name = $dict; | |||
| 167 | 3 | 5 | $name =~ s{/src/index/hunspell/index\.dic$}{}; | |||
| 168 | 3 | 16 | $name =~ s{.*/}{}; | |||
| 169 | 3 | 6 | my $aff = $dict; | |||
| 170 | 3 | 3 | my $encoding; | |||
| 171 | 3 | 9 | $aff =~ s/\.dic$/.aff/; | |||
| 172 | 3 | 28 | if (open AFF, '<', $aff) { | |||
| 173 | 3 | 23 | while (<AFF>) { | |||
| 174 | 0 | 0 | next unless /^SET\s+(\S+)/; | |||
| 175 | 0 | 0 | $encoding = $1 if ($1 !~ /utf-8/i); | |||
| 176 | 0 | 0 | last; | |||
| 177 | } | |||||
| 178 | 3 | 7 | close AFF; | |||
| 179 | } | |||||
| 180 | return { | |||||
| 181 | 3 | 342 | name => $name, | |||
| 182 | dict => $dict, | |||||
| 183 | aff => $aff, | |||||
| 184 | encoding => $encoding, | |||||
| 185 | engine => Text::Hunspell->new($aff, $dict), | |||||
| 186 | } | |||||
| 187 | } | |||||
| 188 | ||||||
| 189 | sub init { | |||||
| 190 | 9 | 10136 | my ($configuration) = @_; | |||
| 191 | 9 | 13 | our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re); | |||
| 192 | 9 | 28 | our $sandbox = CheckSpelling::Util::get_file_from_env('sandbox', ''); | |||
| 193 | 9 | 14 | our $hunspell_dictionary_path = CheckSpelling::Util::get_file_from_env('hunspell_dictionary_path', ''); | |||
| 194 | 9 | 18 | our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30); | |||
| 195 | 9 | 18 | if ($hunspell_dictionary_path) { | |||
| 196 | 3 | 45 | our @hunspell_dictionaries = (); | |||
| 197 | 1 1 1 1 1 1 1 1 1 3 | 248 1016 21 6 0 17 7 2 22 229 | if (eval 'use Text::Hunspell; 1') { | |||
| 198 | 3 | 127 | my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic"); | |||
| 199 | 3 | 9 | for my $hunspell_dictionary_file (@hunspell_dictionaries_list) { | |||
| 200 | 3 | 12 | push @hunspell_dictionaries, hunspell_dictionary($hunspell_dictionary_file); | |||
| 201 | } | |||||
| 202 | } else { | |||||
| 203 | 0 | 0 | print STDERR "Could not load Text::Hunspell for dictionaries (hunspell-unavailable)\n"; | |||
| 204 | } | |||||
| 205 | } | |||||
| 206 | 9 | 17 | my (@patterns_re_list, %in_patterns_re_list); | |||
| 207 | 9 | 60 | if (-e "$configuration/patterns.txt") { | |||
| 208 | 0 | 0 | @patterns_re_list = file_to_list "$configuration/patterns.txt"; | |||
| 209 | 0 | 0 | $patterns_re = list_to_re @patterns_re_list; | |||
| 210 | 0 0 | 0 0 | %in_patterns_re_list = map {$_ => 1} @patterns_re_list; | |||
| 211 | } else { | |||||
| 212 | 9 | 12 | $patterns_re = undef; | |||
| 213 | } | |||||
| 214 | ||||||
| 215 | 9 | 44 | if (-e "$configuration/forbidden.txt") { | |||
| 216 | 1 | 2 | @forbidden_re_list = file_to_list "$configuration/forbidden.txt"; | |||
| 217 | 1 | 2 | $forbidden_re = list_to_re @forbidden_re_list; | |||
| 218 | } else { | |||||
| 219 | 8 | 7 | $forbidden_re = undef; | |||
| 220 | } | |||||
| 221 | ||||||
| 222 | 9 | 49 | if (-e "$configuration/candidates.txt") { | |||
| 223 | 1 | 2 | @candidates_re_list = file_to_list "$configuration/candidates.txt"; | |||
| 224 | 1 2 2 | 1 2 5 | @candidates_re_list = map { my $quoted = quote_re($_); $in_patterns_re_list{$_} || !test_re($quoted) ? '' : $quoted } @candidates_re_list; | |||
| 225 | 1 | 2 | $candidates_re = list_to_re @candidates_re_list; | |||
| 226 | } else { | |||||
| 227 | 8 | 8 | $candidates_re = undef; | |||
| 228 | } | |||||
| 229 | ||||||
| 230 | 9 | 16 | our $largest_file = CheckSpelling::Util::get_val_from_env('INPUT_LARGEST_FILE', 1024*1024); | |||
| 231 | ||||||
| 232 | 9 | 11 | my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', ''); | |||
| 233 | 9 | 8 | our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/; | |||
| 234 | 9 | 9 | our $disable_minified_file = $disable_flags =~ /(?:^|,|\s)minified-file(?:,|\s|$)/; | |||
| 235 | 9 | 7 | our $disable_single_line_file = $disable_flags =~ /(?:^|,|\s)single-line-file(?:,|\s|$)/; | |||
| 236 | ||||||
| 237 | 9 | 10 | our $check_file_names = CheckSpelling::Util::get_file_from_env('check_file_names', ''); | |||
| 238 | ||||||
| 239 | 9 | 10 | our $use_magic_file = CheckSpelling::Util::get_val_from_env('INPUT_USE_MAGIC_FILE', ''); | |||
| 240 | ||||||
| 241 | 9 | 11 | $word_match = valid_word(); | |||
| 242 | ||||||
| 243 | 9 | 21 | our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words"); | |||
| 244 | 9 | 50 | $base_dict = '/usr/share/dict/words' unless -e $base_dict; | |||
| 245 | 9 | 17 | load_dictionary($base_dict); | |||
| 246 | } | |||||
| 247 | ||||||
| 248 | sub split_line { | |||||
| 249 | 1164 | 514 | our (%dictionary, $word_match, $disable_word_collating); | |||
| 250 | 1164 | 413 | our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||
| 251 | 1164 | 435 | our @hunspell_dictionaries; | |||
| 252 | 1164 | 469 | our $shortest; | |||
| 253 | 1164 | 562 | my $pattern = '.'; | |||
| 254 | # $pattern = "(?:$upper_pattern){$shortest,}|$upper_pattern(?:$lower_pattern){2,}\n"; | |||||
| 255 | ||||||
| 256 | # https://www.fileformat.info/info/unicode/char/2019/ | |||||
| 257 | 1164 | 499 | my $rsqm = "\xE2\x80\x99"; | |||
| 258 | ||||||
| 259 | 1164 | 604 | my ($words, $unrecognized) = (0, 0); | |||
| 260 | 1164 | 754 | my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_; | |||
| 261 | 1164 | 6194 | $line =~ s/(?:$rsqm|'|'|\%27|’|’|’|\\u2019|\x{2019}|')+/'/g; | |||
| 262 | 1164 | 2680 | $line =~ s/(?:$ignore_pattern)+/ /g; | |||
| 263 | 1164 | 1718 | while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {} | |||
| 264 | 1164 | 4606 | while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {} | |||
| 265 | 1164 | 1537 | for my $token (split /\s+/, $line) { | |||
| 266 | 5851 | 5246 | next unless $token =~ /$pattern/; | |||
| 267 | 4689 | 3877 | $token =~ s/^(?:'|$rsqm)+//g; | |||
| 268 | 4689 | 4606 | $token =~ s/(?:'|$rsqm)+s?$//g; | |||
| 269 | 4689 | 2586 | my $raw_token = $token; | |||
| 270 | 4689 | 2637 | $token =~ s/^[^Ii]?'+(.*)/$1/; | |||
| 271 | 4689 | 2284 | $token =~ s/(.*?)'+$/$1/; | |||
| 272 | 4689 | 6796 | next unless $token =~ $word_match; | |||
| 273 | 4554 | 4036 | if (defined $dictionary{$token}) { | |||
| 274 | 1028 | 528 | ++$words; | |||
| 275 | 1028 | 485 | $unique_ref->{$token}=1; | |||
| 276 | 1028 | 824 | next; | |||
| 277 | } | |||||
| 278 | 3526 | 2543 | if (@hunspell_dictionaries) { | |||
| 279 | 3504 | 1608 | my $found = 0; | |||
| 280 | 3504 | 1804 | for my $hunspell_dictionary (@hunspell_dictionaries) { | |||
| 281 | my $token_encoded = defined $hunspell_dictionary->{'encoding'} ? | |||||
| 282 | 3504 | 2620 | encode($hunspell_dictionary->{'encoding'}, $token) : $token; | |||
| 283 | 3504 | 6234 | next unless ($hunspell_dictionary->{'engine'}->check($token_encoded)); | |||
| 284 | 0 | 0 | ++$words; | |||
| 285 | 0 | 0 | $dictionary{$token} = 1; | |||
| 286 | 0 | 0 | $unique_ref->{$token}=1; | |||
| 287 | 0 | 0 | $found = 1; | |||
| 288 | 0 | 0 | last; | |||
| 289 | } | |||||
| 290 | 3504 | 2502 | next if $found; | |||
| 291 | } | |||||
| 292 | 3526 | 2267 | my $key = lc $token; | |||
| 293 | 3526 | 3048 | if (defined $dictionary{$key}) { | |||
| 294 | 3 | 2 | ++$words; | |||
| 295 | 3 | 4 | $unique_ref->{$key}=1; | |||
| 296 | 3 | 3 | next; | |||
| 297 | } | |||||
| 298 | 3523 | 2325 | unless ($disable_word_collating) { | |||
| 299 | 3523 | 1872 | $key =~ s/''+/'/g; | |||
| 300 | 3523 | 1787 | $key =~ s/'[sd]$//; | |||
| 301 | } | |||||
| 302 | 3523 | 2745 | if (defined $dictionary{$key}) { | |||
| 303 | 0 | 0 | ++$words; | |||
| 304 | 0 | 0 | $unique_ref->{$key}=1; | |||
| 305 | 0 | 0 | next; | |||
| 306 | } | |||||
| 307 | 3523 | 1668 | ++$unrecognized; | |||
| 308 | 3523 | 1906 | $unique_unrecognized_ref->{$raw_token}=1; | |||
| 309 | 3523 | 4177 | $unrecognized_line_items_ref->{$raw_token}=1; | |||
| 310 | } | |||||
| 311 | 1164 | 1596 | return ($words, $unrecognized); | |||
| 312 | } | |||||
| 313 | ||||||
| 314 | sub skip_file { | |||||
| 315 | 15 | 32 | my ($temp_dir, $reason) = @_; | |||
| 316 | 15 | 620 | open(SKIPPED, '>:utf8', "$temp_dir/skipped"); | |||
| 317 | 15 | 117 | print SKIPPED $reason; | |||
| 318 | 15 | 572 | close SKIPPED; | |||
| 319 | } | |||||
| 320 | ||||||
| 321 | sub split_file { | |||||
| 322 | 15 | 7606 | my ($file) = @_; | |||
| 323 | our ( | |||||
| 324 | 15 | 13 | $unrecognized, $shortest, $largest_file, $words, | |||
| 325 | $word_match, %unique, %unique_unrecognized, $forbidden_re, | |||||
| 326 | @forbidden_re_list, $patterns_re, %dictionary, | |||||
| 327 | $candidates_re, @candidates_re_list, $check_file_names, $use_magic_file, $disable_minified_file, | |||||
| 328 | $disable_single_line_file, | |||||
| 329 | $sandbox, | |||||
| 330 | ); | |||||
| 331 | 15 | 8 | our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||
| 332 | ||||||
| 333 | # https://www.fileformat.info/info/unicode/char/2019/ | |||||
| 334 | 15 | 13 | my $rsqm = "\xE2\x80\x99"; | |||
| 335 | ||||||
| 336 | 15 | 24 | my @candidates_re_hits = (0) x scalar @candidates_re_list; | |||
| 337 | 15 | 16 | my @candidates_re_lines = (0) x scalar @candidates_re_list; | |||
| 338 | 15 | 18 | my @forbidden_re_hits = (0) x scalar @forbidden_re_list; | |||
| 339 | 15 | 16 | my @forbidden_re_lines = (0) x scalar @forbidden_re_list; | |||
| 340 | 15 | 43 | my $temp_dir = tempdir(DIR=>$sandbox); | |||
| 341 | 15 | 2818 | print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'}; | |||
| 342 | 15 | 353 | open(NAME, '>:utf8', "$temp_dir/name"); | |||
| 343 | 15 | 36 | print NAME $file; | |||
| 344 | 15 | 176 | close NAME; | |||
| 345 | 15 | 56 | my $file_size = -s $file; | |||
| 346 | 15 | 19 | if (defined $largest_file) { | |||
| 347 | 15 | 21 | unless ($check_file_names eq $file) { | |||
| 348 | 15 | 17 | if ($file_size > $largest_file) { | |||
| 349 | 1 | 3 | skip_file($temp_dir, "size `$file_size` exceeds limit `$largest_file`. (large-file)\n"); | |||
| 350 | 1 | 3 | return $temp_dir; | |||
| 351 | } | |||||
| 352 | } | |||||
| 353 | } | |||||
| 354 | 14 | 18 | if ($use_magic_file) { | |||
| 355 | 8 | 16624 | if (open(my $file_fh, '-|', | |||
| 356 | '/usr/bin/file', | |||||
| 357 | '-b', | |||||
| 358 | '--mime', | |||||
| 359 | '-e', 'cdf', | |||||
| 360 | '-e', 'compress', | |||||
| 361 | '-e', 'csv', | |||||
| 362 | '-e', 'elf', | |||||
| 363 | '-e', 'json', | |||||
| 364 | '-e', 'tar', | |||||
| 365 | $file)) { | |||||
| 366 | 8 | 31881 | my $file_kind = <$file_fh>; | |||
| 367 | 8 | 7007 | close $file_fh; | |||
| 368 | 8 | 147 | if ($file_kind =~ /^(.*?); charset=binary/) { | |||
| 369 | 2 | 31 | skip_file($temp_dir, "it appears to be a binary file (`$1`). (binary-file)\n"); | |||
| 370 | 2 | 42 | return $temp_dir; | |||
| 371 | } | |||||
| 372 | } | |||||
| 373 | } | |||||
| 374 | 12 | 155 | open FILE, '<', $file; | |||
| 375 | 12 | 16 | binmode FILE; | |||
| 376 | 12 | 6 | my $head; | |||
| 377 | 12 | 106 | read(FILE, $head, 4096); | |||
| 378 | 12 | 894 | $head =~ s/(?:\r|\n)+$//; | |||
| 379 | 12 | 66 | my $dos_new_lines = () = $head =~ /\r\n/gi; | |||
| 380 | 12 | 48 | my $unix_new_lines = () = $head =~ /\n/gi; | |||
| 381 | 12 | 118 | my $mac_new_lines = () = $head =~ /\r/gi; | |||
| 382 | 12 | 83 | local $/; | |||
| 383 | 12 | 98 | if ($unix_new_lines == 0 && $mac_new_lines == 0) { | |||
| 384 | 3 | 9 | $/ = "\n"; | |||
| 385 | } elsif ($dos_new_lines >= $unix_new_lines && $dos_new_lines >= $mac_new_lines) { | |||||
| 386 | 1 | 7 | $/ = "\r\n"; | |||
| 387 | } elsif ($mac_new_lines > $unix_new_lines) { | |||||
| 388 | 2 | 8 | $/ = "\r"; | |||
| 389 | } else { | |||||
| 390 | 6 | 7 | $/ = "\n"; | |||
| 391 | } | |||||
| 392 | 12 | 29 | seek(FILE, 0, 0); | |||
| 393 | 12 | 25 | ($words, $unrecognized) = (0, 0); | |||
| 394 | 12 | 44 | %unique = (); | |||
| 395 | 12 | 35 | %unique_unrecognized = (); | |||
| 396 | ||||||
| 397 | local $SIG{__WARN__} = sub { | |||||
| 398 | 0 | 0 | my $message = shift; | |||
| 399 | 0 | 0 | $message =~ s/> line/> in $file - line/; | |||
| 400 | 0 | 0 | chomp $message; | |||
| 401 | 0 | 0 | print STDERR "$message\n"; | |||
| 402 | 12 | 136 | }; | |||
| 403 | ||||||
| 404 | 12 | 431 | open(WARNINGS, '>:utf8', "$temp_dir/warnings"); | |||
| 405 | 12 | 14 | our $timeout; | |||
| 406 | 12 | 18 | eval { | |||
| 407 | 12 0 | 112 0 | local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required | |||
| 408 | 12 | 73 | alarm $timeout; | |||
| 409 | ||||||
| 410 | 12 | 12 | my $offset = 0; | |||
| 411 | 12 | 103 | while (<FILE>) { | |||
| 412 | 1167 | 1097 | if ($. == 1) { | |||
| 413 | 12 | 17 | unless ($disable_minified_file) { | |||
| 414 | 12 | 60 | if ($file_size >= 512 && length($_) == $file_size) { | |||
| 415 | 1 | 9 | skip_file($temp_dir, "file is a single line file. (single-line-file)\n"); | |||
| 416 | 1 | 2 | last; | |||
| 417 | } | |||||
| 418 | } | |||||
| 419 | } | |||||
| 420 | 1166 | 2444 | $_ = decode_utf8($_, FB_DEFAULT); | |||
| 421 | 1166 | 2737 | if (/[\x{D800}-\x{DFFF}]/) { | |||
| 422 | 0 | 0 | skip_file($temp_dir, "file contains a UTF-16 surrogate. This is not supported. (utf16-surrogate-file)\n"); | |||
| 423 | 0 | 0 | last; | |||
| 424 | } | |||||
| 425 | 1166 | 1441 | s/\R$//; | |||
| 426 | 1166 | 1076 | s/^\x{FEFF}// if $. == 1; | |||
| 427 | 1166 | 1128 | next unless /./; | |||
| 428 | 1164 | 782 | my $raw_line = $_; | |||
| 429 | ||||||
| 430 | # hook for custom line based text exclusions: | |||||
| 431 | 1164 | 795 | if (defined $patterns_re) { | |||
| 432 | 2 6 | 13 8 | s/($patterns_re)/"="x length($1)/ge; | |||
| 433 | } | |||||
| 434 | 1164 | 774 | my $previous_line_state = $_; | |||
| 435 | 1164 | 507 | my $line_flagged; | |||
| 436 | 1164 | 840 | if ($forbidden_re) { | |||
| 437 | 9 4 | 69 37 | while (s/($forbidden_re)/"="x length($1)/e) { | |||
| 438 | 4 | 4 | $line_flagged = 1; | |||
| 439 | 4 | 11 | my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 440 | 4 | 5 | my $found_trigger_re; | |||
| 441 | 4 | 4 | for my $i (0 .. $#forbidden_re_list) { | |||
| 442 | 4 | 5 | my $forbidden_re_singleton = $forbidden_re_list[$i]; | |||
| 443 | 4 | 16 | my $test_line = $previous_line_state; | |||
| 444 | 4 3 | 34 5 | if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) { | |||
| 445 | 3 | 4 | next unless $test_line eq $_; | |||
| 446 | 3 | 6 | my ($begin_test, $end_test, $match_test) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 447 | 3 | 4 | next unless $begin == $begin_test; | |||
| 448 | 3 | 2 | next unless $end == $end_test; | |||
| 449 | 3 | 2 | next unless $match eq $match_test; | |||
| 450 | 3 | 3 | $found_trigger_re = $forbidden_re_singleton; | |||
| 451 | 3 | 5 | my $hit = "$.:$begin:$end"; | |||
| 452 | 3 | 4 | $forbidden_re_hits[$i]++; | |||
| 453 | 3 | 3 | $forbidden_re_lines[$i] = $hit unless $forbidden_re_lines[$i]; | |||
| 454 | 3 | 5 | last; | |||
| 455 | } | |||||
| 456 | } | |||||
| 457 | 4 | 7 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($match); | |||
| 458 | 4 | 5 | if ($found_trigger_re) { | |||
| 459 | 3 | 8 | $found_trigger_re =~ s/^\(\?:(.*)\)$/$1/; | |||
| 460 | 3 | 3 | my $quoted_trigger_re = CheckSpelling::Util::wrap_in_backticks($found_trigger_re); | |||
| 461 | 3 | 14 | print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry: $quoted_trigger_re. (forbidden-pattern)\n"; | |||
| 462 | } else { | |||||
| 463 | 1 | 5 | print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry. (forbidden-pattern)\n"; | |||
| 464 | } | |||||
| 465 | 4 | 37 | $previous_line_state = $_; | |||
| 466 | } | |||||
| 467 | } | |||||
| 468 | # This is to make it easier to deal w/ rules: | |||||
| 469 | 1164 | 1182 | s/^/ /; | |||
| 470 | 1164 | 691 | my %unrecognized_line_items = (); | |||
| 471 | 1164 | 882 | my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items); | |||
| 472 | 1164 | 606 | $words += $new_words; | |||
| 473 | 1164 | 553 | $unrecognized += $new_unrecognized; | |||
| 474 | 1164 | 855 | my $line_length = length($raw_line); | |||
| 475 | 1164 | 1585 | for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) { | |||
| 476 | 1029 | 486 | my $found_token = 0; | |||
| 477 | 1029 | 468 | my $raw_token = $token; | |||
| 478 | 1029 | 529 | $token =~ s/'/(?:'|\x{2019}|\'|\')+/g; | |||
| 479 | 1029 | 438 | my $before; | |||
| 480 | 1029 | 1563 | if ($token =~ /^$upper_pattern$lower_pattern/) { | |||
| 481 | 5 | 4 | $before = '(?<=.)'; | |||
| 482 | } elsif ($token =~ /^$upper_pattern/) { | |||||
| 483 | 0 | 0 | $before = "(?<!$upper_pattern)"; | |||
| 484 | } else { | |||||
| 485 | 1024 | 556 | $before = "(?<=$not_lower_pattern)"; | |||
| 486 | } | |||||
| 487 | 1029 | 1167 | my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)"; | |||
| 488 | 1029 | 2144 | while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) { | |||
| 489 | 3520 | 1701 | $line_flagged = 1; | |||
| 490 | 3520 | 1407 | $found_token = 1; | |||
| 491 | 3520 | 4936 | my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 492 | 3520 | 3034 | next unless $match =~ /./; | |||
| 493 | 3520 | 2113 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($match); | |||
| 494 | 3520 | 11853 | print WARNINGS ":$.:$begin ... $end: $wrapped\n"; | |||
| 495 | } | |||||
| 496 | 1029 | 1126 | unless ($found_token) { | |||
| 497 | 3 | 47 | if ($raw_line !~ /$token.*$token/ && $raw_line =~ /($token)/) { | |||
| 498 | 3 | 7 | my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 499 | 3 | 4 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token); | |||
| 500 | 3 | 12 | print WARNINGS ":$.:$begin ... $end: $wrapped\n"; | |||
| 501 | } else { | |||||
| 502 | 0 | 0 | my $offset = $line_length + 1; | |||
| 503 | 0 | 0 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token); | |||
| 504 | 0 | 0 | print WARNINGS ":$.:1 ... $offset, Warning - Could not identify whole word $wrapped in line. (token-is-substring)\n"; | |||
| 505 | } | |||||
| 506 | } | |||||
| 507 | } | |||||
| 508 | 1164 | 1914 | if ($line_flagged && $candidates_re) { | |||
| 509 | 1 | 1 | $_ = $previous_line_state; | |||
| 510 | 1 1 | 19 3 | s/($candidates_re)/"="x length($1)/ge; | |||
| 511 | 1 | 2 | if ($_ ne $previous_line_state) { | |||
| 512 | 1 | 1 | $_ = $previous_line_state; | |||
| 513 | 1 | 2 | for my $i (0 .. $#candidates_re_list) { | |||
| 514 | 2 | 4 | my $candidate_re = $candidates_re_list[$i]; | |||
| 515 | 2 | 14 | next unless $candidate_re =~ /./ && $raw_line =~ /$candidate_re/; | |||
| 516 | 1 1 | 6 3 | if (($_ =~ s/($candidate_re)/"="x length($1)/e)) { | |||
| 517 | 1 | 2 | my ($begin, $end) = ($-[0] + 1, $+[0] + 1); | |||
| 518 | 1 | 2 | my $hit = "$.:$begin:$end"; | |||
| 519 | 1 | 1 | $_ = $previous_line_state; | |||
| 520 | 1 1 | 5 2 | my $replacements = ($_ =~ s/($candidate_re)/"="x length($1)/ge); | |||
| 521 | 1 | 1 | $candidates_re_hits[$i] += $replacements; | |||
| 522 | 1 | 2 | $candidates_re_lines[$i] = $hit unless $candidates_re_lines[$i]; | |||
| 523 | 1 | 2 | $_ = $previous_line_state; | |||
| 524 | } | |||||
| 525 | } | |||||
| 526 | } | |||||
| 527 | } | |||||
| 528 | 1164 | 828 | unless ($disable_minified_file) { | |||
| 529 | 1164 | 902 | s/={3,}//g; | |||
| 530 | 1164 | 797 | $offset += length; | |||
| 531 | 1164 | 1066 | my $ratio = int($offset / $.); | |||
| 532 | 1164 | 556 | my $ratio_threshold = 1000; | |||
| 533 | 1164 | 2868 | if ($ratio > $ratio_threshold) { | |||
| 534 | 11 | 40 | skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold). (minified-file)\n"); | |||
| 535 | } | |||||
| 536 | } | |||||
| 537 | } | |||||
| 538 | ||||||
| 539 | 12 | 98 | alarm 0; | |||
| 540 | }; | |||||
| 541 | 12 | 21 | if ($@) { | |||
| 542 | 0 | 0 | die unless $@ eq "alarm\n"; | |||
| 543 | 0 | 0 | print WARNINGS ":$.:1 ... 1, Warning - Could not parse file within time limit. (slow-file)\n"; | |||
| 544 | 0 | 0 | skip_file($temp_dir, "could not parse file within time limit. (slow-file)\n"); | |||
| 545 | } | |||||
| 546 | ||||||
| 547 | 12 | 40 | close FILE; | |||
| 548 | 12 | 208 | close WARNINGS; | |||
| 549 | ||||||
| 550 | 12 | 40 | if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) { | |||
| 551 | 11 | 393 | open(STATS, '>:utf8', "$temp_dir/stats"); | |||
| 552 | 11 | 218 | print STATS "{words: $words, unrecognized: $unrecognized, unknown: ".(keys %unique_unrecognized). | |||
| 553 | ", unique: ".(keys %unique). | |||||
| 554 | (@candidates_re_hits ? ", candidates: [".(join ',', @candidates_re_hits)."]" : ""). | |||||
| 555 | (@candidates_re_lines ? ", candidate_lines: [".(join ',', @candidates_re_lines)."]" : ""). | |||||
| 556 | (@forbidden_re_hits ? ", forbidden: [".(join ',', @forbidden_re_hits)."]" : ""). | |||||
| 557 | (@forbidden_re_lines ? ", forbidden_lines: [".(join ',', @forbidden_re_lines)."]" : ""). | |||||
| 558 | "}"; | |||||
| 559 | 11 | 145 | close STATS; | |||
| 560 | 11 | 259 | open(UNKNOWN, '>:utf8', "$temp_dir/unknown"); | |||
| 561 | 11 19 | 53 33 | print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized; | |||
| 562 | 11 | 93 | close UNKNOWN; | |||
| 563 | } | |||||
| 564 | ||||||
| 565 | 12 | 140 | return $temp_dir; | |||
| 566 | } | |||||
| 567 | ||||||
| 568 | sub main { | |||||
| 569 | 2 | 475 | my ($configuration, @ARGV) = @_; | |||
| 570 | 2 | 0 | our %dictionary; | |||
| 571 | 2 | 4 | unless (%dictionary) { | |||
| 572 | 1 | 1 | init($configuration); | |||
| 573 | } | |||||
| 574 | ||||||
| 575 | # read all input | |||||
| 576 | 2 | 2 | my @reports; | |||
| 577 | ||||||
| 578 | 2 | 2 | for my $file (@ARGV) { | |||
| 579 | 2 | 8 | my $temp_dir = split_file($file); | |||
| 580 | 2 | 4 | push @reports, "$temp_dir\n"; | |||
| 581 | } | |||||
| 582 | 2 | 9 | print join '', @reports; | |||
| 583 | } | |||||
| 584 | ||||||
| 585 | 1; | |||||