blib/lib/Uplug/Web/CWB.pm untested

filter: Statementstmt 0.0% 0 / 389 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 530 CC 54 SCAR 52.1
CC
54
Cov
0%
CorpusQuery
420.0
CorpusQueryForm
156.0
Query
110.0
CRAP 182.9
1 #
2 # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  
18  
19 package Uplug::Web::CWB;
20  
21 use strict;
22 use CGI qw/:standard escapeHTML escape/;
23 use HTML::Entities;
24 use Encode;
25 use lib ('/home/staff/joerg/user_local/lib/perl5/site_perl/5.8.0/');
26 use WebCqp::Query;
27  
28 binmode(STDOUT, ":utf8"); # set UTF8 for STDOUT
29  
30 my $MAXWAIT=60;
31 my $SHOWMAX=20;
32 my $NRCORPORA=0;
33  
34 #------------------------------------------------------
35 # some links and text
36  
37 my ($CQPSYNTAX,$CWBLINK,$CQPEXAMPLES)=
38 (
39 "http://www.ims.uni-stuttgart.de/projekte/CorpusWorkbench/CQPSyntax.html",
40 "http://www.ims.uni-stuttgart.de/projekte/CorpusWorkbench/index.html",
41 "http://www.ims.uni-stuttgart.de/projekte/CorpusWorkbench/CQPExamples.html"
42 );
43 my $CQPtext=
44 'A CQP query consists of a regular'.&br().
45 'expression over '.&i('attribute expressions').'.'.&br().
46 &a({-href=>$CQPSYNTAX},'Introduction of the query syntax').&br.
47 &a({-href=>$CQPEXAMPLES},'Example queries');
48 my $PATTRtext='positional annotation';
49 my $SATTRtext='structural annotation';
50 my %corpora;
51  
52  
53  
54 ############################################################################
55 ############################################################################
56 ############################################################################
57 ############################################################################
58 ############################################################################
59 ############################################################################
60 ############################################################################
61 ############################################################################
62 ############################################################################
63 ############################################################################
64  
65 sub Query{
66  
67 my $url = shift;
68 my $owner = shift;
69 my $corpus = shift;
70  
71 my $lang = url_param('s');
72 if (param('showmax')){$SHOWMAX=param('showmax');}
73  
74 my $advanced = url_param('adv');
75 my @alg = param('alg');
76 my $cqp = decode('utf-8',param('query'));
77 if ($cqp){param('query',$cqp);}
78  
79 binmode (STDOUT,':encoding(utf-8)');
80  
81 if (not -d $ENV{UPLUGCWB}){mkdir $ENV{UPLUGCWB},0755;}
82 if (not -d "$ENV{UPLUGCWB}/reg"){mkdir "$ENV{UPLUGCWB}/reg",0755;}
83 if (not -d "$ENV{UPLUGCWB}/dat"){mkdir "$ENV{UPLUGCWB}/dat",0755;}
84 if (not -d "$ENV{UPLUGCWB}/reg/$owner"){mkdir "$ENV{UPLUGCWB}/reg/$owner",0755;}
85 if (not -d "$ENV{UPLUGCWB}/dat/$owner"){mkdir "$ENV{UPLUGCWB}/dat/$owner",0755;}
86  
87 my $CWBregistry="$ENV{UPLUGCWB}/reg/$owner";
88 chdir $CWBregistry;
89  
90 #------------------------------------------------------
91 # main
92 #------------------------------------------------------
93  
94 my $html; # =&h2("Corpus query (CWB)");
95 &ReadRegistry('.',\%corpora);
96  
97 my $reg=undef;
98  
99 ##----------------------------------------------
100 ## if there is only one corpus: set parameters
101 ## otherwise: create corpus-links for each corpus in the registry
102 ##
103 if ($NRCORPORA==1){
104 ($corpus,$lang)=keys %corpora;
105 ($lang)=keys %{$corpora{$corpus}};
106 }
107 else{
108 $reg=&RegisteryLinks(\%corpora,$url);
109 }
110 ##----------------------------------------------
111  
112 my $form=&CorpusQueryForm($corpus,$lang,$advanced);
113 $html.=&table({},caption(''),&Tr([&td({-valign=>'top'},[$reg,$form])]));
114  
115  
116 if (param('query')){
117 $html.=&p(),&hr();
118 my $style = param('style');
119 $html.=&CorpusQuery($corpus,$lang,$cqp,\@alg,$style);
120 }
121 return $html;
122 }
123  
124 #------------------------------------------------------
125 # end
126 #------------------------------------------------------
127  
128  
129  
130  
131 #------------------------------------------------------
132 # FixString: convert from CWB-format
133 # to UTF-8 if necessary!
134  
135  
136 sub FixString{
137 my ($lang,$string)=@_;
138 if ($lang=~/^(ar|az|be|bg|bs|he|id|jp|ja|ko|ku|mi|mk|ru|ta|th|uk|vi|xh|zh_tw|zu)$/){
139 decode_entities($string);
140 $string=decode('utf-8',$string);
141 }
142 if ($lang eq 'el'){decode_entities($string);}
143 if ($lang=~/^(cs|hr|hu|pl|ro|sk|sl|sr)$/){
144 decode_entities($string);
145 $string=decode('iso-8859-2',$string);
146 }
147 if ($lang=~/^el$/){
148 decode_entities($string);
149 $string=decode('iso-8859-7',$string);
150 }
151 if ($lang=~/^tr$/){
152 decode_entities($string);
153 $string=decode('iso-8859-9',$string);
154 }
155 return $string;
156 }
157  
158  
159  
160 #------------------------------------------------------
161 # CorpusQueryForm: show the query form
162 #
163  
164 sub CorpusQueryForm{
165 my ($corpus,$lang,$advanced)=@_;
166  
167 if (not $corpus){return;}
168 if (not $lang){return;}
169  
170 my %index=%corpora;
171 my $form= &startform();
172  
173  
174 #---------------------------------------------------
175 # cqp-query-field
176  
177 my $query.=&textfield(-size=>'30',-name => 'query',
178 -default=>'[word="a.*"]');
179  
180 #---------------------------------------------------
181 # checkboxes for word attributes
182  
183 my $pattr=undef;
184 if (ref($corpora{$corpus}{$lang}{attr}) eq 'ARRAY'){
185 my @attr=sort grep ($_ ne 'word',@{$corpora{$corpus}{$lang}{attr}});
186 $pattr.=&checkbox_group(-name=>'attr',
187 -values=>('word'),
188 -default=>('word'));
189 $pattr.=&checkbox_group(-name=>'attr',
190 -values=>\@attr);
191 }
192  
193 #---------------------------------------------------
194 # checkboxes for structural attributes
195  
196 my $sattr=undef;
197 if (ref($corpora{$corpus}{$lang}{struc}) eq 'HASH'){
198 my @struc=sort keys %{$corpora{$corpus}{$lang}{struc}};
199 foreach (sort @struc){
200 $sattr.=&checkbox_group(-name=>'attr',-values=>($_));
201 my @strucattr=sort @{$corpora{$corpus}{$lang}{struc}{$_}};
202 $sattr.=&checkbox_group(-name=>'attr',-values=>\@strucattr);
203 $sattr.=&br();
204 }
205 }
206  
207 #---------------------------------------------------
208 # context window
209  
210 my $context=undef;
211 my @struc=('s');
212 if (ref($corpora{$corpus}{$lang}{struc}) eq 'HASH'){
213 @struc=sort keys %{$corpora{$corpus}{$lang}{struc}};
214 }
215  
216 $context.=&popup_menu(-name=> 'bcs',
217 -default => '1',
218 -values => [1,2,3,4,5]);
219 $context.=&popup_menu(-name=> 'bc',
220 -default => 's',
221 -values => \@struc);
222 $context.=&popup_menu(-name=> 'acs',
223 -default => '1',
224 -values => [1,2,3,4,5]);
225 $context.=&popup_menu(-name=> 'ac',
226 -default => 's',
227 -values => \@struc);
228  
229  
230 #---------------------------------------------------
231 # checkboxes (and input fields) for sentence aligned corpora
232  
233 my $align=undef;
234 if (ref($index{$corpus}{$lang}{align}) eq 'ARRAY'){
235 my @lang=sort @{$index{$corpus}{$lang}{align}};
236 my $nr_col=sqrt(@lang);
237 my @rows=();
238 my $i=1;
239 my $nr_rows=0;
240 foreach my $l (sort @{$index{$corpus}{$lang}{align}}){
241 $rows[$nr_rows].=
242 &td({-nowrap=>'1'},
243 [&checkbox_group(-name=>'alg',-values=>($l))]);
244 # if ($advanced){
245 $rows[$nr_rows].=
246 &td([&textfield(-size=>'10',-name => 'query_'.$l)]);
247 # }
248 $i++;
249 if ($i>$nr_col){$nr_rows++;$i=1;}
250 }
251 $align.=&table({-cellspacing=>"0"},caption(''),&Tr(\@rows));
252 }
253  
254 #---------------------------------------------------
255 # display styles
256  
257 my @styles=('vertical','KWIC');
258 if (defined ($align)){push(@styles,'horizontal');}
259 my $style=&radio_group(-name=>'style',
260 -values=>\@styles);
261  
262 #---------------------------------------------------
263 # max number of hits
264  
265 my $show='show max '.&textfield(-size=>'4',
266 -default=>"$SHOWMAX",
267 -name => 'showmax').' hits';
268 if (defined $align){
269 $show.=' and ';
270 $show.=&checkbox(-name=>'skipnoalign',
271 -checked=>'checked',
272 -value=>'skip',
273 -label=>'skip non-aligned segments');
274 }
275  
276 #---------------------------------------------------
277 # submit-query-button and other stuff
278  
279 my $submit=&p().&submit(-name => 'action',-value => 'select');
280 $submit.= ' '.$show.$style;
281 my $link=url();
282 $link=&AddUrlParam($link,'c',$corpus);
283 $link=&AddUrlParam($link,'s',$lang);
284 # if ($advanced){
285 # $link=&AddUrlParam($link,'adv','0');
286 # $submit.=&br().'('.&a({-href=>$link},'simple').' search)';
287 # }
288 # else{
289 # $link=&AddUrlParam($link,'adv','1');
290 # $submit.=&br().'('.&a({-href=>$link},'advanced').' search)';
291 # }
292  
293  
294 #---------------------------------------------------
295 # put everything together
296  
297 my @rows=();
298  
299 if ($advanced){
300 my $header=&a({-href=>$CQPSYNTAX},'CQP query').' ';
301 $header.=&a({-href=>$CWBLINK},'(CWB)');
302 push (@rows,&th([$header,'show attributes']));
303 push (@rows,&td([&p().'query: '.$query.
304 &p().'context: '.$context,
305 $PATTRtext.&br().$pattr.&p().
306 $SATTRtext.&br().$sattr]));
307 push (@rows,&th({-colspan=>'2'},'alignments'));
308 push (@rows,&td({-colspan=>'2'},$align));
309 push (@rows,&td({-colspan=>'2'},$submit));
310 $form.=&table({-cellspacing=>"0"},caption(''),&Tr(\@rows));
311 }
312 else{
313 my $header=&a({-href=>$CQPSYNTAX},'CQP query').' ';
314 $header.=&a({-href=>$CWBLINK},'(CWB)');
315 push (@rows,&th([$header,'show attributes']));
316 if ($align){$rows[-1].=&th(['alignments'])};
317 push (@rows,&td([$CQPtext.&p().$query,
318 $PATTRtext.&p().$pattr.&p().
319 $SATTRtext.&br().$sattr,
320 $align.&br()]));
321 push (@rows,&td({-colspan=>'3'},$submit));
322 $form.=&table({-cellspacing=>"0"},caption(''),&Tr(\@rows));
323 }
324  
325 $form.= &endform();
326 return &div({-class=>'query'},$form);
327 }
328  
329  
330  
331 #-----------------------------------------------------------
332 # CorpusQuery: run the corpus query using WebCqp
333  
334  
335 sub CorpusQuery{
336  
337 my ($corpus,$lang,$cqp,$aligned,$style)=@_;
338  
339 $WebCqp::Query::Registry = $corpus;
340 my $query;
341 eval { $query = new WebCqp::Query("$lang"); };
342 if ($@){print "--$@--$!--$?--";}
343  
344 #---------------------------------------------------------
345  
346 $query->on_error(sub{grep {print "$_".&br()} @_});
347 my @corpora=($lang);
348  
349 my ($bc,$bcs,$ac,$acs)=('s',1,'s',1);
350 if (param('bc')){$bc=param('bc');} # before context
351 if (param('bcs')){$bcs=param('bcs');} # before context size
352 if (param('ac')){$ac=param('ac');} # after context
353 if (param('acs')){$acs=param('acs');} # after context size
354  
355 if ($style ne 'KWIC'){
356 $query->context("$bcs $bc", "$acs $ac");
357 }
358 if ($cqp!~/^[\"\[]/){$cqp='"'.$cqp.'"';}
359  
360 #---------------------------------------------------------
361 # check queries for each alignment
362  
363 if (ref($aligned) eq 'ARRAY'){
364 $query->alignments(sort @{$aligned});
365 push (@corpora,@{$aligned});
366 foreach (@{$aligned}){
367 if (param("query_$_")){
368 my $l=uc($_);
369 $cqp.=" :$l ".param("query_$_");
370 }
371 }
372 }
373  
374 #---------------------------------------------------------
375 # show certain attributes
376  
377 if (defined param('attr')){
378 my @attr=param('attr');
379 $query->attributes(@attr);
380 }
381  
382 #---------------------------------------------------------
383 # run the query
384 $query->reduce($SHOWMAX);
385 my @result=();
386 local $SIG{ALRM}=\&timeout;
387 eval {
388 alarm ($MAXWAIT);
389 @result = $query->query($cqp);
390 alarm 0;
391 };
392 my $nr_result = @result;
393 #---------------------------------------------------------
394  
395 my $html="Query string: \"$cqp\"".&br();
396 $html.="<b>$nr_result</b> hits found";
397  
398 my @rows=();
399 if ($style eq 'vertical'){
400 push (@rows,&th(['',@corpora]));
401 }
402  
403 my $skipnoalign=param('skipnoalign');
404 my $nr;my $i;
405 for ($i = 0; $i < $nr_result; $i++) {
406 $nr = $i + 1;
407 my $m = $result[$i];
408 my $pos = $m->{'cpos'};
409 my $ord = &FixString($lang,$m->{'kwic'}->{'match'});
410 my $res_r = &FixString($lang,$m->{'kwic'}->{'right'});
411 my $res_l = &FixString($lang,$m->{'kwic'}->{'left'});
412 my $noalign=0;
413 my @newrows=();
414 if ($style eq 'KWIC'){ # KWIC style
415 push (@newrows,
416 &td({},[$pos]).
417 &td({-align=>'right'},[$res_l]).
418 &td({-align=>'center'},["<b>$ord</b>"]).
419 &td({},[$res_r]));
420 }
421 else{
422 push (@newrows,&td({},[$pos,"$res_l <b>$ord</b> $res_r"]));
423 }
424  
425 #----------------------------
426 # aligned regions
427  
428 if (ref($aligned) eq 'ARRAY'){
429 my $even=0;
430 foreach (@{$aligned}){
431 my $color='#FFEEDD'; # align-color 1
432 if ($even){$color='#FFFFE0';} # align-color 2
433 # my $color='#FFFF99'; # align-color 1
434 # if ($even){$color='#FFCC99';} # align-color 2
435 $even=not $even;
436 if ($m->{$_}=~/\(no alignment found\)/){$noalign++;}
437 my $string=&FixString($_,$m->{$_});
438 if ($style eq 'vertical'){ # vertical alignment
439 $newrows[-1].=&td({-valign=>'top'},$string);
440 }
441 elsif ($style eq 'KWIC'){ # KWIC style
442 push (@newrows,
443 &th({},[$_]).
444 &td({-bgcolor=>$color,
445 -colspan=>"3",
446 -align=>'center'},[$string]));
447 }
448 else{ # horizontal alignment
449 push (@newrows,
450 &th({},[$_]).
451 &td({-bgcolor=>$color},[$string]));
452 }
453 }
454 }
455 if ((not $skipnoalign) or (not $noalign)){
456 push (@rows,@newrows);
457 }
458 if ($nr>=$SHOWMAX){last;}
459 }
460 $html.=&table({-cellspacing=>'0'},caption(''),&Tr(\@rows));
461 return &div({-class=>'result'},$html);
462 }
463  
464  
465 #--------------------------------------------------
466 # create a table of registry links
467 # (create links for each corpus)
468  
469  
470 sub RegisteryLinks{
471 my $corpora=shift;
472 my $url=shift;
473  
474 my @rows=(&th({},['corpus&nbsp;','&nbsp;languages&nbsp;']));
475 my %trans=();
476 foreach my $c (sort keys %{$corpora}){
477 my $link=&AddUrlParam($url,'c',$c);
478 my $html='';
479 foreach my $l (sort keys %{$$corpora{$c}}){
480 my $link=&AddUrlParam($link,'s',$l);
481 $html.=&a({-href=>$link},$l).' ';
482 $trans{$l}{$c}=$link;
483 }
484 push (@rows,&td({},[$c,$html]));
485 }
486 return &div({-class=>'registry'},
487 &table({-cellpadding=>"0"},caption(''),&Tr(\@rows)));
488 }
489  
490  
491 #-----------------------------------------
492 # add a URL-style parameter to the CGI-URL
493  
494 sub AddUrlParam{
495 my ($url,$name,$val)=@_;
496 $val=escape($val);
497 if ($url!~/[\?\;]$name\=/){
498 if ($url!~/\?/){$url.="\?$name=$val";}
499 else{$url.="\;$name=$val";}
500 }
501 else{$url=~s/$name\=[^\;]*(\;|\Z)/$name=$val$1/;}
502 return $url;
503 }
504  
505  
506 #-----------------------------------------
507 # read all registry files
508 # directory=corpus
509 # language=registry-file
510  
511 sub ReadRegistry{
512 my $dir=shift;
513 my $reg=shift;
514 opendir(DIR, $dir) or die "Can't open it: $!\n";
515 my @files= readdir(DIR);
516 foreach my $f (@files){
517 if ($f=~/^\./){next;}
518 if (-d $dir.'/'.$f){
519 my $subdir=$dir.'/'.$f;
520 $subdir=~s/^\.\///; # delete './'
521 &ReadRegistry($subdir,$reg);
522 }
523 if (-f $dir.'/'.$f && ! -l $dir.'/'.$f){
524 $NRCORPORA++;
525 $$reg{$dir}{$f}={};
526 open F,"<$dir/$f";
527 my @text=<F>;
528 close F;
529 foreach (@text){
530 if (/ATTRIBUTE\s(.*)$/){
531 push (@{$$reg{$dir}{$f}{attr}},$1);
532 }
533 if (/STRUCTURE\s(.*)$/){
534 my $struc=$1;
535 if ($struc=~/^(\S+)\_(\S+)(\s|\Z)/){
536 push (@{$$reg{$dir}{$f}{struc}{$1}},"$1_$2");
537 }
538 else{
539 @{$$reg{$dir}{$f}{struc}{$struc}}=();
540 }
541 }
542 if (/ALIGNED\s(.*)$/){
543 push (@{$$reg{$dir}{$f}{align}},$1);
544 }
545 }
546 }
547 }
548 }
549  
550 #-----------------------------------------
551 # print timeout-message and die!
552  
553 sub timeout{
554 print &h2("Your query took more than $MAXWAIT seconds!");
555 print "The process has been timed out!",&br();
556 print "Possible reasons:<ul>";
557 print "<li>The server is busy</li>";
558 print "<li>Your query is very complicated</li>";
559 print "</ul>";
560 print "try<ul>";
561 print "<li>another (simple) query</li>";
562 print "<li>to reduce the number of results (show max)</li>";
563 print "<li>to run queries without aligned regions</li>";
564 print "</ul>";
565 print "Sorry for any inconvenience!",&hr();
566 print &end_html;
567 exit;
568 }
569  
570 ############################################################################
571 ############################################################################
572 ############################################################################
573 ############################################################################
574 ############################################################################
575 ############################################################################
576 ############################################################################
577 ############################################################################
578  
579  
580 1;