summaryrefslogtreecommitdiff
path: root/bucky2/lib/Bucky/Search.pm
blob: 43c23f7defd608d793a547d9702aba1112ec6257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package Bucky::Search;

use base 'Bucky';

use Data::Dumper;
use DB_File;

sub index
	{
	my ($self, $index) = @_;
	$self->{'_index'} = $index || $self->index_read;
	return $self->{'_index'};
	}
sub index_read
	{
	my ($self) = @_;
	my %index;
	tie %index, "DB_File", $self->index_filename, O_RDONLY, 0666, $DB_HASH ;
	return \%index;
	}
sub index_write
	{
	my ($self) = @_;
	my %index;
	tie %index, "DB_File", $self->index_filename, O_CREAT|O_RDWR, 0666, $DB_HASH ;
	return \%index;
	}
sub index_close
	{
	my ($self, $index) = @_;
	$index ||= $self->index;
	untie %$index;
	}
sub index_filename
	{ "gross.db" }
sub auto_index
	{
	my ($self, $auto_index) = @_;
	$self->{'_auto_index'} = $auto_index || $self->auto_index_read;
	return $self->{'_auto_index'};
	}
sub auto_index_read
	{
	my ($self) = @_;
	my %auto_index;
	tie %auto_index, "DB_File", $self->auto_index_filename, O_RDONLY, 0666, $DB_HASH ;
	return \%auto_index;
	}
sub auto_index_write
	{
	my ($self) = @_;
	my %auto_index;
	tie %auto_index, "DB_File", $self->auto_index_filename, O_CREAT|O_RDWR, 0666, $DB_HASH ;
	return \%auto_index;
	}
sub auto_index_close
	{
	my ($self, $auto_index) = @_;
	$auto_index ||= $self->auto_index;
	untie %$auto_index;
	}
sub auto_index_filename
	{ "auto.db" }
sub lexicon_store
	{
	my ($self, $lexicon) = @_;
	my $index = $self->index_write;
	foreach my $term (keys %$lexicon)
		{
		next if $self->is_stopword($term);
		my $serialized = $self->serialize_matches($lexicon->{$term});
		next unless $serialized;
		$index->{$term} = $serialized;
		}
	$self->index_close($index);
	}
sub unserialize_matches
	{
	my ($self, $serialized_string) = @_;
	my @serialized_matches = split ",", $serialized_string;
	my @matches;
	foreach my $serialized_match (@serialized_matches)
		{
		my ($thread, $comment, $file, $strength) = split " ", $serialized_match;
		my $match = {};
		$match->{'thread'} = $thread;
		$match->{'comment'} = $comment;
		$match->{'file'} = $file;
		$match->{'strength'} = $strength;
		push @matches, $match;
		}
	return \@matches;
	}
sub serialize_matches
	{
	my ($self, $matches) = @_;
	my @serialized_matches;
	foreach my $match (values %$matches)
		{
		next unless $match && ref($match) eq "HASH";
		my $string = join " ",
			$match->{'thread'},
			$match->{'comment'},
			$match->{'file'},
			$match->{'strength'};
		next unless $string;
		push @serialized_matches, $string if $string;
		}
	return undef unless scalar @serialized_matches;
	return join ",", @serialized_matches;
	}
my $STOPWORDS = {( map { lc $_, 1 } qw(
a about above across adj after again against all almost alone along also 
although always am among an and another any anybody anyone anything anywhere 
apart are around as aside at away be because been before behind being below 
besides between beyond both but by can cannot could deep did do does doing done 
down downwards during each either else enough etc even ever every everybody 
everyone except far few for forth from get gets got had hardly has have having 
her here herself him himself his how however i if in indeed instead into inward 
is it its itself just kept many maybe might mine more most mostly much must 
myself near neither next no nobody none nor not nothing nowhere of off often on 
only onto or other others ought our ours out outside over own p per please plus 
pp quite rather really said seem self selves several shall she should since so 
some somebody somewhat still such than that the their theirs them themselves 
then there therefore these they this thorough thoroughly those through thus to 
together too toward towards under until up upon v very was well were what 
whatever when whenever where whether which while who whom whose will with
within without would yet young your yourself s ) )};
sub is_stopword
	{
	my ($self, $term) = @_;
	return exists $STOPWORDS->{lc $term};
	}
