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

backport from official repos.

Location:
platform/tdiary/filter
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • platform/tdiary/filter/plugin/spambayes.rb

    r10806 r12244  
    3737                @conf = conf 
    3838                filter_path = conf.filter_path || "#{PATH}/tdiary/filter" 
    39                 require "#{filter_path}/spambayes" 
     39                require "#{filter_path}/spambayes" unless ::TDiary::Filter::SpambayesFilter.const_defined?(:Misc) 
    4040 
    4141                extend ::TDiary::Filter::SpambayesFilter::Misc 
    4242                ::TDiary::Filter::SpambayesFilter::Misc.conf = conf 
     43                bayes_filter 
    4344        end 
    4445 
     
    113114                @conf[conf_log] = @cgi.params[conf_log][0] || nil 
    114115                @conf[conf_mail] = @cgi.params[conf_mail][0] || nil 
    115                 @conf[conf_threshold] = @cgi.params[conf_threshold][0] || nil 
     116                cpt_ham = (@cgi.params[conf_threshold_ham][0]||threshold_ham).to_f 
     117                cpt_spam = (@cgi.params[conf_threshold][0]||threshold).to_f 
     118                if 0<cpt_ham and cpt_ham<=cpt_spam and cpt_spam<1.0 
     119                        @conf[conf_threshold] = cpt_spam 
     120                        @conf[conf_threshold_ham] = cpt_ham 
     121                end 
    116122                prm = @cgi.params[conf_filter][0] || "Plain" 
    117123                @conf[conf_filter] ||= "Plain" 
     
    139145<li>#{Res.use_filter_to_referer} : <input type='checkbox' name='#{conf_for_referer}' #{@conf[conf_for_referer] ? "checked='checked'" : ""}> 
    140146<li>#{Res.save_error_log} : <input type='checkbox' name='#{conf_log}' #{@conf[conf_log] ? "checked='checked'" : ""}> 
    141 <li>#{Res.threshold} : <input type="text" name="#{conf_threshold}" value="#{threshold}"></li> 
     147<li>#{Res.threshold}: 0 &lt;= Ham &lt; <input type="text" name="#{conf_threshold_ham}" value="#{threshold_ham}"> &lt;= Doubt &lt;= <input type="text" name="#{conf_threshold}" value="#{threshold}"> &lt; Spam &lt;= 1.0</li> 
    142148<li>#{Res.receiver_addr} : <input type="text" name="#{conf_mail}" value="#{@conf[conf_mail]}"></li> 
    143149<li><select name='#{conf_filter}'> 
     
    452458                spams = [] 
    453459                hams = [] 
    454                 processed = false 
    455460                @cgi.params.each do |k, v| 
    456461                        next unless k=~/^r([shd])(.*)$/ 
    457                         processed = true 
    458462                        type = $1 
    459463                        referer = Referer.from_html($2) 
     
    468472                        end 
    469473                end 
    470                 if processed 
    471                         bayes_filter.save 
    472                         ["ham", "spam", "doubt"].each do |k| 
    473                                 size = (@cgi.params[k[0, 1]+"size"][0]||"0").to_i 
    474                                 Referer.truncate_list(referer_cache(k), size) 
    475                         end 
    476  
    477                         PStore.new(referer_corpus).transaction do |db| 
    478                                 spams.concat(db['spam']||[]) 
    479                                 hams.concat(db['ham']||[]) 
    480                                 db["spam"] = spams.uniq 
    481                                 db["ham"] = hams.uniq 
    482                         end 
     474 
     475                bayes_filter.save 
     476                ["ham", "spam", "doubt"].each do |k| 
     477                        size = (@cgi.params[k[0, 1]+"size"][0]||"0").to_i 
     478                        Referer.truncate_list(referer_cache(k), size) 
     479                end 
     480 
     481                PStore.new(referer_corpus).transaction do |db| 
     482                        spams.concat(db['spam']||[]) 
     483                        hams.concat(db['ham']||[]) 
     484                        db["spam"] = spams.uniq 
     485                        db["ham"] = hams.uniq 
    483486                end 
    484487 
  • platform/tdiary/filter/spambayes.rb

    r5705 r12244  
    33 
    44require "bayes" 
    5 require "uri" 
    65 
    76module TDiary::Filter 
     
    4847                        def conf_mail; "#{PREFIX}.mail"; end 
    4948                        def conf_threshold; "#{PREFIX}.threshold"; end 
     49                        def conf_threshold_ham; "#{PREFIX}.threshold_ham"; end 
    5050                        def conf_use; "#{PREFIX}.use"; end 
    5151                        def conf_log; "#{PREFIX}.log"; end 
     
    9292 
    9393                        def bayes_db 
    94                                  "#{@conf.data_path}/bayes.db" 
     94                                "#{@conf.data_path}/bayes.db" 
    9595                        end 
    9696 
     
    103103                                case @conf[conf_filter] 
    104104                                when /graham/i 
    105                                         @bayes_filter ||= Bayes::PaulGraham.new(bayes_db) 
     105                                        @bayes_filter ||= Bayes::PaulGraham.new(bayes_db, Bayes::CHARSET::UTF8) 
    106106                                else 
    107                                         @bayes_filter ||= Bayes::PlainBayes.new(bayes_db) 
    108                                 end 
     107                                        @bayes_filter ||= Bayes::PlainBayes.new(bayes_db, Bayes::CHARSET::UTF8) 
     108                                end 
     109                                convert_to_utf8 unless @bayes_filter.charset==Bayes::CHARSET::UTF8 
    109110                                @bayes_filter 
     111                        end 
     112 
     113                        def convert_to_utf8 
     114                                require "bayes/convert" 
     115                                require "kconv" 
     116 
     117                                @bayes_filter.convert(Bayes::CHARSET::UTF8, Bayes::CHARSET::EUC) 
     118                                @bayes_filter.save 
     119                                comments = [] 
     120                                ["S", "H", "D"].each do |c| 
     121                                        comments.concat(Dir["#{bayes_cache}/#{c}*"]) 
     122                                end 
     123                                ["S", "H"].each do |c| 
     124                                        comments.concat(Dir["#{corpus_path}/#{c}*"]) 
     125                                end 
     126                                comments.each do |f| 
     127                                        Comment.load(f).convert_to_utf8.save(f) 
     128                                end 
    110129                        end 
    111130 
    112131                        def threshold 
    113132                                (@conf[conf_threshold]||"0.95").to_f 
     133                        end 
     134 
     135                        def threshold_ham 
     136                                (@conf[conf_threshold_ham]||"0.05").to_f 
    114137                        end 
    115138 
     
    118141                                        path 
    119142                                else 
    120                                         @conf.base_url.sub(/\/*$/, '/') + (path||'') 
    121                                 end 
    122                         end 
    123  
    124                         def url2(path=nil) 
    125                                 if path && URI.parse(path).absolute? 
    126                                         path 
    127                                 else 
    128                                         base = URI.parse @conf.base_url 
    129                                         base.path = base.path.sub(%r{/*$}, '/') + (path || '') 
    130                                         base.to_s 
     143                                        File.join(@conf.base_url, (path||"")) 
    131144                                end 
    132145                        end 
     
    161174                                @remote_addr = cgi.remote_addr || "" 
    162175                                d = cgi.params['date'][0] || Time.now.strftime("%Y%m%d") 
    163                                 @diary_date = Time::local(*d.scan(/^(\d{4})(\d{2})(\d{2})$/)[0]) + 12*60*60 
     176                                @diary_date = Time::local(*d.scan(/^(\d{4})(\d\d)(\d\d)$/)[0]) + 12*60*60 
     177                        end 
     178 
     179                        def convert_to_utf8 
     180                                @name = @name.kconv(Kconv::UTF8, Kconv::EUC) 
     181                                @body = @body.kconv(Kconv::UTF8, Kconv::EUC) 
     182                                self 
     183                        end 
     184 
     185                        def save(filename) 
     186                                open(filename, "w") do |f| 
     187                                        f.flock(File::LOCK_SH) 
     188                                        f.rewind 
     189                                        Marshal.dump(self, f) 
     190                                end 
    164191                        end 
    165192 
     
    168195                        end 
    169196 
     197                        RE_URL = %r[(?:https?|ftp)://[a-zA-Z0-9;/?:@&=+$,\-_.!~*\'()%]+] 
    170198                        def token 
    171199                                r = TokenList.new 
     
    178206                                r.add_mail_addr(@mail, "M") 
    179207                                b = @body.dup 
    180                                 URI.extract(b, %w[http https ftp]) do |url| 
    181                                         r.add_url(url, "U") 
     208                                b.gsub!(RE_URL) do |m| 
     209                                        r.add_url(m, "U") 
     210                                        "" 
    182211                                end 
    183212                                r.add_message(b) 
     
    269298 
    270299                        def split_url 
    271                                 begin 
    272                                         url = URI.parse(@referer) 
    273                                         query    = url.query 
    274                                         fragment = url.fragment 
    275                                         url.query    = nil 
    276                                         url.fragment = nil 
    277                                         base = url.to_s 
    278                                 rescue 
    279                                         base, query, fragment = @referer.scan(/^(.*?)(?:\?([^#]*?)(?:#(.*))?)?$/)[0] 
    280                                 end 
    281                                 [base, query, fragment] 
     300                                base, request, anchor = @referer.scan(/^(.*?)(?:\?(.*?)(?:\#(.*))?)?$/)[0] 
    282301                        end 
    283302 
     
    410429                end 
    411430 
     431                def ham?(tokens) 
     432                        e = bayes_filter.estimate(tokens) || (threshold_ham+threshold)/2 
     433                        case 
     434                        when e<threshold_ham 
     435                                true 
     436                        when e>threshold 
     437                                false 
     438                        else 
     439                                nil 
     440                        end 
     441                end 
     442 
    412443                def comment_filter(diary, comment) 
    413444                        return false if force_filtering? 
     
    420451                        ham_url = "Register as ham : #{base_url}confirm_ham" 
    421452 
    422                         e = bayes_filter.estimate(data.token) 
    423                         case 
    424                         when e == nil 
    425                                 r = false 
    426                                 tag = "DOUBT" 
    427                                 url = "#{spam_url}\n#{ham_url}" 
    428                         when e>threshold 
     453                        case ham?(data.token) 
     454                        when true 
     455                                r = true 
     456                                tag = "HAM" 
     457                                url = spam_url 
     458                        when false 
    429459                                r = false 
    430460                                tag = "SPAM" 
    431461                                url = ham_url 
    432462                        else 
    433                                 r = true 
    434                                 tag = "HAM" 
    435                                 url = spam_url 
     463                                r = false 
     464                                tag = "DOUBT" 
     465                                url = "#{spam_url}\n#{ham_url}" 
    436466                        end 
    437467                        cn = tag[0,1]+data.cache_name 
    438                         open("#{bayes_cache}/#{cn}", "w") do |f| 
    439                                 f.flock(File::LOCK_SH) 
    440                                 f.rewind 
    441                                 Marshal.dump(data, f) 
    442                         end 
     468                        data.save("#{bayes_cache}/#{cn}") 
    443469                        url.gsub!(/(\n|\z)/){";comment_id=#{cn}#$1"} 
    444470 
     
    487513                        referer = Referer.new(referer, ENV["REMOTE_ADDR"]) 
    488514                        token = referer.token 
    489                         e = bayes_filter.estimate(token) 
    490                         case 
    491                         when e==nil 
    492                                 r = false 
    493                                 key = "doubt" 
    494                         when e>threshold 
     515                        case ham?(token) 
     516                        when true 
     517                                key = "ham" 
     518                        when false 
    495519                                r = false 
    496520                                key = "spam" 
    497521                        else 
    498                                 key = "ham" 
     522                                r = false 
     523                                key = "doubt" 
    499524                        end 
    500525                        open(referer_cache(key), "a") do |f|