__init__.py 1.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
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
import requests
import json

import sys

print(sys.path)
from senpy.plugin import SentimentPlugin

class Sentiment140Plugin(SentimentPlugin):
    def __init__(self, **kwargs):
        super(Sentiment140Plugin, self).__init__(name="Sentiment140",
                                                 version="1.0",
                                                 **kwargs)

    def analyse(self, **params):
        res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
                            json.dumps({
                                "language": "auto",
                                "data": [{"text": params["input"]}]}
                                    ))


        response = {"analysis": [{}], "entries": []}
        response["analysis"][0].update({ "marl:algorithm": "SimpleAlgorithm",
                                        "marl:minPolarityValue": 0,
                                        "marl:maxPolarityValue": 100})
        polarityValue = int(res.json()["data"][0]["polarity"]) * 25
        polarity = "marl:Neutral"
        if polarityValue > 50:
            polarity = "marl:Positive"
        elif polarityValue < 50:
            polarity = "marl:Negative"

        response["entries"] = [
            {
                "isString": params["input"],
                "opinions": [{
                    "marl:polarityValue": polarityValue,
                    "marl:hasPolarity": polarity

                }]
            }
        ]

        return response


plugin = Sentiment140Plugin()