models.py 1.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import os
from collections import defaultdict

class Leaf(defaultdict):
    def __init__(self, ofclass=list):
        super(Leaf, self).__init__(ofclass)

    def __getattr__(self, name):
        return super(Leaf, self).__getitem__(name)

    def __setattr__(self, name, value):
        self[name] = value

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

class Response(Leaf):
    def __init__(self, context=None):
        super(Response, self).__init__()
        self["analysis"] = []
        self["entries"] = []
        if context is None:
            context = "{}/context.jsonld".format(os.path.dirname(
                                                 os.path.realpath(__file__)))
        if isinstance(context, dict):
            self["@context"] = context
        if isinstance(context, basestring):
            try:
                with open(context) as f:
                    self["@context"] = json.loads(f.read())
            except IOError:
                self["@context"] = context


class Entry(Leaf):
    def __init__(self, text=None, emotionSets=None, opinions=None, **kwargs):
        super(Entry, self).__init__(**kwargs)
        if text:
            self.text = text
        if emotionSets:
            self.emotionSets = emotionSets
        if opinions:
            self.opinions = opinions

class Opinion(Leaf):
    def __init__(self, polarityValue=None, polarity=None, **kwargs):
        super(Opinion, self).__init__(**kwargs)
        if polarityValue is not None:
            self.polarityValue = polarityValue
        if polarity is not None:
            self.polarity = polarity



class EmotionSet(Leaf):
    def __init__(self, emotions=[], **kwargs):
        super(EmotionSet, self).__init__(**kwargs)
        self.emotions = emotions or []