Changeset 12244 for platform/tdiary/lib

Show
Ignore:
Timestamp:
05/23/08 17:24:47 (6 months ago)
Author:
hiraku
Message:

backport from official repos.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • platform/tdiary/lib/bayes.rb

    r6084 r12244  
    33 
    44require "pstore" 
    5 require "uri" 
    65 
    76module Bayes 
     
    9796 
    9897                def add_url(url, prefix=nil) 
    99                         if URI.regexp(%w[http https ftp]) === url 
    100                                 url  = URI.parse url 
    101                                 host = url.host                       # $4 
    102                                 path = url.path.gsub(%r{^/+|/+$}, '') # $7 
     98                        if %r[^(?:https?|ftp)://(.*?)(?::\d+)?/(.*?)\/?(\?.*)?$] =~ url 
     99                                host, path = $1, $2 
    103100 
    104101                                add_host(host, prefix) 
     
    137134 
    138135        class FilterBase 
    139                 attr_reader :spam, :ham, :db_name 
    140  
    141                 def initialize(db_name=nil) 
     136                attr_reader :spam, :ham, :db_name, :charset 
     137 
     138                def initialize(db_name=nil, charset=nil) 
    142139                        @spam = self.class::Corpus.new 
    143140                        @ham = self.class::Corpus.new 
     141                        @charset = charset 
    144142 
    145143                        @db_name = db_name 
     
    148146                                        @spam = db["spam"] 
    149147                                        @ham = db["ham"] 
     148                                        @charset = db["charset"] 
    150149                                end 
    151150                        end 
     
    159158                                db["spam"] = @spam 
    160159                                db["ham"] = @ham 
     160                                db["charset"] = @charset 
    161161                                yield(db) if block_given? 
    162162                        end 
     
    218218                        g = @ham.count==0 ? 0.0 : [1.0, 2*@ham[token]/@ham.count.to_f].min 
    219219                        b = @spam.count==0 ? 0.0 : [1.0, @spam[token]/@spam.count.to_f].min 
    220                         if g+b==0 
    221                                 raise "OOO" 
    222                         end 
    223220                        r = [0.01, [0.99, b/(g+b)].min].max 
    224221                        r