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)
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('/')
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)
def brainit(): brain = Bayes() data = base64.encodestring(brain.saves()) return data
posts = Post.objects.filter(user=user) bayes = Brain.objects.get(user=user) brain = Bayes() #brain.loads(base64.decodestring(bayes.data)) # retrain the brain based on existing tags def retrain(): for post in posts: for tag in post.tags.all(): text = "%s %s %s" % (post.title, post.author, post.summary) brain.train(tag, text) print "%s :: %s" % (tag, post.title) retrain() bayes.data = base64.encodestring(brain.saves()) bayes.save() from BeautifulSoup import BeautifulSoup from mainapp.models import Post from reverend.thomas import Bayes brain = Bayes() brain.load('fish.db') tag = 'Dead' posts = Post.objects.filter(read=read) posts = posts.filter(tags__in=tag) #brain.train('Dead', post.summary) t1 = Tag.objects.get(id=flag)