1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 16:12:29 +00:00
senpy/affect/affect.py

56 lines
1.8 KiB
Python
Raw Normal View History

2017-01-13 13:23:07 +00:00
import time
import requests
import json
import string
import os
import json
import logging
from os import path
import time
from senpy.plugins import SentimentPlugin, SenpyPlugin
2017-01-16 16:11:10 +00:00
from senpy.models import Results, Entry, Sentiment, Error
2017-01-13 13:23:07 +00:00
logger = logging.getLogger(__name__)
class unifiedPlugin(SentimentPlugin):
2017-01-16 16:11:10 +00:00
def analyse(self, **kwargs):
params = dict(kwargs)
2017-01-13 13:23:07 +00:00
txt = params["input"]
2017-01-16 16:11:10 +00:00
logger.info('TXT:%s' % txt)
2017-01-13 13:23:07 +00:00
endpoint = params["endpoint"]
lang = params.get("language")
2017-01-17 10:49:01 +00:00
key = params["apiKey"]
2017-01-13 13:23:07 +00:00
sentiplug = params["sentiments-plugin"]
2017-01-16 16:11:10 +00:00
s_params = params.copy()
2017-01-17 10:49:01 +00:00
s_params.update({'algo':sentiplug,'language':lang, 'meaningCloud-key':key})
2017-01-16 16:11:10 +00:00
senti_response = requests.get(endpoint, params=s_params).json()
logger.info('SENTIPARAMS: %s' % s_params)
2017-01-17 10:49:01 +00:00
logger.info('SENTIRESPONSE: %s' % senti_response)
2017-01-16 16:11:10 +00:00
if 'entries' not in senti_response:
raise Error(senti_response)
senti_response = Results(senti_response)
logger.info('SENTI: %s' % senti_response)
logger.info(senti_response)
2017-01-13 13:23:07 +00:00
emoplug = params["emotions-plugin"]
2017-01-16 16:11:10 +00:00
e_params = params.copy()
e_params.update({'algo':emoplug,'language':lang})
emo_response = requests.get(endpoint, params=e_params).json()
if 'entries' not in emo_response:
raise Error(emo_response)
emo_response = Results(emo_response)
logger.info('EMO: %s' % emo_response)
logger.info(emo_response)
2017-01-13 13:23:07 +00:00
#Senpy Response
response = Results()
response.analysis = [senti_response.analysis, emo_response.analysis]
unified = senti_response.entries[0]
unified["emotions"] = emo_response.entries[0]["emotions"]
response.entries.append(unified)
return response