mirror of
https://github.com/gsi-upm/senpy
synced 2025-12-21 10:58:16 +00:00
Added random plugin and other features
This commit is contained in:
179
senpy/models.py
179
senpy/models.py
@@ -1,21 +1,41 @@
|
||||
import json
|
||||
import os
|
||||
from collections import defaultdict
|
||||
from pyld import jsonld
|
||||
|
||||
|
||||
class Leaf(defaultdict):
|
||||
|
||||
class Leaf(dict):
|
||||
_prefix = None
|
||||
_frame = {}
|
||||
_context = {}
|
||||
|
||||
def __init__(self, id=None, context=None, prefix=None, ofclass=list):
|
||||
super(Leaf, self).__init__(ofclass)
|
||||
if context:
|
||||
def __init__(self,
|
||||
id=None,
|
||||
context=None,
|
||||
vocab=None,
|
||||
prefix=None,
|
||||
frame=None):
|
||||
super(Leaf, self).__init__()
|
||||
if context is not None:
|
||||
self.context = context
|
||||
if id:
|
||||
self.id = id
|
||||
elif self._context:
|
||||
self.context = self._context
|
||||
else:
|
||||
self.context = {}
|
||||
if frame is not None:
|
||||
self._frame = frame
|
||||
self._prefix = prefix
|
||||
self.id = id
|
||||
|
||||
def __getattr__(self, key):
|
||||
return super(Leaf, self).__getitem__(self._get_key(key))
|
||||
try:
|
||||
return object.__getattr__(self, key)
|
||||
except AttributeError:
|
||||
try:
|
||||
return super(Leaf, self).__getitem__(self._get_key(key))
|
||||
except KeyError:
|
||||
raise AttributeError()
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
try:
|
||||
@@ -23,20 +43,40 @@ class Leaf(defaultdict):
|
||||
object.__setattr__(self, key, value)
|
||||
except AttributeError:
|
||||
key = self._get_key(key)
|
||||
value = self.get_context(value) if key == "@context" else value
|
||||
if key == "@context":
|
||||
value = self.get_context(value)
|
||||
elif key == "@id":
|
||||
value = self.get_id(value)
|
||||
if key[0] == "_":
|
||||
object.__setattr__(self, key, value)
|
||||
else:
|
||||
super(Leaf, self).__setitem__(key, value)
|
||||
if value is None:
|
||||
try:
|
||||
super(Leaf, self).__delitem__(key)
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
super(Leaf, self).__setitem__(key, value)
|
||||
|
||||
def get_id(self, id):
|
||||
"""
|
||||
This is not the most elegant solution to change the @id attribute, but it
|
||||
is the quickest way to have it included in the dictionary without extra
|
||||
boilerplate.
|
||||
"""
|
||||
if id and self._prefix and ":" not in id:
|
||||
return "{}{}".format(self._prefix, id)
|
||||
else:
|
||||
return id
|
||||
|
||||
def __delattr__(self, key):
|
||||
return super(Leaf, self).__delitem__(self._get_key(key))
|
||||
|
||||
def _get_key(self, key):
|
||||
if key in ["context", "id"]:
|
||||
if key[0] == "_":
|
||||
return key
|
||||
elif key in ["context", "id"]:
|
||||
return "@{}".format(key)
|
||||
elif self._prefix:
|
||||
return "{}:{}".format(self._prefix, key)
|
||||
else:
|
||||
return key
|
||||
|
||||
@@ -56,55 +96,128 @@ class Leaf(defaultdict):
|
||||
except IOError:
|
||||
return context
|
||||
|
||||
def compact(self):
|
||||
return jsonld.compact(self, self.context)
|
||||
|
||||
def frame(self, frame=None, options=None):
|
||||
if frame is None:
|
||||
frame = self._frame
|
||||
if options is None:
|
||||
options = {}
|
||||
return jsonld.frame(self, frame, options)
|
||||
|
||||
def jsonable(self, parameters=False, frame=None, options=None, context=None):
|
||||
if frame is None:
|
||||
frame = self._frame
|
||||
if options is None:
|
||||
options = {}
|
||||
if context is None:
|
||||
context = self._context
|
||||
return jsonld.compact(jsonld.frame(self, frame, options), context)
|
||||
#if parameters:
|
||||
#resp["parameters"] = self.params
|
||||
#elif self.extra_params:
|
||||
#resp["extra_parameters"] = self.extra_params
|
||||
#return resp
|
||||
|
||||
|
||||
def to_JSON(self):
|
||||
return json.dumps(self,
|
||||
default=lambda o: o.__dict__,
|
||||
sort_keys=True, indent=4)
|
||||
|
||||
|
||||
|
||||
class Response(Leaf):
|
||||
def __init__(self, context=None, base=None, *args, **kwargs):
|
||||
_frame = { "@context": {
|
||||
"analysis": {
|
||||
"@container": "@set",
|
||||
"@id": "prov:wasInformedBy"
|
||||
},
|
||||
"date": {
|
||||
"@id": "dc:date",
|
||||
"@type": "xsd:dateTime"
|
||||
},
|
||||
"dc": "http://purl.org/dc/terms/",
|
||||
"dc:subject": {
|
||||
"@type": "@id"
|
||||
},
|
||||
"emotions": {
|
||||
"@container": "@set",
|
||||
"@id": "onyx:hasEmotionSet"
|
||||
},
|
||||
"entries": {
|
||||
"@container": "@set",
|
||||
"@id": "prov:generated"
|
||||
},
|
||||
"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#",
|
||||
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
|
||||
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
|
||||
"opinions": {
|
||||
"@container": "@set",
|
||||
"@id": "marl:hasOpinion"
|
||||
},
|
||||
"prov": "http://www.w3.org/ns/prov#",
|
||||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
|
||||
"strings": {
|
||||
"@container": "@set",
|
||||
"@reverse": "nif:hasContext"
|
||||
},
|
||||
"wnaffect": "http://www.gsi.dit.upm.es/ontologies/wnaffect#",
|
||||
"xsd": "http://www.w3.org/2001/XMLSchema#"
|
||||
},
|
||||
"analysis": {},
|
||||
"entries": {}
|
||||
}
|
||||
|
||||
def __init__(self, context=None, *args, **kwargs):
|
||||
if context is None:
|
||||
context = "{}/context.jsonld".format(os.path.dirname(
|
||||
os.path.realpath(__file__)))
|
||||
super(Response, self).__init__(*args, context=context, **kwargs)
|
||||
if base:
|
||||
self.context["@base"] = base
|
||||
self["analysis"] = []
|
||||
self["entries"] = []
|
||||
self.analysis = []
|
||||
self.entries = []
|
||||
|
||||
|
||||
class Entry(Leaf):
|
||||
_context = {
|
||||
"@vocab": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#"
|
||||
|
||||
}
|
||||
def __init__(self, text=None, emotion_sets=None, opinions=None, **kwargs):
|
||||
super(Entry, self).__init__(**kwargs)
|
||||
if text:
|
||||
self.text = text
|
||||
if emotion_sets:
|
||||
self.emotionSets = emotion_sets
|
||||
if opinions:
|
||||
self.opinions = opinions
|
||||
|
||||
self.emotionSets = emotion_sets if emotion_sets else []
|
||||
self.opinions = opinions if opinions else []
|
||||
|
||||
class Opinion(Leaf):
|
||||
#opinionContext = {"@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"}
|
||||
_context = {
|
||||
"@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"
|
||||
}
|
||||
def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
|
||||
super(Opinion, self).__init__( prefix="marl",
|
||||
*args,
|
||||
super(Opinion, self).__init__(*args,
|
||||
**kwargs)
|
||||
if polarityValue is not None:
|
||||
self.polarityValue = polarityValue
|
||||
self.hasPolarityValue = polarityValue
|
||||
if hasPolarity is not None:
|
||||
self.hasPolarity = hasPolarity
|
||||
|
||||
|
||||
class EmotionSet(Leaf):
|
||||
emotionContext = {}
|
||||
_context = {}
|
||||
def __init__(self, emotions=None, *args, **kwargs):
|
||||
if not emotions:
|
||||
emotions = []
|
||||
super(EmotionSet, self).__init__(context=EmotionSet.emotionContext,
|
||||
super(EmotionSet, self).__init__(context=EmotionSet._context,
|
||||
*args,
|
||||
**kwargs)
|
||||
self.emotions = emotions or []
|
||||
|
||||
class Emotion(Leaf):
|
||||
emotionContext = {}
|
||||
def __init__(self, emotions=None, *args, **kwargs):
|
||||
super(EmotionSet, self).__init__(context=Emotion.emotionContext,
|
||||
*args,
|
||||
**kwargs)
|
||||
_context = {}
|
||||
|
||||
class Error(Leaf):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Error, self).__init__(*args)
|
||||
self.update(kwargs)
|
||||
|
||||
Reference in New Issue
Block a user