models.py 3.06 KB
Newer Older
1
2
3
4
import json
import os
from collections import defaultdict

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

6
class Leaf(defaultdict):
7
8
9
    _prefix = None

    def __init__(self, context=None, prefix=None, ofclass=list):
10
        super(Leaf, self).__init__(ofclass)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11
12
        if context:
            self.context = context
13
14
15
16
        self._prefix = prefix

    def __getattr__(self, key):
        return super(Leaf, self).__getitem__(self._get_key(key))
17

18
19
20
21
22
23
24
25
26
27
28
    def __setattr__(self, key, value):
        try:
            object.__getattr__(self, key)
            object.__setattr__(self, key, value)
        except AttributeError:
            key = self._get_key(key)
            value = self.get_context(value) if key == "@context" else value
            if key[0] == "_":
                object.__setattr__(self, key, value)
            else:
                super(Leaf, self).__setitem__(key, value)
29

30
31
    def __delattr__(self, key):
        return super(Leaf, self).__delitem__(self._get_key(key))
32

33
34
35
36
37
38
39
    def _get_key(self, key):
        if key is "context":
            return "@context"
        elif self._prefix:
            return "{}:{}".format(self._prefix, key)
        else:
            return key
40

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    @staticmethod
    def get_context(context):
        if isinstance(context, list):
            contexts = []
            for c in context:
                contexts.append(Response.get_context(c))
            return contexts
        elif isinstance(context, dict):
            return context
        elif isinstance(context, basestring):
            try:
                with open(context) as f:
                    return json.loads(f.read())
            except IOError:
                return context

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

58
class Response(Leaf):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
59
    def __init__(self, context=None, *args, **kwargs):
60
61
        if context is None:
            context = "{}/context.jsonld".format(os.path.dirname(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
62
                os.path.realpath(__file__)))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64
65
        super(Response, self).__init__(*args, context=context, **kwargs)
        self["analysis"] = []
        self["entries"] = []
66
67
68


class Entry(Leaf):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
69
    def __init__(self, text=None, emotion_sets=None, opinions=None, **kwargs):
70
71
72
        super(Entry, self).__init__(**kwargs)
        if text:
            self.text = text
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
73
74
        if emotion_sets:
            self.emotionSets = emotion_sets
75
76
77
        if opinions:
            self.opinions = opinions

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

79
class Opinion(Leaf):
80
    opinionContext = {}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
81
82
83
84
85
86
87
88
    def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
        super(Opinion, self).__init__(context=self.opinionContext,
                                      *args,
                                      **kwargs)
        if polarityValue is not None:
            self.polarityValue = polarityValue
        if hasPolarity is not None:
            self.hasPolarity = hasPolarity
89
90
91


class EmotionSet(Leaf):
92
    emotionContext = {}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
93
    def __init__(self, emotions=None, *args, **kwargs):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94
95
        if not emotions:
            emotions = []
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
96
97
98
        super(EmotionSet, self).__init__(context=self.emotionContext,
                                         *args,
                                         **kwargs)
99
        self.emotions = emotions or []