sub autocomplete
	{
	my ($self, $query) = @_;
	return unless $query;
	my $terms = parse_terms($query);
	my $last_term = pop @$terms;

	my $auto_index = $self->auto_index;
	my $guess_term = $auto_index->{$last_term};
	my $guess_full = join " ", @$terms, $guess_term;
	my $guess_tail = $guess_term;
	$guess_tail =~ s/^$last_term//;

	my $guess = {};
	$guess->{'full'} = $guess_full;
	$guess->{'tail'} = $guess_tail;
	$guess->{'term'} = $guess_term;
	return $guess;
	}
sub search_light
	{
	my ($self, $query, $start, $limit) = @_;
	return unless $query;
	$start ||= 0;
	$limit ||= 10;
	my $scores = {};
	my $terms = parse_terms($query);
	my $index = $self->index;
	foreach my $term (@$terms)
		{
		next if $self->is_stopword($term);
		next unless my $serial = $index->{$term};
		my $results = $self->unserialize_matches($serial);
		foreach my $result (@$results)
			{
			my $thread = $result->{'thread'};
			$scores->{$thread} ||= {};
			$scores->{$thread}->{thread} ||= $result->{'thread'};
			$scores->{$thread}->{file} ||= $result->{'file'};
			$scores->{$thread}->{strength} += $result->{'strength'};
			$scores->{$thread}->{count}++;
			}
		}
	my $total = scalar keys %$scores;
	my $i = 0;
	my $to_display = $limit;
	my $threads = {};
	my $comments_to_get = [];
	my $files_to_get = [];
	foreach my $match (sort { $b->{count} <=> $a->{count} || $b->{strength} * $b->{count} <=> $a->{strength} * $a->{count} } values %$scores )
		{
		next if $i++ < $start;
		my $thread = $self->thread( $match->{'thread'} );
		next unless $thread;
		# next if $thread->{'private'};
		last if $to_display-- == 0;
        push @$results, $match;
		push @$comments_to_get, $match->{'comment'} if $match->{'comment'};
		if ( $match->{'file'} )
			{ push @$files_to_get, $match->{'file'}; }
		if ( $thread->{'flagged'} )
			{ push @$files_to_get, $thread->{'flagged'}; }
		$threads->{ $thread->{'id'} } = $thread;
        }
	my $files = $self->files_by_id($files_to_get);
	# $self->log_query($query, $total);
    return
        {
		start => $start + $limit,
		limit => $limit,
        total => $total,
        results => $results,
		threads => $threads,
		files => $files,
		terms => $terms,
        };
	}
sub search
	{
	my ($self, $query, $start, $limit) = @_;
	return unless $query;
	$start ||= 0;
	$limit ||= 10;
	my $scores = {};
	my $terms = parse_terms($query);
	my $index = $self->index;
	foreach my $term (@$terms)
		{
		next if $self->is_stopword($term);
		next unless my $serial = $index->{$term};
		my $results = $self->unserialize_matches($serial);
		foreach my $result (@$results)
			{
			my $thread = $result->{'thread'};
			$scores->{$thread} ||= {};
			$scores->{$thread}->{thread} ||= $result->{'thread'};
			$scores->{$thread}->{comment} ||= $result->{'comment'};
			$scores->{$thread}->{file} ||= $result->{'file'};
			$scores->{$thread}->{strength} += $result->{'strength'};
			$scores->{$thread}->{count}++;
			}
		}
	my $total = scalar keys %$scores;
	my $i = 0;
	my $to_display = $limit;
	my $threads = {};
	my $comments_to_get = [];
	my $files_to_get = [];
	foreach my $match (sort { $b->{count} <=> $a->{count} || $b->{strength} * $b->{count} <=> $a->{strength} * $a->{count} } values %$scores )
		{
		next if $i++ < $start;
		my $thread = $self->thread( $match->{'thread'} );
		next unless $thread;
		next if $thread->{'private'};
		last if $to_display-- == 0;
        push @$results, $match;
		push @$comments_to_get, $match->{'comment'} if $match->{'comment'};
		if ( $match->{'file'} )
			{ push @$files_to_get, $match->{'file'}; }
		if ( $thread->{'flagged'} )
			{ push @$files_to_get, $thread->{'flagged'}; }
		$threads->{ $thread->{'id'} } = $thread;
        }
	my $files = $self->files_by_id($files_to_get);
	my $comments = $self->comments_by_id($comments_to_get);
	$self->log_query($query, $total);
    return
        {
		start => $start + $limit,
		limit => $limit,
        total => $total,
        results => $results,
		threads => $threads,
		comments => $comments,
		files => $files,
		terms => $terms,
        };
	}
