File Coverage

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

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