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

logger = logging.getLogger(__name__)

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
29
30
31
32
          "prefix": {"aliases": ["prefix", "p"],
                     "required": True,
                     "default": "",
          },
33
34
35
36
37
38
39
          "urischeme": {"aliases": ["urischeme", "u"],
                        "required": False,
                        "default": "RFC5147String",
                        "options": "RFC5147String"
                        },
          }

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

41
class SenpyPlugin(object):
42
43
44
45
46
47
48
49
50
51
52
    def __init__(self, info=None):
        if not info:
            raise ValueError("You need to provide configuration information for the plugin.")
        logger.debug("Initialising {}".format(info))
        self.name = info["name"]
        self.version = info["version"]
        self.params = info.get("params", PARAMS.copy())
        self.extra_params = info.get("extra_params", {})
        self.params.update(self.extra_params)
        self.is_activated = False
        self.info = info
53
54

    def analyse(self, *args, **kwargs):
55
        logger.debug("Analysing with: {} {}".format(self.name, self.version))
56
57
        pass

58
59
60
61
62
    def activate(self):
        pass

    def deactivate(self):
        pass
63
64


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

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

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

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

    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
98

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

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