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
107329
1
use 5.022;
7
1
1
1
2
3
3
use utf8;
8
1
1
1
12
0
45
use feature 'unicode_strings';
9
1
1
1
1
1
16
use warnings;
10
1
1
1
2
0
20
use File::Path qw(remove_tree);
11
1
1
1
276
0
3254
use CheckSpelling::Util;
12
13my %letter_map;
14my $disable_word_collating;
15my $shortest_word;
16
17my %last_seen;
18
19sub get_field {
20
40
30
  my ($record, $field) = @_;
21
40
333
  return 0 unless $record =~ (/\b$field:\s*(\d+)/);
22
30
34
  return $1;
23}
24
25sub get_array {
26
4
4
  my ($record, $field) = @_;
27
4
27
  return () unless $record =~ (/\b$field: \[([^\]]+)\]/);
28
4
4
  my $values = $1;
29
4
5
  return split /\s*,\s*/, $values;
30}
31
32sub maybe {
33
7
3
  my ($next, $value) = @_;
34
7
10
  $next = $value unless $next && $next < $value;
35
7
7
  return $next;
36}
37
38my %expected = ();
39sub expect_item {
40
94
64
  my ($item, $value) = @_;
41
94
47
  $item =~ s/’/'/g;
42
94
43
  our %expected;
43
94
35
  my $next;
44
94
93
  if (defined $expected{$item}) {
45
22
12
    $next = $expected{$item};
46
22
20
    $next = $value if $value < $next;
47  } elsif ($item =~ /^([A-Z])(.*)/) {
48
12
11
    $item = $1 . lc $2;
49
12
9
    if (defined $expected{$item}) {
50
2
2
      $next = $expected{$item};
51
2
1
      $next = maybe($next, $value + .1);
52    } else {
53
10
8
      $item = lc $item;
54
10
6
      if (defined $expected{$item}) {
55
5
1
        $next = $expected{$item};
56
5
6
        $next = maybe($next, $value + .2);
57      }
58    }
59  }
60
94
97
  return 0 unless defined $next;
61
29
22
  $expected{$item} = $next;
62
29
56
  return $value;
63}
64
65sub skip_item {
66
50
27
  my ($word) = @_;
67
50
24
  return 1 if expect_item($word, 1);
68
32
24
  my $key = lc $word;
69
32
15
  return 2 if expect_item($key, 2);
70
32
42
  if ($key =~ /.s$/) {
71
2
6
    if ($key =~ /ies$/) {
72
1
1
      $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
20
    return 0;
82  }
83
7
5
  return 3 if expect_item($key, 3);
84
0
0
  return 0;
85}
86
87sub should_skip_warning {
88
63
36
  my ($warning) = @_;
89
63
82
  if ($warning =~ /\(([-\w]+)\)$/) {
90
59
28
    my ($code) = ($1);
91
59
46
    return 1 if CheckSpelling::Util::is_ignoring_event($code);
92  }
93
63
54
  return 0;
94}
95
96sub should_skip_warning_while_counting {
97
11
6
  my ($warning) = @_;
98
11
21
  return 0 unless ($warning =~ /\(([-\w]+)\)$/);
99
11
10
  my ($code) = ($1);
100
11
9
  return 1 if CheckSpelling::Util::is_ignoring_event($code);
101
102
9
6
  our %counters;
103
9
5
  ++$counters{$code};
104
9
10
  return 0;
105}
106
107sub log_skip_item {
108
46
59
  my ($item, $file, $warning, $unknown_word_limit) = @_;
109
46
34
  return 1 if should_skip_warning $warning;
110
46
27
  return 1 if skip_item($item);
111
21
14
  our %seen;
112
21
9
  my $seen_count = $seen{$item};
113
21
15
  if (defined $seen_count) {
114
8
9
    if (!defined $unknown_word_limit || ($seen_count++ < $unknown_word_limit)) {
115
7
58
      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
14
    $seen{$item} = $seen_count;
121
8
13
    return 1;
122  }
123
13
10
  $seen{$item} = 1;
124
13
11
  return 0;
125}
126
127sub stem_word {
128
28
825
  my ($key) = @_;
129
28
13
  our $disable_word_collating;
130
28
16
  return $key if $disable_word_collating;
131
28
14
  our $shortest_word;
132
28
15
  $shortest_word = 2 unless defined $shortest_word;
133
28
18
  my $key_length = length $key;
134
135
28
46
  if ($key =~ /.s$/) {
136
5
21
    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
17
      $key =~ s/s$//;
140    }
141  } elsif ($key_length > ($shortest_word + 1) && $key =~ /.[^aeiou]ed$/) {
142
2
3
    $key =~ s/ed$//;
143  }
144
28
25
  return $key;
145}
146
147sub collate_key {
148
81
80
  my ($key) = @_;
149
81
24
  our $disable_word_collating;
150
81
31
  my $char;
151
81
51
  if ($disable_word_collating) {
152
8
7
    $char = lc substr $key, 0, 1;
153  } else {
154
73
50
    $key = lc $key;
155
73
44
    $key =~ s/''+/'/g;
156
73
45
    $key =~ s/'[sd]$//;
157
73
36
    $key =~ s/^[^Ii]?'+(.*)/$1/;
158
73
39
    $key =~ s/(.*?)'$/$1/;
159
73
57
    $char = substr $key, 0, 1;
160  }
161
81
106
  return ($key, $char);
162}
163
164sub load_expect {
165
12
488
  my ($expect) = @_;
166
12
4
  our %expected;
167
12
12
  %expected = ();
168
12
96
  if (open(EXPECT, '<:utf8', $expect)) {
169
12
60
    while (my $word = <EXPECT>) {
170
43
55
      $word =~ s/\R//;
171
43
81
      $expected{$word} = 0;
172    }
173
12
29
    close EXPECT;
174  }
175}
176
177sub harmonize_expect {
178
11
8
  our $disable_word_collating;
179
11
3
  our %letter_map;
180
11
8
  our %expected;
181
182
11
14
  for my $word (keys %expected) {
183
40
20
    my ($key, $char) = collate_key $word;
184
40
20
    my %word_map = ();
185
40
48
    next unless defined $letter_map{$char}{$key};
186
13
13
6
17
    %word_map = %{$letter_map{$char}{$key}};
187
13
16
    next if defined $word_map{$word};
188
3
2
    my $words = scalar keys %word_map;
189
3
2
    next if $words > 2;
190
3
2
    if ($word eq $key) {
191
1
2
      next if ($words > 1);
192    }
193
2
2
    delete $expected{$word};
194  }
195}
196
197sub group_related_words {
198
12
3
  our %letter_map;
199
12
7
  our $disable_word_collating;
200
12
7
  return if $disable_word_collating;
201
202  # group related words
203
11
28
  for my $char (sort CheckSpelling::Util::number_biased keys %letter_map) {
204
19
19
9
15
    for my $plural_key (sort keys(%{$letter_map{$char}})) {
205
22
18
      my $key = stem_word $plural_key;
206
22
21
      next if $key eq $plural_key;
207
4
4
      next unless defined $letter_map{$char}{$key};
208
3
3
2
5
      my %word_map = %{$letter_map{$char}{$key}};
209
3
3
2
4
      for my $word (keys(%{$letter_map{$char}{$plural_key}})) {
210
3
3
        $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
9
  my ($warning) = @_;
220
9
8
  our %counters;
221
9
16
  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
1
    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
19
  my ($path) = @_;
238
24
22
  return unless defined $ENV{$path};
239
24
23
  $ENV{$path} =~ /(.*)/;
240
24
128
  return unless open ITEMS, '<:utf8', $1;
241
242
24
15
  my @items;
243
24
10
  my $context = '';
244
24
85
  while (<ITEMS>) {
245
5
4
    my $pattern = $_;
246
5
7
    if ($pattern =~ /^#/) {
247
2
2
      if ($pattern =~ /^# /) {
248
2
3
        $context .= $pattern;
249      } else {
250
0
0
        $context = '';
251      }
252
2
4
      next;
253    }
254
3
4
    chomp $pattern;
255
3
4
    unless ($pattern =~ /./) {
256
1
0
      $context = '';
257
1
2
      next;
258    }
259
2
3
    push @items, $context.$pattern;
260
2
5
    $context = '';
261  }
262
24
51
  close ITEMS;
263
24
23
  return @items;
264}
265
266sub summarize_totals {
267
24
21
  my ($formatter, $path, $items, $totals, $file_counts) = @_;
268
24
24
7
22
  return unless @{$totals};
269
2
54
  return unless open my $fh, '>:utf8', $path;
270
2
2
0
2
  my $totals_count = scalar(@{$totals}) - 1;
271
2
1
  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
1
0
      $totals->[$b] <=> $totals->[$a]
280    } 0 .. $totals_count;
281  }
282
2
1
  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
5
    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
3
    print $fh $formatter->(
292      $totals->[$i],
293      ($file_counts ? " file-count: $file_counts->[$i]" : ""),
294      $description,
295      $rule
296    );
297  }
298
2
79
  close $fh;
299}
300
301sub get_special {
302
19
16
  my ($file, $special) = @_;
303
19
23
  return 'file-list' if $file eq $special->{'file_list'};
304
17
18
  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
23
  return 'commit-message' if !rindex($file, $special->{'commit_messages'});
307
11
15
  return 'file';
308}
309
310sub main {
311
12
21163
  my @directories;
312  my @cleanup_directories;
313
12
0
  my @check_file_paths;
314
315
12
15
  CheckSpelling::Util::build_ignored_event_map();
316
12
8
  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
7
  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
6
  my $should_exclude_file = CheckSpelling::Util::get_file_from_env('should_exclude_file', '/dev/null');
321
12
10
  my $unknown_word_limit = CheckSpelling::Util::get_val_from_env('unknown_word_limit', undef);
322
12
8
  my $unknown_file_word_limit = CheckSpelling::Util::get_val_from_env('unknown_file_word_limit', undef);
323
12
7
  my $candidate_example_limit = CheckSpelling::Util::get_file_from_env('INPUT_CANDIDATE_EXAMPLE_LIMIT', '3');
324
12
9
  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
7
  my $disable_noisy_file = $disable_flags =~ /(?:^|,|\s)noisy-file(?:,|\s|$)/;
327
12
29
  our $disable_word_collating = $only_check_changed_files || $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
328
12
8
  our $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
329
12
7
  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
6
  my $pr_description_file = CheckSpelling::Util::get_file_from_env('pr_description_file', '');
332
12
8
  my $commit_messages = CheckSpelling::Util::get_file_from_env('commit_messages', '');
333
12
9
  my $timing_report = CheckSpelling::Util::get_file_from_env('timing_report', '');
334
12
15
  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
9
  my ($start_time, $end_time);
341
342
12
226
  open WARNING_OUTPUT, '>:utf8', $warning_output;
343
12
154
  open MORE_WARNINGS, '>:utf8', $more_warnings;
344
12
136
  open COUNTER_SUMMARY, '>:utf8', $counter_summary;
345
12
80
  open SHOULD_EXCLUDE, '>:utf8', $should_exclude_file;
346
12
10
  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
9
  my @candidates = get_pattern_with_context('candidates_path');
352
12
10
  my @candidate_totals = (0) x scalar @candidates;
353
12
9
  my @candidate_file_counts = (0) x scalar @candidates;
354
355
12
5
  my @forbidden = get_pattern_with_context('forbidden_path');
356
12
8
  my @forbidden_totals = (0) x scalar @forbidden;
357
358
12
3
  my @delayed_warnings;
359
12
25
  our %letter_map = ();
360
361
12
4
  my %file_map = ();
362
363
12
29
  for my $directory (<>) {
364
15
14
    chomp $directory;
365
15
25
    next unless $directory =~ /^(.*)$/;
366
15
10
    $directory = $1;
367
15
44
    unless (-e $directory) {
368
1
2
      print STDERR "Could not find: $directory\n";
369
1
1
      next;
370    }
371
14
34
    unless (-d $directory) {
372
1
12
      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
80
    next unless open(NAME, '<:utf8', "$directory/name");
378
12
78
    my $file=<NAME>;
379
12
24
    close NAME;
380
381
12
23
    $file_map{$file} = $directory;
382  }
383
384
12
18
  for my $file (sort keys %file_map) {
385
12
10
    my $directory = $file_map{$file};
386
12
9
    if ($timing_report) {
387
0
0
      $start_time = (stat "$directory/name")[9];
388    }
389
390
12
63
    if (-e "$directory/skipped") {
391
1
6
      open SKIPPED, '<:utf8', "$directory/skipped";
392
1
17
      my $reason=<SKIPPED>;
393
1
4
      close SKIPPED;
394
1
1
      chomp $reason;
395
1
5
      push @delayed_warnings, "$file:1:1 ... 1, Warning - Skipping `$file` because $reason\n";
396
1
3
      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
1
      next;
400    }
401
402    # stats isn't written if there was nothing interesting in the file
403
11
33
    unless (-s "$directory/stats") {
404
1
1
      report_timing($file, $start_time, $directory, 'warnings') if ($timing_report);
405
1
1
      push @directories, $directory;
406
1
1
      next;
407    }
408
409
10
10
    if ($file eq $file_list) {
410
1
5
      open FILE_LIST, '<:utf8', $file_list;
411
1
1
      push @check_file_paths, '0 placeholder';
412
1
6
      for my $check_file_path (<FILE_LIST>) {
413
4
2
        chomp $check_file_path;
414
4
4
        push @check_file_paths, $check_file_path;
415      }
416
1
2
      close FILE_LIST;
417    }
418
419
10
5
    my ($words, $unrecognized, $unknown, $unique);
420
421    {
422
10
10
7
51
      open STATS, '<:utf8', "$directory/stats";
423
10
39
      my $stats=<STATS>;
424
10
19
      close STATS;
425
10
10
      $words=get_field($stats, 'words');
426
10
9
      $unrecognized=get_field($stats, 'unrecognized');
427
10
8
      $unknown=get_field($stats, 'unknown');
428
10
5
      $unique=get_field($stats, 'unique');
429
10
5
      my @candidate_list;
430
10
7
      if (@candidate_totals) {
431
1
1
        @candidate_list=get_array($stats, 'candidates');
432
1
1
        my @lines=get_array($stats, 'candidate_lines');
433
1
1
        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
2
                my $pattern = (split /\n/,$candidates[$i])[-1];
440
1
2
                my $position = $lines[$i];
441
1
4
                $position =~ s/:(\d+)$/ ... $1/;
442
1
1
                my $wrapped = CheckSpelling::Util::truncate_with_ellipsis(CheckSpelling::Util::wrap_in_backticks($pattern), 99);
443
1
2
                my $candidate_label = '';
444
1
2
                if ($candidates[$i] =~ /^#\s+(\S.+)/) {
445
1
1
                  $candidate_label = " ($1)";
446                }
447
1
5
                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
1
        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
2
              $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
12
    report_timing($file, $start_time, $directory, 'unknown') if ($timing_report);
469
10
9
    my $kind = get_special($file, $special);
470    # These heuristics are very new and need tuning/feedback
471
10
11
    if (
472        ($unknown > $unique)
473        # || ($unrecognized > $words / 2)
474    ) {
475
1
1
      unless ($disable_noisy_file) {
476
1
1
        if ($kind eq 'file') {
477
1
2
          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
7
    push @directories, $directory;
487
9
38
    unless ($kind =~ /^file/ && -s "$directory/unknown") {
488
4
7
      next;
489    }
490
5
36
    open UNKNOWN, '<:utf8', "$directory/unknown";
491
5
38
    for my $token (<UNKNOWN>) {
492
43
45
      $token =~ s/\R//;
493
43
40
      next unless $token =~ /./;
494
41
21
      my ($key, $char) = collate_key $token;
495
41
45
      $letter_map{$char} = () unless defined $letter_map{$char};
496
41
21
      my %word_map = ();
497
41
14
35
16
      %word_map = %{$letter_map{$char}{$key}} if defined $letter_map{$char}{$key};
498
41
36
      $word_map{$token} = 1;
499
41
48
      $letter_map{$char}{$key} = \%word_map;
500    }
501
5
19
    close UNKNOWN;
502  }
503
12
28
  close SHOULD_EXCLUDE;
504
12
9
  close TIMING_REPORT if $timing_report;
505
506  summarize_totals(
507    sub {
508
1
2
      my ($hits, $files, $context, $pattern) = @_;
509
1
2
      return "# hit-count: $hits$files\n$context$pattern\n\n",
510    },
511
12
29
    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
4
      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
0
      $context = 'Pattern' unless $context;
528
1
5
      return "##### $context$details\n```\n$pattern\n```\n\n";
529    },
530
12
39
    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
17
  if (defined $ENV{'expect'}) {
538
11
11
    $ENV{'expect'} =~ /(.*)/;
539
11
11
    load_expect $1;
540
11
6
    harmonize_expect;
541  }
542
543
12
8
  my %seen = ();
544
12
5
  our %counters;
545
12
7
  %counters = ();
546
547
12
37
  if (-s $early_warnings) {
548
1
6
    open WARNINGS, '<:utf8', $early_warnings;
549
1
6
    for my $warning (<WARNINGS>) {
550
1
1
      chomp $warning;
551
1
1
      next if should_skip_warning_while_counting $warning;
552
1
9
      print WARNING_OUTPUT "$warning\n";
553    }
554
1
2
    close WARNINGS;
555  }
556
557
12
7
  our %last_seen;
558
12
5
  my %unknown_file_word_count;
559
12
12
  for my $directory (@directories) {
560
10
27
    next unless (-s "$directory/warnings");
561
9
54
    next unless open(NAME, '<:utf8', "$directory/name");
562
9
24
    my $file=<NAME>;
563
9
16
    close NAME;
564
9
7
    my $kind = get_special($file, $special);
565
9
46
    open WARNINGS, '<:utf8', "$directory/warnings";
566
9
8
    if ($kind ne 'file-list') {
567
8
51
      for my $warning (<WARNINGS>) {
568
50
24
        my $code;
569
50
37
        chomp $warning;
570
50
83
        if ($warning =~ m/:(\d+):(\d+ \.\.\. \d+): `(.*)`/) {
571
46
44
          my ($line, $range, $item) = ($1, $2, $3);
572
46
33
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($item);
573
46
23
          my $reason = 'unrecognized-spelling';
574
46
31
          $reason .= "-$kind" unless $kind eq 'file';
575
46
119
          $warning =~ s/:\d+:\d+ \.\.\. \d+: `.*`/:$line:$range, Warning - $wrapped is not a recognized word ($reason)/;
576
46
29
          next if log_skip_item($item, $file, $warning, $unknown_word_limit);
577
13
11
          count_warning $warning if $kind ne 'file';
578
13
6
          $code = $reason;
579        } else {
580
4
2
          if ($warning =~ /\`(.*?)\` in line \(token-is-substring\)/) {
581
0
0
            next if skip_item($1);
582          }
583
4
3
          $code = count_warning $warning;
584        }
585
17
16
        next if should_skip_warning $warning, $code;
586
17
56
        print WARNING_OUTPUT "$file$warning\n";
587      }
588    } else {
589
1
7
      for my $warning (<WARNINGS>) {
590
6
5
        chomp $warning;
591
6
12
        next unless $warning =~ s/^:(\d+)/:1/;
592
6
5
        $file = $check_file_paths[$1];
593
6
15
        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
10
          $warning =~ s/:\d+ \.\.\. \d+: `.*`/:$range, Warning - $wrapped is not a recognized word (check-file-path)/;
597
4
3
          next if skip_item($item);
598
4
3
          if (defined $unknown_file_word_limit) {
599
4
6
            next if ++$unknown_file_word_count{$item} > $unknown_file_word_limit;
600          }
601        }
602
5
4
        next if should_skip_warning_while_counting $warning;
603
4
13
        print WARNING_OUTPUT "$file$warning\n";
604      }
605    }
606
9
34
    close WARNINGS;
607  }
608
12
275
  close MORE_WARNINGS;
609
610
12
11
  for my $warning (@delayed_warnings) {
611
3
3
    next if should_skip_warning_while_counting $warning;
612
3
7
    print WARNING_OUTPUT $warning;
613  }
614
12
6
  if (defined $unknown_word_limit) {
615
1
2
    for my $warned_word (sort keys %last_seen) {
616
1
2
      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
312
  close WARNING_OUTPUT;
625
626
12
14
  if (%counters) {
627
3
3
    my $continue='';
628
3
6
    print COUNTER_SUMMARY "{\n";
629
3
6
    for my $code (sort keys %counters) {
630
6
9
      print COUNTER_SUMMARY qq<$continue"$code": $counters{$code}\n>;
631
6
4
      $continue=',';
632    }
633
3
3
    print COUNTER_SUMMARY "}\n";
634  }
635
12
97
  close COUNTER_SUMMARY;
636
637  # display the current unknown
638
12
27
  for my $char (sort keys %letter_map) {
639
43
43
17
99
    for my $key (sort CheckSpelling::Util::case_biased keys(%{$letter_map{$char}})) {
640
24
24
15
33
      my %word_map = %{$letter_map{$char}{$key}};
641
24
19
      my @words = keys(%word_map);
642
24
37
      if (scalar(@words) > 1) {
643
13
19
12
71
        print $key." (".(join ", ", sort { length($a) <=> length($b) || $a cmp $b } @words).")";
644      } else {
645
11
32
        print $words[0];
646      }
647
24
87
      print "\n";
648    }
649  }
650}
651
652sub collate_expect {
653
1
647
  my ($collated, $notes) = @_;
654
1
1
  my @words;
655
1
6
  open EXPECT, '<', $collated;
656
1
6
  while (<EXPECT>) {
657
5
5
    chomp;
658
5
10
    next unless /(.*) \((.*)\)/;
659
1
1
    my ($key, $list) = ($1, $2);
660
1
1
    my @variants = split /, /, $list;
661
1
2
1
3
    @variants = grep { $_ ne $key } @variants;
662
1
1
    push @words, @variants;
663  }
664
1
3
  close EXPECT;
665
1
1
1
2
  my $pattern = '\`(?:'.join('|', map { quotemeta($_) } @words).')`';
666
1
5
  open SOURCES, '<', $notes;
667
1
9
  while (<SOURCES>) {
668
9
38
    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
5
      $print = 1 if s/unrecognized-spelling/ignored-expect-variant/;
672
4
4
      next unless $print;
673    } else {
674
5
10
      next unless /\(((?:\w+-)+\w+)\)$/;
675
2
4
      next if $1 eq 'unrecognized-spelling';
676    }
677
4
21
    print;
678  }
679
1
3
  close SOURCES;
680
1
2
  return 0;
681}
682
6831;