]> the.earth.li Git - htag.git/blob - example-scripts/10grep
Import Upstream version 0.0.19
[htag.git] / example-scripts / 10grep
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2001 Simon Huggins
4
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 2 of the License, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc., 59
17 # Temple Place, Suite 330, Boston, MA 02111-1307  USA
18
19 use strict;
20
21 my (@tags,%words,%blacklist);
22
23 if (defined $cfg{'grep_debug'} and $cfg{'grep_debug'}) {
24         open(DEBUG, ">$cfg{'grep_debugfile'}");
25 }
26
27 open(HANDLE, "<$cfg{'tagfile'}") or htagdie "Could not open $cfg{'tagfile'}: $!\n";
28 @tags=<HANDLE>;
29 close(HANDLE);
30
31 while(<DATA>) {
32         chomp;
33         s/\s+//g;
34         $blacklist{$_}++;
35 }
36
37 open(MSG, "<$cfg{'msgfile'}");
38 while(<MSG>) {
39         s/[\s\t\n]+/ /g;
40         tr/A-Za-z0-9 //dc; # delete non-alphanumeric
41         s/\b\d+\b//g;
42         $_ = lc $_;
43         my @words = split;
44         foreach (@words) {
45                 next if length($_)>9;
46                 $words{$_}++ if not exists $blacklist{$_};
47         }
48 }
49 close(MSG);
50
51 my @goodtags;
52 my $count=0;
53 foreach my $key (sort { $words{$b} <=> $words{$a} }
54                 keys %words) {
55         print DEBUG "$key occurred $words{$key} times\n" if $cfg{'grep_debug'};
56         my @foundtags = grep { /\b$key\b/i } @tags;
57         push @goodtags,@foundtags; # Tags with more than one matching word will get
58                                    # pushed on more than one time
59         print DEBUG join "\n",@foundtags if $cfg{'grep_debug'};
60         $count++;
61         last if $count >20;
62 }
63
64 open(OUT, ">$cfg{'tmptagfile'}")
65         or htagdie "$0: Could not open $cfg{'tmptagfile'}: $!\n";
66 reg_deletion("$cfg{'tmptagfile'}");
67 if (@goodtags) {
68         print OUT $goodtags[rand(@goodtags)];
69 } else {
70         exit(5);
71 }
72
73 END {
74         close(OUT);
75
76         if ($cfg{'grep_debug'}) {
77                 close(DEBUG);
78         }
79 }
80
81 __DATA__
82
83 about 
84 again 
85 all 
86 am 
87 an 
88 and 
89 another 
90 any 
91 apr
92 are 
93 arent 
94 as 
95 at 
96 aug
97 be 
98 because 
99 been 
100 before 
101 being 
102 but 
103 by 
104 can 
105 cant 
106 cat 
107 could 
108 dec
109 did 
110 do 
111 doesnt 
112 dont 
113 down 
114 ehlo
115 esmtp
116 even 
117 every 
118 feb
119 for 
120 fri
121 from 
122 gmt
123 go
124 great 
125 had
126 hadnt 
127 has 
128 have 
129 he 
130 her
131 here 
132 hers
133 herself 
134 him 
135 himself
136 his 
137 how 
138 however 
139
140 id
141 if 
142 im 
143 in 
144 instead 
145 into 
146 is 
147 it 
148 its 
149 itself 
150 ive 
151 jan
152 jul
153 jun
154 know 
155 like 
156 lots 
157 mar
158 may
159 maybe
160 me
161 might
162 might 
163 mine
164 mon
165 more 
166 must 
167 my 
168 near 
169 need 
170 new 
171 no 
172 not 
173 nov
174 now 
175 oct
176 of 
177 off 
178 oh 
179 on 
180 or 
181 ought
182 ours
183 out 
184 over 
185 please 
186 quite 
187 received
188 said 
189 same 
190 sat
191 seem
192 seemed 
193 seems
194 sep
195 she
196 should
197 should 
198 smtp
199 so 
200 some 
201 such 
202 sun
203 than 
204 that 
205 thats 
206 the 
207 their
208 theirs
209 them
210 then 
211 there 
212 theres 
213 these 
214 they
215 this 
216 thu
217 to 
218 tom
219 too 
220 tue
221 up 
222 us
223 very 
224 want 
225 was 
226 we 
227 wed
228 well 
229 went 
230 were
231 what 
232 when 
233 which 
234 while 
235 who 
236 why 
237 will 
238 with 
239 wont 
240 would
241 would 
242 yes
243 yet 
244 you 
245 your 
246 youre 
247 yours
248 youve