Changeset 14186 for lang/python

Show
Ignore:
Timestamp:
06/17/08 23:07:51 (5 months ago)
Author:
ayu
Message:
  • added README&sample.
Location:
lang/python/googlebayes
Files:
7 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/python/googlebayes/googlebayes/learn.py

    r14179 r14186  
    44 
    55import re 
     6import sys 
    67import cPickle as pickle 
    78from itertools import chain, groupby, imap 
     
    1819        yield buf[i:i+n] 
    1920 
    20 def addcount(dct, tpl): 
     21def _addcount(dct, tpl): 
    2122    u""" 
    2223    合計計算用辞書dct に、データ列tpl内の頻度を加算する。 
     
    2526        dct[k] = dct.get(k, 0) + len(list(g)) 
    2627 
    27 def toprob(dct, docnum): 
     28def _toprob(dct, docnum): 
    2829    u""" 
    2930    頻度辞書を頻度確率辞書に変換する。 
     
    3536    return ans 
    3637 
    37 def countngramprob(stream): 
     38def _countngramprob(stream): 
    3839    u""" 
    3940    bi-gram と、 tri-gramの確率辞書を計算して返します。 
     
    5152        bbuf.extend(set(ngram(line, 2))) 
    5253        if linenum % BATCH == 0: 
    53             addcount(trigram, tbuf) 
    54             addcount(bigram, bbuf) 
     54            _addcount(trigram, tbuf) 
     55            _addcount(bigram, bbuf) 
    5556            tbuf = [] 
    5657            bbuf = [] 
    5758    else: 
    58         addcount(trigram, tbuf) 
    59         addcount(bigram, bbuf) 
    60     return toprob(bigram, linenum), toprob(trigram, linenum) 
     59        _addcount(trigram, tbuf) 
     60        _addcount(bigram, bbuf) 
     61    return _toprob(bigram, linenum), _toprob(trigram, linenum) 
    6162 
    6263RSSURL = "http://blogsearch.google.co.jp/blogsearch_feeds?hl=ja&client=firefox-a&um=1&q=%s&lr=lang_ja&ie=utf-8&num=50&output=rss" 
    6364REMOVEHTML = re.compile(u"<[^>]+>") 
    64 def create_data_stream(kw): 
     65def _create_data_stream(kw): 
    6566    u""" 
    6667    キーワードから、blog検索を行い、その結果の概要文の列を返す。 
     
    7172        yield REMOVEHTML.sub(u"", ent.get("summary", u"")) 
    7273 
    73 def calcbayes(okkw, spamkw): 
     74def _calcbayes(okkw, spamkw): 
    7475    u""" 
    7576    OKキーワード、spamキーワードをもとに、blog検索を行い、それぞれの文たちから 
    7677    Bayes計算用の確率一覧を作成して返す。 
    7778    """ 
    78     okprob = countngramprob(chain(*(imap(create_data_stream, okkw)))) 
    79     spamprob = countngramprob(chain(*(imap(create_data_stream, spamkw)))) 
     79    okprob = _countngramprob(chain(*(imap(_create_data_stream, okkw)))) 
     80    spamprob = _countngramprob(chain(*(imap(_create_data_stream, spamkw)))) 
    8081    return okprob, spamprob 
    8182 
    82 def test(): 
    83     for line in create_data_stream(u"Python"): 
    84         print line["summary"].encode("utf-8") 
     83def learn(okwords, spamwords, fname): 
     84    u""" 
     85    学習を行います。学習結果はファイルに保存されます。 
    8586 
    86 def main(fname): 
     87    引数 
     88    okwords Unicodeで書かれた、OK語のリスト 
     89    spamwords Unicodeで書かれた、SPAM語のリスト 
     90    fname 学習結果を保存するファイル名 
     91    """ 
    8792    fp = file(fname, "w") 
    88     okprob, spamprob = calcbayes( 
    89         [u"python", u"C++", u"ruby 開発", u"自然言語処理"], 
    90         [u"巨乳", u"熟女", u"ハメ撮り", u"オナニー", u"儲かる 情報商材"] 
    91         ) 
     93    okprob, spamprob = _calcbayes(okwords, spamwords) 
    9294    pickle.dump(okprob, fp) 
    9395    pickle.dump(spamprob, fp) 
    9496    fp.close() 
    9597 
     98def _test(fname): 
     99    learn( 
     100        [u"python", u"C++", u"ruby 開発", u"自然言語処理"], 
     101        [u"巨乳", u"熟女", u"ハメ撮り", u"オナニー", u"儲かる 情報商材"], 
     102        fname 
     103        )     
     104 
    96105if __name__ == "__main__": 
    97     import optparse 
    98     
    99     parser = optparse.OptionParser(u""" 
    100     学習データを作成します。 
    101     """) 
    102     parser.add_option("-o", "--output", dest="output", help=u"出力ファイル名。", default=None) 
    103     (options, args) = parser.parse_args() 
    104      
    105     #test() 
    106     main(options.output) 
    107      
    108  
     106    _test("sample.dat") 
  • lang/python/googlebayes/setup.py

    r14179 r14186  
    1010    author="Hiroshi Ayukawa", 
    1111    author_email="ayukawa.hiroshi@gmail.com", 
    12     packages=["googlebayes"] 
     12    packages=["googlebayes"], 
     13    scripts=["scripts/gblearn.py", "scripts/gbbayes.py"] 
    1314) 
    1415