line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Biblio::Thesaurus::SQLite; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
51163
|
use 5.008006; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
74
|
|
4
|
2
|
|
|
2
|
|
20
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
62
|
|
5
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
74
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
2
|
|
|
2
|
|
1781
|
use DBIx::Simple; |
|
2
|
|
|
|
|
298380
|
|
|
2
|
|
|
|
|
74
|
|
9
|
2
|
|
|
2
|
|
2799
|
use Data::Dumper; |
|
2
|
|
|
|
|
19092
|
|
|
2
|
|
|
|
|
193
|
|
10
|
2
|
|
|
2
|
|
2376
|
use Biblio::Thesaurus; |
|
2
|
|
|
|
|
92830
|
|
|
2
|
|
|
|
|
446
|
|
11
|
2
|
|
|
2
|
|
1995
|
use locale; |
|
2
|
|
|
|
|
449
|
|
|
2
|
|
|
|
|
13
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ |
15
|
|
|
|
|
|
|
qw(ISOthe2TheSql |
16
|
|
|
|
|
|
|
TheSql2ISOthe |
17
|
|
|
|
|
|
|
getTermAsXHTML |
18
|
|
|
|
|
|
|
getTermAsISOthe |
19
|
|
|
|
|
|
|
getTermAsPerl |
20
|
|
|
|
|
|
|
setTerm |
21
|
|
|
|
|
|
|
deleteTerm |
22
|
|
|
|
|
|
|
changeTerm |
23
|
|
|
|
|
|
|
) ] ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
26
|
|
|
|
|
|
|
our @EXPORT = qw(); |
27
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our ($rel,@terms,$term); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
## |
32
|
|
|
|
|
|
|
# This method converts a ISO Thesaurus file in a SQLite database |
33
|
|
|
|
|
|
|
# @param ficheiro de texto iso thesaurus |
34
|
|
|
|
|
|
|
# @param ficheiro com base de dados sqlite |
35
|
|
|
|
|
|
|
sub ISOthe2TheSql { |
36
|
1
|
50
|
|
1
|
1
|
1771
|
my $file = shift or die; |
37
|
1
|
50
|
|
|
|
6
|
my $dbfile = shift or die; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# parse the thesaurus file |
40
|
1
|
|
|
|
|
9
|
my $the = thesaurusLoad($file); |
41
|
|
|
|
|
|
|
# connect to the database |
42
|
1
|
50
|
|
|
|
1054
|
my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile) |
43
|
|
|
|
|
|
|
or die DBIx::Simple->error; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# clear the database! TODO: check if the database exists and try |
46
|
|
|
|
|
|
|
# to append the new data |
47
|
1
|
|
|
|
|
753
|
$db->query('DROP TABLE rel'); |
48
|
0
|
|
|
|
|
0
|
$db->query('CREATE TABLE rel (term, rel, def)'); |
49
|
0
|
|
|
|
|
0
|
$db->query('DROP TABLE meta'); |
50
|
0
|
|
|
|
|
0
|
$db->query('CREATE TABLE meta (term, val)'); |
51
|
0
|
|
|
|
|
0
|
$db->query('DROP TABLE lang'); |
52
|
0
|
|
|
|
|
0
|
$db->query('CREATE TABLE lang (ori, lang, dest)'); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# parse metadata (we need this cause I dunno if Biblio::Thesaurus |
55
|
|
|
|
|
|
|
# handles this the right way O:-)) |
56
|
0
|
0
|
|
|
|
0
|
open(F, "<$file") or die; |
57
|
0
|
|
|
|
|
0
|
$db->begin_work; |
58
|
0
|
|
|
|
|
0
|
my $lang_dest = ''; |
59
|
0
|
|
|
|
|
0
|
while() { |
60
|
0
|
|
|
|
|
0
|
chomp; |
61
|
|
|
|
|
|
|
# this is metadata (starting with %) |
62
|
0
|
0
|
|
|
|
0
|
if($_ =~ /^\%([^\s]+)\s+(.*)/) { |
|
|
0
|
|
|
|
|
|
63
|
0
|
|
|
|
|
0
|
my @vals = split(/\s+/, $2); |
64
|
0
|
|
|
|
|
0
|
for (@vals) { |
65
|
0
|
|
|
|
|
0
|
$db->query( |
66
|
|
|
|
|
|
|
'INSERT INTO meta VALUES (?, ?)', |
67
|
|
|
|
|
|
|
$1, $_ |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
# this is language data |
72
|
|
|
|
|
|
|
elsif($_ =~ /(.*)==(.*)/) { |
73
|
0
|
0
|
|
|
|
0
|
if($1 eq $the->baselang) { |
|
|
0
|
|
|
|
|
|
74
|
0
|
|
|
|
|
0
|
$lang_dest = $2; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
elsif($lang_dest ne '') { |
77
|
0
|
|
|
|
|
0
|
$db->query( |
78
|
|
|
|
|
|
|
'INSERT INTO lang VALUES (?, ?, ?)', |
79
|
|
|
|
|
|
|
$1, $lang_dest, $2 |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
0
|
|
|
|
|
0
|
$db->commit; |
85
|
0
|
|
|
|
|
0
|
close(F); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# parse all terms :D (the hard work is handed by Biblio::Thesaurus |
88
|
0
|
|
|
|
|
0
|
$db->begin_work; |
89
|
|
|
|
|
|
|
print $the->downtr ( |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
-default => sub { |
92
|
|
|
|
|
|
|
# ignore language data... |
93
|
0
|
0
|
|
0
|
|
0
|
return '' if ($term.$rel) =~ /.*==.*/; |
94
|
0
|
|
|
|
|
0
|
for (@terms) { |
95
|
0
|
|
|
|
|
0
|
$db->query( |
96
|
|
|
|
|
|
|
'INSERT INTO rel VALUES (?, ?, ?)', |
97
|
|
|
|
|
|
|
$term, $rel, $_ |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
}, |
101
|
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
0
|
); |
103
|
0
|
|
|
|
|
0
|
$db->commit; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
## |
108
|
|
|
|
|
|
|
# This method convert a SQLite database to a ISO thesaurus text file |
109
|
|
|
|
|
|
|
# @param The SQLite database |
110
|
|
|
|
|
|
|
# @param The output ISO Thesaurus file |
111
|
|
|
|
|
|
|
# @note This method is VERY VERY slow! I tried to know why, run a profiller |
112
|
|
|
|
|
|
|
# and saw that most of the time we are consuming CPU in the ->hashes |
113
|
|
|
|
|
|
|
# function of the DBIx::Simple module.... TODO: get this think faster :D |
114
|
|
|
|
|
|
|
sub TheSql2ISOthe { |
115
|
2
|
50
|
|
2
|
1
|
25
|
my $dbfile = shift or die; |
116
|
2
|
50
|
|
|
|
9
|
my $file = shift or die; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# ok so this is easy :D |
119
|
|
|
|
|
|
|
# connect to the database |
120
|
2
|
50
|
|
|
|
29
|
my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile) |
121
|
|
|
|
|
|
|
or die DBIx::Simple->error; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# process meta-data |
124
|
2
|
|
|
|
|
343570
|
open(F, ">$file"); |
125
|
2
|
|
|
|
|
18
|
for my $row ($db->query('SELECT DISTINCT term FROM meta')->flat) { |
126
|
0
|
|
|
|
|
0
|
print F '%' . $row . ' ' . |
127
|
|
|
|
|
|
|
join(' ', |
128
|
|
|
|
|
|
|
$db->query( |
129
|
|
|
|
|
|
|
'SELECT val FROM META WHERE term = ?', |
130
|
|
|
|
|
|
|
$row |
131
|
|
|
|
|
|
|
)->flat |
132
|
|
|
|
|
|
|
), "\n"; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# process translations |
136
|
2
|
|
|
|
|
1156
|
$db->query('SELECT val FROM meta WHERE term = ?', 'baselang')->into(my $baselang); |
137
|
2
|
50
|
|
|
|
720
|
if(defined($baselang)) { |
138
|
0
|
|
|
|
|
0
|
$db->query('SELECT lang FROM lang LIMIT 1')->into(my $lang); |
139
|
0
|
|
|
|
|
0
|
print F $baselang, '==', $lang, "\n\n"; |
140
|
0
|
|
|
|
|
0
|
for my $row ($db->query('SELECT * FROM lang')->hashes) { |
141
|
0
|
|
|
|
|
0
|
print F $row->{ori}, '==', $row->{dest}, "\n"; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# process the main data |
146
|
2
|
|
|
|
|
155
|
for my $row ($db->query('SELECT DISTINCT term FROM rel')->flat) { |
147
|
4
|
|
|
|
|
659
|
print F "\n\n$row\n"; |
148
|
4
|
|
|
|
|
16
|
for my $row2 ($db->query('SELECT rel, def FROM rel WHERE term = ?', $row)->hashes) { |
149
|
4
|
|
|
|
|
973
|
print F $row2->{rel}, ' ', $row2->{def}, "\n"; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
2
|
|
|
|
|
194
|
close(F); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
## |
156
|
|
|
|
|
|
|
# this method tries to output the result of a term as a xhtml table |
157
|
|
|
|
|
|
|
# maybe to use with a cgi module |
158
|
|
|
|
|
|
|
# @param the term to find data |
159
|
|
|
|
|
|
|
# @param the sqlite database file |
160
|
|
|
|
|
|
|
sub getTermAsXHTML { |
161
|
0
|
0
|
|
0
|
0
|
|
my $termo = shift or die; |
162
|
0
|
0
|
|
|
|
|
my $dbfile = shift or die; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# connect to the database |
165
|
0
|
0
|
|
|
|
|
my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile) |
166
|
|
|
|
|
|
|
or die DBIx::Simple->error; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# try to see if we got any results avaiable |
169
|
0
|
|
|
|
|
|
my $count; |
170
|
0
|
|
|
|
|
|
$db->query( |
171
|
|
|
|
|
|
|
'SELECT COUNT(term) FROM rel WHERE term = ?' |
172
|
|
|
|
|
|
|
, $termo |
173
|
|
|
|
|
|
|
)->into($count); |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
return ( 'Termo ' . $termo . |
176
|
|
|
|
|
|
|
' nao encontrado' ) |
177
|
|
|
|
|
|
|
if $count == 0; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# now starting the output of the table |
180
|
0
|
|
|
|
|
|
my $res = '' . $termo . ' | Relacao | ' .
181
|
|
|
|
|
|
|
' | Definição | ' . "\n";
182
|
|
|
|
|
|
|
# this is ugly.... |
183
|
0
|
|
|
|
|
|
for my $row ($db->query('SELECT * FROM rel WHERE term = ?', |
184
|
|
|
|
|
|
|
$termo)->hashes) { |
185
|
0
|
|
|
|
|
|
$res .= ' | ' . $row->{rel} . |
186
|
|
|
|
|
|
|
' | ' . $row->{def} . |
187
|
|
|
|
|
|
|
' | ' . "\n";
188
|
|
|
|
|
|
|
} |
189
|
0
|
|
|
|
|
|
$res .= ' | '; |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
return $res; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
## |
195
|
|
|
|
|
|
|
# Does the same thing as the previous method, but outputs the data in an |
196
|
|
|
|
|
|
|
# ISO Thesaurus format |
197
|
|
|
|
|
|
|
# @param ..... |
198
|
|
|
|
|
|
|
# @param guess w00t ? |
199
|
|
|
|
|
|
|
sub getTermAsISOthe { |
200
|
0
|
0
|
|
0
|
0
|
|
my $termo = shift or die; |
201
|
0
|
0
|
|
|
|
|
my $dbfile = shift or die; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
# connect to the database |
204
|
0
|
0
|
|
|
|
|
my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile) |
205
|
|
|
|
|
|
|
or die DBIx::Simple->error; |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
my $count; |
208
|
0
|
|
|
|
|
|
$db->query( |
209
|
|
|
|
|
|
|
'SELECT COUNT(term) FROM rel WHERE term = ?' |
210
|
|
|
|
|
|
|
, $termo |
211
|
|
|
|
|
|
|
)->into($count); |
212
|
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
return '' if $count == 0; |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
my $res = $termo . "\n"; |
216
|
0
|
|
|
|
|
|
for my $row ($db->query('SELECT * FROM rel WHERE term = ?', |
217
|
|
|
|
|
|
|
$termo)->hashes) { |
218
|
0
|
|
|
|
|
|
$res .= '- ' . $row->{rel} . ' -> ' . |
219
|
|
|
|
|
|
|
$row->{def} . "\n"; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
chomp($res); |
223
|
0
|
|
|
|
|
|
return $res; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
## |
227
|
|
|
|
|
|
|
# bla bla bla (i'm tired of this...) |
228
|
|
|
|
|
|
|
# .... |
229
|
|
|
|
|
|
|
# .... |
230
|
|
|
|
|
|
|
sub getTermAsPerl { |
231
|
0
|
0
|
|
0
|
0
|
|
my $termo = shift or die; |
232
|
0
|
0
|
|
|
|
|
my $dbfile = shift or die; |
233
|
|
|
|
|
|
|
|
234
|
0
|
|
|
|
|
|
my %res; # our data! |
235
|
0
|
|
|
|
|
|
$res{$termo} = {}; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
# connect to the database |
238
|
0
|
0
|
|
|
|
|
my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile) |
239
|
|
|
|
|
|
|
or die DBIx::Simple->error; |
240
|
|
|
|
|
|
|
|
241
|
0
|
|
|
|
|
|
my $count; |
242
|
0
|
|
|
|
|
|
$db->query( |
243
|
|
|
|
|
|
|
'SELECT COUNT(term) FROM rel WHERE term = ?' |
244
|
|
|
|
|
|
|
, $termo |
245
|
|
|
|
|
|
|
)->into($count); |
246
|
|
|
|
|
|
|
|
247
|
0
|
0
|
|
|
|
|
return Dumper \%res if $count == 0; |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
for my $row ($db->query('SELECT * FROM rel WHERE term = ?', |
250
|
|
|
|
|
|
|
$termo)->hashes) { |
251
|
0
|
|
|
|
|
|
my $mainhash = $res{$termo}; |
252
|
0
|
|
|
|
|
|
my $termoarray = $mainhash->{$row->{rel}}; |
253
|
0
|
0
|
|
|
|
|
$termoarray = [] unless defined $termoarray; |
254
|
0
|
|
|
|
|
|
push @$termoarray, $row->{def}; |
255
|
0
|
|
|
|
|
|
$mainhash->{$row->{rel}} = $termoarray; |
256
|
0
|
|
|
|
|
|
$res{$termo} = $mainhash; |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
0
|
|
|
|
|
|
return Dumper \%res; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
## |
264
|
|
|
|
|
|
|
# Well, a new method! Add the new term to the sqlite database |
265
|
|
|
|
|
|
|
# TODO: do some cheking before blinding insert the data? |
266
|
|
|
|
|
|
|
# @param the term to insert |
267
|
|
|
|
|
|
|
# @param the relation |
268
|
|
|
|
|
|
|
# @param the definition |
269
|
|
|
|
|
|
|
# @param the database file |
270
|
|
|
|
|
|
|
sub setTerm { |
271
|
0
|
0
|
|
0
|
1
|
|
my $termo = shift or die; |
272
|
0
|
0
|
|
|
|
|
my $rel = shift or die; |
273
|
0
|
0
|
|
|
|
|
my $def = shift or die; |
274
|
0
|
0
|
|
|
|
|
my $dbfile = shift or die; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
# connect to the database |
277
|
0
|
0
|
|
|
|
|
my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile) |
278
|
|
|
|
|
|
|
or die DBIx::Simple->error; |
279
|
|
|
|
|
|
|
|
280
|
0
|
|
|
|
|
|
$db->query('INSERT INTO rel VALUES (?, ?, ?)', $termo, $rel, $def); |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
## |
284
|
|
|
|
|
|
|
# Delete the term |
285
|
|
|
|
|
|
|
sub deleteTerm { |
286
|
0
|
0
|
|
0
|
1
|
|
my $termo = shift or die; |
287
|
0
|
0
|
|
|
|
|
my $rel = shift or die; |
288
|
0
|
0
|
|
|
|
|
my $def = shift or die; |
289
|
0
|
0
|
|
|
|
|
my $dbfile = shift or die; |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
# connect to the database |
292
|
0
|
0
|
|
|
|
|
my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile) |
293
|
|
|
|
|
|
|
or die DBIx::Simple->error; |
294
|
|
|
|
|
|
|
|
295
|
0
|
|
|
|
|
|
$db->query('DELETE FROM rel WHERE term = ? AND rel = ? AND def = ?', |
296
|
|
|
|
|
|
|
$termo, $rel, $def); |
297
|
0
|
|
|
|
|
|
$db->query('DELETE FROM rel WHERE term = ? AND rel = ? AND def = ?', |
298
|
|
|
|
|
|
|
$def, $rel, $termo); |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
## |
302
|
|
|
|
|
|
|
# Change the term in the database |
303
|
|
|
|
|
|
|
# @param term to change |
304
|
|
|
|
|
|
|
# @param old relation |
305
|
|
|
|
|
|
|
# @param old definition |
306
|
|
|
|
|
|
|
# @param new relation |
307
|
|
|
|
|
|
|
# @param new definition |
308
|
|
|
|
|
|
|
# @param the sqlite database file |
309
|
|
|
|
|
|
|
sub changeTerm { |
310
|
0
|
0
|
|
0
|
1
|
|
my $termo = shift or die; |
311
|
0
|
0
|
|
|
|
|
my $oldrel = shift or die; |
312
|
0
|
0
|
|
|
|
|
my $olddef = shift or die; |
313
|
0
|
0
|
|
|
|
|
my $newrel = shift or die; |
314
|
0
|
0
|
|
|
|
|
my $newdef = shift or die; |
315
|
0
|
0
|
|
|
|
|
my $dbfile = shift or die; |
316
|
|
|
|
|
|
|
|
317
|
0
|
|
|
|
|
|
deleteTerm($termo, $oldrel, $olddef, $dbfile); |
318
|
|
|
|
|
|
|
# use our beautiful setTerm :) |
319
|
0
|
|
|
|
|
|
setTerm($termo, $newrel, $newdef, $dbfile); |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
1; |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
__END__ |