mirror of
https://github.com/gsi-upm/senpy
synced 2024-11-14 20:42:27 +00:00
ff14925056
Moved from Yapsy again (it is not flexible enough), now we use a custom solution. The activation and deactivation of plugins is asynchronous, so that plugins that take a long time don't interfere with the rest.
35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
import requests
|
|
import json
|
|
|
|
from senpy.plugins import SentimentPlugin
|
|
from senpy.models import Response, Opinion, Entry
|
|
|
|
|
|
class Sentiment140Plugin(SentimentPlugin):
|
|
def analyse(self, **params):
|
|
lang = params.get("language", "auto")
|
|
res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
|
|
json.dumps({"language": lang,
|
|
"data": [{"text": params["input"]}]
|
|
}
|
|
)
|
|
)
|
|
|
|
response = Response()
|
|
polarity_value = int(res.json()["data"][0]["polarity"]) * 25
|
|
polarity = "marl:Neutral"
|
|
if polarity_value > 50:
|
|
polarity = "marl:Positive"
|
|
elif polarity_value < 50:
|
|
polarity = "marl:Negative"
|
|
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
|
|
entry.opinions.append(opinion)
|
|
entry.language = lang
|
|
response.entries.append(entry)
|
|
return response
|