BosonNLP试用

来源:互联网 发布:midi控制器软件 编辑:程序博客网 时间:2024/05/03 12:11
  • 安装pip
wget https://bootstrap.pypa.io/get-pip.py  --no-check-certificatesudo python get-pip.py
  • 安装BosonNLP
pip install -U bosonnlp
  • Test
    使用Pycharm,要确保Pycharm的python版本和系统一致,可参考http://www.51testing.com/html/44/15047444-3646855.html
# -*- coding:utf-8 -*-from bosonnlp import BosonNLPnlp = BosonNLP('你的Key')print(nlp.sentiment('这家味道还不错'))

结果输出:
2016-07-16 11:16:58,962 INFO Starting new HTTP connection (1): api.bosonnlp.com
[[0.9934679658749207, 0.006532034125079303]]

  • 使用情感分析和文本摘要
# -*- encoding: utf-8 -*-from __future__ import print_function, unicode_literalsimport jsonimport requestsSENTIMENT_URL = 'http://api.bosonnlp.com/sentiment/analysis'# 注意:在测试时请更换为您的API Tokenheaders = {'X-Token': '你的Key'}# 读取文件with open('1.txt', 'rb') as f:    strs = [line.decode('utf-8') for line in f if line]# 情感分析(支持批量最多一次100笔)data = json.dumps(strs)resp = requests.post(SENTIMENT_URL, headers=headers, data=data.encode('utf-8'))print(resp.text)# 摘要(支持单文本)# 遍历文件内容for s in strs:    # print(s)    source = {        'not_exceed': 0,        'percentage': 0.2,        'title': '',        'content': (s)    }    SENTIMENT_URL = 'http://api.bosonnlp.com/summary/analysis'    resp = requests.post(        SENTIMENT_URL,        headers=headers,        data=json.dumps(source).encode('utf-8'))    resp.raise_for_status()    print(resp.text)

