You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
senpy/sentiment-meaningCloud/sentiment-meaningCloud.py

47 lines
1.4 KiB
Python

import time
import requests
import json
import string
import os
from os import path
import time
from senpy.plugins import SentimentPlugin, SenpyPlugin
8 years ago
from senpy.models import Results, Entry, Sentiment,Error
class DaedalusPlugin(SentimentPlugin):
7 years ago
def analyse_entry(self, entry, params):
7 years ago
txt = entry.get("text",None)
model = "general" # general_es / general_es / general_fr
api = 'http://api.meaningcloud.com/sentiment-2.1'
lang = params.get("language")
key = params["apiKey"]
parameters = {'key': key,
'model': model,
'lang': lang,
'of': 'json',
'txt': txt,
'src': 'its-not-a-real-python-sdk'
}
r = requests.post(api, params=parameters)
value = r.json().get('score_tag', None)
8 years ago
if not value:
raise Error(r.json())
#Senpy Response
response = Results()
polarityValue = 0
polarity = 'marl:Neutral'
if 'N' in value:
polarity = 'marl:Negative'
polarityValue = -1
elif 'P' in value:
polarity = 'marl:Positive'
polarityValue = 1
7 years ago
opinion = Sentiment(id="Opinion0",marl__hasPolarity=polarity,marl__polarityValue = polarityValue)
entry.sentiments.append(opinion)
7 years ago
yield entry