1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-10 19:12:28 +00:00
senpy/sentiment-vader/vader_plugin.py

97 lines
2.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from vaderSentiment import sentiment
from senpy.plugins import SentimentPlugin, SenpyPlugin
from senpy.models import Results, Sentiment, Entry
import logging
2018-06-14 17:38:08 +00:00
class VaderSentimentPlugin(SentimentPlugin):
'''
Sentiment classifier using vaderSentiment module. Params accepted: Language: {en, es}. The output uses Marl ontology developed at GSI UPM for semantic web.
'''
name = "sentiment-vader"
module = "sentiment-vader"
author = "@icorcuera"
version = "0.1.1"
extra_params = {
"language": {
"@id": "lang_rand",
"aliases": ["language", "l"],
"default": "auto",
"options": ["es", "en", "auto"]
},
2018-06-14 17:38:08 +00:00
"aggregate": {
"aliases": ["aggregate","agg"],
"options": ["true", "false"],
"default": False
}
2018-06-14 17:38:08 +00:00
}
requirements = {}
2018-06-14 17:38:08 +00:00
def analyse_entry(self, entry, params):
self.log.debug("Analysing with params {}".format(params))
text_input = entry.text
aggregate = params['aggregate']
score = sentiment(text_input)
opinion0 = Sentiment(id= "Opinion_positive",
marl__hasPolarity= "marl:Positive",
marl__algorithmConfidence= score['pos']
)
2018-06-14 17:38:08 +00:00
opinion0.prov(self)
opinion1 = Sentiment(id= "Opinion_negative",
marl__hasPolarity= "marl:Negative",
marl__algorithmConfidence= score['neg']
)
2018-06-14 17:38:08 +00:00
opinion1.prov(self)
opinion2 = Sentiment(id= "Opinion_neutral",
marl__hasPolarity = "marl:Neutral",
marl__algorithmConfidence = score['neu']
)
2018-06-14 17:38:08 +00:00
opinion2.prov(self)
if aggregate == 'true':
res = None
confident = max(score['neg'],score['neu'],score['pos'])
if opinion0.marl__algorithmConfidence == confident:
res = opinion0
elif opinion1.marl__algorithmConfidence == confident:
res = opinion1
elif opinion2.marl__algorithmConfidence == confident:
res = opinion2
entry.sentiments.append(res)
else:
entry.sentiments.append(opinion0)
entry.sentiments.append(opinion1)
entry.sentiments.append(opinion2)
yield entry
2018-06-14 17:38:08 +00:00
test_cases = []
test_cases = [
{
'input': 'I am tired :(',
'polarity': 'marl:Negative'
},
{
'input': 'I love pizza :(',
'polarity': 'marl:Positive'
},
{
'input': 'I enjoy going to the cinema :)',
'polarity': 'marl:Negative'
},
{
'input': 'This cake is disgusting',
'polarity': 'marl:Negative'
},
]