plugins.py 3.67 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
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"],
                       },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
28
29
30
31
          "prefix": {"aliases": ["prefix", "p"],
                     "required": True,
                     "default": "",
          },
32
33
34
35
36
37
38
          "urischeme": {"aliases": ["urischeme", "u"],
                        "required": False,
                        "default": "RFC5147String",
                        "options": "RFC5147String"
                        },
          }

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

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

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

57
58
    def enable(self):
        self.enabled = True
59

60
61
    def disable(self):
        self.enabled = False
62

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64
65
66
    @property
    def id(self):
        return "{}_{}".format(self.name, self.version)

67
    def jsonable(self, parameters=False):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
68
        resp = {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
69
            "@id": self.id,
70
            "enabled": self.enabled,
71
        }
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
72
        if hasattr(self, "repo") and self.repo:
73
            resp["repo"] = self.repo.remotes[0].url
74
        if parameters:
75
76
77
            resp["parameters"] = self.params
        elif self.extraparams:
            resp["extra_parameters"] = self.extraparams
78
79
        return resp

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

81
82
class SentimentPlugin(SenpyPlugin):
    def __init__(self,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
83
84
                 min_polarity_value=0,
                 max_polarity_value=1,
85
86
                 **kwargs):
        super(SentimentPlugin, self).__init__(**kwargs)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
87
88
        self.minPolarityValue = min_polarity_value
        self.maxPolarityValue = max_polarity_value
89
90
91
92
93
94
95

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

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

97
98
class EmotionPlugin(SenpyPlugin):
    def __init__(self,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
99
100
101
                 min_emotion_value=0,
                 max_emotion_value=1,
                 emotion_category=None,
102
103
                 **kwargs):
        super(EmotionPlugin, self).__init__(**kwargs)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
104
105
106
        self.minEmotionValue = min_emotion_value
        self.maxEmotionValue = max_emotion_value
        self.emotionCategory = emotion_category
107
108
109
110
111
112

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