python 利用bottle提供post接口,供第三方调用

该博客展示了如何使用Python的Bottle框架创建一个POST接口,该接口接收第三方请求并进行情感分析。通过去除文本中的标点符号,使用jieba进行分词,再利用SnowNLP进行情感判断。最后返回包含情感分析结果的JSON对象。
摘要由CSDN通过智能技术生成

# coding=utf-8 
''' 
Created on 2017年5月9日 

@author: chenkai 
''' 
import bottle 
import urllib 
import struct 
from getscoretext import GetScore 

def geturltoutf8(srcString): 
    unquotedString = urllib.unquote(srcString) 
    #print repr(unquotedString) 
    textString = unquotedString.decode('utf-8') 
    #print textString 
    return textString 


@bottle.route('/score', method='POST') 
def score(): 
    data =bottle.request.body.readlines()#获取请求参数列表 
    #转换参数编码格式 
    pltext=geturltoutf8(data[0]) #第一个参数,URL编码转utf8 
    print pltext #打印请求参数值 
    #开始情感分析 
    result=GetScore(pltext) #这个功能是我写在getscoretext.py文件中的一个函数 
    print result 
    return result #返回参数 

bottle.run(host='localhost', port=8083) 


==================getscoretext.py代码 
# coding=utf-8 
''' 
Created on 2017年5月9日 

@author: chenkai 
''' 
from snownlp import SnowNLP 
from nltk.collocations import  BigramCollocationFinder 
from nltk.metrics import  BigramAssocMeasures 
import re 
import jieba 
import jieba.analyse 
import json 



def GetScore(text): 
    #1.标点符号处理 
    text = re.sub("[\s+\.\!\/_,$%^*(+\"\']+|[+——!&#x

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值