| 1 | package CWB::Web::Query; | ||
| 2 | ## -*-cperl-*- | ||
| 3 | |||
| 4 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use CWB::CQP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use CWB::CL; | ||
| 7 | use HTML::Entities; | ||
| 8 | |||
| 9 | |||
| 10 | # global registry setting for new CWB::Web::Query objects | ||
| 11 | # changing this will set the registry for all objects created afterwards | ||
| 12 | $Registry = ""; | ||
| 13 | |||
| 14 | # error handler: print to STDERR and exit :or: user-defined callback | ||
| 15 | sub error { | ||
| 16 | my $self = shift; | ||
| 17 | if (defined $self->{'error_handler'}) { | ||
| 18 | &{$self->{'error_handler'}}(@_); | ||
| 19 | exit 1; # user-defined error handler should exit | ||
| 20 | } | ||
| 21 | else { | ||
| 22 | print STDERR "\nCWB::Web::Query Error:\n"; | ||
| 23 | grep { chomp; print STDERR "\t$_\n"; } @_; | ||
| 24 | exit 1; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | # execute CQP command with error checking & abort on error (used internally) | ||
| 29 | # usage: $self->exec("HGC;", "Can't activate corpus:"); | ||
| 30 | sub exec { | ||
| 31 | my $self = shift; | ||
| 32 | my $cmd = shift; | ||
| 33 | my $error = shift; | ||
| 34 | my $cqp = $self->{'cqp'}; | ||
| 35 | $cqp->exec($cmd); | ||
| 36 | $self->error("$error because:", $cqp->error_message) | ||
| 37 | unless $cqp->ok; | ||
| 38 | } | ||
| 39 | |||
| 40 | # CWB::Web::Query object struct: | ||
| 41 | # { | ||
| 42 | # 'cqp' => CWB::CQP object | ||
| 43 | # 'error_handler' => user-defined error handler (standard fallback if undefined) | ||
| 44 | # 'corpus' => name of query corpus | ||
| 45 | # 'ch' => CWB::CL::Corpus object | ||
| 46 | # 'attributes' => list of attributes to show | ||
| 47 | # 'aligned' => list of aligned corpora to show | ||
| 48 | # 'structures' => s-attributes whose values will be returned (separately) | ||
| 49 | # { | ||
| 50 | # <attribute> => CL::Attribute object, | ||
| 51 | # ... | ||
| 52 | # } | ||
| 53 | # } | ||
| 54 | # object constructor: spawns CQP server & initialises variables | ||
| 55 | sub new { | ||
| 56 | croak "Usage: \$q = new CWB::Web::Query 'HGC';" | ||
| 57 | unless @_ == 2; | ||
| 58 | my $class = shift; | ||
| 59 | my $corpus = uc shift; | ||
| 60 | my $self = {}; | ||
| 61 | bless($self, $class); # so we can use object methods for initialisation | ||
| 62 | $self->{'error_handler'} = undef; | ||
| 63 | # spawn CQP server process | ||
| 64 | my $cqp = new CWB::CQP; | ||
| 65 | $self->error("Can't spawn CQP server process.") | ||
| 66 | unless defined $cqp; | ||
| 67 | $cqp->set_error_handler('ignore'); # ignore CQP error messages (handled by $self->exec()) | ||
| 68 | $self->{'cqp'} = $cqp; | ||
| 69 | # init some variables to default values | ||
| 70 | $self->{'cut'} = 0; # 0 means: don't cut/reduce | ||
| 71 | $self->{'reduce'} = 0; | ||
| 72 | $self->{'attributes'} = ['word']; # CQP default setting | ||
| 73 | $self->{'structures'} = {}; | ||
| 74 | $self->{'aligned'} = []; | ||
| 75 | # if CQP generated any error messages during startup, the first command executed | ||
| 76 | # (i.e. the corpus activation) will fail; to avoid that, execute a dummy command | ||
| 77 | # now, so the CQP module reads all pending error messages | ||
| 78 | # [this is a hack for Verbmobil because they're having trouble with the new | ||
| 79 | # Apache HTTP server] | ||
| 80 | $cqp->exec("show"); | ||
| 81 | # if $Registry is set, change corpus registry before we activate the query corpus | ||
| 82 | if (defined $Registry and $Registry ne "") { | ||
| 83 | $self->exec("set registry '$Registry';", "Can't change to user-defined registry"); | ||
| 84 | $CWB::CL::Registry = $Registry; # we'll also access the corpus through the CL | ||
| 85 | } | ||
| 86 | # activate query corpus | ||
| 87 | $self->exec("$corpus;", "Can't activate corpus $corpus"); | ||
| 88 | $self->{'corpus'} = $corpus; | ||
| 89 | my $ch = new CWB::CL::Corpus $corpus; # get CL corpus handle | ||
| 90 | $self->error("Can't open corpus $corpus (CL)") | ||
| 91 | unless defined $ch; | ||
| 92 | $self->{'ch'} = $ch; | ||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | # object destructor: undef backends | ||
| 97 | sub DESTROY { | ||
| 98 | my $self = shift; | ||
| 99 | delete $self->{'cqp'}; | ||
| 100 | delete $self->{'structures'}; # undef's attribute handles | ||
| 101 | delete $self->{'ch'}; | ||
| 102 | } | ||
| 103 | |||
| 104 | # error handler | ||
| 105 | sub on_error { | ||
| 106 | my $self = shift; | ||
| 107 | my $handler = shift; | ||
| 108 | croak 'Usage: $q->on_error(\&my_error_handler);' | ||
| 109 | unless (not defined $handler or ref $handler eq 'CODE') and @_ == 0; | ||
| 110 | $self->{'error_handler'} = $handler; | ||
| 111 | } | ||
| 112 | |||
| 113 | # CQP query settings | ||
| 114 | sub cut { | ||
| 115 | my $self = shift; | ||
| 116 | croak 'Usage: $q->cut($n);' | ||
| 117 | unless @_ == 1; | ||
| 118 | $self->{'cut'} = shift; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub reduce { | ||
| 122 | my $self = shift; | ||
| 123 | croak 'Usage: $q->reduce($n);' | ||
| 124 | unless @_ == 1; | ||
| 125 | $self->{'reduce'} = shift; | ||
| 126 | } | ||
| 127 | |||
| 128 | # CQP output settings | ||
| 129 | sub context { | ||
| 130 | my $self = shift; | ||
| 131 | croak 'Usage: $q->context($left, $right);' | ||
| 132 | unless @_ == 2; | ||
| 133 | my ($lc, $rc) = @_; | ||
| 134 | $self->exec("set LeftContext $lc;", "Can't set left context to '$lc'"); | ||
| 135 | $self->exec("set RightContext $rc;", "Can't set right context to '$rc'"); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub attributes { | ||
| 139 | my $self = shift; | ||
| 140 | croak 'Usage: $q->attributes("word", "pos", "s");' | ||
| 141 | unless @_; | ||
| 142 | # hide all previously shown attributes | ||
| 143 | my @attributes = @{$self->{'attributes'}}; | ||
| 144 | foreach my $att (@attributes) { | ||
| 145 | $self->exec("show -$att;", "Internal error"); | ||
| 146 | } | ||
| 147 | # show requested attributes | ||
| 148 | @attributes = @_; | ||
| 149 | foreach $att (@attributes) { | ||
| 150 | $self->exec("show +$att", "Can't show attribute '$att'"); | ||
| 151 | } | ||
| 152 | $self->{'attributes'} = \@attributes; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub alignments { | ||
| 156 | my $self = shift; | ||
| 157 | # unshow previous alignments | ||
| 158 | my @aligned = @{$self->{'aligned'}}; | ||
| 159 | foreach my $att (@aligned) { | ||
| 160 | $self->exec("show -$att", "Internal error"); | ||
| 161 | } | ||
| 162 | # now show requested alignments | ||
| 163 | @aligned = map lc, @_; | ||
| 164 | foreach $att (@aligned) { | ||
| 165 | $self->exec("show +$att", "No alignment to '$att' found"); | ||
| 166 | } | ||
| 167 | $self->{'aligned'} = \@aligned; | ||
| 168 | } | ||
| 169 | |||
| 170 | # return ''structure values'' in 'data' field of $line struct | ||
| 171 | sub structures { | ||
| 172 | my $self = shift; | ||
| 173 | my %structures = (); | ||
| 174 | foreach my $att (@_) { | ||
| 175 | my $ah = $self->{'ch'}->attribute($att, 's'); | ||
| 176 | $self->error("Structural attribute '$att' not found in corpus ".$self->{'corpus'}) | ||
| 177 | unless defined $ah; | ||
| 178 | $self->error("Structural attribute '$att' has no values!") | ||
| 179 | unless $ah->struc_values; | ||
| 180 | $structures{$att} = $ah; | ||
| 181 | } | ||
| 182 | delete $self->{'structures'}; | ||
| 183 | $self->{'structures'} = \%structures; | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | # execute query & post-process results; returns list of $line structs | ||
| 188 | sub query { | ||
| 189 | croak 'Usage: $q->query($command);' | ||
| 190 | unless @_ == 2; | ||
| 191 | my $self = shift; | ||
| 192 | my $query = shift; | ||
| 193 | if ($self->{'cut'} > 0) { | ||
| 194 | $query =~ s/;\s*$//; # remove trailing ';' if present | ||
| 195 | $query = "$query cut ".$self->{'cut'}; | ||
| 196 | } | ||
| 197 | $self->exec("show +cpos"); # corpus position will be parsed | ||
| 198 | $self->exec("set ld '-::-::- ';"); # use left/right match delimiter for splitting | ||
| 199 | $self->exec("set rd ' -::-::-';"); | ||
| 200 | my $cqp = $self->{'cqp'}; # $query is assumed to be tainted, so we use the CQP->query() method | ||
| 201 | $cqp->exec_query($query); | ||
| 202 | $self->error("Query execution failed because:", $cqp->error_message) | ||
| 203 | unless $cqp->ok; | ||
| 204 | my ($size) = $cqp->exec("size Last"); | ||
| 205 | $self->error($cqp->error_message) | ||
| 206 | unless $cqp->ok; | ||
| 207 | if ($size == 0) { | ||
| 208 | return (); # query produced no results | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $cqp->exec("reduce Last to ".$self->{'reduce'}) | ||
| 212 | if $self->{'reduce'} > 0; | ||
| 213 | my @kwic = $cqp->exec("cat Last"); | ||
| 214 | my @lines = (); # collect result lines here | ||
| 215 | while (@kwic) { | ||
| 216 | my $kwic = shift @kwic; # kwic line currently being processed | ||
| 217 | $kwic =~ /^\s*([0-9]+):/ | ||
| 218 | or $self->error("Can't parse CQP kwic output [step A], line:", $kwic); | ||
| 219 | my $cpos = $1; $kwic = $'; | ||
| 220 | my @parts = split /-::-::-/, $kwic, -1; # this should split line into left context, matcht, right context | ||
| 221 | if (@parts != 3) { | ||
| 222 | $self->error("Can't parse CQP kwic output [step B], line:", $kwic); | ||
| 223 | } | ||
| 224 | my ($left, $match, $right) = @parts; | ||
| 225 | map {s/^\s+//; s/\s+$//;} ($left, $match, $right); # strip whitespace | ||
| 226 | encode_entities($left); # encode special chars as HTML entities | ||
| 227 | encode_entities($match); | ||
| 228 | encode_entities($right); | ||
| 229 | my $line = {}; # build result line structure | ||
| 230 | $line->{'cpos'} = $cpos; | ||
| 231 | $line->{'kwic'}->{'left'} = $left; | ||
| 232 | $line->{'kwic'}->{'match'} = $match; | ||
| 233 | $line->{'kwic'}->{'right'} = $right; | ||
| 234 | foreach my $att (keys %{$self->{'structures'}}) { | ||
| 235 | my $value = $self->{'structures'}->{$att}->cpos2struc2str($cpos); | ||
| 236 | if (defined $value) { | ||
| 237 | encode_entities($value); | ||
| 238 | $line->{'data'}->{$att} = $value; | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | $line->{'data'}->{$att} = ""; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | # read alignment lines; $att is a dummy since CQP might provide aligned lines in an order | ||
| 245 | # different from that specified in the 'aligned' field -- but we need to know how many | ||
| 246 | # alignment lines to expect | ||
| 247 | foreach $att (@{$self->{'aligned'}}) { | ||
| 248 | my $kwic = shift @kwic; | ||
| 249 | $self->error("Can't parse CQP kwic output [alignment], line:", "$kwic") | ||
| 250 | unless $kwic =~ /^-->(.*?):/; | ||
| 251 | my $aligned_corpus = $1; | ||
| 252 | $kwic = $'; | ||
| 253 | $kwic =~ s/^\s+//; $kwic =~ s/\s+$//; # strip whitespace | ||
| 254 | encode_entities($kwic); | ||
| 255 | $line->{$aligned_corpus} = $kwic; | ||
| 256 | } | ||
| 257 | push @lines, $line; | ||
| 258 | } | ||
| 259 | |||
| 260 | return @lines; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | return 1; | ||
| 265 | |||
| 266 | __END__ | ||