#!/usr/bin/python
# coding: UTF-8
# Rec10 TS Recording Tools
# Copyright (C) 2009 Yukikaze
import n_gram
import rec10d
def get_db_key(key,chtxt):
    return rec10d.rec10db.select_by_key_in_auto_bayes_key(key, chtxt)
def change_db_epg(chtxt,beforenum,newnum):
    rec10d.rec10db.change_ratio_all_reduce_in_auto_bayes_key(chtxt, beforenum, newnum)
def change_db_num(chtxt,recaddnum,alladdnum):
    """
    add num.(Not change)
    """
    rec10d.rec10db.add_num_in_auto_bayes_key(chtxt, recaddnum,alladdnum)
def change_db_ratio_rec(key,chtxt,beforenum,addnum):
    rec10d.rec10db.change_ratio_rec_in_auto_bayes_key(key, chtxt, beforenum, addnum)
def change_db_ratio_rec_many(chtxt,beforenum,list):
    rec10d.rec10db.change_multi_ratio_rec_in_auto_bayes_key(chtxt, beforenum, list)
def change_db_reduce_ratio_rec(chtxt,beforenum,addnum):
    rec10d.rec10db.change_ratio_rec_reduce_in_auto_bayes_key(chtxt, beforenum, addnum)
def change_db_ratio_all(key,chtxt,beforenum,addnum):
    rec10d.rec10db.change_ratio_all_in_auto_bayes_key(key, chtxt, beforenum, addnum)
def change_db_ratio_all_many(chtxt,beforenum,list):
    rec10d.rec10db.change_multi_ratio_all_in_auto_bayes_key(chtxt,beforenum, list)
def get_db_num(chtxt):
    ret=get_db_key("NUM", chtxt)
    if len(ret)<3:
        rec10d.rec10db.add_in_auto_bayes_key("NUM", chtxt, 1,1)
        ret=get_db_key("NUM", chtxt)
    return ret
def update_recall(chtxt,titles,descs,newnum):
    """
    update recall
    """
    titles=" "+titles+" "
    descs=" "+descs+" "
    str1=titles*2+descs+titles*2
    noun=n_gram.get_noun_quad_gram(str1)
    bnum=get_db_num(chtxt)[3]
    if bnum<1:
        bnum=100
    change_db_ratio_all_many(chtxt, bnum,noun)
    change_db_epg(chtxt, bnum, newnum)
    change_db_num(chtxt, 0, newnum)
def add_key(chtxt,title,desc):
    title=u" "+title+u" "
    desc=u" "+desc+u" "
    str1=title*2+desc+title*2##タイトルは重視したいので幾度か足す。
    print str1
    noun=n_gram.get_noun_quad_gram(str1)#nounは辞書のキーに文字を、中身に出現回数を書いたもの。
    bnum=get_db_num(chtxt)[2]
    print noun
    if bnum<1:
        bnum=1
    change_db_ratio_rec_many(chtxt, bnum, noun)
    change_db_num(chtxt,1,0)
    change_db_reduce_ratio_rec(chtxt, bnum,1)
def calc_key(chtxt,title,desc):
    """

    """
    kyoukai=800
    title=" "+title+" "
    desc=" "+desc+" "
    str1=title*2+desc+title*2##タイトルは重視したいので幾度か足す。
    noun=n_gram.get_noun_quad_gram(str1)#nounは辞書のキーに文字を、中身に出現回数を書いたもの。
    tnum=1
    nump=0
    for key,num in noun.items():
        pp=get_db_key(key,chtxt)
        pp2=get_db_key(key,"ALL")
        if len(pp)>3:
            trec=pp[2]
            tall=pp[3]
            tarec=pp2[2]
            taall=pp2[3]
            tnum=tnum+1
            p=(1000*trec+5)/(1000*tall+5)
            p=p*p*100
            p2=(1000*tarec+5)/(1000*taall+5)
            p2=p2*100
            nump=nump+p+p2
    if (nump/tnum)>kyoukai:
        return 1
    else:
        return 0
