plugins.py 3.44 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
3
4
import logging

logger = logging.getLogger(__name__)

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
PARAMS = {"input": {"aliases": ["i", "input"],
                    "required": True,
                    "help": "Input text"
                    },
          "informat": {"aliases": ["f", "informat"],
                       "required": False,
                       "default": "text",
                       "options": ["turtle", "text"],
                       },
          "intype": {"aliases": ["intype", "t"],
                     "required": False,
                     "default": "direct",
                     "options": ["direct", "url", "file"],
                     },
          "outformat": {"aliases": ["outformat", "o"],
                        "default": "json-ld",
                        "required": False,
                        "options": ["json-ld"],
                        },
          "language": {"aliases": ["language", "l"],
                       "required": False,
                       "options": ["es", "en"],
                       },
          "urischeme": {"aliases": ["urischeme", "u"],
                        "required": False,
                        "default": "RFC5147String",
                        "options": "RFC5147String"
                        },
          }

35
class SenpyPlugin(object):
36
    def __init__(self, name=None, version=None, extraparams=None, params=None):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
        logger.debug("Initialising {}".format(name))
38
39
        self.name = name
        self.version = version
40
41
42
43
44
45
46
47
        if params:
            self.params = params
        else:
            self.params = PARAMS.copy()
            if extraparams:
                self.params.update(extraparams)
        self.extraparams = extraparams or {}
        self.enabled = True
48
49
50
51

    def analyse(self, *args, **kwargs):
        pass

52
53
    def enable(self):
        self.enabled = True
54

55
56
    def disable(self):
        self.enabled = False
57
58
59
60

    def jsonable(self, parameters=False):
        resp =  {
            "@id": "{}_{}".format(self.name, self.version),
61
            "enabled": self.enabled,
62
        }
63
64
        if self.repo:
            resp["repo"] = self.repo.remotes[0].url
65
        if parameters:
66
67
68
            resp["parameters"] = self.params
        elif self.extraparams:
            resp["extra_parameters"] = self.extraparams
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        return resp

class SentimentPlugin(SenpyPlugin):
    def __init__(self,
                 minPolarityValue=0,
                 maxPolarityValue=1,
                 **kwargs):
        super(SentimentPlugin, self).__init__(**kwargs)
        self.minPolarityValue = minPolarityValue
        self.maxPolarityValue = maxPolarityValue

    def jsonable(self, *args, **kwargs):
        resp = super(SentimentPlugin, self).jsonable(*args, **kwargs)
        resp["marl:maxPolarityValue"] = self.maxPolarityValue
        resp["marl:minPolarityValue"] = self.minPolarityValue
        return resp

class EmotionPlugin(SenpyPlugin):
    def __init__(self,
                 minEmotionValue=0,
                 maxEmotionValue=1,
                 emotionCategory=None,
                 **kwargs):
        super(EmotionPlugin, self).__init__(**kwargs)
        self.minEmotionValue = minEmotionValue
        self.maxEmotionValue = maxEmotionValue
        self.emotionCategory = emotionCategory

    def jsonable(self, *args, **kwargs):
        resp = super(EmotionPlugin, self).jsonable(*args, **kwargs)
        resp["onyx:minEmotionValue"] = self.minEmotionValue
        resp["onyx:maxEmotionValue"] = self.maxEmotionValue
        return resp