2014-10-17 10:47:17 +00:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
2014-10-17 17:06:19 +00:00
|
|
|
from senpy.plugins import SentimentPlugin
|
|
|
|
from senpy.models import Response, Opinion, Entry
|
2014-10-17 10:47:17 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-10-17 10:47:17 +00:00
|
|
|
class Sentiment140Plugin(SentimentPlugin):
|
2014-11-04 20:31:41 +00:00
|
|
|
EXTRA_PARAMS = {
|
2014-10-17 17:06:19 +00:00
|
|
|
"language": {"aliases": ["language", "l"],
|
|
|
|
"required": False,
|
|
|
|
"options": ["es", "en", "auto"],
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-10-17 10:47:17 +00:00
|
|
|
def __init__(self, **kwargs):
|
2014-11-04 20:31:41 +00:00
|
|
|
super(Sentiment140Plugin, self).__init__(name="sentiment140",
|
|
|
|
version="2.0",
|
|
|
|
extraparams=self.EXTRA_PARAMS,
|
2014-10-17 10:47:17 +00:00
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
def analyse(self, **params):
|
2014-10-17 17:06:19 +00:00
|
|
|
lang = params.get("language", "auto")
|
2014-10-17 10:47:17 +00:00
|
|
|
res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
|
2014-11-20 18:29:49 +00:00
|
|
|
json.dumps({"language": lang,
|
|
|
|
"data": [{"text": params["input"]}]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2014-10-17 10:47:17 +00:00
|
|
|
|
2014-10-17 17:06:19 +00:00
|
|
|
response = Response()
|
2014-11-20 18:29:49 +00:00
|
|
|
polarity_value = int(res.json()["data"][0]["polarity"]) * 25
|
2014-10-17 10:47:17 +00:00
|
|
|
polarity = "marl:Neutral"
|
2014-11-20 18:29:49 +00:00
|
|
|
if polarity_value > 50:
|
2014-10-17 10:47:17 +00:00
|
|
|
polarity = "marl:Positive"
|
2014-11-20 18:29:49 +00:00
|
|
|
elif polarity_value < 50:
|
2014-10-17 10:47:17 +00:00
|
|
|
polarity = "marl:Negative"
|
2014-10-17 17:06:19 +00:00
|
|
|
entry = Entry(text=params["input"])
|
2014-11-27 09:51:00 +00:00
|
|
|
opinion = Opinion(hasPolarity=polarity, polarityValue=polarity_value)
|
2014-10-17 17:06:19 +00:00
|
|
|
entry.opinions.append(opinion)
|
|
|
|
entry.language = lang
|
|
|
|
response.entries.append(entry)
|
2014-10-17 10:47:17 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
plugin = Sentiment140Plugin()
|