OSDN Git Service

implement new calc of bayes suggestion.
[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=250
64     title=" "+title+" "
65     desc=" "+desc+" "
66     str1=title*2+desc+title*2##タイトルは重視したいので幾度か足す。
67     noun=n_gram.get_noun_quad_gram(str1)#nounは辞書のキーに文字を、中身に出現回数を書いたもの。
68     tnum=1
69     nump=0
70     for key,num in noun.items():
71         pp=get_db_key(key,chtxt)
72         pp2=get_db_key(key,"ALL")
73         if len(pp)>3:
74             trec=pp[2]
75             tall=pp[3]
76             tarec=pp2[2]
77             taall=pp2[3]
78             if tall<=trec or taall<=tarec:
79                 tnum=tnum+1
80                 p=(1000*trec+5)*100/(1000*tall+5)
81                 p2=(1000*tarec+5)*100/(1000*taall+5)
82                 nump=nump+p+p2
83     if (nump/tnum)>kyoukai:
84         return 1
85     else:
86         return 0