-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentimentBot.py
90 lines (76 loc) · 2.6 KB
/
sentimentBot.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
# This example show how to write an inline mode telegramt bot use pyTelegramBotAPI.
import telebot
import time
import sys
import logging
from telebot import types
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
API_TOKEN = 'Yourtoken'
bot = telebot.TeleBot(API_TOKEN)
telebot.logger.setLevel(logging.DEBUG)
extremes = importr('syuzhet')
codigo_r_library = """
libreryLoad <- function(name) {
requiredPackages = c(name)
for(p in requiredPackages){
if(!require(p,character.only = TRUE)) install.packages(p,repos="http://cran.rstudio.com/")
library(p,character.only = TRUE)
}
}
"""
ro.r(codigo_r_library);
codigo_r = """
resultSent <- function(cadena) {
vectorS<-get_nrc_sentiment(cadena);
yourText<-paste("Your text: ",cadena);
anger<-paste("Anger: ",vectorS$anger);
anticipation<-paste("Anticipation: ",vectorS$anticipation);
disgust<-paste("Disgust: ",vectorS$disgust);
fear<-paste("Fear: ",vectorS$fear);
joy<-paste("Joy: ",vectorS$joy);
sadness<-paste("Sadness: ",vectorS$sadness);
surprise<-paste("Surprise: ",vectorS$surprise);
trust<-paste("Trust: ",vectorS$trust);
negative<-paste("Negative: ",vectorS$negative);
positive<-paste("Positive: ",vectorS$positive);
totalSum<-rowSums(vectorS)
probi<-vectorS/totalSum
logpi<-log(probi)
Hlocal<- (-probi*logpi)
resultH <- rowSums(Hlocal/log(length(vectorS)),na.rm = TRUE);
diversitySent<-paste("Diversity sentiment[0-1]: ",resultH);
totalResult<-paste(yourText,positive,negative,trust,surprise,sadness,joy,fear,disgust,anticipation,anger,diversitySent,sep="\n")
return(totalResult)
}
"""
ro.r(codigo_r);
loadLib = ro.globalenv['libreryLoad'];
loadLib('syuzhet');
resultSent_py = ro.globalenv['resultSent'];
@bot.inline_handler(lambda query: query.query != '')
def query_text(inline_query):
try:
res = resultSent_py(inline_query.query);
textOut=res[0];
r = types.InlineQueryResultArticle('1', 'View score', types.InputTextMessageContent(textOut))
bot.answer_inline_query(inline_query.id, [r])
except Exception as e:
print(e)
#bot.inline_handler(lambda query: len(query.query) is 0)
def default_query(inline_query):
try:
r = types.InlineQueryResultArticle('1', 'waiting', types.InputTextMessageContent('waiting'))
bot.answer_inline_query(inline_query.id, [r])
except Exception as e:
print(e)
def main_loop():
bot.polling(True)
while 1:
time.sleep(3)
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print >> sys.stderr, '\nExiting by user request.\n'
sys.exit(0)