-
Notifications
You must be signed in to change notification settings - Fork 33
/
perl_code_spellcheck.pl
executable file
·224 lines (180 loc) · 5.9 KB
/
perl_code_spellcheck.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 29 January 2017
# https://github.com/trizen
# Checks English words for spelling errors in Perl code.
# It tries to minimize false positives as much as possible.
use 5.014;
use strict;
use warnings;
use Text::Hunspell;
use List::Util qw(max);
use File::Find qw(find);
use Encode qw(decode_utf8);
use Perl::Tokenizer qw(perl_tokens);
use Text::JaroWinkler qw(strcmp95);
use Getopt::Long qw(GetOptions :config no_ignore_case);
binmode(STDOUT, ':utf8');
my $similarity = 90 / 100;
my $min_word_len = 6;
my $aggressive = 0;
my $non_word_split = 0;
my $scan_cats = 'com,str';
sub help {
my ($code) = @_;
my $p = sprintf('%.0f', $similarity * 100);
print <<"HELP";
usage: $0 [options] [files]
Options:
-m --minimum=f : minimum length for words (default: $min_word_len)
-p --percentage=f : minimum similarity percentage (default: $p)
-W --W-split! : split by non-word characters (default: by space)
-s --scan=s : categories of tokens to scan (default: "$scan_cats")
All the possible categories for --scan are:
pod : scan pod sections (including __END__)
str : scan strings (including here-documents)
com : scan comments
var : scan variable names
sub : scan subroutine declarations
bar : scan barewords (including subroutine/method calls)
all : scan all categories
Example:
$0 --scan=pod,com --percentage=75 /my/script.pl
HELP
exit($code);
}
my $percentage;
GetOptions(
'm|minimum=i' => \$min_word_len,
'p|percentage=f' => \$percentage,
's|scan=s' => \$scan_cats,
'W|W-split!' => \$non_word_split,
'h|help' => sub { help(0) },
)
or die("Error in command line arguments");
my $scan_pod = $scan_cats =~ /\bpod/;
my $scan_strings = $scan_cats =~ /\bstr/;
my $scan_comments = $scan_cats =~ /\bcom/;
my $scan_variables = $scan_cats =~ /\bvar/;
my $scan_subroutines = $scan_cats =~ /\bsub/;
my $scan_barewords = $scan_cats =~ /\bbar/;
if ($scan_cats =~ /\ball/) {
$scan_pod = 1;
$scan_strings = 1;
$scan_comments = 1;
$scan_variables = 1;
$scan_subroutines = 1;
$scan_barewords = 1;
}
if ( not $scan_pod
and not $scan_strings
and not $scan_comments
and not $scan_variables
and not $scan_subroutines
and not $scan_barewords) {
die "Invalid value for `--scan`: <<$scan_cats>>";
}
if (defined $percentage) {
$similarity = $percentage / 100;
}
#<<<
my $speller = Text::Hunspell->new(
"/usr/share/hunspell/en_US.aff",
"/usr/share/hunspell/en_US.dic",
) or die "Can't create the speller object: $!";
#>>>
@ARGV || help(2);
@ARGV = reverse(@ARGV);
while (@ARGV) {
my %seen;
my $file = pop @ARGV;
if (-d $file) {
find {
no_chdir => 1,
wanted => sub {
if (-f($_) and /\.p[lm]\z/) {
push @ARGV, $_;
}
},
} => $file;
next;
}
$file = decode_utf8($file);
open my $fh, '<:encoding(UTF-8)', $file or next;
local $SIG{__WARN__} = sub { };
my $code = eval { local $/; <$fh> } // next;
say "\n** Scanning: $file";
perl_tokens {
my ($token, $i, $j) = @_;
my $string;
if ($scan_strings) {
if ($token eq 'q_string') {
$string = substr($code, $i + 2, $j - $i - 3);
}
elsif ( $token eq 'qq_string'
or $token eq 'qw_string') {
$string = substr($code, $i + 3, $j - $i - 4);
}
elsif ( $token eq 'double_quoted_string'
or $token eq 'single_quoted_string') {
$string = substr($code, $i + 1, $j - $i - 2);
}
elsif ($token eq 'heredoc') {
$string = substr($code, $i, $j - $i);
$string =~ s/.*\K\R.*//s;
}
}
if ($scan_comments) {
if ($token eq 'comment') {
$string = substr($code, $i + 1, $j - $i - 1);
}
}
if ($scan_pod) {
if ( $token eq 'pod'
or $token eq 'data') {
$string = substr($code, $i, $j - $i);
}
}
if ($scan_variables) {
if ($token eq 'var_name') {
$string = substr($code, $i, $j - $i);
}
}
if ($scan_subroutines) {
if ($token eq 'sub_name') {
$string = substr($code, $i, $j - $i);
}
}
if ($scan_barewords) {
if ($token eq 'bare_word') {
$string = substr($code, $i, $j - $i);
}
}
if (defined $string) {
foreach my $word (
$non_word_split
? split(/[^\pL]+/, $string)
: split(' ', $string)
) {
if (!$non_word_split) {
$word =~ s/^[^\pL]+//;
$word =~ s/[^\pL]+\z//;
}
$word !~ /^[\pL]+\z/ and next;
length($word) < $min_word_len and next;
$seen{$word}++ and next;
$speller->check($word) and next;
my @suggestions = $speller->suggest($word);
if ( @suggestions
and lc($suggestions[0]) ne lc($word)
and $suggestions[0] !~ / /) {
my $score = strcmp95($suggestions[0], $word, max(length($suggestions[0]), length($word)));
if ($score >= $similarity) {
printf "[%.2f] %-20s => [%s]\n", $score, $word, join(', ', @suggestions);
}
}
}
}
} $code;
}