def politeness(request): '''Method to get politeness score''' try: print "Reached", type(request) print "request is", request sentence = request[u'sentence'] print "sentence is", sentence try: result = model.get_score(sentence) except Exception as e: raise ValueError(e.message) if(request.has_key('pthr')): pthr = float(request[u'pthr']) else: pthr = 0.15 if(request.has_key('ipthr')): ipthr = float(request[u'ipthr']) else: ipthr = 0.15 ipthreshold = ipthr pthreshold = pthr print result ans = 'Impolite' if (result['impolite'] >= (.5 + ipthreshold)) else ('Neutral' if (result['impolite'] >= (0.5 - pthreshold)) else 'Polite') res = {} res['score'] = ans res['value'] = result res['msg'] = "Fine" return json.dumps(res) except KeyError: raise ValueError("No sentence detected") except ValueError as e: errres = {} errres['score'] = 'Neutral' errres['value'] = {} errres['msg'] = e.message return json.dumps(errres)
def politeness(): '''Method to get politeness score''' try: if request.method == 'POST': try: #f = open('log.txt', 'a+') sentence = request.form[u'sentence'] #f.write(sentence+"\n") #f.close try: result = model.get_score(sentence) except Exception as e: #print e, "for sentence: ", sentence print "ERROR ",sys.exc_info()[0] raise ValueError(sentence,'There is a problem: {0}'.format(e)) if(request.form.has_key('pthr')): pthr = float(request.form[u'pthr']) else: pthr = 0.15 if(request.form.has_key('ipthr')): ipthr = float(request.form[u'ipthr']) else: ipthr = 0.15 ipthreshold = ipthr pthreshold = pthr print result ans = 'Impolite' if (result['impolite'] >= (.5 + ipthreshold)) else ('Neutral' if (result['impolite'] >= (0.5 - pthreshold)) else 'Polite') res = {} res['score'] = ans res['value'] = result res['msg'] = "Fine" if(request.form.has_key('txt')):res['txt'] = request.form[u'txt'] if(request.form.has_key('id')): res['id'] = request.form[u'id'] access = '' if(request.form.has_key('access')):access = request.form[u'access'] print os.path.join(os.path.split(__file__)[0],'impolite.txt') f = open(os.path.join(os.path.split(__file__)[0],'impolite.txt'), 'a+') f.write(str(datetime.datetime.utcnow())+"\t") f.write(str(res)+"\t"+access+"\n") f.close() return json.dumps(res) except KeyError: raise ValueError("Using POST. No sentence detected.") else: return "Using GET. Use POST instead. " except ValueError as e: errres = {} errres['score'] = 'Neutral' errres['value'] = {} errres['msg'] = str(e) #if(request.form.has_key('txt')):erres['txt'] = request.form[u'txt'] #if(request.form.has_key('id')): erres['id'] = request.form[u'id'] return json.dumps(errres)
def politeness(): '''Method to get politeness score''' try: if request.method == 'POST': try: #f = open('log.txt', 'a+') sentence = request.form[u'sentence'] #f.write(sentence+"\n") #f.close try: result = model.get_score(sentence) except Exception as e: #print e, "for sentence: ", sentence print "ERROR ", sys.exc_info()[0] raise ValueError(sentence, 'There is a problem: {0}'.format(e)) if (request.form.has_key('pthr')): pthr = float(request.form[u'pthr']) else: pthr = 0.15 if (request.form.has_key('ipthr')): ipthr = float(request.form[u'ipthr']) else: ipthr = 0.15 ipthreshold = ipthr pthreshold = pthr print result ans = 'Impolite' if (result['impolite'] >= (.5 + ipthreshold)) else ('Neutral' if ( result['impolite'] >= (0.5 - pthreshold)) else 'Polite') res = {} res['score'] = ans res['value'] = result res['msg'] = "Fine" if (request.form.has_key('txt')): res['txt'] = request.form[u'txt'] if (request.form.has_key('id')): res['id'] = request.form[u'id'] access = '' if (request.form.has_key('access')): access = request.form[u'access'] print os.path.join(os.path.split(__file__)[0], 'impolite.txt') f = open( os.path.join(os.path.split(__file__)[0], 'impolite.txt'), 'a+') f.write(str(datetime.datetime.utcnow()) + "\t") f.write(str(res) + "\t" + access + "\n") f.close() return json.dumps(res) except KeyError: raise ValueError("Using POST. No sentence detected.") else: return "Using GET. Use POST instead. " except ValueError as e: errres = {} errres['score'] = 'Neutral' errres['value'] = {} errres['msg'] = str(e) #if(request.form.has_key('txt')):erres['txt'] = request.form[u'txt'] #if(request.form.has_key('id')): erres['id'] = request.form[u'id'] return json.dumps(errres)
from simplejson import loads import jsonrpclib import cPickle as pickle import os.path from politeness import model server = jsonrpclib.Server("http://127.0.0.1:8080") text = 'Please help me out here. Hi, woops, I really appreciate your unlikely honesty.' print model.get_score(text)
from politeness import model text = 'Please help me out here. Hi, woops, I really appreciate your unlikely honesty.' print model.get_score(text)