-
Notifications
You must be signed in to change notification settings - Fork 0
/
account-kind.py
214 lines (160 loc) · 8.14 KB
/
account-kind.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import re
import asyncio
import logging
from numpy import mean
import paddlehub as hub
from typing import Optional, Union
from wechaty_puppet import FileBox, ScanStatus # type: ignore
from wechaty import Wechaty, Contact
from wechaty.user import Message, Room, contact
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
# 定义主模型
modle_main = hub.Module(name="ernie_skep_sentiment_analysis")
# 定义辅助模型
model_lstm = hub.Module(name="senta_lstm")
model_bilstm = hub.Module(name="senta_bilstm")
model_gru = hub.Module(name="senta_gru")
def analyse_kind(text):
# 转换文本为列表形式
text = [text]
# 主模型输出结果
results_main = modle_main.predict_sentiment(text, use_gpu=True)
result_main = results_main[0]
# 辅助模型输出结果
results_lstm = model_lstm.sentiment_classify(text, use_gpu=True)
result_lstm = results_lstm[0]
results_bilstm = model_bilstm.sentiment_classify(text, use_gpu=True)
result_bilstm = results_bilstm[0]
results_gru = model_gru.sentiment_classify(text, use_gpu=True)
result_gru = results_gru[0]
results = [result_lstm, result_bilstm, result_gru]
list_score = []
# 如果消息是友好的
if result_main['sentiment_label'] == 'positive':
score = result_main['positive_probs'] * 5
list_score.append(score)
for result in results:
if result['sentiment_label'] == 'positive':
score = result['positive_probs'] * 5
list_score.append(score)
# 如果消息是不友好的
elif result_main['sentiment_label'] == 'negative':
score = result_main['negative_probs'] * (-5)
list_score.append(score)
for result in results:
if result['sentiment_label'] == 'negative':
score = result['negative_probs'] * (-5)
list_score.append(score)
score = mean(list_score)
score = round(score, 2)
return score
class MyBot(Wechaty):
"""
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""
def __init__(self):
super().__init__()
# 主人的contact
self.host_contact = None
# 所有好友的contact
self.friend_contacts = None
# 好友友好账户
self.account_kind = {}
async def on_message(self, msg: Message):
"""
listen for message event
"""
from_contact = msg.talker()
text = msg.text()
type = msg.type()
room = msg.room()
# 不处理群消息
if room is None:
# 识别主人
if text == '你好我的机器人' and self.host_contact is None:
# 记录主人的contact
self.host_contact = from_contact
# 列举所有好友的contact
friend_contacts = await self.Contact.find_all()
# 过滤一些contact
self.friend_contacts = [contact for contact in friend_contacts if contact.is_personal()]
# 移除主人的contact
self.friend_contacts.remove(self.host_contact)
# 初始化好友的友好账户
self.account_kind = {contact: 0 for contact in self.friend_contacts}
# 给主人发消息
conversation = self.host_contact
await conversation.ready()
await conversation.say('你好我亲爱的主人,我是主人的好友友好账户管家,目前的功能有:\n1 管理主人的好友友好账户\n2 查询主人的好友友好账户\n主人回复相应数字即可查询详细功能')
# 如果是主人的消息
if from_contact == self.host_contact:
conversation = self.host_contact
await conversation.ready()
if text == '1':
await conversation.say('当有好友给主人发消息时,将自动分析这句话的友好分数,并记录在主人的好友友好账户上')
if text == '2':
await conversation.say('主人按照以下格式即可查询好友的友好账户\n查询 好友 张三\n\n主人按照以下格式即可查询好友友好账户排名前五\n查询 前5\n\n主人按照以下格式即可查询好友友好账户排名后五\n查询 后5')
if '查询 好友' in text:
# 提取好友备注或昵称
friend_name = text.split(' ')[-1]
# 遍历字典,找打好友的友好账户分数并返回
for contact, score in list(self.account_kind.items()):
if friend_name == contact.name or friend_name == contact.payload.alias:
friend_score = round(score, 2)
break
await conversation.say(f'亲爱的主人,{friend_name}目前的友好分数为:{friend_score}')
if '查询 前' in text:
# 提取数字
number = re.findall(r'\d+', text)
number = int(number[0])
# 按照账户分数大小给字典排序
sorted_account_kind = {contact: score for contact, score in sorted(self.account_kind.items(), key=lambda item: item[1], reverse=True)}
# 给主人发的消息内容
msg_to_host = f'亲爱的主人,目前好友友好账户排名前{number}的是:'
for contact, score in list(sorted_account_kind.items())[:number]:
# 获取好友备注或昵称
friend_name = contact.payload.alias if contact.payload.alias != '' else contact.name
msg_to_host += f'\n{friend_name}:{round(score, 2)}'
await conversation.say(msg_to_host)
if '查询 后' in text:
# 提取数字
number = re.findall(r'\d+', text)
number = int(number[0])
sorted_account_kind = {contact: score for contact, score in sorted(self.account_kind.items(), key=lambda item: item[1])}
# 给主人发的消息内容
msg_to_host = f'亲爱的主人,目前好友友好账户排名后{number}的是:'
for contact, score in list(sorted_account_kind.items())[:number]:
# 获取好友备注或昵称
friend_name = contact.payload.alias if contact.payload.alias != '' else contact.name
msg_to_host += f'\n{friend_name}:{round(score, 2)}'
await conversation.say(msg_to_host)
# 好友的消息
if from_contact in self.friend_contacts and type == Message.Type.MESSAGE_TYPE_TEXT:
# 计算好友消息的友好分数
score = analyse_kind(text)
# 更新好友的友好账户
self.account_kind[from_contact] += score
self.account_kind[from_contact] = round(self.account_kind[from_contact], 2)
# 获取好友备注或昵称
friend_name = from_contact.payload.alias if from_contact.payload.alias != '' else from_contact.name
# 给主人汇报消息
conversation = self.host_contact
await conversation.ready()
await conversation.say(f'亲爱的主人,{friend_name}给您发了一条消息\n消息内容是: {text}\n友好分数是: {score}\n{friend_name}目前的友好分数为:{self.account_kind[from_contact]}')
async def on_login(self, contact: Contact):
print(f'user: {contact} has login')
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
data: Optional[str] = None):
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')
bot: Optional[MyBot] = None
async def main():
"""doc"""
# pylint: disable=W0603
global bot
bot = MyBot()
await bot.start()
asyncio.run(main())