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)
|
2015-02-24 06:15:25 +00:00
|
|
|
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)
|
2014-12-02 12:31:15 +00:00
|
|
|
opinion = Opinion(id="Opinion0",
|
2015-02-23 01:13:31 +00:00
|
|
|
prefix=p,
|
2014-12-02 12:31:15 +00:00
|
|
|
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
|