blib/lib/Lingua/Stardict/Gen.pm

filter: Statementstmt 21.9% 30 / 137 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.5% 8 / 13 Podpod 100% 3 / 3 totaltotal 19.4% 41 / 211 CC 32 SCAR 50.5
CC
32
Cov
16%
carregaDic
240.0
escreveDic
216.2
mostraDic
12.0
CRAP 156.7
↑ Summary
1 package Lingua::Stardict::Gen;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use POSIX qw(locale_h);
10 #setlocale(LC_ALL,"C");
11  
12 2 $Data::Dumper::Indent=1;
13 2 $Data::Dumper::Terse=1;
14  
15 2 our $VERSION = '0.02_2';
16  
17  
18 2 my $nome; my %dic;
19 0 ▶sub carregaDic {
Subroutine carregaDic: not called CC=15 SCAR=54.8
Pod documented
20 0 my %opt =(type=> "default");
21 0 local $/;
22 0 ▶ if(ref($_[0]) eq "HASH") {%opt = (%opt , %{shift(@_)}) } ;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 0 0 0
23  
24 0 ▶ if ($opt{type} eq "default"){ $/ = "\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} eq "default") 0 0 0
25 0 ▶ if ($opt{type} eq "term") { $/ = ""; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} eq "term") 0 0 0
26  
27 0 my $file = shift;
28 0 my %dic;
29 0 ▶ open IN,"<$file" or die "Can load $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<$file" or die "Can load $file\n" 0 0 0
30 0 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
31 0 chomp;
32 0 ▶ if (m!^%enc(oding)? ([a-zA-Z0-9-]+)!) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^%enc(oding)? ([a-zA-Z0-9-]+)/l) { } 0 0 0
elsif ($opt{'type'} eq 'term') { } 0 0 0
elsif ($opt{'type'} eq 'default' and /(.*?)\s*\{\s*(.*?)\s*\}/l) { } 0 0 0
33 0 binmode IN, ":$2";
34 next
35 0 } elsif ($opt{type} eq "term") {
36 0 ▶ $opt{lang} = $1 if(!$opt{lang} && m((\w+)));
Branch: 1 branch
BranchTrueFalseTotal
if not $opt{"lang"} and /(\w+)/l 0 0 0
Condition0%0/3
not $opt{"lang"} and /(\w+)/l
!ll&&!rl&&r
000
MC/DC0%0/2
not $opt{"lang"} and /(\w+)/l
not $opt{"lang"} /(\w+)/l
Truth table0%0/3
not(A) and B
A= not $opt{"lang"}B= /(\w+)/l
ABresult
0X0
100
111
37  
38 0 my $inf={};
39 0 my @ls = split (/\n(?=\S)/,$_);
40 0 for (@ls){
41 0 ▶ if(/(\w+)\s+(.*)/s){ push( @{$inf->{$1}}, split (/\s*[;,]\s*/,$2));}
Branch: 1 branch
BranchTrueFalseTotal
if (/(\w+)\s+(.*)/ls) 0 0 0
42 }
43 0 for(@{$inf->{$opt{lang}}}){
44 0 $dic{$_} = $inf;
45 }
46 ▶ } elsif ($opt{type} eq "default" && /(.*?)\s*\{\s*(.*?)\s*\}/) {
Condition0%0/3
$opt{"type"} eq "default" and /(.*?)\s*\{\s*(.*?)\s*\}/l
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"type"} eq "default" and /(.*?)\s*\{\s*(.*?)\s*\}/l
$opt{"type"} eq "default" /(.*?)\s*\{\s*(.*?)\s*\}/l
Truth table0%0/3
A and B
A= $opt{"type"} eq "default"B= /(.*?)\s*\{\s*(.*?)\s*\}/l
ABresult
0X0
100
111
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 0 ▶sub mostraDic {
Subroutine mostraDic: not called CC=3 SCAR=24.8
Pod documented
56 0 $nome = shift;
57 0 %dic = %{$nome};
58 0 for my $chave (sort (keys %dic)) {
59 0 for (@{$dic{$chave}}) {
60 0 print "$chave -> $_\n";
61 }
62 }
63 }
64  
65 0 ▶sub escreveDic {
Subroutine escreveDic: not called CC=15 SCAR=53.8
Pod documented
66 0 my $hash= shift;
67 0 my $dic = shift;
68 0 my $dirpath=shift;
69 0 ▶ $dirpath ||= "/usr/share/stardict/dic/" if -d "/usr/share/stardict/dic/";
Branch: 1 branch
BranchTrueFalseTotal
if -d "/usr/share/stardict/dic/" 0 0 0
Condition0%0/2
$dirpath ||= "/usr/share/stardict/dic/"
l!l
00
MC/DC0%0/1
$dirpath ||= "/usr/share/stardict/dic/"
$dirpath
Truth table0%0/2
A
A= $dirpath
Aresult
11
00
70 0 ▶ $dirpath ||= "/usr/local/share/stardict/dic/" if -d "/usr/local/share/stardict/dic/";
Branch: 1 branch
BranchTrueFalseTotal
if -d "/usr/local/share/stardict/dic/" 0 0 0
Condition0%0/2
$dirpath ||= "/usr/local/share/stardict/dic/"
l!l
00
MC/DC0%0/1
$dirpath ||= "/usr/local/share/stardict/dic/"
$dirpath
Truth table0%0/2
A
A= $dirpath
Aresult
11
00
71 0 ▶ unless(-d "$dirpath$dic"){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "$dirpath$dic") 0 0 0
72 0 ▶ mkdir($dirpath.$dic,0755) or die "Cant create directory $dirpath$dic\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dirpath . $dic, 493 or die "Cant create directory $dirpath$dic\n" 0 0 0
73 }
74 0 chdir($dirpath.$dic);
75  
76 0 ▶ open DICT,">$dic.dict" or die ("Cant create $dic.dict\n");
Branch: 1 branch
BranchTrueFalseTotal
open DICT, ">$dic.dict" or die "Cant create $dic.dict\n" 0 0 0
77 0 binmode(DICT,":utf8");
78 0 ▶ open IDX,">$dic.idx" or die ("Cant create $dic.idx\n");
Branch: 1 branch
BranchTrueFalseTotal
open IDX, ">$dic.idx" or die "Cant create $dic.idx\n" 0 0 0
79 0 ▶ open IFO,">$dic.ifo" or die ("Cant create $dic.ifo\n");
Branch: 1 branch
BranchTrueFalseTotal
open IFO, ">$dic.ifo" or die "Cant create $dic.ifo\n" 0 0 0
80 0 my @keys =();
81 0 ▶ { no locale;
Subroutine BEGIN: called CC=1 SCAR=0.2
82 0 @keys = sort (keys %{$hash});
83 }
84 0 my $byteCount = 0;
85 0 for my $chave (@keys) {
86 0 my $posInicial = $byteCount;
87 0 ▶ if (utf8::is_utf8($chave)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($chave)) { } 0 0 0
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 ▶ if(ref($hash->{$chave}) eq "ARRAY"){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{$chave} eq 'ARRAY') { } 0 0 0
elsif (ref $hash->{$chave}) { } 0 0 0
99 0 for (@{$hash->{$chave}}) {
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});
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 0 ▶sub _len2{
Subroutine _len2: not called CC=2 SCAR=11.6
137 0 my $string = shift;
138 0 ▶ $string = encode_utf8($string) unless utf8::is_utf8($string);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($string) 0 0 0
139 0 ▶ do { use bytes; length($string) }
Subroutine BEGIN: called CC=1 SCAR=0.6
140 }
141 #sub len2{ do { length($_[0]) } }
142  
143 0 ▶sub _dumperpp{
Subroutine _dumperpp: not called CC=1 SCAR=6.9
144 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;
152 0 $a =~ s/'(.*?)'/$1/g;
153 0 $a =~ s/"(.*?)"/$1/g;
154 0 $a;
155 }
156  
157 2 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<escreveDic>).
179  
180 This module also imports a simple dictionary (lines with C<word {def1; def2...}>)(function
181 C<carragaDic>).
182  
183  
184 =head1 ABSTRACT
185  
186 C<Lingua::Stardict::Gen> 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<dir> is the directory where the stardict files are written.
198  
199 If no C<dir> is provided, Lingua::Stardict::Gen will try to write it in
200 C</usr/share/stardict/dic/...> (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<term2stardic> 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 2 1; # End of Lingua::Stardict::Gen
↑ Summary