| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # | ||
| 4 | # Running the query filter in an ALVIS environment. | ||
| 5 | # Reads all parameters from alvis.cnf so just needs directory | ||
| 6 | # to target. These are: | ||
| 7 | # QF_PORT YAZ_PORT QF_TEXT | ||
| 8 | # Also tries to read resource files from | ||
| 9 | # <ALVISdirectory>/resources | ||
| 10 | # Note this *only responds to requests locally, i.e., | ||
| 11 | # the incoming URL must be "http://localhost....". | ||
| 12 | |||
| 13 | |||
| 14 | our $VERSION = "0.1"; | ||
| 15 | |||
| 16 | use Alvis::QueryFilter; | ||
| 17 | use LWP::UserAgent; | ||
| 18 | use HTTP::Daemon; | ||
| 19 | use HTTP::Status; | ||
| 20 | use HTTP::Response; | ||
| 21 | |||
| 22 | my $options; | ||
| 23 | my $testquery; | ||
| 24 | my $verbose = 0; | ||
| 25 | if ( $ARGV[0] =~ /^--/ ) { | ||
| 26 | $options = shift(); | ||
| 27 | } | ||
| 28 | |||
| 29 | die("Usage: $0" . | ||
| 30 | " <ALVISdirectory>") if @ARGV != 1; | ||
| 31 | |||
| 32 | my $ALVISHOME = shift @ARGV; | ||
| 33 | my $QF_PORT; | ||
| 34 | my $YAZ_PORT; | ||
| 35 | my $QF_TEXT = "text"; | ||
| 36 | my $F_BN = "$ALVISHOME/resources"; | ||
| 37 | |||
| 38 | &readargs(); | ||
| 39 | |||
| 40 | if ( !$QF_PORT || ! $YAZ_PORT ) { | ||
| 41 | print STDERR "Cannot locate ports in alvis.cnf\n"; | ||
| 42 | exit(1); | ||
| 43 | } | ||
| 44 | |||
| 45 | # here are the expected resources files | ||
| 46 | # | ||
| 47 | my $lemma_dict_f="${F_BN}/lemmas"; | ||
| 48 | my $term_dict_f="${F_BN}/terms"; | ||
| 49 | my $NE_dict_f="${F_BN}/NEs"; | ||
| 50 | my $typing_rules_f="${F_BN}/types"; | ||
| 51 | my $onto_nodes_f="${F_BN}/onto_nodes"; | ||
| 52 | my $onto_mapping_f="${F_BN}/onto_paths"; | ||
| 53 | |||
| 54 | # method to standardise terms and named entities | ||
| 55 | # lower case, ignore space and '-' | ||
| 56 | sub tcanonise { | ||
| 57 | $_ = shift(); | ||
| 58 | s/\s+//g; | ||
| 59 | s/\-//g; | ||
| 60 | $_ = lc($_); | ||
| 61 | return $_; | ||
| 62 | } | ||
| 63 | sub ncanonise { | ||
| 64 | $_ = shift(); | ||
| 65 | s/\s//g; | ||
| 66 | if ( ! /[0-9]/ ) { | ||
| 67 | s/[\-_]//g; | ||
| 68 | } | ||
| 69 | return $_; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub cleanspaces() { | ||
| 73 | $_ = shift(); | ||
| 74 | s/\s+/ /g; | ||
| 75 | s/^ //g; | ||
| 76 | s/ $//g; | ||
| 77 | return $_; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub checkdict() { | ||
| 81 | my $f = shift(); | ||
| 82 | my $canonise = shift(); | ||
| 83 | my %dict = (); | ||
| 84 | my %line = (); | ||
| 85 | |||
| 86 | if (!defined(open(F,"<:utf8",$f))) | ||
| 87 | { | ||
| 88 | return undef; | ||
| 89 | } | ||
| 90 | |||
| 91 | while (my $l=<F>) | ||
| 92 | { | ||
| 93 | chomp $l; | ||
| 94 | my ($form,$can)=split(/\t/,$l,-1); | ||
| 95 | $form = &cleanspaces($form); | ||
| 96 | $can = &cleanspaces($can); | ||
| 97 | $line{$can} .= $l . "\n"; | ||
| 98 | my $cf = &$canonise($form); | ||
| 99 | if ( defined($dict{$cf}) && $dict{$cf} ne $can ) { | ||
| 100 | print STDERR "Item of form '$form' has canonical form '$can'\n" | ||
| 101 | . " but maps to the another canonical form '$dict{$cf}'\n" | ||
| 102 | . "Relevent items: \n"; | ||
| 103 | print STDERR $line{$can}; | ||
| 104 | print STDERR $line{$dict{$cf}}; | ||
| 105 | print STDERR "\n"; | ||
| 106 | } | ||
| 107 | $dict{$cf}=$can; | ||
| 108 | } | ||
| 109 | close(F); | ||
| 110 | } | ||
| 111 | |||
| 112 | if ( defined($options) && $options eq "--testdict" ) { | ||
| 113 | print STDERR "======= checking $term_dict_f =======\n"; | ||
| 114 | &checkdict($term_dict_f,\&tcanonise); | ||
| 115 | print STDERR "======= checking $NE_dict_f =======\n"; | ||
| 116 | &checkdict($NE_dict_f,\&ncanonise); | ||
| 117 | exit(0); | ||
| 118 | } | ||
| 119 | if ( defined($options) ) { | ||
| 120 | if ( $options eq "--verbose" ) { | ||
| 121 | $verbose = 1; | ||
| 122 | } elsif ( $options eq "--testquery" ) { | ||
| 123 | $testquery = 1; | ||
| 124 | } else { | ||
| 125 | print STDERR "Unknown option $options\n"; | ||
| 126 | exit(1); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | $Alvis::QueryFilter::verbose = $verbose; | ||
| 131 | my $QF=Alvis::QueryFilter->new(); | ||
| 132 | if (!defined($QF)) | ||
| 133 | { | ||
| 134 | die("Unable to instantiate QueryFilter."); | ||
| 135 | } | ||
| 136 | |||
| 137 | if ( 0 ) { | ||
| 138 | # cannot figure out how to get HTTP::Daemon | ||
| 139 | # to respond to remote requests, so this server *must* | ||
| 140 | # always be accessed locally | ||
| 141 | open(S,"hostname --fqdn |"); | ||
| 142 | $host = <S>; | ||
| 143 | close(S); | ||
| 144 | if ( ! $host ) { | ||
| 145 | die("Unable to get fully qualified domain name."); | ||
| 146 | } | ||
| 147 | print STDERR "Setting host to $host\n"; | ||
| 148 | } | ||
| 149 | |||
| 150 | $QF->set_text_fields($QF_TEXT); | ||
| 151 | $QF->set_canon(\&tcanonise,\&ncanonise); | ||
| 152 | |||
| 153 | if (!$QF->read_dicts($lemma_dict_f, | ||
| 154 | $term_dict_f, | ||
| 155 | $NE_dict_f, | ||
| 156 | $typing_rules_f, | ||
| 157 | $onto_nodes_f, | ||
| 158 | $onto_mapping_f)) | ||
| 159 | { | ||
| 160 | die("Reading the dictionaries failed."); | ||
| 161 | } | ||
| 162 | |||
| 163 | my $daemon = new HTTP::Daemon | ||
| 164 | agent => "ALVIS Zebra Filter/$VERSION", | ||
| 165 | Proto => "tcp", | ||
| 166 | LocalAddr => 'localhost', | ||
| 167 | # PeerAddr => $host, | ||
| 168 | LocalPort => $QF_PORT; | ||
| 169 | if ( ! $daemon ) { | ||
| 170 | print STDERR "Starting daemon at port=$QF_PORT failed\n"; | ||
| 171 | exit(1); | ||
| 172 | } | ||
| 173 | |||
| 174 | # | ||
| 175 | # UI ----> Zebra | ||
| 176 | # | ||
| 177 | while (my $c = $daemon->accept) { | ||
| 178 | while (my $r = $c->get_request) { | ||
| 179 | if ($r->method eq 'GET' ) { | ||
| 180 | # && $r->url =~ /\?version=1.1\&operation=searchRetrieve\&/ ) { | ||
| 181 | # remember, this is *not* recommened practice :-) | ||
| 182 | # print "URI: " . $r->uri . "\n"; | ||
| 183 | # print "URL: " . $r->url . "\n"; | ||
| 184 | # print "STRING: " . $r->as_string . "\n"; | ||
| 185 | # print "CONTENT: " . $r->content . "\n"; | ||
| 186 | $result = &processSRU($r->url); | ||
| 187 | $c->send_response($result); | ||
| 188 | } else { | ||
| 189 | print STDERR "Bad request: " . $r->url . " \n"; | ||
| 190 | $c->send_error(RC_FORBIDDEN) | ||
| 191 | } | ||
| 192 | } | ||
| 193 | $c->close; | ||
| 194 | undef($c); | ||
| 195 | } | ||
| 196 | print STDERR "SRU query transformer shut down\n"; | ||
| 197 | exit(1); | ||
| 198 | |||
| 199 | sub processSRU() { | ||
| 200 | my $SRU = shift(); | ||
| 201 | |||
| 202 | if ( $verbose ) { | ||
| 203 | print STDERR "SRU: $SRU\n\n"; | ||
| 204 | } | ||
| 205 | |||
| 206 | my $ToZebra=$QF->UI2Zebra($SRU); | ||
| 207 | if (!defined($ToZebra)) { | ||
| 208 | # print STDERR "Expansion failed: ".$QF->errmsg(); | ||
| 209 | $ToZebra = ""; | ||
| 210 | } | ||
| 211 | |||
| 212 | # $ToZebra=~s/%([a-f0-9][a-f0-9])/pack("C", hex($1))/eig; | ||
| 213 | # print STDERR " ----> Zebra: \n\n$actual\n\n$ToZebra\n\n"; | ||
| 214 | |||
| 215 | print STDERR "QueryFilter [" . localtime() | ||
| 216 | . "] URL: http://localhost:$YAZ_PORT$ToZebra\n"; | ||
| 217 | |||
| 218 | my $response; | ||
| 219 | if ( $testquery) { | ||
| 220 | my $content = "<zs:searchRetrieveResponse>\n<zs:version>1.1</zs:version>\n" | ||
| 221 | . " <zs:numberOfRecords/><zs:records><zs:record>\n" | ||
| 222 | . " <zs:recordSchema>D9.1</zs:recordSchema>\n" | ||
| 223 | . " <zs:recordPacking>xml</zs:recordPacking>\n" | ||
| 224 | . " <zs:recordData><document/></zs:recordData>\n" | ||
| 225 | . " <zs:recordPosition>1</zs:recordPosition>\n" | ||
| 226 | . " </zs:record></zs:records>\n" | ||
| 227 | . "</zs:searchRetrieveResponse>\n"; | ||
| 228 | $response = HTTP::Response->new(200, "", undef, $content ); | ||
| 229 | } else { | ||
| 230 | my $ua = LWP::UserAgent->new; | ||
| 231 | $ua->timeout(10); | ||
| 232 | $ua->env_proxy; | ||
| 233 | $response = $ua->get("http://localhost:$YAZ_PORT$ToZebra"); | ||
| 234 | } | ||
| 235 | |||
| 236 | # print STDERR "MSG: " . $response->message ."\n"; | ||
| 237 | |||
| 238 | if ( ! $response->is_success ) { | ||
| 239 | # pass on error return from Zebra | ||
| 240 | return $response; | ||
| 241 | } | ||
| 242 | |||
| 243 | # | ||
| 244 | # Zebra ----> UI | ||
| 245 | # | ||
| 246 | |||
| 247 | if ( ! $QF->Zebra2UI( $response->content_ref ) ) { | ||
| 248 | print STDERR "Unable to insert query for $SRU\n"; | ||
| 249 | } | ||
| 250 | |||
| 251 | if ( $verbose && ${$response->content_ref} =~ | ||
| 252 | /(<zs:extraResponseData>.*?<\/zs:extraResponseData>)/ms) { | ||
| 253 | print STDERR "UI XML:\n$1\n"; | ||
| 254 | } | ||
| 255 | |||
| 256 | return $response; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub readargs() { | ||
| 260 | if ( ! open(A,"<$ALVISHOME/alvis.cnf") ) { | ||
| 261 | print STDERR "No alvis.cnf: $!\n"; | ||
| 262 | exit(1); | ||
| 263 | } | ||
| 264 | while ( ($_=<A>) ) { | ||
| 265 | chomp(); | ||
| 266 | s/#.*//; | ||
| 267 | s/^\s+//; | ||
| 268 | if ( /^QF_TEXT=\s*(\S.*)/ ) { | ||
| 269 | print STDERR "Setting text fields to $1\n"; | ||
| 270 | $QF_TEXT = $1; | ||
| 271 | } | ||
| 272 | if ( /^QF_PORT=\s*([0-9]+)/ ) { | ||
| 273 | $QF_PORT = int($1); | ||
| 274 | print STDERR "Setting filter QF_PORT to $QF_PORT\n"; | ||
| 275 | } | ||
| 276 | if ( /^YAZ_PORT=\s*([0-9]+)/ ) { | ||
| 277 | $YAZ_PORT = int($1); | ||
| 278 | print STDERR "Setting YAZ PORT to $YAZ_PORT\n"; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | close(A); | ||
| 282 | } | ||
| 283 | |||
| 284 | =pod | ||
| 285 | |||
| 286 | =head1 NAME | ||
| 287 | |||
| 288 | run_QF.pl -- simple HTTP server for query filtering of SRU | ||
| 289 | |||
| 290 | =head1 SYNOPSIS | ||
| 291 | |||
| 292 | run_QF.pl [--testdict] [--testquery] [--verbose] <AlvisDir> | ||
| 293 | |||
| 294 | =head1 DESCRIPTION | ||
| 295 | |||
| 296 | B<--testdict> Load up dictionaries, do simple checking, then quit. | ||
| 297 | |||
| 298 | B<--testquery> Transform queries and return response without forwarding query to a real SRU server. | ||
| 299 | |||
| 300 | B<--verbose> Some additional trace data provided. | ||
| 301 | |||
| 302 | This is a simple SRU query filter built using HTTP::Daemon. All configuration data is read from the ALVIS configuration file at <AlvisDir>/alvis.cnf. Error messages and a simple URL trail go to stderr. The linguistic resources used by | ||
| 303 | Alvis::Query filter are located in <AlvisDir>/resources. | ||
| 304 | |||
| 305 | It is intended to be copied and modified for any application. | ||
| 306 | |||
| 307 | =head1 CONFIGURATION | ||
| 308 | |||
| 309 | B<QF_PORT> Port number for this server. | ||
| 310 | |||
| 311 | B<QF_TEXT> Space delimited list of fields that text matches go to. | ||
| 312 | |||
| 313 | B<YAZ_PORT> Port number to forward transformed SRU queries to. | ||
| 314 | |||
| 315 | =head1 DATA | ||
| 316 | |||
| 317 | All resources have one entry per line, and each entry has fields that are tab delimited. Spacing within a field should be standardised to single spaces. The "types" file should be non-existant if named entities are also listed as having ontology nodes. | ||
| 318 | |||
| 319 | <AlvisDir>/resources/lemmas : Lists (text-occurrence,lemma-form) for lemmatising words. | ||
| 320 | |||
| 321 | <AlvisDir>/resources/NEs : Lists (text-occurrence,canonical-form) for matching named entities. | ||
| 322 | |||
| 323 | <AlvisDir>/resources/onto_nodes : Lists (canonical-form,ontology-node) for matching lemmas, terms and named entities that are located in the ontology. | ||
| 324 | |||
| 325 | <AlvisDir>/resources/onto_paths : Lists (ontology-node,ontology-path) giving fully expanded path for each node. | ||
| 326 | |||
| 327 | <AlvisDir>/resources/terms : Lists (text-occurence,canonical-form) for matching terms. | ||
| 328 | |||
| 329 | <AlvisDir>/resources/types : Lists (canonical-form,type) for named entities. Types are short text items (e.g., 'species', 'company', 'person') used to categorise named entities when no ontology is in use. | ||
| 330 | |||
| 331 | Entries in "NEs" and "terms" are applied as rules to query words, with longest match applying first. Once all these are done, the typing or ontology forms are applied. | ||
| 332 | |||
| 333 | Resources are best manipulated and iported/exported as a | ||
| 334 | single XML file using the routines of | ||
| 335 | B<zebractl>(1). | ||
| 336 | |||
| 337 | =head1 SEE ALSO | ||
| 338 | |||
| 339 | B<Alvis::QueryFilter>(3), | ||
| 340 | B<zebractl>(1), | ||
| 341 | B<zebrad>(1), | ||
| 342 | B<HTTP::Daemon>(3). | ||
| 343 | |||
| 344 | See http://www.alvis.info/alvis/Architecture_2fFormats#queryfilter | ||
| 345 | for sample use, the XML formats and the schema. | ||
| 346 | See http://www.alvis.info/alvis/Architecture_2fFormats#filterresources | ||
| 347 | for description of the linguistic resources and an XML Schema. | ||
| 348 | |||
| 349 | =head1 AUTHOR | ||
| 350 | |||
| 351 | Kimmo Valtonen, Wray Buntine | ||
| 352 | |||
| 353 | =head1 COPYRIGHT AND LICENSE | ||
| 354 | |||
| 355 | Copyright (C) 2006 Kimmo Valtonen, Wray Buntine | ||
| 356 | |||
| 357 | This program is free software; you can redistribute it and/or | ||
| 358 | modify it under the terms of the GNU General Public License | ||
| 359 | as published by the Free Software Foundation; either | ||
| 360 | version 2 of the License, or (at your option) any later | ||
| 361 | version. | ||
| 362 | |||
| 363 | This program is distributed in the hope that it will be useful, | ||
| 364 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 365 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 366 | GNU General Public License for more details. | ||
| 367 | |||
| 368 | You should have received a copy of the GNU General Public License | ||
| 369 | along with this program; if not, write to the Free Software | ||
| 370 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 371 | |||
| 372 | =cut |