1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-09-07 17:17:44 +00:00
senpy/sentiment-meaningCloud/sentiment-meaningCloud.py

52 lines
1.5 KiB
Python
Raw Normal View History

2017-01-12 12:30:14 +00:00
import time
import requests
import json
import string
import os
from os import path
import time
from senpy.plugins import SentimentPlugin, SenpyPlugin
2017-01-17 10:49:01 +00:00
from senpy.models import Results, Entry, Sentiment,Error
2017-01-12 12:30:14 +00:00
class DaedalusPlugin(SentimentPlugin):
def activate(self, *args, **kwargs):
pass
def deactivate(self, *args, **kwargs):
self.close()
def analyse(self, **params):
txt = params["input"]
model = params["model"] # general_es / general_es / general_fr
api = 'http://api.meaningcloud.com/sentiment-2.1'
lang = params.get("language")
2017-01-16 16:11:10 +00:00
key = params["apiKey"]
2017-01-12 12:30:14 +00:00
parameters = {'key': key,'model': model,'lang': lang,'of': 'json','txt': txt,'src': 'its-not-a-real-python-sdk'}
r = requests.post(api, params=parameters)
print(r.text)
value = r.json().get('score_tag', None)
2017-01-17 10:49:01 +00:00
if not value:
raise Error(r.json())
2017-01-12 12:30:14 +00:00
#Senpy Response
response = Results()
polarityValue = 0
2017-01-12 12:30:14 +00:00
polarity = 'marl:Neutral'
if 'N' in value:
polarity = 'marl:Negative'
polarityValue = -1
2017-01-12 12:30:14 +00:00
elif 'P' in value:
polarity = 'marl:Positive'
polarityValue = 1
2017-01-12 12:30:14 +00:00
entry = Entry(id="Entry0",nif_isString=txt)
opinion = Sentiment(id="Opinion0",marl__hasPolarity=polarity,marl__polarityValue = polarityValue)
2017-01-12 12:30:14 +00:00
opinion["prov:wasGeneratedBy"] = self.id
entry.sentiments = []
entry.sentiments.append(opinion)
response.entries.append(entry)
return response