forked from mk12/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtw.pl
More file actions
executable file
·103 lines (86 loc) · 2.74 KB
/
tw.pl
File metadata and controls
executable file
·103 lines (86 loc) · 2.74 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
#!/usr/bin/env perl
# This scripts translates a word using WordReference or Wikipedia. Translating
# via Wikipedia is useful for technical words that don't appear in the
# dictionary but that have Wikipedia articles in the desired language.
use strict;
use warnings;
use utf8;
use charnames ':full';
binmode(STDOUT, ":utf8");
use Browser::Open qw(open_browser);
use File::Basename qw(basename);
use Getopt::Long qw(GetOptions);
use HTML::Entities qw(decode_entities);
use LWP::Simple qw(get);
sub show {
my ($s, $g) = @_;
$s =~ s/^\s+|\s+$//g;
print decode_entities($s);
if ($g) {
print ' (' . $g . ')';
}
print "\n";
}
my $P = basename($0);
my $usage_ft = '[--from LANG] [--to LANG]';
my $usage_src = '[--src wr | --src wiki]';
my $usage = "usage: $P $usage_ft $usage_src [--browser] WORD\n";
GetOptions(
'from|f=s' => \(my $from_lang = 'en'),
'to|t=s' => \(my $to_lang = 'fr'),
'reverse|r' => \(my $reverse),
'src|s=s' => \(my $source = 'wr'),
'browser|b' => \(my $browser)
) or die $usage;
if ($reverse) {
($from_lang, $to_lang) = ($to_lang, $from_lang);
}
if (scalar(@ARGV) <= 0) {
die "$P: expecting word to translate\n";
}
if ($source eq 'wr') {
my $name = join('%20', @ARGV);
my $url = "http://wordreference.com/$from_lang$to_lang/$name";
if ($browser) {
open_browser($url);
exit 0;
}
my $html = get($url);
my $no_entry = 'No translation found for';
if (not defined $html or index($html, $no_entry) != -1) {
die "$P: $name: no such entry\n";
}
my $no_exact1 = "WordReference can't translate this exact phrase";
my $no_exact2 = "WordReference ne peut pas traduire cette expression";
if (index($html, $no_exact1) != -1 || index($html, $no_exact2) != -1) {
die "$P: $name: no exact entry\n";
}
my $pattern = qr/< *td +class *= *["']ToWrd['"].*?>(.+?)<(?:.*?tooltip +POS2['"].*?>(.+?)<)?/;
if ($html =~ $pattern && substr($html, $+[0]) =~ $pattern) {
show $1, $2;
} else {
die "$P: $name: failed to parse page\n";
}
} elsif ($source eq 'wiki') {
my $name = join('_', @ARGV);
my $url = "http://$from_lang.wikipedia.org/wiki/$name";
if ($browser) {
open_browser($url);
exit 0;
}
my $html = get($url);
if (not defined $html) {
die "$P: $name: no such article\n";
}
my $disambig = '/wiki/Help:Disambiguation';
if (index($html, $disambig) != -1) {
die "$P: $name: ambiguous\n";
}
if ($html =~ /<a href="\/\/$to_lang\.wikipedia\.org\/wiki\/.*?" title="(.+?) –.+?"/) {
show $1;
} else {
die "$P: $name: no article for language '$to_lang'\n";
}
} else {
die "$P: $source: invalid source (try 'wr' or 'wiki')\n";
}