forked from cpssd-students/steely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment.py
46 lines (38 loc) · 1.38 KB
/
sentiment.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
'''.sentiment tracks user positivity or negativity in previous message for all the autismos'''
import requests
import json
__author__ = 'oskarmcd'
COMMAND = 'sentiment'
BASE_URL = 'http://text-processing.com/api/sentiment/'
FULL_STRINGS = {
'pos': 'positive',
'neg': 'negative',
'neutral': 'neutral'
}
def sanitized(input):
replacements = (('`', ''),
('\n', ' '))
for bad, good in replacements:
input = input.replace(bad, good)
return input
def get_sentiment(string):
response = requests.post(BASE_URL, f'text={string}')
data = response.json()
probability = data['probability']
body = f"The phrase you've checked is {string!r}.\n"
for feeling in FULL_STRINGS:
body += f"We're {probability[feeling] * 100:.2f}% sure " + \
f"the message is {FULL_STRINGS[feeling]}\n"
conclusion = FULL_STRINGS[data['label']]
body += f"I'm gonna guess this shit is {conclusion}"
return body
def main(bot, author_id, message, thread_id, thread_type, **kwargs):
def send_message(message):
bot.sendMessage(message, thread_id=thread_id, thread_type=thread_type)
last_message = bot.fetchThreadMessages(
thread_id=thread_id, limit=2)[1].text
if len(last_message) > 150:
send_message('no')
return
last_message = sanitized(last_message)
send_message(get_sentiment(last_message))