| 1 | #!/var/local/gna/bin/perl5 | ||
| 2 | |||
| 3 | |||
| 4 | =pod | ||
| 5 | These are various procedures to guess topics | ||
| 6 | |||
| 7 | Use refine_topics1 | ||
| 8 | =cut | ||
| 9 | |||
| 10 | my(@exclude_words) = | ||
| 11 | ("the", "and", "to", "of", "a", "an","for", "what", "are", "in", "our"); | ||
| 12 | |||
| 13 | my($remap_table) = { | ||
| 14 | "masters"=>"master", | ||
| 15 | "bachelors"=>"bachelor" | ||
| 16 | }; | ||
| 17 | |||
| 18 | my($exclude_table) = { | ||
| 19 | "master"=>["science", "arts"], | ||
| 20 | "bachelor"=>["science", "arts"], | ||
| 21 | "associate"=>["science", "arts"] | ||
| 22 | }; | ||
| 23 | |||
| 24 | require 'edit-catalog.pl'; | ||
| 25 | use strict; | ||
| 26 | |||
| 27 | my($table) = $::catalog_file; | ||
| 28 | my($table1) = $::tabledir . "/wordtable1.rdb"; | ||
| 29 | my($topic_list) = $::tabledir . "/topics.rdb"; | ||
| 30 | |||
| 31 | sub use_bayes { | ||
| 32 | my($title, $restrict) = @_; | ||
| 33 | my(%prob) = (); | ||
| 34 | my($word, $exclude_word, $topic, $score); | ||
| 35 | |||
| 36 | topicloop: | ||
| 37 | foreach $word (split(/\s+/, $title)) { | ||
| 38 | $word =~ tr/A-Z/a-z/; | ||
| 39 | #print "Looking for $word\n"; | ||
| 40 | foreach $exclude_word (@exclude_words) { | ||
| 41 | if ($word eq $exclude_word) { | ||
| 42 | # print " Ignoring\n"; | ||
| 43 | next topicloop; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | my(%transfer_prob) = (); | ||
| 47 | open (DATA, "echo '$word' | search -mi conting.rdb word |"); | ||
| 48 | $_ = <DATA>; | ||
| 49 | $_ = <DATA>; | ||
| 50 | while (<DATA>) { | ||
| 51 | ($topic, $word, $score) = split(/\t/, $_); | ||
| 52 | $transfer_prob{$topic} = $score; | ||
| 53 | } | ||
| 54 | close(DATA); | ||
| 55 | |||
| 56 | my(%new_prob) = (); | ||
| 57 | foreach $word (keys %transfer_prob) { | ||
| 58 | if ($prob{$word} ne "") { | ||
| 59 | $new_prob{$word} = $prob{$word} * $transfer_prob{$word}; | ||
| 60 | } else { | ||
| 61 | $new_prob{$word} = 0.01 * $transfer_prob{$word}; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | foreach $word (keys %prob) { | ||
| 66 | if ($new_prob{$word} eq "") { | ||
| 67 | $new_prob{$word} = 0.01 * $prob{$word}; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | if ($restrict ne "") { | ||
| 72 | foreach $word (keys %prob) { | ||
| 73 | if ($word !~ /^$restrict/) { | ||
| 74 | delete $new_prob{$word}; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | # Normalize | ||
| 80 | my($norm) =0.0; | ||
| 81 | foreach $word (keys %new_prob) { | ||
| 82 | $norm += $new_prob{$word}; | ||
| 83 | } | ||
| 84 | |||
| 85 | foreach $word (keys %new_prob) { | ||
| 86 | $new_prob{$word} /= $norm; | ||
| 87 | } | ||
| 88 | |||
| 89 | %prob = %new_prob; | ||
| 90 | |||
| 91 | #print "Probablity vector table\n---------------------\n"; | ||
| 92 | #foreach $word (keys %new_prob) { | ||
| 93 | #print "$word\t$prob{$word}\n"; | ||
| 94 | #} | ||
| 95 | } | ||
| 96 | return %prob; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub find_top { | ||
| 100 | my ($probref, $number) = @_; | ||
| 101 | |||
| 102 | my(@topic_list); | ||
| 103 | my($I, $J); | ||
| 104 | if ($number eq "") { | ||
| 105 | $number = 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | my($word); | ||
| 109 | wordloop: | ||
| 110 | foreach $word (keys (%{$probref})) { | ||
| 111 | my(@list) = split(/;/, $word); | ||
| 112 | my($words) = $#list; | ||
| 113 | my($i); | ||
| 114 | for ($i=0; $i < $number; $i++) { | ||
| 115 | my(@list_count) = split(/;/, $topic_list[$i]); | ||
| 116 | my($topic_count) = $#list_count; | ||
| 117 | my($j); | ||
| 118 | |||
| 119 | if ($topic_list[$i] eq "" || | ||
| 120 | $probref->{$word} > $probref->{$topic_list[$i]} || | ||
| 121 | ($probref->{$word} == $probref->{$topic_list[$i]} | ||
| 122 | && | ||
| 123 | $words > $topic_count)) { | ||
| 124 | for ($j=$number-1; $j > $i; $j--) { | ||
| 125 | $topic_list[$j] = $topic_list[$j-1]; | ||
| 126 | } | ||
| 127 | $topic_list[$i] = $word; | ||
| 128 | next wordloop; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | return @topic_list; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub refine_topic { | ||
| 136 | my($title, $listref) = @_; | ||
| 137 | local($_, *FILE); | ||
| 138 | my(%array) = (); | ||
| 139 | my(@title_words) = split(/\s+/, $title); | ||
| 140 | my($check_topic); | ||
| 141 | |||
| 142 | foreach $check_topic (@{$listref}) { | ||
| 143 | open(FILE, "echo '$check_topic' | search -mi -x $table topic | column title topic | headoff |"); | ||
| 144 | while (<FILE>) { | ||
| 145 | my($test, $topic) = split(/[\t\n]/); | ||
| 146 | $test =~ y/A-Z/a-z/; | ||
| 147 | my(@test_words) = split(/\s+/, $test); | ||
| 148 | |||
| 149 | my(%bucket) = (); | ||
| 150 | my(%title_words) = (); | ||
| 151 | my(%test_words) = (); | ||
| 152 | my($match_count) = 0; | ||
| 153 | my($total_word_count) = 0; | ||
| 154 | my($word); | ||
| 155 | foreach $word (@title_words) { | ||
| 156 | $bucket{$word} = 1; | ||
| 157 | $title_words{$word} = 1; | ||
| 158 | } | ||
| 159 | foreach $word (@test_words) { | ||
| 160 | $bucket{$word} = 1; | ||
| 161 | $test_words{$word} = 1; | ||
| 162 | } | ||
| 163 | my(%exclude_words); | ||
| 164 | foreach $word (keys %bucket) { | ||
| 165 | if (! $exclude_words{$word}) { | ||
| 166 | if ($title_words{$word} == 1 | ||
| 167 | && $test_words{$word} == 1) { | ||
| 168 | $match_count ++; | ||
| 169 | } | ||
| 170 | $total_word_count++; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | my($scale_factor) = $match_count / $total_word_count; | ||
| 174 | if ($scale_factor > $array{$topic}) { | ||
| 175 | $array{$topic} = $scale_factor; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | close(FILE); | ||
| 179 | } | ||
| 180 | |||
| 181 | return (%array); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub refine_topic1 { | ||
| 185 | my($title, $listref) = @_; | ||
| 186 | local($_, *FILE); | ||
| 187 | my(%array) = (); | ||
| 188 | my($cache_file) = "/tmp/topic.cache.$$"; | ||
| 189 | |||
| 190 | my($title_words) = &tokenize_title($title, $remap_table); | ||
| 191 | my($exclude_words) = | ||
| 192 | &get_exclude_list($title_words, | ||
| 193 | \@exclude_words, | ||
| 194 | $exclude_table); | ||
| 195 | |||
| 196 | my($check_topic, $word_score); | ||
| 197 | |||
| 198 | foreach $check_topic (@{$listref}) { | ||
| 199 | if ($check_topic eq "") { | ||
| 200 | if (!-e $cache_file) { | ||
| 201 | `column title topic < $table > $cache_file`; | ||
| 202 | } | ||
| 203 | open(FILE, $cache_file); | ||
| 204 | } else { | ||
| 205 | open(FILE, "echo '$check_topic' | search -mi -x $table topic | column title topic | headoff |"); | ||
| 206 | } | ||
| 207 | while (<FILE>) { | ||
| 208 | my($test, $topic) = split(/[\t\n]/); | ||
| 209 | $test =~ y/A-Z/a-z/; | ||
| 210 | my(@test_words) = split(/\s+/, $test); | ||
| 211 | my($has_word) = 0; | ||
| 212 | my(%bucket) = (); | ||
| 213 | my(%title_words) = (); | ||
| 214 | my(%test_words) = (); | ||
| 215 | my($match_count) = 0; | ||
| 216 | my($total_word_count) = 0; | ||
| 217 | my($word); | ||
| 218 | foreach $word (@test_words) { | ||
| 219 | if (! $exclude_words->{$word}) { | ||
| 220 | $bucket{$word} = 1; | ||
| 221 | $test_words{$word} = 1; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | foreach $word (@$title_words) { | ||
| 226 | if (! $exclude_words->{$word}) { | ||
| 227 | if ($bucket{$word} == 1) { | ||
| 228 | $has_word = 1; | ||
| 229 | } | ||
| 230 | $bucket{$word} = 1; | ||
| 231 | $title_words{$word} = 1; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | if ($has_word) { | ||
| 235 | foreach $word (keys %bucket) { | ||
| 236 | if (! $exclude_words->{$word}) { | ||
| 237 | if ($title_words{$word} > 0 && | ||
| 238 | $test_words{$word} > 0) { | ||
| 239 | $word_score = &get_word_score($topic,$word); | ||
| 240 | $match_count += $word_score; | ||
| 241 | # print "$word -> $word_score\n"; | ||
| 242 | if ($word_score > 0) { | ||
| 243 | $total_word_count += | ||
| 244 | &get_word_score($topic,$word); | ||
| 245 | } else { | ||
| 246 | $total_word_count += 1; | ||
| 247 | } | ||
| 248 | } else { | ||
| 249 | $total_word_count += 1; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | my($scale_factor) = $match_count / $total_word_count; | ||
| 254 | |||
| 255 | if ($scale_factor > $array{$topic}) { | ||
| 256 | # print "-->$topic $test $scale_factor\n"; | ||
| 257 | $array{$topic} = $scale_factor; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } # | ||
| 261 | close(FILE); | ||
| 262 | } | ||
| 263 | |||
| 264 | return (%array); | ||
| 265 | } | ||
| 266 | |||
| 267 | my(%cache) = (); | ||
| 268 | my(%loaded) = (); | ||
| 269 | sub get_word_score { | ||
| 270 | my($topic, $word) = @_; | ||
| 271 | my($foo, $foo1, $foo2, $score); | ||
| 272 | if (!defined($loaded{$word})) { | ||
| 273 | # print "Loading $word\n"; | ||
| 274 | open(FILE1, "echo '$word' | search -mb $table1 word | "); | ||
| 275 | $foo = <FILE1>; | ||
| 276 | $foo = <FILE1>; | ||
| 277 | while ($foo = <FILE1>) { | ||
| 278 | ($foo1, $foo2, $score) = split(/[\t\n]+/, $foo); | ||
| 279 | $cache{$foo2 . "//" . $foo1} = $score; | ||
| 280 | } | ||
| 281 | close(FILE1); | ||
| 282 | $loaded{$word} = 1; | ||
| 283 | if ($score eq "") {$score = 0;} | ||
| 284 | # print "$topic - $word - $score\n"; | ||
| 285 | return $cache{$word . "//" . $topic}; | ||
| 286 | } else { | ||
| 287 | return $cache{$word . "//" . $topic}; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | use strict; | ||
| 292 | |||
| 293 | sub tokenize_title { | ||
| 294 | my ($title, $remap) = @_; | ||
| 295 | my (@word_list) = (); | ||
| 296 | my ($word); | ||
| 297 | foreach $word (split(/[\s\-\:]+/, $title)) { | ||
| 298 | $word =~ tr/A-Z/a-z/; | ||
| 299 | $word =~ s/[:\(\)\"\'\,\?\-\;\*\&\#\.\/]//g; | ||
| 300 | $word =~ s/<.*?>//g; | ||
| 301 | if ($remap->{$word} ne "") { | ||
| 302 | $word = $remap->{$word}; | ||
| 303 | # print "Mapping $word to $remap->{$word}\n"; | ||
| 304 | } | ||
| 305 | push(@word_list, $word); | ||
| 306 | } | ||
| 307 | return \@word_list; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub get_exclude_list { | ||
| 311 | my ($title_word_list, $global_excludes, $local_exclude) = @_; | ||
| 312 | my ($exclude_list) = {}; | ||
| 313 | my ($word, $word1); | ||
| 314 | foreach $word (@$global_excludes) { | ||
| 315 | $exclude_list->{$word} = 1; | ||
| 316 | } | ||
| 317 | foreach $word (@$title_word_list) { | ||
| 318 | if (defined($local_exclude->{$word})) { | ||
| 319 | foreach $word1 (@{$local_exclude->{$word}}) { | ||
| 320 | $exclude_list->{$word1} = 1; | ||
| 321 | print "Excluding $word -> $word1\n"; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | return $exclude_list; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub guess_topic { | ||
| 329 | my($fref, $assignh) = @_; | ||
| 330 | |||
| 331 | my($title) = $fref->{'title'} ne "" ? | ||
| 332 | $fref->{'title'} : $fref->{'name'}; | ||
| 333 | my($id) = $fref->{'classid'} ne "" ? | ||
| 334 | $fref->{'classid'} : $fref->{'progid'}; | ||
| 335 | my($topic) = $fref->{'topic'}; | ||
| 336 | |||
| 337 | my($topich) = IO::File->new($topic_list); | ||
| 338 | my($logstring) = ""; | ||
| 339 | my(%in, %guess ); | ||
| 340 | @in{"topic", "title", "classid"} = ($topic, $title, $id); | ||
| 341 | |||
| 342 | $in{"title"} =~ y/A-Z/a-z/; | ||
| 343 | $in{"classid"} =~ y/A-Z/a-z/; | ||
| 344 | |||
| 345 | # Try to see if topics is already assigned | ||
| 346 | if ($in{"classid"} ne "") { | ||
| 347 | $topic = `echo "$in{'classid'}" | search -mi $::tabledir/catalog.rdb-c classid | column topic | headoff | head -1`; | ||
| 348 | chop $topic; | ||
| 349 | if ($topic ne "") { | ||
| 350 | $in{"topic"} = $topic; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | # Ignore headers | ||
| 355 | $topich->getline(); | ||
| 356 | $topich->getline(); | ||
| 357 | |||
| 358 | while ($_ = $topich->getline()) { | ||
| 359 | /^\s*$/ && next; | ||
| 360 | /^#/ && next; | ||
| 361 | chop; | ||
| 362 | @guess{"oldtopic", "title", "classid", "newtopic"} = | ||
| 363 | split(/\|/, $_); | ||
| 364 | my($match) = 1; | ||
| 365 | foreach (keys %guess) { | ||
| 366 | if ($guess{$_} eq "" || $_ eq "newtopic") { | ||
| 367 | next; | ||
| 368 | } | ||
| 369 | if ($_ eq "oldtopic") { | ||
| 370 | if ($guess{"oldtopic"} eq "*") { | ||
| 371 | next; | ||
| 372 | } elsif ($guess{"oldtopic"} eq "" && $in{"topic"} ne "") { | ||
| 373 | $match = 0; last; | ||
| 374 | } elsif ($in{"topic"} !~ m!^$guess{"oldtopic"}!) { | ||
| 375 | $match = 0; last; | ||
| 376 | } | ||
| 377 | } elsif ($in{$_} !~ /$guess{$_}/) { | ||
| 378 | $match = 0; last; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 382 | if ($match) { | ||
| 383 | |||
| 384 | if ($guess{"newtopic"} =~ /^\+/) { | ||
| 385 | $guess{"newtopic"} =~ s/\+//; | ||
| 386 | if ($in{"topic"} ne "" && $in{"topic"} !~ /$guess{"newtopic"}/ | ||
| 387 | && $in{'topic'} !~ /Countries/ && $in{'topic'} !~ /Regions/) { | ||
| 388 | $in{"topic"} .= ";" . $guess{"newtopic"}; | ||
| 389 | } | ||
| 390 | } else { | ||
| 391 | ($in{"topic"} = $guess{"newtopic"}); | ||
| 392 | |||
| 393 | } | ||
| 394 | } | ||
| 395 | } | ||
| 396 | $topich->close(); | ||
| 397 | if ($in{"topic"} eq "") { | ||
| 398 | $logstring .= | ||
| 399 | join(":", "NOTOPIC" ,@in{"classid", "title"}) . "\n"; | ||
| 400 | } else { | ||
| 401 | $logstring .= | ||
| 402 | join(":", @in{"topic", "classid", "title"}) . "\n"; | ||
| 403 | } | ||
| 404 | my($check_topic) = | ||
| 405 | &gna_catalog_edit_check_topic($::catalog_file, $in{'topic'}); | ||
| 406 | |||
| 407 | |||
| 408 | if ($check_topic ne "") { | ||
| 409 | $logstring .= " ****$check_topic \n"; | ||
| 410 | } | ||
| 411 | if ($check_topic =~ /too many/i || $in{'topic'} eq "") { | ||
| 412 | $logstring .= " *** Refining "; | ||
| 413 | my(%prob_vec) = &refine_topic1($in{'title'}, [$in{'topic'}]); | ||
| 414 | my($new_topic) = &find_top(\%prob_vec, 1); | ||
| 415 | if ($new_topic ne "") { | ||
| 416 | $logstring .= "to $new_topic"; | ||
| 417 | $in{'topic'} = $new_topic; | ||
| 418 | } | ||
| 419 | $logstring .= "\n"; | ||
| 420 | } | ||
| 421 | if (defined($assignh)) { | ||
| 422 | $assignh->print($logstring); | ||
| 423 | $assignh->flush(); | ||
| 424 | } | ||
| 425 | return $in{"topic"}; | ||
| 426 | } | ||
| 427 | |||
| 428 | |||
| 429 | |||
| 430 | 1; | ||
| 431 | |||
| 432 | |||
| 433 |