本文整理匯總了Python中reverend.thomas.Bayes.train方法的典型用法代碼示例。如果您正苦於以下問題:Python Bayes.train方法的具體用法?Python Bayes.train怎麽用?Python Bayes.train使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類reverend.thomas.Bayes
的用法示例。
在下文中一共展示了Bayes.train方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: treinar
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def treinar():
print """>>> Carregando categorias..."""
CATEGORIAS = os.listdir('./data')
if '.svn' in CATEGORIAS:
CATEGORIAS.remove('.svn')
print ">>> Instanciando treinador\n"
guesser = Bayes()
try:
for categoria in CATEGORIAS:
print ">>> Treinando categoria %s" % categoria
arquivos = os.listdir("%s/%s" % (CAMINHO_CATEGORIAS, categoria))
if '.svn' in arquivos:
arquivos.remove('.svn')
for arquivo in arquivos:
arquivo = open('%s/%s/%s' % (CAMINHO_CATEGORIAS, categoria, arquivo), 'r')
texto = arquivo.read()
guesser.train(categoria, texto)
print "\n>>> Salvando base de conhecimento...\n"
guesser.save("conhecimento.bay")
print "Voil?!\n"
except:
print "N?o foi poss?vel treinar a base"
示例2: retrain
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def retrain(request):
# Retrain your brain
user = User.objects.get(user=request.user)
posts = Post.objects.filter(user=user)
bayes = Brain.objects.get(user=user)
brain = Bayes()
#brain.loads(base64.decodestring(bayes.data))
tagcount = 0
# retrain the brain based on existing tags
for post in posts:
print post.title, "::",
for tag in post.tags.all():
text = "%s %s %s" % (post.title, post.author, post.summary)
brain.train(tag, text)
tagcount += 1
print tag,
print
brain.save('%s.db' % user)
bayes.data = base64.encodestring(brain.saves())
bayes.save()
message = 'Found %s tags' % tagcount
params = {'Messages': [message,]}
return response(request, 'mainapp/index.html', params)
示例3: Guesser
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
class Guesser(object):
def __init__(self, project):
self.project = project
self.bayes = Bayes()
self._train()
self.data = []
self.best = []
def _train(self):
for sentence in self.project.classified():
self.bayes.train(sentence.get_classification(), sentence.sentence)
def guess(self):
for sentence in self.project.to_classify():
data = {'sentence_id': sentence.id}
data['guesses'] = self.bayes.guess(sentence.sentence)
self.data.append(data)
return self.data
def best_matches(self):
if not self.data: return []
for matches in self.data:
try:
matches['guesses'] = sorted(matches['guesses'], key=lambda x:x[1], reverse=True)[0]
except:
matches['guesses'] = (None, None)
match = {}
match['id'] = matches['sentence_id']
match['guess'] = matches['guesses'][0]
match['certainty'] = matches['guesses'][1]
self.best.append(match)
return self.best
示例4: train
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def train(self,bucket,words):
"""
Nominate a bucket to which the words apply, and train accordingly
"""
if bucket != "" and words != "":
try:
Bayes.train(self,bucket,words)
Bayes.save(self,self.brain)
except:
print "Failed to learn"
else:
return None
示例5: action_train
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def action_train(self, cr, uid, ids, context=None):
cat_obj = self.pool.get('crm.bayes.categories')
group_obj = self.pool.get('crm.bayes.group')
message_obj = self.pool.get('crm.bayes.test.guess')
for id in ids:
cat_id = self.read(cr, uid, id, ['category_id','name'])
cat_id = cat_id[0]['category_id']
if result :
max_list = max(result, key=lambda k: k[1])
if cat_id:
cat_guess_msg = cat_obj.read(cr, uid, cat_id, ['train_messages'])
cat_obj.write(cr, uid, cat_id, {'train_messages' :cat_guess_msg['train_messages'] + 1})
if max_list[1] > 0 and not cat_id:
cat_id = cat_obj.search(cr, uid, [('name','=',max_list[0])])[0]
cat_guess_msg = cat_obj.read(cr, uid, cat_id, ['guess_messages'])
cat_obj.write(cr, uid, cat_id, {'guess_messages' :cat_guess_msg['guess_messages'] + 1})
self.write(cr, uid, ids, {'category_id':cat_id})
if cat_id :
cat_rec = cat_obj.read(cr, uid, cat_id, [])
guesser = Bayes()
data = ""
for rec in group_obj.browse(cr, uid, [cat_rec['group_id'][0]]):
if rec['train_data']:
data += rec['train_data']
if data :
myfile = file(file_path+"crm_bayes.bay", 'w')
myfile.write(data)
myfile.close()
guesser.load(file_path+"crm_bayes.bay")
guesser.train(cat_rec['name'], message_obj.read(cr, uid, id)[0]['name'])
guesser.save(file_path+"crm_bayes.bay")
myfile = file(file_path+"crm_bayes.bay", 'r')
data=""
for fi in myfile.readlines():
data += fi
cr.execute("select sum(train_messages) as tot_train,sum(guess_messages) as tot_guess from crm_bayes_categories where group_id=%d"% cat_rec['group_id'][0])
rec = cr.dictfetchall()
if not rec[0]['tot_guess']:
rec[0]['tot_guess'] =0
percantage = float(rec[0]['tot_guess'] *100) / float(rec[0]['tot_guess'] + rec[0]['tot_train'])
group_obj.write(cr, uid, cat_rec['group_id'][0], {'train_data': data,'automate_test':percantage})
else :
raise osv.except_osv(_('Error !'),_('Please Select Category! '))
return {
'view_type': 'form',
"view_mode": 'form',
'res_model': 'crm.bayes.train.message',
'type': 'ir.actions.act_window',
'target':'new',
}
示例6: getCategoryGuesses
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def getCategoryGuesses(self, corpus1, corpus2, corpus3):
from reverend.thomas import Bayes
# instantiate guesser
guesser = Bayes()
# train category guesser with first corpus
guesser.train('first reference text', corpus1)
guesser.train('second reference text', corpus2)
# compare with second corpus
guesses = guesser.guess(corpus3)
return guesses
示例7: treino
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def treino (self):
banco_do_jornal = Server()
genero=[banco_do_jornal[doc] for doc in GENEROS]
#treinando o reverend
from reverend.thomas import Bayes
guesser = Bayes()
guesser.train('artigo', ' '.join(genero[0][doc]['texto'] for doc in genero[0]))
guesser.train('resenha',' '.join(genero[6][doc]['texto'] for doc in genero[6]))
guesser.train('noticia',' '.join(genero[1][doc]['texto'] for doc in genero [1]))
guesser.train('cronica',' '.join(genero[5][doc]['texto']for doc in genero[5] if 'texto' in genero[5][doc] ))
guesser.train('horoscopo',' '.join(genero[3][doc]['texto']for doc in genero[3]))
guesser.train('manchete',' '.join(genero[2][doc]['titulo']for doc in genero[2]))
guesser.train('receita',' '.join(genero[4][doc]['texto']for doc in genero[4]))
guesser.save('my_guesser.bay')
variavel = guesser.guess('Cidad?o se descuidou e roubaram seu celular. Como era um executivo e n?o sabia mais viver sem celular, ficou furioso. Deu parte do roubo, de Quara?.? Pois ?.? Carol.? Hein?? Meu nome. ? Carol.? Ah. Voc?s s?o...? N?o, n?o. Nos conhecemos h? pouco.? Escute Carol. Eu trouxe uma encomenda para o Amleto. De Quara?. Uma pessegada, mas n?o me lembro do endere?o.')
print 'Resultado = ', variavel
示例8: treino
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def treino (self):
banco_do_jornal = Server()
genero=[banco_do_jornal[doc] for doc in GENEROS]
#treinando o reverend
from reverend.thomas import Bayes
guesser = Bayes()
guesser.train('artigo', ' '.join(genero[0][doc]['texto'] for doc in genero[0]))
guesser.train('resenha',' '.join(genero[6][doc]['texto'] for doc in genero[6]))
guesser.train('noticia',' '.join(genero[1][doc]['texto'] for doc in genero [1]))
guesser.train('cronica',' '.join(genero[5][doc]['texto']for doc in genero[5] if 'texto' in genero[5][doc] ))
guesser.train('horoscopo',' '.join(genero[3][doc]['texto']for doc in genero[3]))
guesser.train('manchete',' '.join(genero[2][doc]['titulo']for doc in genero[2]))
guesser.train('receita',' '.join(genero[4][doc]['texto']for doc in genero[4]))
guesser.save('my_guesser.bay')
variavel = guesser.guess('Bolo de chocolate :ingredientes : 6 ovos, 2 xicaras de farinha, 1 colher de achocolatado, 1 lata de leite condensado, 2 copos de leite e 3 colheres de açucar')
print 'Resultado = ', variavel
示例9: mark
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def mark(request, flag):
id = request.GET.get('post', None)
feed = request.GET.get('feed', None)
category = request.GET.get('category')
tag = request.GET.get('tag') or None
try:
if feed:
posts = Post.objects.filter(feed=feed)
else:
posts = Post.objects.filter(id=id)
except Post.DoesNotExist:
return HttpResponseRedirect('/')
bayes = Brain.objects.get(user=request.user) #login required
brain = Bayes()
brain.loads(base64.decodestring(bayes.data))
if flag in ('read', 'unread'):
flag = flag == 'read'
posts.update(read=flag)
else:
for post in posts:
text = "%s %s %s" % (post.title, post.author, post.summary)
t1 = Tag.objects.get(id=flag)
if t1 in post.tags.all() and not feed:
post.tags.remove(t1)
brain.untrain(t1.name, text)
else:
post.tags.add(t1)
brain.train(t1.name, text)
post.save()
bayes.data = base64.encodestring(brain.saves())
bayes.save()
if category:
return HttpResponseRedirect('/?category=%s' % category)
elif feed:
return HttpResponseRedirect('/?feed=%s' % feed)
elif tag:
return HttpResponseRedirect('/?tag=%s' % tag)
else:
return HttpResponseRedirect('/')
示例10: read
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def read(request, id):
try:
post = Post.objects.get(id=id)
post.read = True
post.save()
try:
bayes = Brain.objects.get(user=request.user) #login required
brain = Bayes()
brain.loads(base64.decodestring(bayes.data))
text = post.title + ' ' + post.author + post.summary
brain.train('Interesting', text)
bayes.data = base64.encodestring(brain.saves())
bayes.save()
except Exception, e:
print "Couldn't train %s because %s" % (post.title, e)
return HttpResponseRedirect(post.link)
示例11: getLanguageGuesses
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def getLanguageGuesses(self, stopWords, corpus, languages):
from reverend.thomas import Bayes
# charset
charset = 'us-ascii'
# instantiate guesser
guesser = Bayes()
# go through language in order to train guesser
for selectLanguage in languages:
if selectLanguage != 'automatic':
stopWordString = stopWords.getStopWordString(selectLanguage)
guesser.train(selectLanguage, stopWordString.encode(charset, 'replace'))
# get list of possible languages
languageGuesses = guesser.guess(corpus.encode(charset, 'replace'))
return languageGuesses
示例12: trained
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
def trained(self, cr, uid, ids, context=None):
for id in ids:
record = self.read(cr, uid, id, ['category_id','description'])
if not record['description'] :
raise osv.except_osv(_('Error!'),_("Description Not Define!"))
if not record['category_id']:
raise osv.except_osv(_('Error!'),_("Statistics Category Not Define!"))
group_obj = self.pool.get('crm.bayes.group')
cat_obj = self.pool.get('crm.bayes.categories')
cat_rec = cat_obj.read(cr, uid, record['category_id'][0], [])
guesser = Bayes()
data =""
for rec in group_obj.browse(cr, uid, [cat_rec['group_id'][0]]):
if rec['train_data']:
data += rec['train_data']
if data :
myfile = file(file_path+"crm_bayes.bay", 'w')
myfile.write(data)
myfile.close()
guesser.load(file_path+"crm_bayes.bay")
guesser.train(cat_rec['name'], record['description'])
guesser.save(file_path+"crm_bayes.bay")
myfile = file(file_path+"crm_bayes.bay", 'r')
data=""
for fi in myfile.readlines():
data += fi
cat_obj.write(cr, uid, record['category_id'][0], {'train_messages':int(cat_rec['train_messages']) + 1})
cr.execute("select sum(train_messages) as tot_train,sum(guess_messages) as tot_guess from crm_bayes_categories where group_id=%d"% cat_rec['group_id'][0])
rec = cr.dictfetchall()
if not rec[0]['tot_guess']:
rec[0]['tot_guess'] =0
percantage = float(rec[0]['tot_guess'] *100) / float(rec[0]['tot_guess'] + rec[0]['tot_train'])
group_obj.write(cr, uid, cat_rec['group_id'][0], {'train_data': data,'automate_test':percantage})
self.write(cr, uid, id, {'state_bayes':'trained'})
return True
示例13: Bayes
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
from reverend.thomas import Bayes
guesser = Bayes()
for each in ('north','south','east','west'):
guesser.train('do_move', 'to the %s' % each)
guesser.train('do_move', 'head %s' % each)
guesser.train('do_move', 'spin %s' % each)
# if we don't do this 'do_move' is going to get too dominant
guesser.train('do_take', 'grab')
guesser.train('do_take', 'grab the')
guesser.train('do_take', 'pick up')
guesser.train('do_take', 'pick up the')
guesser.train('do_take', 'lift')
guesser.train('do_take', 'lift the')
guesser.train('do_take', 'fetch')
guesser.train('do_take', 'fetch the')
bulk = """wear suit of armour
put on suit of armour
use armor
climb into armour
wear the armor
place armour on me
place armor on self"""
for line in bulk.splitlines():
guesser.train('do_take', line)
guesser.save('commands.bays')
示例14: Bayes
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
from reverend.thomas import Bayes
guesser = Bayes()
guesser.train('french', 'le la les du un une je il elle de en')
guesser.train('german', 'der die das ein eine')
guesser.train('spanish', 'el uno una las de la en')
guesser.train('english', 'the it she he they them are were to')
guesser.guess('they went to el cantina')
guesser.guess('they were flying planes')
guesser.train('english', 'the rain in spain falls mainly on the plain')
guesser.save('my_guesser.bay')
示例15: Bayes
# 需要導入模塊: from reverend.thomas import Bayes [as 別名]
# 或者: from reverend.thomas.Bayes import train [as 別名]
from reverend.thomas import Bayes
guesser = Bayes()
guesser.train('fish', 'salmon trout cod carp')
guesser.train('fowl', 'hen chicken duck goose')
guesser.guess('chicken tikka marsala')
guesser.untrain('fish','salmon carp')