plugins.py 2.72 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
from future import standard_library
standard_library.install_aliases()
3

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
5
6
import inspect
import os.path
import shelve
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
import logging
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
8
from .models import Response, PluginModel, Error
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
10
11

logger = logging.getLogger(__name__)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
class SenpyPlugin(PluginModel):
13

14
15
    def __init__(self, info=None):
        if not info:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
17
            raise Error(message=("You need to provide configuration"
                                 "information for the plugin."))
18
        logger.debug("Initialising {}".format(info))
19
        super(SenpyPlugin, self).__init__(info)
20
        self.id = '{}_{}'.format(self.name, self.version)
21
22
        self.params = info.get("extra_params", {})
        self._info = info
23
24
        if "@id" not in self.params:
            self.params["@id"] = "params_%s" % self.id
25
        self.is_activated = False
26

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
27
28
29
    def get_folder(self):
        return os.path.dirname(inspect.getfile(self.__class__))

30
    def analyse(self, *args, **kwargs):
31
        logger.debug("Analysing with: {} {}".format(self.name, self.version))
32
33
        pass

34
35
36
37
38
    def activate(self):
        pass

    def deactivate(self):
        pass
39

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40
41
42
    def __del__(self):
        ''' Destructor, to make sure all the resources are freed '''
        self.deactivate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
43

44
class SentimentPlugin(SenpyPlugin):
45

46
47
48
49
    def __init__(self, info, *args, **kwargs):
        super(SentimentPlugin, self).__init__(info, *args, **kwargs)
        self.minPolarityValue = float(info.get("minPolarityValue", 0))
        self.maxPolarityValue = float(info.get("maxPolarityValue", 1))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
50
        self["@type"] = "marl:SentimentAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
51

52

53
class EmotionPlugin(SenpyPlugin):
54

55
56
57
58
    def __init__(self, info, *args, **kwargs):
        resp = super(EmotionPlugin, self).__init__(info, *args, **kwargs)
        self.minEmotionValue = float(info.get("minEmotionValue", 0))
        self.maxEmotionValue = float(info.get("maxEmotionValue", 0))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
59
        self["@type"] = "onyx:EmotionAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
60
61
62
63
64
65


class ShelfMixin(object):

    @property
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
66
        if not hasattr(self, '_sh') or self._sh is None:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
67
68
69
70
71
            self._sh = shelve.open(self.shelf_file, writeback=True)
        return self._sh

    @sh.deleter
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
72
73
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
74
75
76
77
78
        self.close()

    def __del__(self):
        self.close()        
        self.deactivate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
80
81
82
83
84
85
86
87
88
89

    @property
    def shelf_file(self):
        if not hasattr(self, '_shelf_file') or not self._shelf_file:
            if hasattr(self, '_info') and 'shelf_file' in self._info:
                self._shelf_file = self._info['shelf_file']
            else:
                self._shelf_file = os.path.join(self.get_folder(), self.name + '.db')
        return self._shelf_file 

    def close(self):
90
        self.sh.close()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
91
        del(self._sh)