Lemur.pm

filter: Statementstmt 73.0% 92 / 126 Branchbran 75.0% 27 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 78.9% 15 / 19 Podpod 100% 6 / 6 totaltotal 74.8% 140 / 187 CC 21 SCAR 16.6
CC
21
Cov
73%
v
12.0
_makedbs
10.0
m
6.0
CRAP 5.3
« Lemur/Database.pm ↑ Summary Lemur/Result.pm »
1 package Search::Lemur;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Search::Lemur::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Search::Lemur::ResultItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Search::Lemur::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Lemur - class to query a Lemur server, and parse the results
19  
20 =head1 VERSION
21  
22 Version 1.00
23  
24 =cut
25  
26 6 our $VERSION = '1.00';
27  
28 =head1 SYNOPSYS
29  
30 use Search::Lemur;
31  
32 my $lem = Search::Lemur->new("http://url/to/lemur.cgi");
33  
34 # run some queries, and get back an array of results
35 # a query with a single term:
36 my @results1 = $lem->query("encryption");
37 # a query with two terms:
38 my @results2 = $lem->query("encryption MD5");
39  
40 # get corpus term frequency of 'MD5':
41 my $md5ctf = $results2[1]->ctf();
42  
43 =head1 DESCRIPTION
44  
45 This module will make it easy to interact with a Lemur
46 Toolkit for Language Modeling and Information Retrieval
47 server for information retreival exercises. For more
48 information on Lemur, see L<http://www.lemurproject.org/>.
49  
50 This module takes care of all parsing of responses from
51 the server. You can just pass a query as a
52 space-separated list of terms, and the module will give
53 you back an array of C<result> objects.
54  
55 =cut
56  
57  
58 =head2 Main Methods
59  
60 =over 2
61  
62 =item new($url)
63  
64 Create a new Lemur object, connecting to the given Lemur server.
65 The C<$url> should be a full URL, ending in something like 'lemur.cgi'.
66  
67 =cut
68  
69 4 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
70 4 my $class = shift;
71 4 my $url;
72 4 ▶ if (@_) { $url = shift;
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 1 4
73 1 } else { return undef; }
74 3 my $self = { baseurl => $url,
75 db => 0,
76 n => undef,
77 fullurl => undef };
78 3 bless $self, $class;
79 3 $self->{fullurl} = $self->_makeurl();
80 3 return $self;
81 }
82  
83 =item url()
84  
85 Return the URL of the Lemur server
86  
87 =cut
88  
89 7 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod documented
90 7 my $self = shift;
91 7 return $self->{baseurl};
92 }
93  
94 =item listdb()
95  
96 Get some information about the databases available
97  
98 Returns an array of Lemur::Database objects.
99  
100 =cut
101  
102 0 ▶sub listdb {
Subroutine listdb: not called CC=1 SCAR=6.9
Pod documented
103 0 my $self = shift;
104 0 $self->_makeurl();
105 0 my $url = $self->{fullurl} . "&d=?";
106 0 my $result = $self->_strip($url);
107 0 return $self->_makedbs($result);
108 }
109  
110 =item d([num])
111  
112 Set the database number to query. This will specify the
113 database number instead of just using the default databse 0.
114  
115 If the C<num> is not specified, the the current database is returned.
116  
117 =cut
118  
119 1 ▶sub d {
Subroutine d: called CC=2 SCAR=7.0
Pod documented
120 1 my $self = shift;
121 1 ▶ if (@_) { $self->{d} = shift; $self->_makeurl(); }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
122 1 return $self->{d};
123 }
124  
125
126  
127 =item v(string)
128  
129 Make a query to the Lemur server. The query should be a space-delimited
130 list of query terms. If the URL is has not been specified, this will die.
131  
132 Be sure there is only one space between words, or something unexpected may
133 happen.
134  
135 Returns an array of results (See L<Lemur::result>). There will
136 be a result for each query term.
137  
138 =cut
139  
140 # This method really just queries the server, and passes the response on to
141 # &_parse(string). This was done to make testing easier, without having to
142 # query a real server for testing.
143 0 ▶sub v {
Subroutine v: not called CC=3 SCAR=24.8
Pod documented
144 0 my $self = shift;
145 0 my $query = shift;
146 0 $query =~ s/ +/ /g;
147 0 ▶ croak("Something went wrong; I have no URL") unless $self->{baseurl};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"baseurl"} 0 0 0
148 0 my @terms = split(/ +/, $query);
149 0 my $url = $self->{fullurl};
150 0 foreach my $term (@terms) {
151 0 $url = "$url&v=$term";
152 }
153 0 return $self->_parse([$query, $self->_strip($url)]);
154 }
155  
156 =item m(string)
157  
158 Returns the lexicalized (stopped & stemmed) version of the given
159 word. This is affected by weather or not the current database
160 is stemmed and/or stopworded. Basically, this is the real word
161 you will end up searching for.
162  
163 Returns a string.
164  
165 =cut
166  
167 0 ▶sub m {
Subroutine m: not called CC=2 SCAR=17.9
Pod documented
168 0 my $self = shift;
169 0 my $word = shift;
170 0 my $url = $self->{fullurl} . "&m=$word";
171 0 my $return = $self->_strip($url);
172 0 ▶ if ($return eq "[OOV]") { $return = ""; }
Branch: 1 branch
BranchTrueFalseTotal
if ($return eq "[OOV]") 0 0 0
173 0 return $return;
174 }
175  
176 # parse information about available databases into an array of
177 # Search::Lemur::Database objects
178 #
179 # string -> arrayref
180 1 ▶sub _makedbs {
Subroutine _makedbs: called CC=10 SCAR=23.0
181 1 my $self = shift;
182 1 my $input = shift;
183 1 my @input = split(/\n/, $input);
184 1 my @return;
185 1 my ($num, $title, $stop, $stem, $numdocs,
186 $numterms, $numuniq, $avgdoclen);
187 1 ▶ while (scalar(@input) >= 1){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @input >= 1) 24 1 25
188 24 my $line = shift(@input);
189 24 ▶ if ($line =~ m/(\d*): ([\w|\d|\s]*) (NOSTOP|STOP) (NOSTEMM|STEMM);/){
Branch: 6 branches
BranchTrueFalseTotal
if ($line =~ /(\d*): ([\w|\d|\s]*) (NOSTOP|STOP) (NOSTEMM|STEMM);/) { } 4 20 24
elsif ($line =~ / NUM_DOCS = ?(\d*);/) { } 4 16 20
elsif ($line =~ / NUM_UNIQUE_TERMS = ?(\d*);/) { } 4 12 16
elsif ($line =~ / NUM_TERMS = ?(\d*);/) { } 4 8 12
elsif ($line =~ / AVE_DOCLEN = ?(\d*);/) { } 4 4 8
elsif ($line =~ /<BR>/) { } 4 0 4
190 4 $num = $1;
191 4 $title = $2;
192 4 ▶ $stop = ($3 eq "STOP") ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq 'STOP' ? : 2 2 4
193 4 ▶ $stem = ($4 eq "STEMM") ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$4 eq 'STEMM' ? : 2 2 4
194 } elsif ($line =~ m/ NUM_DOCS = ?(\d*);/){
195 4 $numdocs = $1;
196 } elsif ($line =~ m/ NUM_UNIQUE_TERMS = ?(\d*);/){
197 4 $numuniq = $1;
198 } elsif ($line =~ m/ NUM_TERMS = ?(\d*);/){
199 4 $numterms = $1;
200 } elsif ($line =~ m/ AVE_DOCLEN = ?(\d*);/){
201 4 $avgdoclen = $1;
202 } elsif ($line =~ m/<BR>/){
203 4 my $db = Search::Lemur::Database->_new($num, $title, $stop,
204 $stem, $numdocs, $numterms, $numuniq, $avgdoclen);
205 4 push @return, $db;
206 }
207 }
208 1 return \@return;
209 }
210  
211 # parse the result from the server
212 #
213 # Takes a reference to an array with two items:
214 # - a string containing the query terms, separated by spaces
215 # - a string containing the response
216 #
217 # returns array of results
218 2 ▶sub _parse {
Subroutine _parse: called CC=4 SCAR=13.9
219 2 my $self = shift;
220 2 my $inputref = shift;
221 2 my @input = @$inputref;
222 2 my @terms = split(/ /, $input[0]);
223 # print Dumper($input[1]);
224 2 my @response = split(/\D+/, $input[1]);
225 2 ▶ shift(@response) if ($response[0] eq ""); #TODO Why am I doing this? this makes tests fail.
Branch: 1 branch
BranchTrueFalseTotal
if $response[0] eq "" 0 2 2
226 2 my $numterms = scalar(@terms);
227  
228 2 my @return;
229
230 # build a result object for each term
231 2 foreach my $term (@terms) {
232 # print Dumper(@response);
233 2 my $ctf = shift(@response);
234 2 my $df = shift(@response);
235 2 my $result = Search::Lemur::Result->_new($term, $ctf, $df);
236 # build a resultItem object for each document
237 2 ▶ for (my $i = 0; $i < $df; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $df) 3 2 5
238 3 my $docid = shift(@response);
239 3 my $doclen = shift(@response);
240 3 my $tf = shift(@response);
241 3 my $resultItem = Search::Lemur::ResultItem->_new($docid, $doclen, $tf);
242 3 $result->_add($resultItem);
243 }
244 2 push(@return, $result);
245 }
246  
247 2 return \@return;
248 }
249  
250 # build the full url to use for all queries
251 # This url consists of the base url (ending in lemur.cgi) plus
252 # d=n (specifies the database) and n=x (the number of results
253 # to return. If either of these are undef, then they are left
254 # off, and the server is free to use its defaults
255 #
256 # the n value seems to only affect the q= query, and not the
257 # inverted list v= query.
258 #
259 # returns a string, and updates the fullurl instance variable
260 7 ▶sub _makeurl {
Subroutine _makeurl: called CC=3 SCAR=11.0
261 7 my $self = shift;
262 7 my $return = $self->url() . "?g=p";
263 7 ▶ if ($self->{d}) { $return = $return . "&d=$self->{d}"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"d"}) 3 4 7
264 7 ▶ if ($self->{n}) { $return = $return . "&n=$self->{n}"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"n"}) 1 6 7
265 7 $self->{fullurl} = $return;
266 7 return $return;
267 }
268
269 # strip_: make a request to the server, and strip out anything
270 # useless
271 #
272 # This will get the result from the server, and strip put any
273 # html, etc that is not useful to the parser.
274 #
275 # string -> string
276 #
277 # takes in a url argument to fetch, and returns the stripped
278 # result.
279 0 ▶sub _strip {
Subroutine _strip: not called CC=2 SCAR=17.9
280 0 my $self = shift;
281 0 my $url = shift;
282 # print "$url\n\n";
283 0 my $ua = LWP::UserAgent->new;
284 0 $ua->agent("Lemur.pm/$VERSION");
285 0 my $req = HTTP::Request->new(GET => $url);
286 0 $req->content_type('application/x-www-form-urlencoded');
287 0 $req->content('query=libwww-perl&mode=dist');
288 # make request
289 0 my $res = $ua->request($req);
290  
291 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
292 0 $res->content() =~ m/.*<BODY>\n\n((\s|\d|\n|\w|\[|\]|:|;|=|<|>)*?)\n<HR>/;
293 # print $1 . "\n\n";
294 0 return $1;
295 }
296 else {
297 0 Carp::carp($res->status_line, "\n");
298 0 return undef;
299 }
300 }
301  
302  
303  
304  
305 =back
306  
307 =head1 AUTHOR
308  
309 Patrick Kaeding, C<< <pkaeding at cpan.org> >>
310  
311 =head1 BUGS
312  
313 Please report any bugs or feature requests to
314 C<bug-search-lemur at rt.cpan.org>, or through the web interface at
315 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Search-Lemur>.
316 I will be notified, and then you'll automatically be notified of progress on
317 your bug as I make changes.
318  
319 =head1 SUPPORT
320  
321 You can find documentation for this module with the perldoc command.
322  
323 perldoc Search::Lemur
324  
325 You can also look for information at:
326  
327 =over 4
328  
329 =item * AnnoCPAN: Annotated CPAN documentation
330  
331 L<http://annocpan.org/dist/Search-Lemur>
332  
333 =item * CPAN Ratings
334  
335 L<http://cpanratings.perl.org/d/Search-Lemur>
336  
337 =item * RT: CPAN's request tracker
338  
339 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Search-Lemur>
340  
341 =item * Search CPAN
342  
343 L<http://search.cpan.org/dist/Search-Lemur>
344  
345 =back
346  
347 =head1 ACKNOWLEDGEMENTS
348  
349 =head1 COPYRIGHT & LICENSE
350  
351 Copyright 2007 Patrick Kaeding, all rights reserved.
352  
353 This program is free software; you can redistribute it and/or modify it
354 under the same terms as Perl itself.
355  
356 =cut
357  
358 6 1; # End of Search::Lemur
« Lemur/Database.pm ↑ Summary Lemur/Result.pm »