File Coverage

File:lib/CheckSpelling/UnknownWordSplitter.pm
Coverage:87.8%

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