输入文本》
互联网金融也带给我们改变:一是更便利。比如原来要跑到银行柜台去办理转账、购买理财产品,今天通过互联网就能实现。二是更普惠。比如淘宝上的店家,传统银行根本不愿意或者不能够提供贷款,但是网络小额贷款就能提供贷款。原来有1000元闲钱都不会买基金,今天有100元甚至1元,都会在网上买基金。三是更便宜。比如通过互联网买卖基金的手续费不到银行柜台的一半。又如,保险产品互联网销售渠道的费用只有银行渠道的十分之一。
网贷之家的数据显示,2015年7月,正常运营的平台数量为2136家,而截至2016年6月底,该数值为2349家,在此期间,出现了1778家停业及问题平台。就目前形势而言,“出问题”已成为大概率事件,因此,如何处理问题,也值得关注。近日,一纸法院判决书,在P2P行业引起轩然大波:浙江省杭州市中级人民法院公布的刑事判决书《(2015)浙杭刑初字第200号》显示,P2P平台银坊金融平台负责人蔡锦聪犯集资诈骗罪,判处无期徒刑,剥夺政治权利终身,并处没收个人全部财产,这是目前P2P领域最重的判罚。目前来看,快鹿兑付危机的处理,则为行业带来一定的示范性:6月22日,快鹿集团召开兑付危机发生以来第三次正发布会,会议上宣布成立投资人占多数的快鹿投资事件管理处置委员会,管委会同时正式聘请了独立第三方上海市联合律师事务所为公司法律顾问,聘请了独立第三方会计师事务所为公司审计,负责委员会指派的有关上海快鹿投资事件管理处置相关的法务、审计工作。同时,快鹿投资集团代表韦炎平承诺10月1日前全面启动兑付的刚性目标不变。监管层面也加强了干预。“e租宝事件”后,公安部组织建设了“非法集资案件投资人信息登记平台”,目前已受理两起非法集资案件的信息登记工作:e租宝和昆明泛亚有色案件。截至3月2日,已有近14万名e租宝投资人在该平台登记信息。平台名称也有考量:因公安机关办理的是刑事案件,并非涉及债权债务纠纷的民事案件,因此,该平台不使用“债权人登记平台”。在海外,P2P鼻祖Lending Club,也依然在危机中奋战。6月29日凌晨消息, 由于贷款量持续下降,Lending Club宣布裁掉179名员工以恢复投资者信心,同时宣布代理首席执行官斯科特·桑伯恩将成为公司的永久的首席执行官和总裁。当然,这些标杆性的P2P平台处理危机的方式,并不能代表全行业,深8君(以为,无论对于投资者、监管层还是平台自身来说,加速行业洗牌都利大于弊。

输出》
[[0.9810511962767625, 0.018948803723237533], [0.24979342780912828, 0.7502065721908717]]
“比如原来要跑到银行柜台去办理转账、购买理财产品,今天通过互联网就能实现。\n又如,保险产品互联网销售渠道的费用只有银行渠道的十分之一。”
“网贷之家的数据显示,2015年7月,正常运营的平台数量为2136家,而截至2016年6月底,该数值为2349家,在此期间,出现了1778家停业及问题平台。\n“e租宝事件”后,公安部组织建设了“非法集资案件投资人信息登记平台”,目前已受理两起非法集资案件的信息登记工作:e租宝和昆明泛亚有色案件。\n当然,这些标杆性的P2P平台处理危机的方式,并不能代表全行业,深8君(以为,无论对于投资者、监管层还是平台自身来说,加速行业洗牌都利大于弊。”

  • 批量文件导入以及批量生成结果文件
# -*- encoding: utf-8 -*-from __future__ import print_function, unicode_literalsimport jsonimport requestsimport osinfo = '/home/yuhz/PycharmProjects/untitled2/fileHome/'file_add = 'nlp'SENTIMENT_URL = 'http://api.bosonnlp.com/sentiment/analysis'# 注意:在测试时请更换为您的API Tokenheaders = {'X-Token': '你的Key'}listfile = os.listdir(info)#read directoryfor line in listfile:  # 把目录下的文件都赋值给line这个参数    if line[-4:] == '.txt' and line.find(file_add) == -1:        # 读取文件        file_name = line.replace('.','_'+file_add+'.')        with open(info+line, 'rb') as f:            #read line            strs = [line.decode('utf-8') for line in f if line]            st_line=''            # connact words            for words in strs:                st_line = st_line + words.strip()            # 情感分析(支持批量最多一次100笔)            SENTIMENT_URL = 'http://api.bosonnlp.com/sentiment/analysis'            data = json.dumps(st_line.strip())            resp = requests.post(SENTIMENT_URL, headers=headers, data=data.encode('utf-8'))            a = resp.text            #摘要            source = {                'not_exceed': 0,                'percentage': 0.2,                'title': '',                'content': (st_line.strip())            }            SENTIMENT_URL = 'http://api.bosonnlp.com/summary/analysis'            resp = requests.post(                SENTIMENT_URL,                headers=headers,                data=json.dumps(source).encode('utf-8'))            resp.raise_for_status()            #reaplce /n            b = resp.text.replace('\\n','')            #write file            f = open(info+file_name, 'w')            f.write(a+b)            f.close()
  • 优化输出格式并生成总文件
# -*- encoding: utf-8 -*-from __future__ import print_function, unicode_literalsfrom tempfile import TemporaryFilefrom xlwt import Workbookimport jsonimport requestsimport osimport xlwtimport traceback#数据目录info = '/home/yuhz/PycharmProjects/untitled2/fileHome/'#结果目录inforst = '/home/yuhz/PycharmProjects/untitled2/result/'#生成的文件增加后缀NLPfile_add = 'nlp'SENTIMENT_URL = 'http://api.bosonnlp.com/sentiment/analysis'# 注意:在测试时请更换为您的API Tokenheaders = {'X-Token': 'key'}listfile = os.listdir(info)all_content = ''#写入Excel(定义Excel表头)book = Workbook()sheet1 = book.add_sheet('Sheet 1')row1 = sheet1.row(0)row1.write(0,'TITLE')row1.write(1,'POSITIVE')row1.write(2,'NEGATIVE')row1.write(3,'SUMMARY')sheet1.col(0).width = 3000sheet1.col(1).width = 5000sheet1.col(2).width = 5000sheet1.col(3).width = 30000index = 0#自动换行style = xlwt.easyxf('align: wrap on')#read directoryfor line in listfile:  # 把目录下的文件都赋值给line这个参数    TITLE = ''    POSITIVE = ''    NEGATIVE = ''    SUMMARY = ''    index = index + 1    if line[-4:] == '.txt' and line.find(file_add) == -1:        # 读取文件        TITLE = line.replace('.txt','')        all_content = all_content+'TITLE:'+TITLE + '\n'        file_name = line.replace('.','_'+file_add+'.')        with open(info+line, 'rb') as f:            #read line            #windows style            try:                strs = [line.decode('GBK') for line in f if line]            except UnicodeDecodeError:                try:                    #linux style                    strs = [line.decode('utf-8') for line in f if line]                except UnicodeDecodeError:                    #else                    print(file_name)            st_line=''            # connact words            for words in strs:                st_line = st_line + words.strip()            #情感分析(支持批量最多一次100笔)            SENTIMENT_URL = 'http://api.bosonnlp.com/sentiment/analysis'            data = json.dumps(st_line.strip())            resp = requests.post(SENTIMENT_URL, headers=headers, data=data.encode('utf-8'))            a = resp.text            #解析返回结果放入变量            rowset = a.replace('[[','').replace(']]','').replace(' ','').split(',')            #print(rowset)            POSITIVE = rowset[0]            NEGATIVE = rowset[1]            #format            a = a.strip().replace(',', ',NEGATIVE:')            aa = ''            #拼接生成的情感分析结果            for words in a.split(','):                aa = aa + words.replace('[[', 'POSITIVE:').replace(']]', '').replace(' ', '')+'\n'            #摘要            source = {                'not_exceed': 0,                'percentage': 0.2,                'title': '',                'content': (st_line.strip())            }            SENTIMENT_URL = 'http://api.bosonnlp.com/summary/analysis'            resp = requests.post(                SENTIMENT_URL,                headers=headers,                data=json.dumps(source).encode('utf-8'))            resp.raise_for_status()            #格式化摘要            b = resp.text.replace('\\n','').strip('"')            #变量赋值            SUMMARY = b            #写入文件            f = open(inforst+file_name, 'w')            all_content = all_content +aa+'SUMMARY:'+b+'\n'            f.write(aa+'SUMMARY:'+b)            f.close()    #写入Excel    row1 = sheet1.row(index)    row1.write(0, TITLE,style)    row1.write(1, POSITIVE*1,style)    row1.write(2, NEGATIVE*1,style)    row1.write(3, SUMMARY,style)#最终结果写入一个文件ff = open(inforst+'all_content.txt', 'w')ff.write(all_content)ff.close()#保存Excelbook.save(inforst+'result.xls')book.save(TemporaryFile())
0 0
原创粉丝点击