File Coverage

blib/lib/RDF/NLP/SPARQLQuery.pm
Criterion Covered Total %
statement 179 204 87.7
branch 54 64 84.3
condition 8 15 53.3
subroutine 24 24 100.0
pod 13 13 100.0
total 278 320 86.8


line stmt bran cond sub pod time code
1             package RDF::NLP::SPARQLQuery;
2              
3 11     11   264848 use utf8;
  11         113  
  11         61  
4 11     11   328 use strict;
  11         21  
  11         355  
5 11     11   55 use warnings;
  11         27  
  11         569  
6              
7             our $VERSION='0.1';
8              
9 11     11   19304 use Data::Dumper;
  11         127017  
  11         1000  
10 11     11   13690 use Config::General;
  11         371967  
  11         1036  
11              
12 11     11   8750 use RDF::NLP::SPARQLQuery::Question;
  11         44  
  11         37998  
13              
14             # DOC
15             sub new {
16 10     10 1 167 my $class = shift;
17 10         32 my %args = @_;
18              
19 10         73 binmode(STDERR, ":utf8");
20 10         33 binmode(STDOUT, ":utf8");
21 10         35 binmode(STDIN, ":utf8");
22              
23 10         219 my $NLQuestion = {
24             "files" => {
25             'config' => undef,
26             # 'questions' => undef,
27             'semtypecorresp' => undef,
28             },
29             'config' => undef,
30             'questions' => undef,
31             'semtypecorresp' => {},
32             'format' => "XML",
33             'verbose' => 0,
34             };
35            
36 10         56 bless $NLQuestion, $class;
37 10         39 return($NLQuestion);
38             }
39              
40             sub _files {
41 40     40   58 my ($self) = @_;
42            
43 40         261 return($self->{'files'});
44             }
45              
46             sub configFile {
47 30     30 1 4087 my $self = shift;
48            
49 30 100       96 if (@_) {
50 10         48 $self->_files->{'config'} = shift;
51             }
52 30         66 return($self->_files->{'config'});
53             }
54              
55             sub format {
56 54     54 1 79 my $self = shift;
57            
58 54 100       147 if (@_) {
59 4         11 $self->{'format'} = shift;
60             }
61 54         249 return($self->{'format'});
62             }
63              
64             # DOC
65             sub verbose {
66 62     62 1 2453 my $self = shift;
67            
68 62 100       329 if (@_) {
69 5         35 $self->{'verbose'} = shift;
70             }
71 62         422 return($self->{'verbose'});
72             }
73              
74             sub config {
75 49     49 1 83 my ($self) = @_;
76            
77 49         755 return($self->{'config'});
78             }
79              
80             # sub _questionsFile {
81             # my $self = shift;
82              
83             # if (@_) {
84             # $self->_files->{'questions'} = shift;
85             # }
86             # return($self->_files->{'questions'});
87             # }
88              
89             sub _semtypecorrespFile {
90 19     19   45 my $self = shift;
91 19         38 my $lang = shift;
92              
93 19         79 return($self->config->{'NLQUESTION'}->{'language="'. uc($lang) . '"'}->{'SEMANTICTYPECORRESPONDANCE'});
94             }
95              
96             sub semtypecorresp {
97 75     75 1 143 my ($self, ) = @_;
98              
99 75 50       294 if (!defined $self->{'semtypecorresp'}) {
100 0         0 $self->{'semtypecorresp'} ={};
101             }
102              
103 75         2702 return($self->{'semtypecorresp'});
104             }
105              
106             # DOC
107             sub loadConfig {
108              
109 10     10 1 30 my ($self, ) = @_;
110 10         58 my $language;
111              
112 10         44 my $cg = new Config::General('-ConfigFile' => $self->configFile,
113             '-InterPolateVars' => 1,
114             '-InterPolateEnv' => 1
115             );
116            
117 10         46607 my %config = $cg->getall;
118              
119 10         151 $self->_printVerbose(Dumper(\%config),2);
120              
121 10         178 $self->{'config'} = \%config;
122              
123 10         25 foreach $language (keys %{$config{'NLQUESTION'}}) {
  10         102  
124 19 50       228 if ($language =~ /language=\"?(?[^"]+)\"?/) {
125 19         363 $self->_loadSemtypecorresp(lc($+{lang}));
126             }
127             }
128              
129 10         64 return($self->config);
130             }
131              
132             # DOC
133             sub loadInput {
134              
135 9     9 1 23 my ($self, $filename) = @_;
136 9         21 my $line;
137             my $docId;
138 0         0 my $language;
139 0         0 my @sentences;
140 0         0 my @postags;
141 0         0 my @semanticUnits;
142 0         0 my $word;
143 0         0 my $postag;
144 0         0 my $lemma;
145 0         0 my $start_offset;
146 0         0 my $semanticUnit;
147 0         0 my $canonical_form;
148 0         0 my $semanticTypes;
149 0         0 my %semTypes;
150 0         0 my $end_offset;
151 0         0 my $semf;
152 0         0 my $question;
153 0         0 my $id;
154              
155 9 50       833 open FILE, "<:utf8", $filename or die "No such file $filename\n";
156             # warn "filename: $filename\n";
157 9         1571 while($line = ) {
158 110         159 chomp($line);
159 110 50       260 if ($line !~ /^\s*#/) {
160 110 100       264 if ($line =~ /^DOC:\s?(?.*)/) {
161 10         131 $docId = $+{id}; #'
162             # warn "docId: $docId\n";
163             }
164 110 100       264 if ($line =~ /^language:\s?(?.*)/) {
165 10         63 $language = $+{lang}; #'
166             }
167 110 100       249 if ($line =~ /^sentence:\s/) {
168 10         45 while($line = ) {
169 30         43 chomp($line);
170 30 50 66     226 if (($line !~ /^\s*#/) && ($line !~ /^\s*$/)) {
171 20 100       63 if ($line ne "_END_SENT_") {
172 10         43 push @sentences, $line;
173             } else {
174 10         23 last;
175             }
176             }
177             }
178             }
179             # warn "line: $line\n";
180 110 100       234 if ($line =~ /^word information:/) {
181 10         17 $id=0;
182 10         46 while($line = ) {
183 104         132 chomp($line);
184 104 100 66     576 if (($line !~ /^\s*#/) && ($line !~ /^\s*$/)) {
185 84 100       152 if ($line ne "_END_POSTAG_") {
186 74         305 ($word, $postag, $lemma, $start_offset) = split /\t/, $line;
187             # push @postags, $line;
188 74         387 push @postags, {
189             "id" => $id,
190             "word" => $word,
191             "postag" => $postag,
192             "lemma" => $lemma,
193             "start_offset" => $start_offset,
194             "line" => $line,
195             };
196 74         287 $id++;
197             } else {
198 10         92 last;
199             }
200             }
201             }
202             }
203 110 100       234 if ($line =~ /^semantic units:/) {
204 10         19 $id = 0;
205 10         16 my %lines;
206 10         49 while($line = ) {
207 82         173 chomp($line);
208 82 100 66     672 if (($line !~ /^\s*#/) && ($line !~ /^\s*$/)&&(!exists $lines{$line})) {
      33        
209 45 100       94 if ($line ne "_END_SEM_UNIT_") {
210 35         93 $lines{$line}++;
211 35         159 ($semanticUnit, $canonical_form, $semanticTypes, $start_offset, $end_offset) = split /\t/, $line;
212             # push @semanticUnits, $line;
213 35         68 %semTypes=();
214 35         100 foreach $semf (split /:/, $semanticTypes) {
215 63         319 $semTypes{$semf} = [split /\//, $semf];
216             }
217             # warn $semanticUnit . " ($semanticTypes)\n";
218             # warn "\t" . join('::', keys(%semTypes)) . "\n";
219 35         253 push @semanticUnits, {
220             "id" => $id,
221             "semanticUnit" => $semanticUnit,
222             "canonical_form" => $canonical_form,
223             "semanticTypes" => {%semTypes},
224             "start_offset" => $start_offset,
225             "end_offset" => $end_offset,
226             "line" => $line,
227             };
228 35         126 $id++;
229             } else {
230 10         32 last;
231             }
232             }
233             }
234             }
235 110 100       489 if ($line eq "_END_DOC_") {
236 10         46 $question = RDF::NLP::SPARQLQuery::Question->new("docId" => $docId,
237             'verbose' => $self->verbose,
238             "language" => uc($language),
239             "sentences" => \@sentences,
240             "postags" => \@postags,
241             "semanticUnits" => \@semanticUnits,
242             "config" => $self->config,
243             );
244 10         70 $self->_addQuestion($question->{'docId'}, $question);
245 10         15 $docId = undef;
246 10         16 $language = undef;
247 10         25 @sentences = ();
248 10         17 @postags = ();
249 10         42 @semanticUnits = ();
250             }
251             }
252              
253             }
254            
255 9         4520 close FILE;
256              
257 9         47 return(scalar($self->questionIds));
258             }
259              
260             sub questions {
261 38     38 1 57 my $self = shift;
262              
263 38 100       102 if (!(defined $self->{'questions'})) {
264 8         20 $self->{'questions'} = {};
265             }
266 38         160 return($self->{'questions'});
267             }
268              
269             sub getQuestionList {
270 12     12 1 386 my $self = shift;
271              
272 12         20 return(values %{$self->{'questions'}});
  12         61  
273             }
274              
275             sub questionIds {
276 9     9 1 23 my $self = shift;
277              
278 9         15 return(keys %{$self->{'questions'}});
  9         93  
279             }
280              
281             sub getQuestionFromId {
282 9     9 1 19 my $self = shift;
283 9         19 my $docId;
284              
285 9 50       38 if (@_) {
286 9         21 $docId = shift;
287 9 50       118 if (exists $self->questions->{$docId}) {
288 9         64 return($self->questions->{$docId});
289             }
290             }
291 0         0 return(undef);
292             }
293              
294             sub _addQuestion {
295 10     10   20 my $self = shift;
296 10         18 my $docId = shift;
297              
298 10 50       36 if (!defined $docId) {
299 0         0 return(undef);
300             }
301              
302 10 50       34 if (@_) {
303 10         39 $self->questions->{$docId} = shift;
304             }
305            
306 10         30 return($self->questions->{$docId});
307             }
308              
309              
310             sub _loadSemtypecorresp {
311 19     19   48 my ($self, $lang) = @_;
312              
313 19         200 my $cg = new Config::General('-ConfigFile' => $self->_semtypecorrespFile($lang),
314             '-InterPolateVars' => 1,
315             '-InterPolateEnv' => 1
316             );
317            
318 19         2305789 my %resource = $cg->getall;
319              
320 19         427 $self->semtypecorresp->{uc($lang)} = \%resource;
321              
322 19         204 $self->_printVerbose( Dumper($self->semtypecorresp->{uc($lang)}), 3);
323              
324 19         7797 return($self->semtypecorresp->{uc($lang)});
325             }
326              
327             # DOC
328             sub Questions2Queries {
329 7     7 1 27 my $self = shift;
330 7         15 my $outStr = shift;
331 7         14 my $question;
332 7         14 my $questionCount = 0;
333 7         14 my $docHeadId;
334             my $docId;
335 0         0 my $outStr2;
336 0         0 my $answer;
337              
338 7 100       32 if ($self->format eq "XML") {
339 4         11 $$outStr = '' . "\n";
340             # warn $self->getQuestionList;
341 4         17 $docId = ($self->getQuestionList)[0]->docId;
342             # warn $docId;
343 4         83 $docId =~ /\-([\d\w]+)$/;
344 4         17 $docHeadId = $`;
345              
346 4         20 $$outStr .= '' . "\n";
347             }
348 7         31 foreach $question ($self->getQuestionList) {
349             # warn $self->semtypecorresp;
350             # $self->format,
351 9         77 $self->_printVerbose($question->docId . "\n");
352 9         39 $questionCount += $question->Question2Query($self->semtypecorresp);
353 9         68 $self->_printVerbose($question->query->queryString);
354             # $$outStr .= $outStr2;
355 9 100       36 if ($self->format eq "XML") {
356             # warn $question->query->queryXMLString;
357 6         39 $$outStr .= $question->query->queryXMLString;
358             }
359 9 100       30 if ($self->format eq "SPARQL") {
360             # warn $question->query->queryString;
361 1         8 $$outStr .= $question->query->queryString;
362             }
363 9 100       100 if ($self->format eq "SPARQLANSWERS") {
364 1         6 $question->query->getQueryAnswers;
365 1         12 $$outStr .= "\n" . $question->docId . "\n";
366 1         3 $$outStr .= join("\n",keys(%{$question->query->queryAnswers}));
  1         10  
367             }
368 9 100       34 if ($self->format eq "XMLANSWERS") {
369 1         7 $$outStr .= $question->query->queryXMLString;
370 1         13 $$outStr =~ s!\n!!;
371              
372 1         9 $question->query->getQueryAnswers;
373 1         6 $$outStr .= "\n";
374 1         3 foreach $answer (keys(%{$question->query->queryAnswers})) {
  1         13  
375 0         0 $$outStr .= "\n";
376 0         0 $$outStr .= "$answer\n";
377 0         0 $$outStr .= "\n";
378             }
379 1         4 $$outStr .= "\n";
380 1         5 $$outStr .= "\n";
381             }
382             }
383 7 100       34 if ($self->format eq "XML") {
384 4         14 $$outStr .= '' . "\n";
385             }
386              
387 7         34 return($questionCount);
388             }
389              
390             sub _printVerbose {
391 47     47   90802 my($self, $msg, $level) = @_;
392              
393 47 100       205 if (!defined $level) {
394 18         29 $level = 1;
395             }
396              
397 47 50 33     192 if (($self->verbose > 0) && ($self->verbose >= $level)) {
398 0           warn "$msg";
399             }
400              
401             }
402              
403              
404              
405              
406             1;
407              
408             __END__