line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::Stardict::Gen; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21877
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
5
|
1
|
|
|
1
|
|
931
|
use Data::Dumper; |
|
1
|
|
|
|
|
9974
|
|
|
1
|
|
|
|
|
76
|
|
6
|
1
|
|
|
1
|
|
711
|
use locale; |
|
1
|
|
|
|
|
190
|
|
|
1
|
|
|
|
|
5
|
|
7
|
1
|
|
|
1
|
|
855
|
use Encode; |
|
1
|
|
|
|
|
16528
|
|
|
1
|
|
|
|
|
97
|
|
8
|
1
|
|
|
1
|
|
938
|
use utf8; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
4
|
|
9
|
|
|
|
|
|
|
#use POSIX qw(locale_h); |
10
|
|
|
|
|
|
|
#setlocale(LC_ALL,"C"); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$Data::Dumper::Indent=1; |
13
|
|
|
|
|
|
|
$Data::Dumper::Terse=1; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.02_2'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $nome; my %dic; |
19
|
|
|
|
|
|
|
sub carregaDic { |
20
|
0
|
|
|
0
|
1
|
|
my %opt =(type=> "default"); |
21
|
0
|
|
|
|
|
|
local $/; |
22
|
0
|
0
|
|
|
|
|
if(ref($_[0]) eq "HASH") {%opt = (%opt , %{shift(@_)}) } ; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
|
if ($opt{type} eq "default"){ $/ = "\n"; } |
|
0
|
|
|
|
|
|
|
25
|
0
|
0
|
|
|
|
|
if ($opt{type} eq "term") { $/ = ""; } |
|
0
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my $file = shift; |
28
|
0
|
|
|
|
|
|
my %dic; |
29
|
0
|
0
|
|
|
|
|
open IN,"<$file" or die "Can load $file\n"; |
30
|
0
|
|
|
|
|
|
while () { |
31
|
0
|
|
|
|
|
|
chomp; |
32
|
0
|
0
|
0
|
|
|
|
if (m!^%enc(oding)? ([a-zA-Z0-9-]+)!) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
binmode IN, ":$2"; |
34
|
|
|
|
|
|
|
next |
35
|
0
|
|
|
|
|
|
} elsif ($opt{type} eq "term") { |
36
|
0
|
0
|
0
|
|
|
|
$opt{lang} = $1 if(!$opt{lang} && m((\w+))); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $inf={}; |
39
|
0
|
|
|
|
|
|
my @ls = split (/\n(?=\S)/,$_); |
40
|
0
|
|
|
|
|
|
for (@ls){ |
41
|
0
|
0
|
|
|
|
|
if(/(\w+)\s+(.*)/s){ push( @{$inf->{$1}}, split (/\s*[;,]\s*/,$2));} |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
|
for(@{$inf->{$opt{lang}}}){ |
|
0
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
$dic{$_} = $inf; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} elsif ($opt{type} eq "default" && /(.*?)\s*\{\s*(.*?)\s*\}/) { |
47
|
0
|
|
|
|
|
|
my @palavras = split (/\s*;\s*/,$2); |
48
|
0
|
|
|
|
|
|
$dic{$1} = [@palavras]; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
|
close IN; |
52
|
0
|
|
|
|
|
|
\%dic |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub mostraDic { |
56
|
0
|
|
|
0
|
1
|
|
$nome = shift; |
57
|
0
|
|
|
|
|
|
%dic = %{$nome}; |
|
0
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
for my $chave (sort (keys %dic)) { |
59
|
0
|
|
|
|
|
|
for (@{$dic{$chave}}) { |
|
0
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
print "$chave -> $_\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub escreveDic { |
66
|
0
|
|
|
0
|
1
|
|
my $hash= shift; |
67
|
0
|
|
|
|
|
|
my $dic = shift; |
68
|
0
|
|
|
|
|
|
my $dirpath=shift; |
69
|
0
|
0
|
0
|
|
|
|
$dirpath ||= "/usr/share/stardict/dic/" if -d "/usr/share/stardict/dic/"; |
70
|
0
|
0
|
0
|
|
|
|
$dirpath ||= "/usr/local/share/stardict/dic/" if -d "/usr/local/share/stardict/dic/"; |
71
|
0
|
0
|
|
|
|
|
unless(-d "$dirpath$dic"){ |
72
|
0
|
0
|
|
|
|
|
mkdir($dirpath.$dic,0755) or die "Cant create directory $dirpath$dic\n"; |
73
|
|
|
|
|
|
|
} |
74
|
0
|
|
|
|
|
|
chdir($dirpath.$dic); |
75
|
|
|
|
|
|
|
|
76
|
0
|
0
|
|
|
|
|
open DICT,">$dic.dict" or die ("Cant create $dic.dict\n"); |
77
|
0
|
|
|
|
|
|
binmode(DICT,":utf8"); |
78
|
0
|
0
|
|
|
|
|
open IDX,">$dic.idx" or die ("Cant create $dic.idx\n"); |
79
|
0
|
0
|
|
|
|
|
open IFO,">$dic.ifo" or die ("Cant create $dic.ifo\n"); |
80
|
0
|
|
|
|
|
|
my @keys =(); |
81
|
1
|
|
|
1
|
|
850
|
{ no locale; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
7
|
|
|
0
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
@keys = sort (keys %{$hash}); |
|
0
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
} |
84
|
0
|
|
|
|
|
|
my $byteCount = 0; |
85
|
0
|
|
|
|
|
|
for my $chave (@keys) { |
86
|
0
|
|
|
|
|
|
my $posInicial = $byteCount; |
87
|
0
|
0
|
|
|
|
|
if (utf8::is_utf8($chave)) { |
88
|
0
|
|
|
|
|
|
print IDX pack('a*x',$chave); |
89
|
|
|
|
|
|
|
} else { |
90
|
0
|
|
|
|
|
|
my $string = encode_utf8($chave); |
91
|
0
|
|
|
|
|
|
print IDX pack('a*x',$string); |
92
|
|
|
|
|
|
|
} |
93
|
0
|
|
|
|
|
|
print IDX pack('N',$byteCount); |
94
|
|
|
|
|
|
|
### print "$chave \@ $byteCount\n"; |
95
|
0
|
|
|
|
|
|
print DICT "$chave\n"; |
96
|
0
|
|
|
|
|
|
$byteCount += (_len2($chave) + 1); |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
if(ref($hash->{$chave}) eq "ARRAY"){ |
|
|
0
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
for (@{$hash->{$chave}}) { |
|
0
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
print DICT "\t$_\n"; |
101
|
0
|
|
|
|
|
|
$byteCount += (_len2($_) + 2); |
102
|
|
|
|
|
|
|
} } |
103
|
|
|
|
|
|
|
elsif(ref($hash->{$chave})) { |
104
|
0
|
|
|
|
|
|
my $a= _dumperpp(Dumper($hash->{$chave})); |
105
|
|
|
|
|
|
|
### print "DEBUG: $chave\n"; |
106
|
0
|
|
|
|
|
|
print DICT " $a\n"; |
107
|
0
|
|
|
|
|
|
$byteCount += (_len2($a) +3); } |
108
|
|
|
|
|
|
|
else { |
109
|
0
|
|
|
|
|
|
my $a=$hash->{$chave}; |
110
|
0
|
|
|
|
|
|
$a =~ s/\s*$//; |
111
|
0
|
|
|
|
|
|
$a =~ s/\n/\n\t/g; |
112
|
|
|
|
|
|
|
### print "DEBUG: $chave\n\t$a\n"; |
113
|
0
|
|
|
|
|
|
print DICT "\t$a\n"; |
114
|
0
|
|
|
|
|
|
$byteCount += (_len2($a) +2); |
115
|
|
|
|
|
|
|
} |
116
|
0
|
|
|
|
|
|
print DICT "\n\n"; |
117
|
0
|
|
|
|
|
|
$byteCount +=2; |
118
|
0
|
|
|
|
|
|
print IDX pack('N',$byteCount-$posInicial); |
119
|
|
|
|
|
|
|
### print "length: ",($byteCount-$posInicial),"\n"; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
|
my $nword = scalar (keys %{$hash}); |
|
0
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
my @t= gmtime(time); |
123
|
0
|
|
|
|
|
|
print IFO "StarDict's dict ifo file\n"; |
124
|
0
|
|
|
|
|
|
print IFO "version=2.4.2\n"; |
125
|
0
|
|
|
|
|
|
print IFO "wordcount=$nword\n"; |
126
|
0
|
|
|
|
|
|
print IFO "bookname=$dic\n"; |
127
|
|
|
|
|
|
|
## print IFO "dictfilesize=$byteCount\n"; |
128
|
0
|
|
|
|
|
|
print IFO "idxfilesize=", tell(IDX),"\n"; |
129
|
0
|
|
|
|
|
|
print IFO "date=", 1900+$t[5], "-" , $t[4]+1 , "-" , $t[3],"\n"; |
130
|
0
|
|
|
|
|
|
print IFO "sametypesequence=x\n"; |
131
|
0
|
|
|
|
|
|
close(IFO); |
132
|
0
|
|
|
|
|
|
close(DICT); |
133
|
0
|
|
|
|
|
|
close(IDX); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub _len2{ |
137
|
0
|
|
|
0
|
|
|
my $string = shift; |
138
|
0
|
0
|
|
|
|
|
$string = encode_utf8($string) unless utf8::is_utf8($string); |
139
|
1
|
|
|
1
|
|
637
|
do { use bytes; length($string) } |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
#sub len2{ do { length($_[0]) } } |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _dumperpp{ |
144
|
0
|
|
|
0
|
|
|
my $a = shift; |
145
|
0
|
|
|
|
|
|
$a =~ s/.*'_NAME_' .*\n// ; |
146
|
|
|
|
|
|
|
# $a =~ s/\$VAR\d*\s*=(\s|[\{\[])*//; |
147
|
0
|
|
|
|
|
|
$a =~ s/^(\s|[\{\[])*//; |
148
|
0
|
|
|
|
|
|
$a =~ s/[\}\]]?\s*$//; |
149
|
|
|
|
|
|
|
## $a =~ s/\n /\n\t/g; |
150
|
0
|
|
|
|
|
|
$a =~ s/\s*(\[|\]|\{|\}),?\s*\n/\n/g; |
151
|
0
|
|
|
|
|
|
$a =~ s/\\x\{(.*?)\}/chr(hex("$1"))/ge; |
|
0
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
$a =~ s/'(.*?)'/$1/g; |
153
|
0
|
|
|
|
|
|
$a =~ s/"(.*?)"/$1/g; |
154
|
0
|
|
|
|
|
|
$a; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
1; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 NAME |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Lingua::Stardict::Gen - Stardict dictionary generator |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 SYNOPSIS |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
use Lingua::Stardict::Gen; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
$dic = { word1 => ... |
169
|
|
|
|
|
|
|
word2 => ... |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Lingua::Stardict::Gen::escreveDic($dic,"dicname" [,"dirpath"]); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$dic=Lingua::Stardict::Gen::carregaDic("file"); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 DESCRIPTION |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This module generates stardict dictionaries from HASH references (function C). |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
This module also imports a simple dictionary (lines with C)(function |
181
|
|
|
|
|
|
|
C). |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 ABSTRACT |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
C generates Stardict dictionaries from perl Hash |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 FUNCTIONS |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head2 escreveDic |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Lingua::Stardict::Gen::escreveDic($dic,"dicname"); |
193
|
|
|
|
|
|
|
Lingua::Stardict::Gen::escreveDic($dic,"dicname", dir); |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Write the necessary files stardict files for dictionary in $dic HASH reference. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
C is the directory where the stardict files are written. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
If no C is provided, Lingua::Stardict::Gen will try to write it in |
200
|
|
|
|
|
|
|
C (the default path for stardict dictionaries). |
201
|
|
|
|
|
|
|
In this case the dictionary will be automatically installed. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 carregaDic |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
This function loads a simple dictionary to a HASH reference. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
$dic=Lingua::Stardict::Gen::carregaDic("file"); |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Where file has the following sintax: |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
word{def 1; def 2;... ;def n} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Example (default format): |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
%encoding utf8 |
217
|
|
|
|
|
|
|
cat{gato; tareco; animal com quatros patas e mia} |
218
|
|
|
|
|
|
|
dog{...} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Example2 (terminology format): |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
%encoding utf8 |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
EN cat ; feline |
225
|
|
|
|
|
|
|
PT gato ; tareco |
226
|
|
|
|
|
|
|
DEF animal com 4 patas e que mia |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
EN house; building; appartment |
229
|
|
|
|
|
|
|
PT house |
230
|
|
|
|
|
|
|
FR maison |
231
|
|
|
|
|
|
|
... |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
In this case we must say the type used: |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
$dic=Lingua::Stardict::Gen::carregaDic({type=>"term"},"file"); |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
or even specify the language: |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
$dic=Lingua::Stardict::Gen::carregaDic( |
240
|
|
|
|
|
|
|
{type=>"term", lang=>"PT"},"file"); |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
See also the script C in the destribution. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head2 mostraDic |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
mostraDic($hash); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Prints to stdio the information in the hash in the form |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
word -> definition |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head1 Authors |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
José João Almeida |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Alberto Simões |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Paulo Silva |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Paulo Soares |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 SEE ALSO |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
stardict |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
perl |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Copyright 2008 J.Joao, All Rights Reserved. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
274
|
|
|
|
|
|
|
under the same terms as Perl itself. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=cut |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
1; # End of Lingua::Stardict::Gen |