diff options
| author | Jules Laplace <carbon@melanarchy.org> | 2013-08-02 17:23:25 -0500 |
|---|---|---|
| committer | Jules Laplace <carbon@melanarchy.org> | 2013-08-02 17:23:25 -0500 |
| commit | e76b691e78e273226cba9284cb8cd22a423319ed (patch) | |
| tree | a58d22f69869fe2bf3885f81bdda4952f87ff6d7 /bucky2/t/autocomplete.pl | |
| parent | 753f60c7d4769fa72d3b910e491f37db6f130898 (diff) | |
bucky2
Diffstat (limited to 'bucky2/t/autocomplete.pl')
| -rwxr-xr-x | bucky2/t/autocomplete.pl | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/bucky2/t/autocomplete.pl b/bucky2/t/autocomplete.pl new file mode 100755 index 0000000..19ece3b --- /dev/null +++ b/bucky2/t/autocomplete.pl @@ -0,0 +1,177 @@ +#!/usr/bin/perl +use lib "../lib"; +use Bucky; +use Bucky::Search; +use Data::Dumper; +use DB_File; + +my $search = new Bucky::Search; + +my $index = $search->index; +my $auto_index = $search->auto_index_write; + +my $partials = {}; +my $partials_with_media = {}; +my $med_count = 0; +my $much_count = 0; +foreach my $word (keys %$index) + { + # goatse operator + my $count =()= $index->{$word} =~ /,/g; + + next unless $word; + + my $par = ''; + my @letters = split "", $word; + for (my $i = 0; $i < scalar @letters; $i++) + { + $par .= $letters[$i]; + if (! exists $partials->{$par} || $partials->{$par}->[0] < $count) + { + $partials->{$par} = [$count,$word]; + } + } + } + +foreach my $par (sort keys %$partials) + { + $auto_index->{$par} = $partials->{$par}->[1]; + } +exit; + + +# event_loop(); + +sub event_loop + { + my $command = "index"; + while (1) + { + print "> "; + $command = line_in(); + last if $command eq "exit"; + search(\%index, $command) + } + } +sub search + { + my ($index, $string) = @_; + my $scores = {}; + print "TERMS : "; + my $terms = parse_terms($string); + foreach my $term (@$terms) + { + print "$term "; + next unless my $match = $index->{$term}; + my @results = split " ", $match; + foreach my $result (@results) + { + $scores->{$result} ||= {}; + $scores->{$result}->{count}++; + $scores->{$result}->{terms} ||= {}; + $scores->{$result}->{terms}->{$term}++; + } + } + print "\n"; + my $total = 0; + foreach my $obj (sort { + scalar(keys %{$scores->{$b}->{terms}}) <=> scalar(keys %{$scores->{$a}->{terms}}) || + $scores->{$b}->{count} <=> $scores->{$a}->{count} } keys %$scores ) + { + print scalar(keys %{$scores->{$obj}->{terms}}) . "x" . $scores->{$obj}->{count} . "\t" . display_object($obj) . "\n" if $total < 10; + $total++; + } + print "$total total\n"; + } +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 parse_terms + { + my ($s) = @_; + my @terms = split /(\W+)/, $s; + my $words = []; + my $count = 0; + foreach my $term (@terms) + { + if ( $term !~ /\W/ ) + { + push @$words, $term; + } + } + return $words; + } + +sub show_index + { + my ($min) = @_; + my $max = $min + 24; + my $i = 0; + foreach my $thread (@$threads) + { + next if $min > $i++; + last if $max == $i; + $commands->{$i} = [ \&show_thread, $thread ]; + printf " T%02d %-70s\n", $i, $thread->{"title"}; + } + return $max - 1; + } +sub show_thread + { + my ($thread) = @_; + print $thread->{'title'} . "\n"; + print $thread->{'username'} . " // " . show_date($thread->{createdate}) . "\n"; + my $i = 0; + foreach my $comment (@$comments) + { + next if ($comment->{'thread'} != $thread->{'id'}); + $commands->{++$i} = [ \&show_comment, $comment ]; + printf " c%02d %-10s %-60s\n", $i, $comment->{"username"}, first_line($comment->{"comment"}); + } + foreach my $file (@$files) + { + next if ($file->{'thread'} != $thread->{'id'}); + printf " f%02d %-10s %-60s\n", ++$i, $file->{'username'}, $file->{'filename'}; + } + return 1; + } +sub show_comment + { + my ($comment) = @_; + print $comment->{'username'} . " // " . show_date($comment->{date}) . "\n"; + my @lines = split "\n", $comment->{'comment'}; + for (my $i = 0; $i < scalar(@lines); $i++) + { + print $lines[$i] . "\n"; + last if $i == 24; + } + return 1; + } +sub show_date + { + return scalar localtime(shift); + } +sub line_in + { + return trim(scalar <STDIN>); + } +sub trim + { + my $s = shift; + $s =~ s/^\s+//; + $s =~ s/\s+$//; + return $s; + } +sub first_line + { + my $s = shift; + $s =~ s/^\s+//; + $s =~ s/\n//g; + return substr($s,0,70); + } + |