sub score_display
    {
    my ($obj) = @_;
    return scalar(keys %{$obj->{terms}}) . "x" . $obj->{count};
    }
sub display_object
	{
	my ($obj) = @_;
	my ($type, $id) = split ":", $obj;
	my $thread = $bucky->thread($id);
	my $title = $thread ? $thread->_title : "* * *";
	return $type . " " . $id . "\t" . $title;
	}
sub bold_terms
	{
	my ($self, $string, $terms) = @_;
	$string = $self->strip_html($string);
	foreach my $term (@$terms)
		{
		$string =~ s/\b($term)\b/<b>$1<\/b>/gi;
		}
	return $string;
	}
sub bold_snippet
	{
	my ($self, $string, $terms) = @_;
	my $snippet = $self->snippet($string, $terms);
	return $self->bold_terms($snippet, $terms);
	}
sub snippet
	{
	my ($self, $string, $terms) = @_;

	# clean up the string we got
	$string = $self->strip_html($string);

	# create a regex out of the search terms
	my $term_re = join "|", @$terms;

	# take the string to be snippetized and split it into words
	my @words = split /\s+/, $string;

	# deduper for matching @words indexes, so we don't add a word twice
	my $index_matches = {};

	# words in the eventual snippet
	my @words_matched;

	# the snippet itself
	my $snippet = '';

	# counter for aggregating context after a match
	my $aggr = 0;

	# amount of context to show, in number of words surrounding a match
	my $pad = 4;

	# loop over each of the words in the string
	for (my $i = 0; $i < scalar @words; $i++)
		{
		# does this word contain a match?
		if ($words[$i] =~ /\b($term_re)\b/i && ! $self->is_stopword($1))
			{
			# if we aren't already aggregating, add an ellipsis
			if (! $aggr)
				{
				push @words_matched, "...";
				}
			# look backward $pad words
			for (my $j = -$pad; $j < 1; $j++)
				{
				# create a new index from the offset
				my $idx = $i + $j;

				# is this a valid index? has it already been encountered?
				next if $idx < 0;
				next if $idx > scalar @words;
				next if exists $index_matches->{$i+$j};

				# checks out, save this word
				push @words_matched, $words[$i+$j];

				# note the matching index in our deduper
				$index_matches->{$i+$j} ++;
				}
			# enter aggregate mode -- add the next $pad words
			$aggr = $pad;
			}
		# have we been told to aggregate?
		elsif ($aggr)
			{
			# save this word
			push @words_matched, $words[$i];

			# add index to the deduper
			$index_matches->{$i} ++;

			# one less word to aggregate
			$aggr--;
			}
		# keep snippets to a modest length
		last if scalar @words_matched > 30;
		}
	# add a trailing ellipsis
	push @words_matched, "...";

	# create the snippet from the saved context words
	$snippet = join " ", @words_matched;

	return $snippet;
	}
sub parse_terms
	{
	my ($s) = @_;
	my @terms = split /(\W+)/, lc($s);
	my $words = [];
	my $count = 0;
	foreach my $term (@terms)
		{
		if ( $term !~ /\W/ )
			{
			push @$words, $term;
			}
		}
	return $words;
	}
sub log
	{
	my ($self, $date) = @_;
	my $criteria = {};
	$criteria->{'date'} = $date if $date;
	return $self->db->select("search_log", $criteria);
	}
sub log_query
	{
	my ($self, $query, $total) = @_;
	return unless $query;
	my $date = time;
	$matches ||= '0';
	$self->db->insert("search_log", { query => $query, date => $date, matches => $total });
	}
1;