configurable_plugin.py 2.54 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/local/bin/python
# coding: utf-8

from senpy import easy_test, models, plugins

import basic


class Dictionary(plugins.SentimentPlugin):
    '''Sentiment annotation using a configurable lexicon'''

    author = '@balkian'
    version = '0.2'

    dictionaries = [basic.emojis, basic.emoticons]

17
18
    mappings = {'pos': 'marl:Positive', 'neg': 'marl:Negative'}

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
19
20
    def analyse_entry(self, entry, params):
        polarity = basic.get_polarity(entry.text, self.dictionaries)
21
22
        if polarity in self.mappings:
            polarity = self.mappings[polarity]
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

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

    test_cases = [{
        'input': 'Hello :)',
        'polarity': 'marl:Positive'
    }, {
        'input': 'So sad :(',
        'polarity': 'marl:Negative'
    }, {
        'input': 'Yay! Emojis  😁',
        'polarity': 'marl:Positive'
    }, {
        'input': 'But no emoticons 😢',
        'polarity': 'marl:Negative'
    }]


class EmojiOnly(Dictionary):
    '''Sentiment annotation with a basic lexicon of emojis'''
    description = 'A plugin'
    dictionaries = [basic.emojis]

    test_cases = [{
        'input': 'Hello :)',
        'polarity': 'marl:Neutral'
    }, {
        'input': 'So sad :(',
        'polarity': 'marl:Neutral'
    }, {
        'input': 'Yay! Emojis  😁',
        'polarity': 'marl:Positive'
    }, {
        'input': 'But no emoticons 😢',
        'polarity': 'marl:Negative'
    }]


class EmoticonsOnly(Dictionary):
    '''Sentiment annotation with a basic lexicon of emoticons'''
    dictionaries = [basic.emoticons]

    test_cases = [{
        'input': 'Hello :)',
        'polarity': 'marl:Positive'
    }, {
        'input': 'So sad :(',
        'polarity': 'marl:Negative'
    }, {
        'input': 'Yay! Emojis  😁',
        'polarity': 'marl:Neutral'
    }, {
        'input': 'But no emoticons 😢',
        'polarity': 'marl:Neutral'
    }]


class Salutes(Dictionary):
    '''Sentiment annotation with a custom lexicon, for illustration purposes'''
    dictionaries = [{
        'marl:Positive': ['Hello', '!'],
87
        'marl:Negative': ['Good bye', ]
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
88
89
90
91
92
93
    }]

    test_cases = [{
        'input': 'Hello :)',
        'polarity': 'marl:Positive'
    }, {
94
        'input': 'Good bye :(',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
95
96
97
98
99
100
101
102
103
104
105
106
        'polarity': 'marl:Negative'
    }, {
        'input': 'Yay! Emojis  😁',
        'polarity': 'marl:Positive'
    }, {
        'input': 'But no emoticons 😢',
        'polarity': 'marl:Neutral'
    }]


if __name__ == '__main__':
    easy_test()