forked from chenkaie/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathydict_
201 lines (187 loc) · 5.3 KB
/
ydict_
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
#!/usr/bin/env perl
# Shih-Yuan Lee <[email protected]>
# http://fd.idv.tw
use Term::ANSIColor qw(:constants);
use LWP::Simple;
use strict;
use warnings;
use Encode;
my $debug = 0;
my $trigger = 0;
if ($#ARGV == -1) {
print "<Yahoo!奇摩字典> ";
while (<>) {
chomp;
if ("$_" eq '') {
print "<Yahoo!奇摩字典> ";
next;
}
ydict($_);
print "<Yahoo!奇摩字典> ";
}
} else {
while ($#ARGV != -1) {
if ($trigger) {
print "\n";
} else {
$trigger = 1;
}
ydict(shift);
}
}
sub ydict
{
my $p = shift;
die if !$p;
my $html = get("http://tw.dictionary.yahoo.com/search?ei=UTF-8&p=$p");
$html =~ s/\r//g;
$html =~ s/\n//g;
if ($html =~ m{<em class="warning">}) {
if ($html =~ m{<em class="warning">.*?>(\S+)<.*?</em>}) {
my $q = $1;
print BOLD . YELLOW . "拼字檢查: $p -> $q" . RESET . "\n\n";
return ydict($q);
} else {
print BOLD . YELLOW . "查無此字: $p\n" . RESET;
return;
}
}
print BOLD . YELLOW . "$p 的查詢結果:" . RESET . "\n\n";
while ($html =~ m{<div class="break"></div>}) {
my $block = $`;
$html = $';
parser($block);
}
parser($html);
}
sub parser
{
my $html = shift;
if ($html =~ m{<h2>(.*?)</h2>}) {
my $line = $1;
$line =~ s,<sup>,[,g;
$line =~ s,</sup>,],g;
$line =~ s/<[^>]+>//g;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
print "<h2>" if $debug;
print BOLD."$line".RESET."\n";
}
parsermore($html);
}
sub parsermore
{
my $i = 0;
my $flag = 0;
my $html = shift;
# http://tw.dictionary.yahoo.com/search?ei=UTF-8&p=$p
while ($html =~ m{<div class="?(p\w+|chinese-explain pexplain|chinese)"?>(.*?)</div>}i)
{
my $type = $1;
my $line = $2;
my $color = RESET;
my $bold = BOLD;
my $reset = RESET;
$html = $';
$line =~ s/^\s+//;
$line =~ s/\s+$//;
print "type=$type\nline=$line\n" if $debug;
if ($type eq 'pcixin')
{
$flag = 0;
$i = 0;
$color = BOLD . RED;
next if not ($line);
print "<pcixin>\n" if $debug;
}
elsif ($type eq 'pchi')
{
$color = GREEN;
$reset = RESET.$color;
$line = " $line";
print "<pchi>\n" if $debug;
}
elsif ($type eq 'peng')
{
$color = CYAN;
$reset = RESET.$color;
$line = " $line";
print "<peng>\n" if $debug;
}
elsif ($type eq 'chinese')
{
my $num = $line =~ s/<br>/\n /g;
$line = " $line";
print "<chinese>\n" if $debug;
}
elsif ($type eq 'pexplain')
{
if ($flag == 0) {
$i++;
if ($line =~ m{<li>}) {
my $reval;
do {
$reval = $line =~ s/<li>/ $i. /;
$i++;
} while ($reval);
my $num = $line =~ s/<br>/\n/g;
$i = 0;
} else {
$line = " $i. $line";
print "<pexplain 1>\n" if $debug;
}
} else {
$line = " $line" if ($line);
print "<pexplain 2>\n" if $debug;
}
}
elsif ($type eq 'chinese-explain pexplain')
{
if ($flag == 0) {
$i++;
if ($line =~ m{<li>}) {
my $reval;
do {
$reval = $line =~ s/<li>/\n $i. /;
$i++;
} while ($reval);
my $num = $line =~ s/<br>/\n/g;
$i = 0;
} else {
$line = " $i. $line";
print "<pexplain 1>\n" if $debug;
}
} else {
$line = " $line" if ($line);
print "<pexplain 2>\n" if $debug;
}
}
elsif ($type eq 'ptitle')
{
$flag = 1;
$color = BOLD . BLUE;
# next if ($line =~ m{KK} && $line =~ m{DJ});
my $dir = 'http://tw.yimg.com/i/tw/dictionary/pic';
my $map = Encode::decode('utf8', " æa: ɑ ʊɔɝəɚʌeɛŋ ʃʒ θð iɪuopbtdkfvszmnhlrwj`,g ḷṃṇ");
$line =~ s/\s+(DJ)/\n$1/g;
while ($line =~ m{<img[^>]*['"]$dir/0+(\d+)\.gif['"][^>]*>}ig) {
my $char = Encode::encode('utf8', substr($map, $1, 1));
my $fname = $1 < 10 ? "00$1" : $1 < 100 ? "0$1" : $1;
$line =~ s!<img[^>]*['"]$dir/$fname\.gif["'][^>]*>!$char!ig;
}
print "<ptitle>\n" if $debug;
}
else
{
$color = BOLD . BLUE;
print "<others>\n" if $debug;
next;
}
$line =~ s,<b>,$bold,g;
$line =~ s,</b>,$reset,g;
$line =~ s,<sup>,[,g;
$line =~ s,</sup>,],g;
$line =~ s/<[^>]+>//g;
print $color."$line".RESET."\n";
}
}