OSDN Git Service

bayes suggest implemented.
[rec10/rec10-git.git] / rec10 / trunk / src / auto_rec.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import n_gram
6 import rec10d
7 def get_db_key(key,chtxt):
8     return rec10d.rec10db.select_by_key_bayeskey(key, chtxt)
9 def change_db_epg(chtxt,beforenum,newnum):
10     rec10d.rec10db.change_ratio_all_reduce_bayeskey(chtxt, beforenum, newnum)
11 def change_db_num(chtxt,recaddnum,alladdnum):
12     """
13     add num.(Not change)
14     """
15     rec10d.rec10db.add_num_bayeskey(chtxt, recaddnum,alladdnum)
16 def change_db_ratio_rec(key,chtxt,beforenum,addnum):
17     rec10d.rec10db.change_ratio_rec_bayeskey(key, chtxt, beforenum, addnum)
18 def change_db_ratio_rec_many(chtxt,beforenum,list):
19     rec10d.rec10db.change_multi_ratio_rec_bayeskey(chtxt, beforenum, list)
20 def change_db_reduce_ratio_rec(chtxt,beforenum,addnum):
21     rec10d.rec10db.change_ratio_rec_reduce_bayeskey(chtxt, beforenum, addnum)
22 def change_db_ratio_all(key,chtxt,beforenum,addnum):
23     rec10d.rec10db.change_ratio_all_bayeskey(key, chtxt, beforenum, addnum)
24 def change_db_ratio_all_many(chtxt,beforenum,list):
25     rec10d.rec10db.change_multi_ratio_all_bayeskey(chtxt,beforenum, list)
26 def get_db_num(chtxt):
27     ret=get_db_key("NUM", chtxt)
28     if len(ret)<3:
29         rec10d.rec10db.add_bayeskey("NUM", chtxt, 1,1)
30         ret=get_db_key("NUM", chtxt)
31     return ret
32 def update_recall(chtxt,titles,descs,newnum):
33     """
34     update recall
35     """
36     titles=" "+titles+" "
37     descs=" "+descs+" "
38     str1=titles*2+descs+titles*2
39     noun=n_gram.get_noun_quad_gram(str1)
40     bnum=get_db_num(chtxt)[3]
41     if bnum<1:
42         bnum=100
43     change_db_ratio_all_many(chtxt, bnum,noun)
44     change_db_epg(chtxt, bnum, newnum)
45     change_db_num(chtxt, 0, newnum)
46 def add_key(chtxt,title,desc):
47     title=u" "+title+u" "
48     desc=u" "+desc+u" "
49     str1=title*2+desc+title*2##タイトルは重視したいので幾度か足す。
50     print str1
51     noun=n_gram.get_noun_quad_gram(str1)#nounは辞書のキーに文字を、中身に出現回数を書いたもの。
52     bnum=get_db_num(chtxt)[2]
53     print noun
54     if bnum<1:
55         bnum=1
56     change_db_ratio_rec_many(chtxt, bnum, noun)
57     change_db_num(chtxt,1,0)
58     change_db_reduce_ratio_rec(chtxt, bnum,1)
59 def calc_key(chtxt,title,desc):
60     """
61
62     """
63     kyoukai=600
64     kyoukai_num=30
65     title=" "+title+" "
66     desc=" "+desc+" "
67     str1=title*2+desc+title*2##タイトルは重視したいので幾度か足す。
68     noun=n_gram.get_noun_quad_gram(str1)#nounは辞書のキーに文字を、中身に出現回数を書いたもの。
69     tnum=0
70     for key,num in noun.items():
71         addnum=0
72         pp=get_db_key(key,chtxt)
73         if len(pp)>3:
74             trec=pp[2]
75             tall=pp[3]
76             if tall<trec:
77                 p=1000*trec/(3*tall+trec)
78                 if p>kyoukai:
79                     addnum=addnum+1
80         tnum=tnum+num*addnum
81     if kyoukai_num<tnum:
82         return 1
83     else:
84         return 0