models.py 3.51 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
    _prefix = None

9
    def __init__(self, id=None, 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
        if id:
            self.id = id
15
16
17
18
        self._prefix = prefix

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

20
21
22
23
24
25
26
27
28
29
30
    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)
31

32
33
    def __delattr__(self, key):
        return super(Leaf, self).__delitem__(self._get_key(key))
34

35
    def _get_key(self, key):
36
37
        if key in ["context", "id"]:
            return "@{}".format(key)
38
39
40
41
        elif self._prefix:
            return "{}:{}".format(self._prefix, key)
        else:
            return key
42

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    @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
59

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


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

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

83
class Opinion(Leaf):
84
    #opinionContext = {"@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
85
    def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
86
        super(Opinion, self).__init__( prefix="marl",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
87
88
89
90
91
92
                                      *args,
                                      **kwargs)
        if polarityValue is not None:
            self.polarityValue = polarityValue
        if hasPolarity is not None:
            self.hasPolarity = hasPolarity
93
94
95


class EmotionSet(Leaf):
96
    emotionContext = {}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
97
    def __init__(self, emotions=None, *args, **kwargs):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
98
99
        if not emotions:
            emotions = []
100
        super(EmotionSet, self).__init__(context=EmotionSet.emotionContext,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
101
102
                                         *args,
                                         **kwargs)
103
        self.emotions = emotions or []
104
105
106
107
108
109
110

class Emotion(Leaf):
    emotionContext = {}
    def __init__(self, emotions=None, *args, **kwargs):
        super(EmotionSet, self).__init__(context=Emotion.emotionContext,
                                         *args,
                                         **kwargs)