【山东大学软件学院创新项目实训】时空漫游 自然语言情感分析

选择大模型

NLP 服务 情感分析V2-API 文档-文档中心-腾讯云 (tencent.com)

在这里插入图片描述

接口&SDK

通过官网的控制台注册账号获取API Key并且下载SDK

这里SDK我选择Python版的

后端搭建

获取分类:

def get_class(sentence):
  try:
      cred = credential.Credential(SecretId, SecretKey)
      # 实例化一个http选项,可选的,没有特殊需求可以跳过
      httpProfile = HttpProfile()
      httpProfile.endpoint = "nlp.tencentcloudapi.com"
      # 实例化一个client选项,可选的,没有特殊需求可以跳过
      clientProfile = ClientProfile()
      clientProfile.httpProfile = httpProfile
      # 实例化要请求产品的client对象,clientProfile是可选的
      client = nlp_client.NlpClient(cred, "", clientProfile)
      # 实例化一个请求对象,每个接口都会对应一个request对象
      req = models.AnalyzeSentimentRequest()
      params = {
          "Text": sentence
      }
      req.from_json_string(json.dumps(params))
      # 返回的resp是一个AnalyzeSentimentResponse的实例,与请求对象对应
      resp = client.AnalyzeSentiment(req)
      # 输出json格式的字符串回包
      json_obj = json.loads(resp.to_json_string())
      return json_obj['Sentiment']
  except TencentCloudSDKException as err:
      print(err)
      return 'positive'

Mapper:

@app.route('/getClass', methods=['POST'])
def getClass():
  text = request.form.get('text')
  res = classify.get_class(text)
  return res
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值