File Coverage

File:lib/CheckSpelling/SpellingCollator.pm
Coverage:90.4%

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3package CheckSpelling::SpellingCollator;
4
5our $VERSION='0.1.0';
6
1
1
114657
2
use 5.022;
7
1
1
1
2
0
4
use utf8;
8
1
1
1
19
0
55
use feature 'unicode_strings';
9
1
1
1
2
1
24
use warnings;
10
1
1
1
1
1
28
use File::Path qw(remove_tree);
11
1
1
1
283
1
3301
use CheckSpelling::Util;
12
13my %letter_map;
14my $disable_word_collating;
15my $shortest_word;
16
17my %last_seen;
18
19sub get_field {
20
40
34
  my ($record, $field) = @_;
21
40
347
  return 0 unless $record =~ (/\b$field:\s*(\d+)/);
22
30
38
  return $1;
23}
24
25sub get_array {
26
4
4
  my ($record, $field) = @_;
27
4
33
  return () unless $record =~ (/\b$field: \[([^\]]+)\]/);
28
4
3
  my $values = $1;
29
4
8
  return split /\s*,\s*/, $values;
30}
31
32sub maybe {
33
7
5
  my ($next, $value) = @_;
34
7
8
  $next = $value unless $next && $next < $value;
35
7
5
  return $next;
36}
37
38my %expected = ();
39sub expect_item {
40
94
56
  my ($item, $value) = @_;
41
94
52
  $item =~ s/’/'/g;
42
94
39
  our %expected;
43
94
37
  my $next;
44
94
117
  if (defined $expected{$item}) {
45
22
17
    $next = $expected{$item};
46
22
19
    $next = $value if $value < $next;
47  } elsif ($item =~ /^([A-Z])(.*)/) {
48
12
8
    $item = $1 . lc $2;
49
12
11
    if (defined $expected{$item}) {
50
2
2
      $next = $expected{$item};
51
2
3
      $next = maybe($next, $value + .1);
52    } else {
53
10
9
      $item = lc $item;
54
10
7
      if (defined $expected{$item}) {
55
5
3
        $next = $expected{$item};
56
5
5
        $next = maybe($next, $value + .2);
57      }
58    }
59  }
60
94
105
  return 0 unless defined $next;
61
29
19
  $expected{$item} = $next;
62
29
63
  return $value;
63}
64
65sub skip_item {
66
50
23
  my ($word) = @_;
67
50
32
  return 1 if expect_item($word, 1);
68
32
25
  my $key = lc $word;
69
32
15
  return 2 if expect_item($key, 2);
70
32
44
  if ($key =~ /.s$/) {
71
2
4
    if ($key =~ /ies$/) {
72
1
3
      $key =~ s/ies$/y/;
73    } else {
74
1
2
      $key =~ s/s$//;
75    }
76  } elsif ($key =~ /^(.+[^aeiou])ed$/) {
77
1
1
    $key = $1;
78  } elsif ($key =~ /^(.+)'[ds]$/) {
79
4
3
    $key = $1;
80  } else {
81
25
24
    return 0;
82  }
83
7
7
  return 3 if expect_item($key, 3);
84
0
0
  return 0;
85}
86
87sub should_skip_warning {
88
63
41
  my ($warning) = @_;
89
63
80
  if ($warning =~ /\(([-\w]+)\)$/) {
90
59
35
    my ($code) = ($1);
91
59
35
    return 1 if CheckSpelling::Util::is_ignoring_event($code);
92  }
93
63
115
  return 0;
94}
95
96sub should_skip_warning_while_counting {
97
11
7
  my ($warning) = @_;
98
11
26
  return 0 unless ($warning =~ /\(([-\w]+)\)$/);
99
11
7
  my ($code) = ($1);
100
11
10
  return 1 if CheckSpelling::Util::is_ignoring_event($code);
101
102
9
6
  our %counters;
103
9
7
  ++$counters{$code};
104
9
8
  return 0;
105}
106
107sub log_skip_item {
108
46
61
  my ($item, $file, $warning, $unknown_word_limit) = @_;
109
46
30
  return 1 if should_skip_warning $warning;
110
46
33
  return 1 if skip_item($item);
111
21
9
  our %seen;
112
21
12
  my $seen_count = $seen{$item};
113
21
18
  if (defined $seen_count) {
114
8
11
    if (!defined $unknown_word_limit || ($seen_count++ < $unknown_word_limit)) {
115
7
40
      print MORE_WARNINGS "$file$warning\n";
116    } else {
117
1
1
      our %last_seen;
118
1
2
      $last_seen{$item} = "$file$warning";
119    }
120
8
9
    $seen{$item} = $seen_count;
121
8
13
    return 1;
122  }
123
13
11
  $seen{$item} = 1;
124
13
12
  return 0;
125}
126
127sub stem_word {
128
28
904
  my ($key) = @_;
129
28
11
  our $disable_word_collating;
130
28
20
  return $key if $disable_word_collating;
131
28
11
  our $shortest_word;
132
28
17
  $shortest_word = 2 unless defined $shortest_word;
133
28
14
  my $key_length = length $key;
134
135
28
52
  if ($key =~ /.s$/) {
136
5
22
    if ($key_length > ($shortest_word + 1) && $key =~ /ies$/) {
137
1
2
      $key =~ s/ies$/y/;
138    } elsif ($key_length > $shortest_word && $key !~ /ies$/) {
139
3
5
      $key =~ s/s$//;
140    }
141  } elsif ($key_length > ($shortest_word + 1) && $key =~ /.[^aeiou]ed$/) {
142
2
5
    $key =~ s/ed$//;
143  }
144
28
27
  return $key;
145}
146
147sub collate_key {
148
81
44
  my ($key) = @_;
149
81
35
  our $disable_word_collating;
150
81
28
  my $char;
151
81
52
  if ($disable_word_collating) {
152
8
10
    $char = lc substr $key, 0, 1;
153  } else {
154
73
50
    $key = lc $key;
155
73
50
    $key =~ s/''+/'/g;
156
73
40
    $key =~ s/'[sd]$//;
157
73
45
    $key =~ s/^[^Ii]?'+(.*)/$1/;
158
73
30
    $key =~ s/(.*?)'$/$1/;
159
73
73
    $char = substr $key, 0, 1;
160  }
161
81
109
  return ($key, $char);
162}
163
164sub load_expect {
165
12
541
  my ($expect) = @_;
166
12
5
  our %expected;
167
12
12
  %expected = ();
168
12
115
  if (open(EXPECT, '<:utf8', $expect)) {
169
12
65
    while (my $word = <EXPECT>) {
170
43
59
      $word =~ s/\R//;
171
43
88
      $expected{$word} = 0;
172    }
173
12
35
    close EXPECT;
174  }
175}
176
177sub harmonize_expect {
178
11
9
  our $disable_word_collating;
179
11
1
  our %letter_map;
180
11
11
  our %expected;
181
182
11
15
  for my $word (keys %expected) {
183
40
26
    my ($key, $char) = collate_key $word;
184
40
23
    my %word_map = ();
185
40
47
    next unless defined $letter_map{$char}{$key};
186
13
13
5
18
    %word_map = %{$letter_map{$char}{$key}};
187
13
19
    next if defined $word_map{$word};
188
3
1
    my $words = scalar keys %word_map;
189
3
3
    next if $words > 2;
190
3
3
    if ($word eq $key) {
191
1
1
      next if ($words > 1);
192    }
193
2
2
    delete $expected{$word};
194  }
195}
196
197sub group_related_words {
198
12
8
  our %letter_map;
199
12
4
  our $disable_word_collating;
200
12
10
  return if $disable_word_collating;
201
202  # group related words
203
11
23
  for my $char (sort CheckSpelling::Util::number_biased keys %letter_map) {
204
19
19
9
21
    for my $plural_key (sort keys(%{$letter_map{$char}})) {
205
22
13
      my $key = stem_word $plural_key;
206
22
20
      next if $key eq $plural_key;
207
4
5
      next unless defined $letter_map{$char}{$key};
208
3
3
1
4
      my %word_map = %{$letter_map{$char}{$key}};
209
3
3
3
3
      for my $word (keys(%{$letter_map{$char}{$plural_key}})) {
210
3
2
        $word_map{$word} = 1;
211      }
212
3
3
      $letter_map{$char}{$key} = \%word_map;
213
3
4
      delete $letter_map{$char}{$plural_key};
214    }
215  }
216}
217
218sub count_warning {
219
9
11
  my ($warning) = @_;
220
9
10
  our %counters;
221
9
19
  if ($warning =~ /\(([-\w]+)\)$/) {
222
3
3
    my ($code) = ($1);
223
3
3
    return if CheckSpelling::Util::is_ignoring_event($code);
224
2
2
    ++$counters{$code};
225
2
2
    return $code;
226  }
227}
228
229sub report_timing {
230
0
0
  my ($name, $start_time, $directory, $marker) = @_;
231
0
0
  my $end_time = (stat "$directory/$marker")[9];
232
0
0
  $name =~ s/"/\\"/g;
233
0
0
  print TIMING_REPORT "\"$name\", $start_time, $end_time\n";
234}
235
236sub get_pattern_with_context {
237
24
15
  my ($path) = @_;
238
24
25
  return unless defined $ENV{$path};
239
24
25
  $ENV{$path} =~ /(.*)/;
240
24
164
  return unless open ITEMS, '<:utf8', $1;
241
242
24
15
  my @items;
243
24
14
  my $context = '';
244
24
99
  while (<ITEMS>) {
245
5
5
    my $pattern = $_;
246
5
8
    if ($pattern =~ /^#/) {
247
2
4
      if ($pattern =~ /^# /) {
248
2
3
        $context .= $pattern;
249      } else {
250
0
0
        $context = '';
251      }
252
2
3
      next;
253    }
254
3
2
    chomp $pattern;
255
3
4
    unless ($pattern =~ /./) {
256
1
1
      $context = '';
257
1
2
      next;
258    }
259
2
4
    push @items, $context.$pattern;
260
2
6
    $context = '';
261  }
262
24
65
  close ITEMS;
263
24
29
  return @items;
264}
265
266sub summarize_totals {
267
24
20
  my ($formatter, $path, $items, $totals, $file_counts) = @_;
268
24
24
14
24
  return unless @{$totals};
269
2
60
  return unless open my $fh, '>:utf8', $path;
270
2
2
1
2
  my $totals_count = scalar(@{$totals}) - 1;
271
2
2
  my @indices;
272
2
2
  if ($file_counts) {
273    @indices = sort {
274
1
0
2
0
      $totals->[$b] <=> $totals->[$a] ||
275      $file_counts->[$b] <=> $file_counts->[$a]
276    } 0 .. $totals_count;
277  } else {
278    @indices = sort {
279
1
0
2
0
      $totals->[$b] <=> $totals->[$a]
280    } 0 .. $totals_count;
281  }
282
2
7
  for my $i (@indices) {
283
2
2
    last unless $totals->[$i] > 0;
284
2
2
    my $rule_with_context = $items->[$i];
285
2
2
    my ($description, $rule);
286
2
7
    if ($rule_with_context =~ /^(.*\n)([^\n]+)$/s) {
287
2
3
      ($description, $rule) = ($1, $2);
288    } else {
289
0
0
      ($description, $rule) = ('', $rule_with_context);
290    }
291
2
4
    print $fh $formatter->(
292      $totals->[$i],
293      ($file_counts ? " file-count: $file_counts->[$i]" : ""),
294      $description,
295      $rule
296    );
297  }
298
2
80
  close $fh;
299}
300
301sub get_special {
302
19
16
  my ($file, $special) = @_;
303
19
28
  return 'file-list' if $file eq $special->{'file_list'};
304
17
19
  return 'pr-title' if $file eq $special->{'pr_title_file'};
305
15
13
  return 'pr-description' if $file eq $special->{'pr_description_file'};
306
13
27
  return 'commit-message' if !rindex($file, $special->{'commit_messages'});
307
11
14
  return 'file';
308}
309
310sub main {
311
12
23186
  my @directories;
312  my @cleanup_directories;
313
12
0
  my @check_file_paths;
314
315
12
13
  CheckSpelling::Util::build_ignored_event_map();
316
12
12
  my $early_warnings = CheckSpelling::Util::get_file_from_env('early_warnings', '/dev/null');
317
12
6
  my $warning_output = CheckSpelling::Util::get_file_from_env('warning_output', '/dev/stderr');
318
12
10
  my $more_warnings = CheckSpelling::Util::get_file_from_env('more_warnings', '/dev/stderr');
319
12
11
  my $counter_summary = CheckSpelling::Util::get_file_from_env('counter_summary', '/dev/stderr');
320
12
8
  my $should_exclude_file = CheckSpelling::Util::get_file_from_env('should_exclude_file', '/dev/null');
321
12
11
  my $unknown_word_limit = CheckSpelling::Util::get_val_from_env('unknown_word_limit', undef);
322
12
10
  my $unknown_file_word_limit = CheckSpelling::Util::get_val_from_env('unknown_file_word_limit', undef);
323
12
8
  my $candidate_example_limit = CheckSpelling::Util::get_file_from_env('INPUT_CANDIDATE_EXAMPLE_LIMIT', '3');
324
12
8
  my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', '');
325
12
8
  my $only_check_changed_files = CheckSpelling::Util::get_file_from_env('INPUT_ONLY_CHECK_CHANGED_FILES', '');
326
12
9
  my $disable_noisy_file = $disable_flags =~ /(?:^|,|\s)noisy-file(?:,|\s|$)/;
327
12
31
  our $disable_word_collating = $only_check_changed_files || $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
328
12
7
  our $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
329
12
8
  my $file_list = CheckSpelling::Util::get_file_from_env('check_file_names', '');
330
12
9
  my $pr_title_file = CheckSpelling::Util::get_file_from_env('pr_title_file', '');
331
12
7
  my $pr_description_file = CheckSpelling::Util::get_file_from_env('pr_description_file', '');
332
12
9
  my $commit_messages = CheckSpelling::Util::get_file_from_env('commit_messages', '');
333
12
8
  my $timing_report = CheckSpelling::Util::get_file_from_env('timing_report', '');
334
12
21
  my $special = {
335    'file_list' => $file_list,
336    'pr_title_file' => $pr_title_file,
337    'pr_description_file' => $pr_description_file,
338    'commit_messages' => $commit_messages,
339  };
340
12
7
  my ($start_time, $end_time);
341
342
12
258
  open WARNING_OUTPUT, '>:utf8', $warning_output;
343
12
166
  open MORE_WARNINGS, '>:utf8', $more_warnings;
344
12
150
  open COUNTER_SUMMARY, '>:utf8', $counter_summary;
345
12
111
  open SHOULD_EXCLUDE, '>:utf8', $should_exclude_file;
346
12
12
  if ($timing_report) {
347
0
0
    open TIMING_REPORT, '>:utf8', $timing_report;
348
0
0
    print TIMING_REPORT "file, start, finish\n";
349  }
350
351
12
11
  my @candidates = get_pattern_with_context('candidates_path');
352
12
9
  my @candidate_totals = (0) x scalar @candidates;
353
12
6
  my @candidate_file_counts = (0) x scalar @candidates;
354
355
12
9
  my @forbidden = get_pattern_with_context('forbidden_path');
356
12
9
  my @forbidden_totals = (0) x scalar @forbidden;
357
358
12
5
  my @delayed_warnings;
359
12
25
  our %letter_map = ();
360
361
12
3
  my %file_map = ();
362
363
12
32
  for my $directory (<>) {
364
15
13
    chomp $directory;
365
15
41
    next unless $directory =~ /^(.*)$/;
366
15
15
    $directory = $1;
367
15
48
    unless (-e $directory) {
368
1
3
      print STDERR "Could not find: $directory\n";
369
1
1
      next;
370    }
371
14
40
    unless (-d $directory) {
372
1
13
      print STDERR "Not a directory: $directory\n";
373
1
1
      next;
374    }
375
376    # if there's no filename, we can't report
377
13
101
    next unless open(NAME, '<:utf8', "$directory/name");
378
12
68
    my $file=<NAME>;
379
12
28
    close NAME;
380
381
12
43
    $file_map{$file} = $directory;
382  }
383
384
12
21
  for my $file (sort keys %file_map) {
385
12
12
    my $directory = $file_map{$file};
386
12
8
    if ($timing_report) {
387
0
0
      $start_time = (stat "$directory/name")[9];
388    }
389
390
12
68
    if (-e "$directory/skipped") {
391
1
10
      open SKIPPED, '<:utf8', "$directory/skipped";
392
1
13
      my $reason=<SKIPPED>;
393
1
5
      close SKIPPED;
394
1
1
      chomp $reason;
395
1
4
      push @delayed_warnings, "$file:1:1 ... 1, Warning - Skipping `$file` because $reason\n";
396
1
17
      print SHOULD_EXCLUDE "$file\n";
397
1
1
      push @cleanup_directories, $directory;
398
1
1
      report_timing($file, $start_time, $directory, 'skipped') if ($timing_report);
399
1
2
      next;
400    }
401
402    # stats isn't written if there was nothing interesting in the file
403
11
38
    unless (-s "$directory/stats") {
404
1
1
      report_timing($file, $start_time, $directory, 'warnings') if ($timing_report);
405
1
0
      push @directories, $directory;
406
1
2
      next;
407    }
408
409
10
9
    if ($file eq $file_list) {
410
1
8
      open FILE_LIST, '<:utf8', $file_list;
411
1
1
      push @check_file_paths, '0 placeholder';
412
1
7
      for my $check_file_path (<FILE_LIST>) {
413
4
4
        chomp $check_file_path;
414
4
3
        push @check_file_paths, $check_file_path;
415      }
416
1
3
      close FILE_LIST;
417    }
418
419
10
7
    my ($words, $unrecognized, $unknown, $unique);
420
421    {
422
10
10
6
66
      open STATS, '<:utf8', "$directory/stats";
423
10
46
      my $stats=<STATS>;
424
10
21
      close STATS;
425
10
10
      $words=get_field($stats, 'words');
426
10
8
      $unrecognized=get_field($stats, 'unrecognized');
427
10
5
      $unknown=get_field($stats, 'unknown');
428
10
7
      $unique=get_field($stats, 'unique');
429
10
5
      my @candidate_list;
430
10
9
      if (@candidate_totals) {
431
1
1
        @candidate_list=get_array($stats, 'candidates');
432
1
1
        my @lines=get_array($stats, 'candidate_lines');
433
1
2
        if (@candidate_list) {
434
1
1
          for (my $i=0; $i < scalar @candidate_list; $i++) {
435
1
1
            my $hits = $candidate_list[$i];
436
1
1
            if ($hits) {
437
1
1
              $candidate_totals[$i] += $hits;
438
1
1
              if ($candidate_file_counts[$i]++ < $candidate_example_limit) {
439
1
3
                my $pattern = (split /\n/,$candidates[$i])[-1];
440
1
1
                my $position = $lines[$i];
441
1
4
                $position =~ s/:(\d+)$/ ... $1/;
442
1
2
                my $wrapped = CheckSpelling::Util::truncate_with_ellipsis(CheckSpelling::Util::wrap_in_backticks($pattern), 99);
443
1
1
                my $candidate_label = '';
444
1
3
                if ($candidates[$i] =~ /^#\s+(\S.+)/) {
445
1
1
                  $candidate_label = " ($1)";
446                }
447
1
6
                push @delayed_warnings, "$file:$position, Notice - Line matches candidate pattern$candidate_label $wrapped (candidate-pattern)\n";
448              }
449            }
450          }
451        }
452      }
453
10
10
      if (@forbidden_totals) {
454
1
2
        my @forbidden_list=get_array($stats, 'forbidden');
455
1
1
        my @lines=get_array($stats, 'forbidden_lines');
456
1
1
        if (@forbidden_list) {
457
1
2
          for (my $i=0; $i < scalar @forbidden_list; $i++) {
458
1
1
            my $hits = $forbidden_list[$i];
459
1
1
            if ($hits) {
460
1
3
              $forbidden_totals[$i] += $hits;
461            }
462          }
463        }
464      }
465      #print STDERR "$file (unrecognized: $unrecognized; unique: $unique; unknown: $unknown, words: $words, candidates: [".join(", ", @candidate_list)."])\n";
466    }
467
468
10
11
    report_timing($file, $start_time, $directory, 'unknown') if ($timing_report);
469
10
10
    my $kind = get_special($file, $special);
470    # These heuristics are very new and need tuning/feedback
471
10
13
    if (
472        ($unknown > $unique)
473        # || ($unrecognized > $words / 2)
474    ) {
475
1
1
      unless ($disable_noisy_file) {
476
1
2
        if ($kind eq 'file') {
477
1
7
          print SHOULD_EXCLUDE "$file\n";
478        }
479
1
1
        my $warning = "noisy-$kind";
480
1
1
        count_warning $warning;
481
1
2
        push @delayed_warnings, "$file:1:1 ... 1, Warning - Skipping `$file` because it seems to have more noise ($unknown) than unique words ($unique) (total: $unrecognized / $words). ($warning)\n";
482
1
0
        push @cleanup_directories, $directory;
483
1
2
        next;
484      }
485    }
486
9
11
    push @directories, $directory;
487
9
45
    unless ($kind =~ /^file/ && -s "$directory/unknown") {
488
4
6
      next;
489    }
490
5
39
    open UNKNOWN, '<:utf8', "$directory/unknown";
491
5
40
    for my $token (<UNKNOWN>) {
492
43
39
      $token =~ s/\R//;
493
43
42
      next unless $token =~ /./;
494
41
26
      my ($key, $char) = collate_key $token;
495
41
46
      $letter_map{$char} = () unless defined $letter_map{$char};
496
41
25
      my %word_map = ();
497
41
14
36
18
      %word_map = %{$letter_map{$char}{$key}} if defined $letter_map{$char}{$key};
498
41
30
      $word_map{$token} = 1;
499
41
64
      $letter_map{$char}{$key} = \%word_map;
500    }
501
5
21
    close UNKNOWN;
502  }
503
12
41
  close SHOULD_EXCLUDE;
504
12
11
  close TIMING_REPORT if $timing_report;
505
506  summarize_totals(
507    sub {
508
1
2
      my ($hits, $files, $context, $pattern) = @_;
509
1
6
      return "# hit-count: $hits$files\n$context$pattern\n\n",
510    },
511
12
33
    CheckSpelling::Util::get_file_from_env('candidate_summary', '/dev/stderr'),
512    \@candidates,
513    \@candidate_totals,
514    \@candidate_file_counts,
515  );
516
517  summarize_totals(
518    sub {
519
1
1
      my (undef, undef, $context, $pattern) = @_;
520
1
2
      $context =~ s/^# //gm;
521
1
1
      chomp $context;
522
1
0
      my $details;
523
1
3
      if ($context =~ /^(.*?)$(.*)/ms) {
524
1
2
        ($context, $details) = ($1, $2);
525
1
1
        $details = "\n$details" if $details;
526      }
527
1
1
      $context = 'Pattern' unless $context;
528
1
6
      return "##### $context$details\n```\n$pattern\n```\n\n";
529    },
530
12
56
    CheckSpelling::Util::get_file_from_env('forbidden_summary', '/dev/stderr'),
531    \@forbidden,
532    \@forbidden_totals,
533  );
534
535
12
37
  group_related_words;
536
537
12
21
  if (defined $ENV{'expect'}) {
538
11
11
    $ENV{'expect'} =~ /(.*)/;
539
11
12
    load_expect $1;
540
11
7
    harmonize_expect;
541  }
542
543
12
9
  my %seen = ();
544
12
6
  our %counters;
545
12
11
  %counters = ();
546
547
12
47
  if (-s $early_warnings) {
548
1
7
    open WARNINGS, '<:utf8', $early_warnings;
549
1
7
    for my $warning (<WARNINGS>) {
550
1
1
      chomp $warning;
551
1
1
      next if should_skip_warning_while_counting $warning;
552
1
6
      print WARNING_OUTPUT "$warning\n";
553    }
554
1
3
    close WARNINGS;
555  }
556
557
12
8
  our %last_seen;
558
12
5
  my %unknown_file_word_count;
559
12
12
  for my $directory (@directories) {
560
10
35
    next unless (-s "$directory/warnings");
561
9
102
    next unless open(NAME, '<:utf8', "$directory/name");
562
9
37
    my $file=<NAME>;
563
9
19
    close NAME;
564
9
7
    my $kind = get_special($file, $special);
565
9
59
    open WARNINGS, '<:utf8', "$directory/warnings";
566
9
6
    if ($kind ne 'file-list') {
567
8
52
      for my $warning (<WARNINGS>) {
568
50
26
        my $code;
569
50
33
        chomp $warning;
570
50
111
        if ($warning =~ m/:(\d+):(\d+ \.\.\. \d+): `(.*)`/) {
571
46
46
          my ($line, $range, $item) = ($1, $2, $3);
572
46
40
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($item);
573
46
25
          my $reason = 'unrecognized-spelling';
574
46
33
          $reason .= "-$kind" unless $kind eq 'file';
575
46
103
          $warning =~ s/:\d+:\d+ \.\.\. \d+: `.*`/:$line:$range, Warning - $wrapped is not a recognized word ($reason)/;
576
46
33
          next if log_skip_item($item, $file, $warning, $unknown_word_limit);
577
13
11
          count_warning $warning if $kind ne 'file';
578
13
11
          $code = $reason;
579        } else {
580
4
4
          if ($warning =~ /\`(.*?)\` in line \(token-is-substring\)/) {
581
0
0
            next if skip_item($1);
582          }
583
4
5
          $code = count_warning $warning;
584        }
585
17
9
        next if should_skip_warning $warning, $code;
586
17
73
        print WARNING_OUTPUT "$file$warning\n";
587      }
588    } else {
589
1
9
      for my $warning (<WARNINGS>) {
590
6
6
        chomp $warning;
591
6
11
        next unless $warning =~ s/^:(\d+)/:1/;
592
6
5
        $file = $check_file_paths[$1];
593
6
14
        if ($warning =~ m/:(\d+ \.\.\. \d+): `(.*)`/) {
594
4
4
          my ($range, $item) = ($1, $2);
595
4
3
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($item);
596
4
11
          $warning =~ s/:\d+ \.\.\. \d+: `.*`/:$range, Warning - $wrapped is not a recognized word (check-file-path)/;
597
4
4
          next if skip_item($item);
598
4
2
          if (defined $unknown_file_word_limit) {
599
4
7
            next if ++$unknown_file_word_count{$item} > $unknown_file_word_limit;
600          }
601        }
602
5
8
        next if should_skip_warning_while_counting $warning;
603
4
12
        print WARNING_OUTPUT "$file$warning\n";
604      }
605    }
606
9
39
    close WARNINGS;
607  }
608
12
258
  close MORE_WARNINGS;
609
610
12
13
  for my $warning (@delayed_warnings) {
611
3
3
    next if should_skip_warning_while_counting $warning;
612
3
6
    print WARNING_OUTPUT $warning;
613  }
614
12
8
  if (defined $unknown_word_limit) {
615
1
2
    for my $warned_word (sort keys %last_seen) {
616
1
4
      my $warning_count = $seen{$warned_word} || 0;
617
1
2
      next unless $warning_count >= $unknown_word_limit;
618
0
0
      my $warning = $last_seen{$warned_word};
619
0
0
      $warning =~ s/\Q (unrecognized-spelling)\E/ -- found $warning_count times (limited-references)\n/;
620
0
0
      next if should_skip_warning_while_counting $warning;
621
0
0
      print WARNING_OUTPUT $warning;
622    }
623  }
624
12
335
  close WARNING_OUTPUT;
625
626
12
15
  if (%counters) {
627
3
3
    my $continue='';
628
3
5
    print COUNTER_SUMMARY "{\n";
629
3
8
    for my $code (sort keys %counters) {
630
6
10
      print COUNTER_SUMMARY qq<$continue"$code": $counters{$code}\n>;
631
6
4
      $continue=',';
632    }
633
3
3
    print COUNTER_SUMMARY "}\n";
634  }
635
12
99
  close COUNTER_SUMMARY;
636
637  # display the current unknown
638
12
31
  for my $char (sort keys %letter_map) {
639
43
43
20
107
    for my $key (sort CheckSpelling::Util::case_biased keys(%{$letter_map{$char}})) {
640
24
24
16
33
      my %word_map = %{$letter_map{$char}{$key}};
641
24
20
      my @words = keys(%word_map);
642
24
19
      if (scalar(@words) > 1) {
643
13
19
16
85
        print $key." (".(join ", ", sort { length($a) <=> length($b) || $a cmp $b } @words).")";
644      } else {
645
11
39
        print $words[0];
646      }
647
24
104
      print "\n";
648    }
649  }
650}
651
652sub collate_expect {
653
1
687
  my ($collated, $notes) = @_;
654
1
1
  my @words;
655
1
7
  open EXPECT, '<', $collated;
656
1
8
  while (<EXPECT>) {
657
5
6
    chomp;
658
5
9
    next unless /(.*) \((.*)\)/;
659
1
1
    my ($key, $list) = ($1, $2);
660
1
2
    my @variants = split /, /, $list;
661
1
2
1
3
    @variants = grep { $_ ne $key } @variants;
662
1
2
    push @words, @variants;
663  }
664
1
3
  close EXPECT;
665
1
1
2
2
  my $pattern = '\`(?:'.join('|', map { quotemeta($_) } @words).')`';
666
1
7
  open SOURCES, '<', $notes;
667
1
12
  while (<SOURCES>) {
668
9
18
    if ($_ =~ /$pattern/) {
669
4
2
      my $print = 0;
670
4
7
      $print = 1 if s/not a recognized word/ignored by check-spelling because another more general variant is also in expect/;
671
4
7
      $print = 1 if s/unrecognized-spelling/ignored-expect-variant/;
672
4
5
      next unless $print;
673    } else {
674
5
13
      next unless /\(((?:\w+-)+\w+)\)$/;
675
2
3
      next if $1 eq 'unrecognized-spelling';
676    }
677
4
23
    print;
678  }
679
1
3
  close SOURCES;
680
1
2
  return 0;
681}
682
6831;