1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-09-29 01:11:42 +00:00
senpy/sentiment-140/sentiment-140.py
militarpancho b671ff51f9 Add support for py3 in emotion-wnaffect
Normalize polarity values in sentiment-basic and sentiment-140
2017-07-14 11:13:59 +02:00

37 lines
1.3 KiB
Python

import requests
import json
from senpy.plugins import SentimentPlugin
from senpy.models import Results, Sentiment, Entry
class Sentiment140Plugin(SentimentPlugin):
def analyse_entry(self,entry,params):
lang = params.get("language", "auto")
res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
json.dumps({"language": lang,
"data": [{"text": entry.get("text",None)}]
}
)
)
p = params.get("prefix", None)
polarity_value = self.maxPolarityValue*int(res.json()["data"][0]
["polarity"]) * 0.25
polarity = "marl:Neutral"
neutral_value = 0
if polarity_value > neutral_value:
polarity = "marl:Positive"
elif polarity_value < neutral_value:
polarity = "marl:Negative"
sentiment = Sentiment(id="Sentiment0",
prefix=p,
marl__hasPolarity=polarity,
marl__polarityValue=polarity_value)
entry.sentiments.append(sentiment)
yield entry