| File: | lib/CheckSpelling/SpellingCollator.pm |
| Coverage: | 84.4% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | #! -*-perl-*- | |||||
| 2 | ||||||
| 3 | package CheckSpelling::SpellingCollator; | |||||
| 4 | ||||||
| 5 | our $VERSION='0.1.0'; | |||||
| 6 | 1 1 1 | 107201 2 21 | use warnings; | |||
| 7 | 1 1 1 | 2 0 24 | use File::Path qw(remove_tree); | |||
| 8 | 1 1 1 | 210 1 2270 | use CheckSpelling::Util; | |||
| 9 | ||||||
| 10 | my %letter_map; | |||||
| 11 | my %ignored_event_map; | |||||
| 12 | my $disable_word_collating; | |||||
| 13 | ||||||
| 14 | my %last_seen; | |||||
| 15 | ||||||
| 16 | sub get_field { | |||||
| 17 | 40 | 31 | my ($record, $field) = @_; | |||
| 18 | 40 | 448 | return 0 unless $record =~ (/\b$field:\s*(\d+)/); | |||
| 19 | 28 | 29 | return $1; | |||
| 20 | } | |||||
| 21 | ||||||
| 22 | sub get_array { | |||||
| 23 | 2 | 1 | my ($record, $field) = @_; | |||
| 24 | 2 | 16 | return () unless $record =~ (/\b$field: \[([^\]]+)\]/); | |||
| 25 | 2 | 2 | my $values = $1; | |||
| 26 | 2 | 3 | return split /\s*,\s*/, $values; | |||
| 27 | } | |||||
| 28 | ||||||
| 29 | sub maybe { | |||||
| 30 | 7 | 4 | my ($next, $value) = @_; | |||
| 31 | 7 | 7 | $next = $value unless $next && $next < $value; | |||
| 32 | 7 | 7 | return $next; | |||
| 33 | } | |||||
| 34 | ||||||
| 35 | my %expected = (); | |||||
| 36 | sub expect_item { | |||||
| 37 | 104 | 62 | my ($item, $value) = @_; | |||
| 38 | 104 | 44 | our %expected; | |||
| 39 | 104 | 37 | my $next; | |||
| 40 | 104 | 101 | if (defined $expected{$item}) { | |||
| 41 | 26 | 30 | $next = $expected{$item}; | |||
| 42 | 26 | 18 | $next = $value if $value < $next; | |||
| 43 | } elsif ($item =~ /^([A-Z])(.*)/) { | |||||
| 44 | 12 | 12 | $item = $1 . lc $2; | |||
| 45 | 12 | 8 | if (defined $expected{$item}) { | |||
| 46 | 2 | 0 | $next = $expected{$item}; | |||
| 47 | 2 | 3 | $next = maybe($next, $value + .1); | |||
| 48 | } else { | |||||
| 49 | 10 | 6 | $item = lc $item; | |||
| 50 | 10 | 7 | if (defined $expected{$item}) { | |||
| 51 | 5 | 3 | $next = $expected{$item}; | |||
| 52 | 5 | 3 | $next = maybe($next, $value + .2); | |||
| 53 | } | |||||
| 54 | } | |||||
| 55 | } | |||||
| 56 | 104 | 95 | return 0 unless defined $next; | |||
| 57 | 33 | 16 | $expected{$item} = $next; | |||
| 58 | 33 | 86 | return $value; | |||
| 59 | } | |||||
| 60 | ||||||
| 61 | sub skip_item { | |||||
| 62 | 55 | 32 | my ($word) = @_; | |||
| 63 | 55 | 28 | return 1 if expect_item($word, 1); | |||
| 64 | 35 | 22 | my $key = lc $word; | |||
| 65 | 35 | 18 | return 2 if expect_item($key, 2); | |||
| 66 | 35 | 49 | if ($key =~ /.s$/) { | |||
| 67 | 2 | 2 | if ($key =~ /ies$/) { | |||
| 68 | 1 | 2 | $key =~ s/ies$/y/; | |||
| 69 | } else { | |||||
| 70 | 1 | 3 | $key =~ s/s$//; | |||
| 71 | } | |||||
| 72 | } elsif ($key =~ /^(.+[^aeiou])ed$/) { | |||||
| 73 | 1 | 1 | $key = $1; | |||
| 74 | } elsif ($key =~ /^(.+)'[ds]$/) { | |||||
| 75 | 6 | 2 | $key = $1; | |||
| 76 | } else { | |||||
| 77 | 26 | 26 | return 0; | |||
| 78 | } | |||||
| 79 | 9 | 5 | return 3 if expect_item($key, 3); | |||
| 80 | 0 | 0 | return 0; | |||
| 81 | } | |||||
| 82 | ||||||
| 83 | sub should_skip_warning { | |||||
| 84 | 75 | 43 | my ($warning) = @_; | |||
| 85 | 75 | 91 | if ($warning =~ /\(([-\w]+)\)$/) { | |||
| 86 | 71 | 51 | my ($code) = ($1); | |||
| 87 | 71 | 22 | our %ignored_event_map; | |||
| 88 | 71 | 60 | return 1 if $ignored_event_map{$code}; | |||
| 89 | } | |||||
| 90 | 74 | 62 | return 0; | |||
| 91 | } | |||||
| 92 | ||||||
| 93 | sub log_skip_item { | |||||
| 94 | 51 | 62 | my ($item, $file, $warning, $unknown_word_limit) = @_; | |||
| 95 | 51 | 38 | return 1 if should_skip_warning $warning; | |||
| 96 | 51 | 26 | return 1 if skip_item($item); | |||
| 97 | 22 | 16 | my $seen_count = $seen{$item}; | |||
| 98 | 22 | 13 | if (defined $seen_count) { | |||
| 99 | 9 | 11 | if (!defined $unknown_word_limit || ($seen_count++ < $unknown_word_limit)) { | |||
| 100 | 8 | 35 | 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 | 9 | $seen{$item} = $seen_count; | |||
| 106 | 9 | 17 | return 1; | |||
| 107 | } | |||||
| 108 | 13 | 8 | $seen{$item} = 1; | |||
| 109 | 13 | 13 | return 0; | |||
| 110 | } | |||||
| 111 | ||||||
| 112 | sub stem_word { | |||||
| 113 | 22 | 10 | my ($key) = @_; | |||
| 114 | 22 | 4 | our $disable_word_collating; | |||
| 115 | 22 | 18 | return $key if $disable_word_collating; | |||
| 116 | ||||||
| 117 | 22 | 22 | if ($key =~ /.s$/) { | |||
| 118 | 3 | 2 | if ($key =~ /ies$/) { | |||
| 119 | 1 | 3 | $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 | 12 | return $key; | |||
| 127 | } | |||||
| 128 | ||||||
| 129 | sub collate_key { | |||||
| 130 | 86 | 55 | my ($key) = @_; | |||
| 131 | 86 | 25 | our $disable_word_collating; | |||
| 132 | 86 | 54 | if ($disable_word_collating) { | |||
| 133 | 16 | 13 | $char = lc substr $key, 0, 1; | |||
| 134 | } else { | |||||
| 135 | 70 | 48 | $key = lc $key; | |||
| 136 | 70 | 47 | $key =~ s/''+/'/g; | |||
| 137 | 70 | 35 | $key =~ s/'[sd]$//; | |||
| 138 | 70 | 34 | $key =~ s/^[^Ii]?'+(.*)/$1/; | |||
| 139 | 70 | 35 | $key =~ s/(.*?)'$/$1/; | |||
| 140 | 70 | 80 | $char = substr $key, 0, 1; | |||
| 141 | } | |||||
| 142 | 86 | 93 | return ($key, $char); | |||
| 143 | } | |||||
| 144 | ||||||
| 145 | sub load_expect { | |||||
| 146 | 12 | 451 | my ($expect) = @_; | |||
| 147 | 12 | 12 | our %expected; | |||
| 148 | 12 | 14 | %expected = (); | |||
| 149 | 12 | 91 | if (open(EXPECT, '<:utf8', $expect)) { | |||
| 150 | 12 | 57 | while ($word = <EXPECT>) { | |||
| 151 | 43 | 66 | $word =~ s/\R//; | |||
| 152 | 43 | 108 | $expected{$word} = 0; | |||
| 153 | } | |||||
| 154 | 12 | 32 | close EXPECT; | |||
| 155 | } | |||||
| 156 | } | |||||
| 157 | ||||||
| 158 | sub harmonize_expect { | |||||
| 159 | 11 | 2 | our $disable_word_collating; | |||
| 160 | 11 | 5 | our %letter_map; | |||
| 161 | 11 | 5 | our %expected; | |||
| 162 | ||||||
| 163 | 11 | 13 | for my $word (keys %expected) { | |||
| 164 | 40 | 21 | my ($key, $char) = collate_key $word; | |||
| 165 | 40 | 25 | my %word_map = (); | |||
| 166 | 40 | 43 | next unless defined $letter_map{$char}{$key}; | |||
| 167 | 15 15 | 5 17 | %word_map = %{$letter_map{$char}{$key}}; | |||
| 168 | 15 | 22 | next if defined $word_map{$word}; | |||
| 169 | 3 | 1 | my $words = scalar keys %word_map; | |||
| 170 | 3 | 5 | 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 | ||||||
| 178 | sub group_related_words { | |||||
| 179 | 12 | 7 | our %letter_map; | |||
| 180 | 12 | 4 | our $disable_word_collating; | |||
| 181 | 12 | 10 | return if $disable_word_collating; | |||
| 182 | ||||||
| 183 | # group related words | |||||
| 184 | 10 | 21 | for my $char (sort CheckSpelling::Util::number_biased keys %letter_map) { | |||
| 185 | 19 19 | 6 21 | for my $plural_key (sort keys(%{$letter_map{$char}})) { | |||
| 186 | 22 | 14 | my $key = stem_word $plural_key; | |||
| 187 | 22 | 23 | next if $key eq $plural_key; | |||
| 188 | 4 | 4 | next unless defined $letter_map{$char}{$key}; | |||
| 189 | 3 3 | 1 6 | my %word_map = %{$letter_map{$char}{$key}}; | |||
| 190 | 3 3 | 12 4 | for $word (keys(%{$letter_map{$char}{$plural_key}})) { | |||
| 191 | 3 | 3 | $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 | ||||||
| 199 | sub count_warning { | |||||
| 200 | 13 | 13 | my ($warning) = @_; | |||
| 201 | 13 | 2 | our %counters; | |||
| 202 | 13 | 7 | our %ignored_event_map; | |||
| 203 | 13 | 39 | if ($warning =~ /\(([-\w]+)\)$/) { | |||
| 204 | 8 | 7 | my ($code) = ($1); | |||
| 205 | 8 | 7 | next if defined $ignored_event_map{$code}; | |||
| 206 | 8 | 9 | ++$counters{$code}; | |||
| 207 | } | |||||
| 208 | } | |||||
| 209 | ||||||
| 210 | sub 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 | ||||||
| 217 | sub get_pattern_with_context { | |||||
| 218 | 24 | 15 | my ($path) = @_; | |||
| 219 | 24 | 27 | return unless defined $ENV{$path}; | |||
| 220 | 12 | 13 | $ENV{$path} =~ /(.*)/; | |||
| 221 | 12 | 66 | return unless open ITEMS, '<:utf8', $1; | |||
| 222 | ||||||
| 223 | 12 | 8 | my @items; | |||
| 224 | 12 | 4 | my $context = ''; | |||
| 225 | 12 | 55 | while (<ITEMS>) { | |||
| 226 | 2 | 2 | my $pattern = $_; | |||
| 227 | 2 | 3 | if ($pattern =~ /^#/) { | |||
| 228 | 1 | 2 | if ($pattern =~ /^# /) { | |||
| 229 | 1 | 1 | $context .= $pattern; | |||
| 230 | } else { | |||||
| 231 | 0 | 0 | $context = ''; | |||
| 232 | } | |||||
| 233 | 1 | 3 | next; | |||
| 234 | } | |||||
| 235 | 1 | 1 | chomp $pattern; | |||
| 236 | 1 | 2 | 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 | 32 | close ITEMS; | |||
| 244 | 12 | 15 | return @items; | |||
| 245 | } | |||||
| 246 | ||||||
| 247 | sub summarize_totals { | |||||
| 248 | 24 | 13 | my ($formatter, $path, $items, $totals, $file_counts) = @_; | |||
| 249 | 24 24 | 13 19 | return unless @{$totals}; | |||
| 250 | 1 | 39 | return unless open my $fh, '>:utf8', $path; | |||
| 251 | 1 1 | 1 0 | my $totals_count = scalar(@{$totals}) - 1; | |||
| 252 | 1 | 1 | my @indices; | |||
| 253 | 1 | 1 | 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 | 0 | my ($description, $rule); | |||
| 267 | 1 | 3 | if ($rule_with_context =~ /^(.*\n)([^\n]+)$/s) { | |||
| 268 | 1 | 2 | ($description, $rule) = ($1, $2); | |||
| 269 | } else { | |||||
| 270 | 0 | 0 | ($description, $rule) = ('', $rule_with_context); | |||
| 271 | } | |||||
| 272 | 1 | 1 | print $fh $formatter->( | |||
| 273 | $totals->[$i], | |||||
| 274 | ($file_counts ? " file-count: $file_counts->[$i]" : ""), | |||||
| 275 | $description, | |||||
| 276 | $rule | |||||
| 277 | ); | |||||
| 278 | } | |||||
| 279 | 1 | 39 | close $fh; | |||
| 280 | } | |||||
| 281 | ||||||
| 282 | sub get_special { | |||||
| 283 | 20 | 14 | my ($file, $special) = @_; | |||
| 284 | 20 | 27 | return 'file-list' if $file eq $special->{'file_list'}; | |||
| 285 | 18 | 17 | return 'pr-title' if $file eq $special->{'pr_title_file'}; | |||
| 286 | 16 | 14 | return 'pr-description' if $file eq $special->{'pr_description_file'}; | |||
| 287 | 14 | 28 | return 'commit-message' if !rindex($file, $special->{'commit_messages'}); | |||
| 288 | 12 | 14 | return 'file'; | |||
| 289 | } | |||||
| 290 | ||||||
| 291 | sub main { | |||||
| 292 | 12 | 19907 | my @directories; | |||
| 293 | my @cleanup_directories; | |||||
| 294 | 12 | 0 | my @check_file_paths; | |||
| 295 | ||||||
| 296 | 12 | 16 | my $early_warnings = CheckSpelling::Util::get_file_from_env('early_warnings', '/dev/null'); | |||
| 297 | 12 | 11 | 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 | 7 | my $counter_summary = CheckSpelling::Util::get_file_from_env('counter_summary', '/dev/stderr'); | |||
| 300 | 12 | 8 | my $ignored_events = CheckSpelling::Util::get_file_from_env('ignored_events', ''); | |||
| 301 | 12 | 15 | if ($ignored_events) { | |||
| 302 | 5 | 3 | our %ignored_event_map; | |||
| 303 | 5 | 6 | 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 | 9 | my $unknown_word_limit = CheckSpelling::Util::get_val_from_env('unknown_word_limit', undef); | |||
| 309 | 12 | 9 | my $unknown_file_word_limit = CheckSpelling::Util::get_val_from_env('unknown_file_word_limit', undef); | |||
| 310 | 12 | 8 | 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 | 6 | my $only_check_changed_files = CheckSpelling::Util::get_file_from_env('INPUT_ONLY_CHECK_CHANGED_FILES', ''); | |||
| 313 | 12 | 11 | my $disable_noisy_file = $disable_flags =~ /(?:^|,|\s)noisy-file(?:,|\s|$)/; | |||
| 314 | 12 | 29 | 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 | 10 | my $pr_title_file = CheckSpelling::Util::get_file_from_env('pr_title_file', ''); | |||
| 317 | 12 | 9 | my $pr_description_file = CheckSpelling::Util::get_file_from_env('pr_description_file', ''); | |||
| 318 | 12 | 7 | my $commit_messages = CheckSpelling::Util::get_file_from_env('commit_messages', ''); | |||
| 319 | 12 | 7 | my $timing_report = CheckSpelling::Util::get_file_from_env('timing_report', ''); | |||
| 320 | 12 | 17 | 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 | 246 | open WARNING_OUTPUT, '>:utf8', $warning_output; | |||
| 329 | 12 | 184 | open MORE_WARNINGS, '>:utf8', $more_warnings; | |||
| 330 | 12 | 148 | open COUNTER_SUMMARY, '>:utf8', $counter_summary; | |||
| 331 | 12 | 85 | open SHOULD_EXCLUDE, '>:utf8', $should_exclude_file; | |||
| 332 | 12 | 12 | 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 | 12 | my @candidates = get_pattern_with_context('candidates_path'); | |||
| 338 | 12 | 9 | my @candidate_totals = (0) x scalar @candidates; | |||
| 339 | 12 | 5 | my @candidate_file_counts = (0) x scalar @candidates; | |||
| 340 | ||||||
| 341 | 12 | 7 | my @forbidden = get_pattern_with_context('forbidden_path'); | |||
| 342 | 12 | 7 | my @forbidden_totals = (0) x scalar @forbidden; | |||
| 343 | ||||||
| 344 | 12 | 5 | my @delayed_warnings; | |||
| 345 | 12 | 21 | our %letter_map = (); | |||
| 346 | ||||||
| 347 | 12 | 6 | my %file_map = (); | |||
| 348 | ||||||
| 349 | 12 | 30 | for my $directory (<>) { | |||
| 350 | 15 | 11 | chomp $directory; | |||
| 351 | 15 | 21 | next unless $directory =~ /^(.*)$/; | |||
| 352 | 15 | 15 | $directory = $1; | |||
| 353 | 15 | 50 | unless (-e $directory) { | |||
| 354 | 1 | 3 | print STDERR "Could not find: $directory\n"; | |||
| 355 | 1 | 1 | next; | |||
| 356 | } | |||||
| 357 | 14 | 32 | unless (-d $directory) { | |||
| 358 | 1 | 11 | 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 | 84 | next unless open(NAME, '<:utf8', "$directory/name"); | |||
| 364 | 12 | 55 | my $file=<NAME>; | |||
| 365 | 12 | 20 | close NAME; | |||
| 366 | ||||||
| 367 | 12 | 26 | $file_map{$file} = $directory; | |||
| 368 | } | |||||
| 369 | ||||||
| 370 | 12 | 23 | for my $file (sort keys %file_map) { | |||
| 371 | 12 | 7 | my $directory = $file_map{$file}; | |||
| 372 | 12 | 12 | if ($timing_report) { | |||
| 373 | 0 | 0 | $start_time = (stat "$directory/name")[9]; | |||
| 374 | } | |||||
| 375 | ||||||
| 376 | 12 | 55 | if (-e "$directory/skipped") { | |||
| 377 | 1 | 15 | open SKIPPED, '<:utf8', "$directory/skipped"; | |||
| 378 | 1 | 4 | my $reason=<SKIPPED>; | |||
| 379 | 1 | 3 | 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 | 0 | push @cleanup_directories, $directory; | |||
| 384 | 1 | 1 | report_timing($file, $start_time, $directory, 'skipped') if ($timing_report); | |||
| 385 | 1 | 2 | next; | |||
| 386 | } | |||||
| 387 | ||||||
| 388 | 11 | 8 | push @directories, $directory; | |||
| 389 | # stats isn't written if there was nothing interesting in the file | |||||
| 390 | 11 | 33 | 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 | 11 | if ($file eq $file_list) { | |||
| 396 | 1 | 5 | open FILE_LIST, '<:utf8', $file_list; | |||
| 397 | 1 | 1 | push @check_file_paths, '0 placeholder'; | |||
| 398 | 1 | 6 | for my $check_file_path (<FILE_LIST>) { | |||
| 399 | 4 | 3 | chomp $check_file_path; | |||
| 400 | 4 | 3 | push @check_file_paths, $check_file_path; | |||
| 401 | } | |||||
| 402 | 1 | 2 | close FILE_LIST; | |||
| 403 | } | |||||
| 404 | ||||||
| 405 | 10 | 4 | my ($words, $unrecognized, $unknown, $unique); | |||
| 406 | ||||||
| 407 | { | |||||
| 408 | 10 10 | 7 52 | open STATS, '<:utf8', "$directory/stats"; | |||
| 409 | 10 | 33 | my $stats=<STATS>; | |||
| 410 | 10 | 16 | close STATS; | |||
| 411 | 10 | 10 | $words=get_field($stats, 'words'); | |||
| 412 | 10 | 9 | $unrecognized=get_field($stats, 'unrecognized'); | |||
| 413 | 10 | 10 | $unknown=get_field($stats, 'unknown'); | |||
| 414 | 10 | 6 | $unique=get_field($stats, 'unique'); | |||
| 415 | 10 | 6 | 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 | 12 | if (@forbidden_totals) { | |||
| 436 | 1 | 1 | @forbidden_list=get_array($stats, 'forbidden'); | |||
| 437 | 1 | 1 | my @lines=get_array($stats, 'forbidden_lines'); | |||
| 438 | 1 | 1 | if (@forbidden_list) { | |||
| 439 | 1 | 1 | 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 | 5 | 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 | 16 | 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 | 44 | unless ($kind =~ /^file/ && -s "$directory/unknown") { | |||
| 468 | 4 | 4 | next; | |||
| 469 | } | |||||
| 470 | 6 | 37 | open UNKNOWN, '<:utf8', "$directory/unknown"; | |||
| 471 | 6 | 45 | for $token (<UNKNOWN>) { | |||
| 472 | 49 | 49 | $token =~ s/\R//; | |||
| 473 | 49 | 48 | next unless $token =~ /./; | |||
| 474 | 46 | 26 | my ($key, $char) = collate_key $token; | |||
| 475 | 46 | 56 | $letter_map{$char} = () unless defined $letter_map{$char}; | |||
| 476 | 46 | 19 | my %word_map = (); | |||
| 477 | 46 14 | 40 18 | %word_map = %{$letter_map{$char}{$key}} if defined $letter_map{$char}{$key}; | |||
| 478 | 46 | 40 | $word_map{$token} = 1; | |||
| 479 | 46 | 64 | $letter_map{$char}{$key} = \%word_map; | |||
| 480 | } | |||||
| 481 | 6 | 22 | close UNKNOWN; | |||
| 482 | } | |||||
| 483 | 12 | 29 | close SHOULD_EXCLUDE; | |||
| 484 | 12 | 7 | 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 | 32 | 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 | 1 | my (undef, undef, $context, $pattern) = @_; | |||
| 500 | 1 | 2 | $context =~ s/^# //gm; | |||
| 501 | 1 | 1 | chomp $context; | |||
| 502 | 1 | 1 | my $details; | |||
| 503 | 1 | 3 | if ($context =~ /^(.*?)$(.*)/ms) { | |||
| 504 | 1 | 1 | ($context, $details) = ($1, $2); | |||
| 505 | 1 | 5 | $details = "\n$details" if $details; | |||
| 506 | } | |||||
| 507 | 1 | 1 | $context = 'Pattern' unless $context; | |||
| 508 | 1 | 4 | return "#### $context$details\n```\n$pattern\n```\n\n"; | |||
| 509 | }, | |||||
| 510 | 12 | 36 | CheckSpelling::Util::get_file_from_env('forbidden_summary', '/dev/stderr'), | |||
| 511 | \@forbidden, | |||||
| 512 | \@forbidden_totals, | |||||
| 513 | ); | |||||
| 514 | ||||||
| 515 | 12 | 33 | group_related_words; | |||
| 516 | ||||||
| 517 | 12 | 10 | if (defined $ENV{'expect'}) { | |||
| 518 | 11 | 12 | $ENV{'expect'} =~ /(.*)/; | |||
| 519 | 11 | 10 | load_expect $1; | |||
| 520 | 11 | 10 | harmonize_expect; | |||
| 521 | } | |||||
| 522 | ||||||
| 523 | 12 | 8 | my %seen = (); | |||
| 524 | 12 | 4 | our %counters; | |||
| 525 | 12 | 9 | %counters = (); | |||
| 526 | ||||||
| 527 | 12 | 36 | if (-s $early_warnings) { | |||
| 528 | 1 | 6 | open WARNINGS, '<:utf8', $early_warnings; | |||
| 529 | 1 | 11 | for my $warning (<WARNINGS>) { | |||
| 530 | 1 | 1 | chomp $warning; | |||
| 531 | 1 | 1 | count_warning $warning; | |||
| 532 | 1 | 1 | next if should_skip_warning $warning; | |||
| 533 | 1 | 4 | print WARNING_OUTPUT "$warning\n"; | |||
| 534 | } | |||||
| 535 | 1 | 3 | close WARNINGS; | |||
| 536 | } | |||||
| 537 | ||||||
| 538 | 12 | 6 | our %last_seen; | |||
| 539 | 12 | 6 | my %unknown_file_word_count; | |||
| 540 | 12 | 10 | for my $directory (@directories) { | |||
| 541 | 11 | 29 | next unless (-s "$directory/warnings"); | |||
| 542 | 10 | 63 | next unless open(NAME, '<:utf8', "$directory/name"); | |||
| 543 | 10 | 40 | my $file=<NAME>; | |||
| 544 | 10 | 20 | close NAME; | |||
| 545 | 10 | 7 | my $kind = get_special($file, $special); | |||
| 546 | 10 | 55 | open WARNINGS, '<:utf8', "$directory/warnings"; | |||
| 547 | 10 | 8 | if ($kind ne 'file-list') { | |||
| 548 | 9 | 73 | for $warning (<WARNINGS>) { | |||
| 549 | 55 | 42 | chomp $warning; | |||
| 550 | 55 | 95 | if ($warning =~ m/:(\d+):(\d+ \.\.\. \d+): `(.*)`/) { | |||
| 551 | 51 | 47 | my ($line, $range, $item) = ($1, $2, $3); | |||
| 552 | 51 | 33 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($item); | |||
| 553 | 51 | 27 | my $reason = 'unrecognized-spelling'; | |||
| 554 | 51 | 35 | $reason .= "-$kind" unless $kind eq 'file'; | |||
| 555 | 51 | 110 | $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 | 11 | 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 | 4 | count_warning $warning; | |||
| 563 | } | |||||
| 564 | 17 | 10 | next if should_skip_warning $warning; | |||
| 565 | 17 | 71 | print WARNING_OUTPUT "$file$warning\n"; | |||
| 566 | } | |||||
| 567 | } else { | |||||
| 568 | 1 | 7 | for $warning (<WARNINGS>) { | |||
| 569 | 6 | 5 | chomp $warning; | |||
| 570 | 6 | 10 | next unless $warning =~ s/^:(\d+)/:1/; | |||
| 571 | 6 | 5 | $file = $check_file_paths[$1]; | |||
| 572 | 6 | 12 | if ($warning =~ m/:(\d+ \.\.\. \d+): `(.*)`/) { | |||
| 573 | 4 | 5 | my ($range, $item) = ($1, $2); | |||
| 574 | 4 | 2 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($item); | |||
| 575 | 4 | 13 | $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 | 3 | if (defined $unknown_file_word_limit) { | |||
| 578 | 4 | 5 | next if ++$unknown_file_word_count{$item} > $unknown_file_word_limit; | |||
| 579 | } | |||||
| 580 | } | |||||
| 581 | 5 | 4 | next if should_skip_warning $warning; | |||
| 582 | 4 | 9 | print WARNING_OUTPUT "$file$warning\n"; | |||
| 583 | 4 | 4 | count_warning $warning; | |||
| 584 | } | |||||
| 585 | } | |||||
| 586 | 10 | 38 | close WARNINGS; | |||
| 587 | } | |||||
| 588 | 12 | 276 | close MORE_WARNINGS; | |||
| 589 | ||||||
| 590 | 12 | 10 | for my $warning (@delayed_warnings) { | |||
| 591 | 1 | 1 | next if should_skip_warning $warning; | |||
| 592 | 1 | 1 | count_warning $warning; | |||
| 593 | 1 | 1 | print WARNING_OUTPUT $warning; | |||
| 594 | } | |||||
| 595 | 12 | 11 | if (defined $unknown_word_limit) { | |||
| 596 | 1 | 2 | for my $warned_word (sort keys %last_seen) { | |||
| 597 | 1 | 3 | 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 | 277 | close WARNING_OUTPUT; | |||
| 607 | ||||||
| 608 | 12 | 15 | if (%counters) { | |||
| 609 | 2 | 2 | my $continue=''; | |||
| 610 | 2 | 2 | print COUNTER_SUMMARY "{\n"; | |||
| 611 | 2 | 4 | for my $code (sort keys %counters) { | |||
| 612 | 4 | 6 | print COUNTER_SUMMARY qq<$continue"$code": $counters{$code}\n>; | |||
| 613 | 4 | 3 | $continue=','; | |||
| 614 | } | |||||
| 615 | 2 | 5 | print COUNTER_SUMMARY "}\n"; | |||
| 616 | } | |||||
| 617 | 12 | 80 | close COUNTER_SUMMARY; | |||
| 618 | ||||||
| 619 | # display the current unknown | |||||
| 620 | 12 | 30 | for my $char (sort keys %letter_map) { | |||
| 621 | 43 43 | 23 92 | for $key (sort CheckSpelling::Util::case_biased keys(%{$letter_map{$char}})) { | |||
| 622 | 29 29 | 14 43 | my %word_map = %{$letter_map{$char}{$key}}; | |||
| 623 | 29 | 22 | my @words = keys(%word_map); | |||
| 624 | 29 | 18 | if (scalar(@words) > 1) { | |||
| 625 | 13 21 | 12 93 | print $key." (".(join ", ", sort { length($a) <=> length($b) || $a cmp $b } @words).")"; | |||
| 626 | } else { | |||||
| 627 | 16 | 54 | print $words[0]; | |||
| 628 | } | |||||
| 629 | 29 | 100 | print "\n"; | |||
| 630 | } | |||||
| 631 | } | |||||
| 632 | } | |||||
| 633 | ||||||
| 634 | 1; | |||||