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

75 lines
1.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from vaderSentiment import sentiment
2019-04-04 10:56:46 +00:00
from senpy.plugins import SentimentBox, SenpyPlugin
from senpy.models import Results, Sentiment, Entry
import logging
2019-04-04 10:56:46 +00:00
class VaderSentimentPlugin(SentimentBox):
2018-06-14 17:38:08 +00:00
'''
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": {
2019-04-04 10:56:46 +00:00
"description": "language of the input",
2018-06-14 17:38:08 +00:00
"@id": "lang_rand",
"aliases": ["language", "l"],
"default": "auto",
"options": ["es", "en", "auto"]
},
2018-06-14 17:38:08 +00:00
"aggregate": {
2019-04-04 10:56:46 +00:00
"description": "Show only the strongest sentiment (aggregate) or all sentiments",
2018-06-14 17:38:08 +00:00
"aliases": ["aggregate","agg"],
2019-04-04 10:56:46 +00:00
"options": [True, False],
2018-06-14 17:38:08 +00:00
"default": False
}
2018-06-14 17:38:08 +00:00
}
requirements = {}
2019-04-04 10:56:46 +00:00
_VADER_KEYS = ['pos', 'neu', 'neg']
binary = False
2018-06-14 17:38:08 +00:00
2019-04-04 10:56:46 +00:00
def predict_one(self, features, activity):
text_input = ' '.join(features)
scores = sentiment(text_input)
2019-04-04 10:56:46 +00:00
sentiments = []
for k in self._VADER_KEYS:
sentiments.append(scores[k])
2019-04-04 10:56:46 +00:00
if activity.param('aggregate'):
m = max(sentiments)
sentiments = [k if k==m else None for k in sentiments]
2018-06-14 17:38:08 +00:00
2019-04-04 10:56:46 +00:00
return sentiments
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'
},
]