1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-14 04:32:29 +00:00
senpy/sentiment-140/sentiment-140.py

36 lines
1.3 KiB
Python
Raw Normal View History

2017-02-01 12:46:34 +00:00
import requests
import json
from senpy.plugins import SentimentPlugin
from senpy.models import Results, Sentiment, Entry
class Sentiment140Plugin(SentimentPlugin):
2017-02-28 13:27:18 +00:00
def analyse(self,entry,params):
2017-02-01 12:46:34 +00:00
lang = params.get("language", "auto")
res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
json.dumps({"language": lang,
2017-02-28 13:27:18 +00:00
"data": [{"text": entry.get("text",None)}]
2017-02-01 12:46:34 +00:00
}
)
)
p = params.get("prefix", None)
2017-02-28 13:27:18 +00:00
2017-02-01 12:46:34 +00:00
polarity_value = self.maxPolarityValue*int(res.json()["data"][0]
["polarity"]) * 0.25
polarity = "marl:Neutral"
neutral_value = self.maxPolarityValue / 2.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)
2017-02-28 13:27:18 +00:00
yield entry