LiuPengNGP commited on
Commit
7ac38d8
·
verified ·
1 Parent(s): 1ffa5b5

Create my_uie.py

Browse files
Files changed (1) hide show
  1. my_uie.py +40 -0
my_uie.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #from paddlenlp import Taskflow
2
+ #用Ie抽取信息
3
+ #def emo_analy():
4
+ # ie = Taskflow("information_extraction")
5
+ #定义schema
6
+ # schema ='[情感分析 正向,负向]'
7
+ # ie.set_schema(schema)
8
+ # while True:
9
+ # text=input()
10
+ # if text == "exit":
11
+ # break
12
+ # result = ie(text)
13
+ # print(result)
14
+ from paddlenlp import Taskflow
15
+ #用Ie抽取信息
16
+ ie = Taskflow("information_extraction")
17
+ #定义schema
18
+ schema ='[情感分析 正向,负向]'
19
+ ie.set_schema(schema)
20
+ def emo_analy(text):
21
+ result = ie(text)
22
+ #print(text)
23
+ # print(result) #后端是否显示结果 代码,如果服务器需要结果 请把此行代码解开
24
+ #return result
25
+ if result and isinstance(result, list) and len(result) > 0:
26
+ # 获取第一个键值
27
+ first_item = result[0]
28
+ if '[情感分析 正向,负向]' in first_item:
29
+ # 获取第一个对应值
30
+ sentiment_info = first_item['[情感分析 正向,负向]']
31
+ if sentiment_info and isinstance(sentiment_info, list) and len(sentiment_info) > 0:
32
+ sentiment_result = sentiment_info[0]
33
+ # print(sentiment_result) #最终结果,也就是值.
34
+ #此时结果有些差错,因为底层原理的原因,输出的结果文本就是一个text如:
35
+ #{'text': '正向', 'probability': 0.9980062049349883}
36
+ #目前要修改text为所输入的文本如:我很开心,那么就要重新创建一个字典了:
37
+ new_sentiment_result = {text:sentiment_result['text'],'probability':sentiment_result['probability']}
38
+ print(new_sentiment_result)
39
+ return new_sentiment_result
40
+ return "无法分析结果"