From 076813cb1a448ab4db025457f104ab6fe6f1f0a7 Mon Sep 17 00:00:00 2001 From: militarpancho Date: Wed, 1 Feb 2017 13:46:34 +0100 Subject: [PATCH] Added sentiment-140 --- sentiment-140/sentiment-140.py | 40 +++++++++++++++++++ sentiment-140/sentiment-140.senpy | 18 +++++++++ .../sentiment-meaningCloud.senpy | 4 +- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 sentiment-140/sentiment-140.py create mode 100644 sentiment-140/sentiment-140.senpy diff --git a/sentiment-140/sentiment-140.py b/sentiment-140/sentiment-140.py new file mode 100644 index 0000000..ad9704a --- /dev/null +++ b/sentiment-140/sentiment-140.py @@ -0,0 +1,40 @@ +import requests +import json + +from senpy.plugins import SentimentPlugin +from senpy.models import Results, Sentiment, Entry + + +class Sentiment140Plugin(SentimentPlugin): + def analyse(self, **params): + lang = params.get("language", "auto") + res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson", + json.dumps({"language": lang, + "data": [{"text": params["input"]}] + } + ) + ) + + p = params.get("prefix", None) + response = Results(prefix=p) + polarity_value = self.maxPolarityValue*int(res.json()["data"][0] + ["polarity"]) * 0.25 + polarity = "marl:Neutral" + neutral_value = self.maxPolarityValue / 2.0 + if polarity_value > neutral_value: + polarity = "marl:Positive" + elif polarity_value < neutral_value: + polarity = "marl:Negative" + + entry = Entry(id="Entry0", + nif__isString=params["input"]) + sentiment = Sentiment(id="Sentiment0", + prefix=p, + marl__hasPolarity=polarity, + marl__polarityValue=polarity_value) + sentiment.prov__wasGeneratedBy = self.id + entry.sentiments = [] + entry.sentiments.append(sentiment) + entry.language = lang + response.entries.append(entry) + return response diff --git a/sentiment-140/sentiment-140.senpy b/sentiment-140/sentiment-140.senpy new file mode 100644 index 0000000..13a5a86 --- /dev/null +++ b/sentiment-140/sentiment-140.senpy @@ -0,0 +1,18 @@ +{ + "name": "sentiment-140", + "module": "sentiment-140", + "description": "What my plugin broadly does", + "author": "@balkian", + "version": "0.1", + "extra_params": { + "language": { + "@id": "lang_sentiment140", + "aliases": ["language", "l"], + "required": false, + "options": ["es", "en", "auto"] + } + }, + "requirements": {}, + "maxPolarityValue": "1", + "minPolarityValue": "0" +} diff --git a/sentiment-meaningCloud/sentiment-meaningCloud.senpy b/sentiment-meaningCloud/sentiment-meaningCloud.senpy index 128e3cd..d029c35 100644 --- a/sentiment-meaningCloud/sentiment-meaningCloud.senpy +++ b/sentiment-meaningCloud/sentiment-meaningCloud.senpy @@ -23,6 +23,8 @@ } }, - "requirements": {} + "requirements": {}, + "maxPolarityValue": "1", + "minPolarityValue": "-1" }