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
|
#!/usr/bin/perl
use lib "../lib";
use Bucky;
use Data::Dumper;
use DB_File;
my $bucky = new Bucky(); # "carbon");
my %index;
tie %index, "DB_File", "gross.db", O_CREAT|O_RDWR, 0666, $DB_HASH ;
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 = {};
my $terms = parse_terms($string);
foreach my $term (@$terms)
{
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}++;
}
}
my $total = 0;
my $results = [];
foreach my $obj (sort {
scalar(keys %{$scores->{$b}->{terms}}) <=> scalar(keys %{$scores->{$a}->{terms}}) ||
$scores->{$b}->{count} <=> $scores->{$a}->{count} } keys %$scores )
{
my $score = score_display( $scores->{$obj} );
push @$results, { score => $score, result => $obj } if $total < 10;
$total++;
}
return
{
total => $total,
results => $results,
};
}
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 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);
}
|