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

38 lines
1.4 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
2015-02-23 01:13:31 +00:00
p = params.get("prefix", None)
response = Response(prefix=p)
polarity_value = self.maxPolarityValue*int(res.json()["data"][0]["polarity"]) * 0.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"
2015-02-23 01:13:31 +00:00
entry = Entry(id="Entry0",
text=params["input"],
prefix=p)
opinion = Opinion(id="Opinion0",
2015-02-23 01:13:31 +00:00
prefix=p,
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