parameterized_plugin.py 1.61 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
3
4
5
6
7
8
9
#!/usr/local/bin/python
# coding: utf-8

from senpy import easy_test, models, plugins

import basic


class ParameterizedDictionary(plugins.SentimentPlugin):
10
    '''This is a basic self-contained plugin'''
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

    author = '@balkian'
    version = '0.2'

    extra_params = {
        'positive-words': {
            'description': 'Comma-separated list of words that are considered positive',
            'aliases': ['positive'],
            'required': True
        },
        'negative-words': {
            'description': 'Comma-separated list of words that are considered negative',
            'aliases': ['negative'],
            'required': False
        }
    }

    def analyse_entry(self, entry, params):
        positive_words = params['positive-words'].split(',')
        negative_words = params['negative-words'].split(',')
        dictionary = {
            'marl:Positive': positive_words,
            'marl:Negative': negative_words,
        }
        polarity = basic.get_polarity(entry.text, [dictionary])

        s = models.Sentiment(marl__hasPolarity=polarity)
        s.prov(self)
        entry.sentiments.append(s)
        yield entry

    test_cases = [
        {
            'input': 'Hello :)',
            'polarity': 'marl:Positive',
            'parameters': {
                'positive': "Hello,:)",
                'negative': "sad,:()"
            }
        },
        {
            'input': 'Hello :)',
            'polarity': 'marl:Negative',
            'parameters': {
                'positive': "",
                'negative': "Hello"
            }
        }
    ]


if __name__ == '__main__':
    easy_test()