-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrad
More file actions
executable file
·321 lines (303 loc) · 6.51 KB
/
Copy pathrad
File metadata and controls
executable file
·321 lines (303 loc) · 6.51 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/perl
use 5.010;
use constant PLANES, 4;
my $CJK = $ENV{HOME} . '/cjk';
chdir $CJK or die "Can't cd to $CJK: $!\n";
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";
$co = 0;
while ($ARGV[0] =~ /^-/) {
if ($ARGV[0] eq '-c') {
$co = 1;
shift;
}
elsif ($ARGV[0] eq '-d') {
$debug = 1;
shift;
}
elsif ($ARGV[0] eq '-h' or $ARGV[0] eq '--help') {
print <<"EOH";
Usage:
rad -[cdhklrv] [args]
Switches:
-d Debug
-h This help message
-k Keep trying if necessary by truncating final arg
-l Look for likely lookalikes
-r Match only radicals
-v Verbose (show precomputed subradical list)
-x Expand subradicals
-X Expand subradicals including lookalikes
EOH
exit;
}
elsif ($ARGV[0] eq '-k') {
$keeptrying = 1;
shift;
}
elsif ($ARGV[0] eq '-K') {
$keeptrying = 0;
shift;
}
elsif ($ARGV[0] eq '-l') {
$likeliness = 1;
shift;
}
elsif ($ARGV[0] eq '-r') {
push(@ARGV, "radical");
shift;
}
elsif ($ARGV[0] eq '-v') {
$verbose = 1;
shift;
}
elsif ($ARGV[0] =~ /-x(\d*)/) {
$expand = 0 + $1;
shift;
}
elsif ($ARGV[0] =~ /-X(\d*)/) {
$expand = 0 + $1;
shift;
$eXpand = 1;
}
else { last }
}
#$co = 1 if $ARGV[0] =~ s/!!$/!/;
use Encode;
eval { binmode STD, ":utf8" };
open ALIAS, "cjkrads/alias" or die "Can't open cjkrads/alias: $!\n";
eval { binmode ALIAS, ":utf8" };
while (<ALIAS>) {
chomp;
($key,$val,$hex,$char,$where) = split(' ',$_);
$alias{$key} = $val;
$alias{$char} = $val;
$alias{$hex} = $val;
}
close ALIAS;
if (not @ARGV) {
require Term::ReadLine;
my $term = Term::ReadLine->new('rad');
while (defined($_ = $term->readline(''))) {
my @words = split ' ', $_;
@OUT = ();
next unless @words;
look(@words);
for (reverse @OUT) {
if (@$_) {
print "\n";
print reverse @$_;
}
}
print '-' x 80, "\n";
}
}
else {
@OUT = ();
look(@ARGV);
for (reverse @OUT) {
if (@$_) {
print "\n";
print reverse @$_;
}
}
}
sub look {
my @args = @_;
my $line;
my $cjkrads;
$likely = $likeliness;
if ($expand and $args[0] =~ /^\+/) {
if (!%plusline) {
open(ALL, "cjkrads/ALL");
eval { binmode ALL, ":utf8" };
while (<ALL>) {
while (m/\+([a-zA-Z][-\w]*)/g) {
$plusline{$1} = $_;
}
}
}
$line = $plusline{substr($args[0],1)};
}
for (;;) {
my $vec = "\xff" x (0x10000 * PLANES / 8);
if (!$line and !$cjkrads) {
my @newargs;
while ($arg = shift @args) {
$file = $arg;
$file =~ s/^\+//;
$file =~ s/^\d+[*.]//;
$file =~ s/!+$//;
$file =~ s/\.\w+$//;
$file =~ s/\~\w*$//;
next if $file eq "";
if ($alias{$file} ne $file and -f "cjkrads/vec/$alias{$file}") {
warn "*** Mapping $file -> $alias{$file}\n";
$file = $alias{$file};
}
if (not -f "cjkrads/vec/$file") {
my @n = split(/-/,$file);
if (@n < 2) {
push @newargs, $arg;
next;
}
$arg = shift @n;
unshift(@args,@n);
}
push @newargs, $arg;
warn "Selecting $file\n" if $debug;
open(BITS, "cjkrads/vec/$file");
sysread(BITS, $bits, -s "cjkrads/vec/$file");
close BITS;
$vec &= $bits;
}
@args = @newargs;
my $filename = "./ALL";
open $cjkrads, "cjkrads/$filename" or die "Can't open cjkrads/$filename: $!";
eval { binmode $cjkrads, ":utf8" };
}
my @pats;
my @negs;
for (@args) {
my $pat = $_;
if ($pat =~ s/^-//) {
my $tmp = transmogrify($pat);
push @pats, qr/$tmp/;
push @negs, 1;
}
else {
my $tmp = transmogrify($pat);
push @pats, qr/$tmp/;
push @negs, 0;
}
}
# if (not $co) {
# unshift(@pats,qr/(?=^....\t)/);
# unshift(@negs, 0);
# }
warn "PAT @pats\n" if $debug;
warn "NEG @negs\n" if $debug;
LINE:
while ($line or $cjkrads) {
if ($line) {
$_ = $line;
$line = "";
}
else {
$_ = <$cjkrads> or last;
my $linenum = $.;
next LINE unless vec($vec,$linenum,1);
# print "LINENUM = $linenum\n";
# print "LINE = $_\n";
foreach my $n (0..$#pats) {
my $matches = /$pats[$n]/;
$matches = !$matches if $negs[$n];
next LINE unless $matches;
}
# ignore non-defining compatibily glyphs
next LINE if /^2?f[^+]*$/ and not $keeptrying;
}
$got++;
s/ \[ [^][]* \]// unless $verbose;
if (defined $expand and $expand > 0) {
s/^(.*\t)/$1 . " " x $expand . substr($args[0],1) . ': '/e;
}
my ($strokes) = /\s(\d\d)\s/;
push(@{$OUT[$strokes]}, $_);
if (defined $expand) {
local $expand = $expand + 1;
my @words = split " ", $_;
shift @words;
shift @words;
my %myxseen;
my $tmpdeb = 1 if /\+tree /;
for (@words) {
last if /^\[/;
next if /^\+/;
s/^\d+[*.]//;
s/\.[a-zA-Z0-9]+$//;
next if s/\~\w*$// and not $eXpand;
next if m#^/#;
next if $_ eq "radical";
next if $_ eq "sym";
next if $_ eq "vbar";
next if $_ eq "hbar";
next if ord($_) > 255;
next if /=/;
next if $xseen{$_}++;
next if $myxseen{$_}++;
next unless /^[a-zA-Z][-a-zA-Z0-9]*$/;
my $word = $_;
look("+$word");
#$xseen{$word} = 0;
}
}
}
last if $got;
if (!$likely) {
warn "*** Trying for lookalikes of @args\n";
$likely++;
$keeptrying //= 1;
seek($cjkrads, 0, 0);
$. = 0;
next;
}
elsif (@args > 1 and $keeptrying) {
warn "*** Ignoring ", pop(@args), "\n" ;
$likely = 0;
close $cjkrads;
undef $cjkrads;
next;
}
else {
die "Nothing matches ", pop(@args), "\n" ;
}
}
close $cjkrads;
}
sub transmogrify {
my $arg = $_[0];
Encode::_utf8_on($arg);
#print $arg,"\n";
my $officially = "";
if ($arg =~ s/!$//) {
$officially = '\t[+]?(?:\d[*.])?'
}
my $def = "";
if ($arg =~ s/^\+//) {
$def = '[+]';
}
my $like = "";
if ($likely) {
$like = '(?:\~\w*)?';
}
if ($alias{$arg}) {
$word = $alias{$arg};
$arg = "$def$officially\\b$word$like\\b(?![-~])";
}
elsif ($arg =~ /^[-~+A-Za-z0-9_.*?]*$/) {
$arg =~ s/\?/\\S/g;
if ($likely) {
$arg =~ s/(.*)\.(.*)/$1${like}[.]\\S*$2/ or $arg .= $like;
# warn "$arg\n";
}
$arg .= "*" if $arg =~ /\..*[io]$/;
$arg =~ s/^/\\b(?![-~])/ unless $arg =~ s/^\*//;
$arg =~ s/$/\\b(?![-~])/ unless $arg =~ s/\*$//;
$arg =~ s/\*/\\*/g;
$arg =~ s/\./\\./g;
# $arg =~ s/\*/[-~+A-Za-z0-9_.]*/g;
$arg = "$def$officially$arg";
}
elsif ($arg =~ s#^/(.*)/?$#$1#) {
$arg = "$def$officially$arg";
}
elsif (ord $arg > 255) {
$arg = "$def$arg\t";
}
else {
die "Don't understand $arg\n";
}
return $arg;
}