1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-09-19 04:52:21 +00:00

V 0.2.2 - Better plugins

This commit is contained in:
J. Fernando Sánchez
2014-10-17 19:06:19 +02:00
parent 8405e5deef
commit e06fc2e671
10 changed files with 200 additions and 68 deletions

59
senpy/models.py Normal file
View File

@@ -0,0 +1,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 []