sentiment140.py 2.17 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

8
class Sentiment140Plugin(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
    def analyse_entry(self, entry, params):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11
        lang = params["language"]
12
        res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
13
14
15
                            json.dumps({
                                "language": lang,
                                "data": [{
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
                                    "text": entry['nif:isString']
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
17
18
                                }]
                            }))
19
        p = params.get("prefix", None)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
20
21
        polarity_value = self.maxPolarityValue * int(
            res.json()["data"][0]["polarity"]) * 0.25
22
        polarity = "marl:Neutral"
23
24
        neutral_value = self.maxPolarityValue / 2.0
        if polarity_value > neutral_value:
25
            polarity = "marl:Positive"
26
        elif polarity_value < neutral_value:
27
            polarity = "marl:Negative"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
28

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
29
30
31
32
        sentiment = Sentiment(
            prefix=p,
            marl__hasPolarity=polarity,
            marl__polarityValue=polarity_value)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
34
35
        sentiment.prov__wasGeneratedBy = self.id
        entry.sentiments = []
        entry.sentiments.append(sentiment)
36
        entry.language = lang
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
        yield entry
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
38

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
39
40
41
42
43
44
45
46
47
48
49
50
    def test(self, *args, **kwargs):
        '''
        To avoid calling the sentiment140 API, we will mock the results
        from requests.
        '''
        from senpy.test import patch_requests
        expected = {"data": [{"polarity": 10}]}
        with patch_requests(expected) as (request, response):
            super(Sentiment140Plugin, self).test(*args, **kwargs)
            assert request.called
            assert response.json.called

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
51
52
53
    test_cases = [
        {
            'entry': {
54
                'nif:isString': 'I love Titanic'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
55
56
57
            },
            'params': {},
            'expected': {
58
                "nif:isString": "I love Titanic",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
59
60
61
62
63
64
65
66
                'sentiments': [
                    {
                        'marl:hasPolarity': 'marl:Positive',
                    }
                ]
            }
        }
    ]