Browse Source

Added se, a command line dictionary util using www.slovnik.cz

Although not particularly related to development, this is very
useful for me.

Also it has it place in programming: say when I need to pick a *really
good* name for a variable but can't recall the word in EN :)
Alois Mahdal 11 years ago
parent
commit
19410fd34e
1 changed files with 151 additions and 0 deletions
  1. 151
    0
      bin/se

+ 151
- 0
bin/se View File

@@ -0,0 +1,151 @@
1
+#!/usr/bin/perl -w
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use LWP::Simple;
7
+use Getopt::Long;
8
+use Pod::Usage;
9
+use File::Basename;
10
+
11
+binmode STDOUT, ":encoding(UTF-8)";
12
+
13
+sub usage;
14
+
15
+my $direction = 'encz.en';
16
+my $lines = 25;
17
+my $man = 0;
18
+my $help = 0;
19
+my $host = 'www.slovnik.cz';
20
+
21
+GetOptions(
22
+    'lines|l=i'     => \$lines,
23
+    'direction|d=s' => \$direction,
24
+    'help|?|h'    => \$help,
25
+    'man'       => \$man
26
+) or pod2usage(2);
27
+pod2usage(1) if $help;
28
+pod2usage(-exitstatus => 0, -verbose => 2) if $man;
29
+
30
+my $word = shift or usage;
31
+
32
+my $query  = sprintf(
33
+    '/bin/mld.fpl?vcb=%s&dictdir=%s&lines=%s',
34
+    $word,
35
+    $direction,
36
+    $lines
37
+);
38
+
39
+my $url = "http://$host$query";
40
+my @lines = split m|\n|, get($url);
41
+@lines = grep m|class="pair"|, @lines;
42
+
43
+foreach (@lines) {
44
+    s/<.*?>//g;
45
+    print;
46
+    print "\n";
47
+}
48
+
49
+sub usage {
50
+    printf STDERR "usage: %s [options] word\n", basename($0); exit 1;
51
+}
52
+
53
+__END__
54
+
55
+=head1 NAME
56
+
57
+se - Translate en-* to/from cs-CZ and many other languages using
58
+www.slovnik.cz service
59
+
60
+=head1 SYNOPSIS
61
+
62
+B<se> [I<OPTIONS>] I<WORD>
63
+
64
+=head1 DESCRIPTION
65
+
66
+B<se> will take English word, compose a query to dictionary service at
67
+http://www.slovnik.cz/, fetch the English - Czech pairs and display them
68
+to stdout.
69
+
70
+Although English to Czech is default, using B<--direction> parameter
71
+you can translate Czech to English or even between Czech and many other
72
+languages.
73
+
74
+For I<WORD> with diacritics, you can provide it
75
+
76
+=over 8
77
+
78
+=item * directly if your terminal supports it (tested with few Czech words
79
+on Debian 6.0 with urxvt)
80
+
81
+=item * directly with diacritics stripped, or
82
+
83
+=item * using special notation described here:
84
+http://www.slovnik.cz/alternative_chars.html
85
+
86
+Few simple examples:
87
+
88
+=over 8
89
+
90
+=item * C<c<ert/ik> - c with caron, i with acute
91
+
92
+=item * C<pu@da> -- u with ring above
93
+
94
+=item * C<Bu:ro> -- u with diaeresis
95
+
96
+=item * C<c\eramique> -- e with grave
97
+
98
+=item * C<valdepen~as> -- n with tilde
99
+
100
+=back
101
+
102
+=back
103
+
104
+=head1 OPTIONS
105
+
106
+=over 8
107
+
108
+=item B<-d> I<DIR>, B<--direction=>I<DIR>
109
+
110
+Direction of translation.  Aside from the default encz.en, it can be
111
+one of following:
112
+
113
+    encz.en, encz.cz, gecz.ge, gecz.cz, frcz.fr, frcz.cz,
114
+    itcz.it, itcz.cz, spcz.sp, spcz.cz, rucz.ru, rucz.cz,
115
+    lacz.la, lacz.cz, eocz.eo, eocz.cz, eosk.eo, eosk.sk,
116
+    plcz.pl, plcz
117
+
118
+=item B<-l> I<LINES>, B<--lines=>I<LINES>
119
+
120
+Number of lines fetched fronm the service and displayed to stdout.
121
+Default is 25, but since www.slovnik.cz provides simplistic interface
122
+with only one term per line, so for words with lot of synonyms, you
123
+might want to specify more.
124
+
125
+=item B<--help>
126
+
127
+Prints a brief help message and exits.
128
+
129
+=item B<--man>
130
+
131
+Prints the manual page and exits.
132
+
133
+=back
134
+
135
+=head1 THANKS
136
+
137
+Big thanks to Martin Vi/t, LangSoft and everybody who contributed to
138
+existence of this awesome service.
139
+
140
+    http://www.slovnik.cz/about.html
141
+
142
+I have been using www.slovnik.cz for B<many years> now, mostly as a custom
143
+search in Opera, so I grew so accustomed to its reliability and simple
144
+interface that I can't even imagine using anything else.  (Or remember
145
+I ever had).  I could never repay.
146
+
147
+=head1 AUTHOR
148
+
149
+...of this scriptie: Alois Mahdal at zxcvb tec<ka cz
150
+
151
+=cut