Test-Space / my_uie.py
LiuPengNGP's picture
Create my_uie.py
7ac38d8 verified
raw
history blame
1.74 kB
#from paddlenlp import Taskflow
#用Ie抽取信息
#def emo_analy():
# ie = Taskflow("information_extraction")
#定义schema
# schema ='[情感分析 正向,负向]'
# ie.set_schema(schema)
# while True:
# text=input()
# if text == "exit":
# break
# result = ie(text)
# print(result)
from paddlenlp import Taskflow
#用Ie抽取信息
ie = Taskflow("information_extraction")
#定义schema
schema ='[情感分析 正向,负向]'
ie.set_schema(schema)
def emo_analy(text):
result = ie(text)
#print(text)
# print(result) #后端是否显示结果 代码,如果服务器需要结果 请把此行代码解开
#return result
if result and isinstance(result, list) and len(result) > 0:
# 获取第一个键值
first_item = result[0]
if '[情感分析 正向,负向]' in first_item:
# 获取第一个对应值
sentiment_info = first_item['[情感分析 正向,负向]']
if sentiment_info and isinstance(sentiment_info, list) and len(sentiment_info) > 0:
sentiment_result = sentiment_info[0]
# print(sentiment_result) #最终结果,也就是值.
#此时结果有些差错,因为底层原理的原因,输出的结果文本就是一个text如:
#{'text': '正向', 'probability': 0.9980062049349883}
#目前要修改text为所输入的文本如:我很开心,那么就要重新创建一个字典了:
new_sentiment_result = {text:sentiment_result['text'],'probability':sentiment_result['probability']}
print(new_sentiment_result)
return new_sentiment_result
return "无法分析结果"