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

34 lines
1.2 KiB
Python
Raw Permalink Normal View History

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):
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
response = Response(base=params.get("prefix", None))
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"
entry = Entry(id="Entry0", text=params["input"])
opinion = Opinion(id="Opinion0",
hasPolarity=polarity,
polarityValue=polarity_value)
2014-12-01 08:38:23 +00:00
opinion["prov:wasGeneratedBy"] = self.id
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