sentiment140_plugin.py 2.53 KB
Newer Older
1
2
3
import requests
import json

4
from senpy.plugins import SentimentPlugin
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
5
from senpy.models import Sentiment
6

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
8
class Sentiment140(SentimentPlugin):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
    '''Connects to the sentiment140 free API: http://sentiment140.com'''
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

    author = "@balkian"
    version = '0.2'
    url = "https://github.com/gsi-upm/senpy-plugins-community"
    extra_params = {
        'language': {
            "@id": 'lang_sentiment140',
            'aliases': ['language', 'l'],
            'required': False,
            'default': 'auto',
            'options': ['es', 'en', 'auto']
        }
    }

    maxPolarityValue = 1
    minPolarityValue = 0

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
27
    def analyse_entry(self, entry, params):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
28
        lang = params["language"]
29
        res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
30
31
32
                            json.dumps({
                                "language": lang,
                                "data": [{
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
                                    "text": entry['nif:isString']
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
34
35
                                }]
                            }))
36
        p = params.get("prefix", None)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
38
        polarity_value = self.maxPolarityValue * int(
            res.json()["data"][0]["polarity"]) * 0.25
39
        polarity = "marl:Neutral"
40
41
        neutral_value = self.maxPolarityValue / 2.0
        if polarity_value > neutral_value:
42
            polarity = "marl:Positive"
43
        elif polarity_value < neutral_value:
44
            polarity = "marl:Negative"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
45

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
46
47
48
49
        sentiment = Sentiment(
            prefix=p,
            marl__hasPolarity=polarity,
            marl__polarityValue=polarity_value)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
50
51
        sentiment.prov__wasGeneratedBy = self.id
        entry.sentiments.append(sentiment)
52
        entry.language = lang
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
53
        yield entry
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
54

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
55
56
57
58
59
    def test(self, *args, **kwargs):
        '''
        To avoid calling the sentiment140 API, we will mock the results
        from requests.
        '''
60
        from senpy.testing import patch_requests
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
61
        expected = {"data": [{"polarity": 4}]}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
62
        with patch_requests(expected) as (request, response):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
            super(Sentiment140, self).test(*args, **kwargs)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
64
65
66
            assert request.called
            assert response.json.called

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
67
68
69
    test_cases = [
        {
            'entry': {
70
                'nif:isString': 'I love Titanic'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
71
72
73
            },
            'params': {},
            'expected': {
74
                "nif:isString": "I love Titanic",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
76
77
78
79
80
81
82
                'sentiments': [
                    {
                        'marl:hasPolarity': 'marl:Positive',
                    }
                ]
            }
        }
    ]