1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-09-28 17:01:43 +00:00
senpy/senpy/models.py
2014-10-17 19:06:19 +02:00

60 lines
1.8 KiB
Python

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 []