plugins.py 2.81 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
import inspect
import os.path
6
import pickle
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
import logging
8
import tempfile
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
from . import models
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10
11
12

logger = logging.getLogger(__name__)

13

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

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

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

32
33
34
35
36
    def activate(self):
        pass

    def deactivate(self):
        pass
37

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
38
39
40
    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
41

42

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
43
class SentimentPlugin(SenpyPlugin, models.SentimentPlugin):
44
45
46
47
    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
48
        self["@type"] = "marl:SentimentAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
49

50

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
51
class EmotionPlugin(SentimentPlugin, models.EmotionPlugin):
52
53
54
    def __init__(self, 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
55
        self["@type"] = "onyx:EmotionAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
56
57
58
59
60


class ShelfMixin(object):
    @property
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
61
        if not hasattr(self, '_sh') or self._sh is None:
62
63
64
            self.__dict__['_sh'] = {}
            if os.path.isfile(self.shelf_file):
                self.__dict__['_sh'] = pickle.load(open(self.shelf_file, 'rb'))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
65
66
67
68
        return self._sh

    @sh.deleter
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
69
70
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
71
72
            del self.__dict__['_sh']
        self.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
73

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
74
75
76
77
    @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:
78
                self.__dict__['_shelf_file'] = self._info['shelf_file']
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
            else:
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
80
81
82
                self._shelf_file = os.path.join(tempfile.gettempdir(),
                                                self.name + '.p')
        return self._shelf_file
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
83

84
    def save(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
85
        logger.debug('saving pickle')
86
87
88
        if hasattr(self, '_sh') and self._sh is not None:
            with open(self.shelf_file, 'wb') as f:
                pickle.dump(self._sh, f)