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