/[bfilter]/trunk/bfilter.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/bfilter.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations)
Fri Sep 10 12:16:21 2004 UTC (19 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 1930 byte(s)
new object-oriented implementation contributed by Matko Adjelinic,
lot more debug output (which goes to debug div), configurable index
name which is argument to constructor

1 #!/usr/bin/perl -w
2 #
3
4 use strict;
5 use locale;
6
7 # maximum entries
8 my $max = 0;
9 # minimum letters to search by
10 my $min_len = 3;
11 # if more than x elements, warn to increase min_len
12 my $increase_at = 500;
13
14 # name of generated index
15 my $headlines = 'headlines';
16
17 my $debug = 1;
18
19 sub print_file {
20 my $f = shift || return;
21 open(F, $f) || die "$f: $!";
22 while(<F>) {
23 print;
24 }
25 close(F);
26 }
27
28 print qq{
29 var $headlines = new Object();
30 };
31
32 my @part_arr;
33 my $last_part = '';
34 my $total = 0;
35
36 my $max_elements = 0;
37
38 while(<STDIN>) {
39 chomp;
40
41 if (!m/\t/ || m/\t$/) {
42 print STDERR "SKIP '$_': no tab\n";
43 next;
44 }
45
46 my ($path,$headline) = split(/\t+/,$_,2);
47
48 if (length($headline) < $min_len) {
49 print STDERR "SKIP '$_': too short\n";
50 next;
51 }
52
53
54 # split into min_len part and rest
55 my ($part,$rest) = ( substr($headline,0,$min_len), substr($headline,$min_len) );
56
57 # escape special chars
58 $part =~ s/(['\\])/\\$1/g && print STDERR "ESCAPED part '$part'\n";
59 $rest =~ s/(['\\])/\\$1/g && print STDERR "ESCAPED rest '$rest'\n";
60 $headline =~ s/(['\\])/\\$1/g;
61
62 # make part lowercase
63 $part = lc($part);
64
65 $last_part = $part if (! $last_part);
66
67 # new part?
68 if ($part ne $last_part) {
69 print STDERR $last_part,"\t",$#part_arr+1,"\n" if ($debug && $#part_arr > $increase_at);
70 $max_elements = $#part_arr if ($#part_arr > $max_elements);
71 print "${headlines}['$last_part'] = [\n ",join(",\n ",@part_arr),"];\n" if (@part_arr);
72 $total += $#part_arr;
73 @part_arr = ();
74 $last_part = $part;
75 }
76 push @part_arr, "['$path','$headline']";
77
78 # break out?
79 last if ($max && $total > $max);
80 }
81
82 print "${headlines}['$last_part'] = [\n ",join(",\n ",@part_arr),"];\n" if (@part_arr);
83 print qq{
84
85 ${headlines}.min_len = $min_len;
86 ${headlines}.length = $total;
87
88 };
89
90 print STDERR "You have more than $increase_at elements, so you should\nincrease min_len to ",$min_len+1," or higher for performance benefit.\n" if ($max_elements > $increase_at);

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26