models.py 2.69 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):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
    def __init__(self, context=None, ofclass=list):
8
        super(Leaf, self).__init__(ofclass)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
10
        if context:
            self.context = context
11
12

    def __getattr__(self, name):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13
14
15
        if name is not "context":
            return super(Leaf, self).__getitem__(name)
        return self["@context"]
16
17

    def __setattr__(self, name, value):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
18
19
        name = "@context" if name is "context" else name
        self[name] = self.get_context(value)
20
21
22
23

    def __delattr__(self, name):
        return super(Leaf, self).__delitem__(name)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    @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
40

41
class Response(Leaf):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
42
    def __init__(self, context=None, *args, **kwargs):
43
44
        if context is None:
            context = "{}/context.jsonld".format(os.path.dirname(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
45
                os.path.realpath(__file__)))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
47
48
        super(Response, self).__init__(*args, context=context, **kwargs)
        self["analysis"] = []
        self["entries"] = []
49
50
51


class Entry(Leaf):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
52
    def __init__(self, text=None, emotion_sets=None, opinions=None, **kwargs):
53
54
55
        super(Entry, self).__init__(**kwargs)
        if text:
            self.text = text
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
56
57
        if emotion_sets:
            self.emotionSets = emotion_sets
58
59
60
        if opinions:
            self.opinions = opinions

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

62
class Opinion(Leaf):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64
65
66
67
68
69
70
71
72
73
    opinionContext = {
        "@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"
    }
    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
74
75
76


class EmotionSet(Leaf):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
77
78
79
80
    emotionContext = {
        "@vocab": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#"
    }
    def __init__(self, emotions=None, *args, **kwargs):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
81
82
        if not emotions:
            emotions = []
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
83
84
85
        super(EmotionSet, self).__init__(context=self.emotionContext,
                                         *args,
                                         **kwargs)
86
        self.emotions = emotions or []