1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-09-21 06:01:43 +00:00
senpy/plugins/sentiment140/sentiment140.py

35 lines
1.3 KiB
Python
Raw 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
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-12-01 08:38:23 +00:00
entry = Entry(text=params["input"],
prefix=params.get("prefix", ""))
opinion = Opinion(hasPolarity=polarity,
polarityValue=polarity_value,
prefix=params.get("prefix", ""))
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