Commit 86f45f81 authored by J. Fernando Sánchez's avatar J. Fernando Sánchez
Browse files

JSON-LD contexts and prefixes

parent 28349670
...@@ -23,12 +23,13 @@ import config ...@@ -23,12 +23,13 @@ import config
from flask import Flask from flask import Flask
from senpy.extensions import Senpy from senpy.extensions import Senpy
import logging import logging
import os
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__) app = Flask(__name__)
mypath = os.path.dirname(os.path.realpath(__file__))
sp = Senpy() sp = Senpy(app, os.path.join(mypath, "plugins"))
sp.init_app(app)
if __name__ == '__main__': if __name__ == '__main__':
app.debug = config.DEBUG app.debug = config.DEBUG
......
...@@ -4,22 +4,39 @@ from collections import defaultdict ...@@ -4,22 +4,39 @@ from collections import defaultdict
class Leaf(defaultdict): class Leaf(defaultdict):
def __init__(self, context=None, ofclass=list): _prefix = None
def __init__(self, context=None, prefix=None, ofclass=list):
super(Leaf, self).__init__(ofclass) super(Leaf, self).__init__(ofclass)
if context: if context:
self.context = context self.context = context
self._prefix = prefix
def __getattr__(self, key):
return super(Leaf, self).__getitem__(self._get_key(key))
def __getattr__(self, name): def __setattr__(self, key, value):
if name is not "context": try:
return super(Leaf, self).__getitem__(name) object.__getattr__(self, key)
return self["@context"] object.__setattr__(self, key, value)
except AttributeError:
key = self._get_key(key)
value = self.get_context(value) if key == "@context" else value
if key[0] == "_":
object.__setattr__(self, key, value)
else:
super(Leaf, self).__setitem__(key, value)
def __setattr__(self, name, value): def __delattr__(self, key):
name = "@context" if name is "context" else name return super(Leaf, self).__delitem__(self._get_key(key))
self[name] = self.get_context(value)
def __delattr__(self, name): def _get_key(self, key):
return super(Leaf, self).__delitem__(name) if key is "context":
return "@context"
elif self._prefix:
return "{}:{}".format(self._prefix, key)
else:
return key
@staticmethod @staticmethod
def get_context(context): def get_context(context):
...@@ -65,6 +82,7 @@ class Opinion(Leaf): ...@@ -65,6 +82,7 @@ class Opinion(Leaf):
} }
def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs): def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
super(Opinion, self).__init__(context=self.opinionContext, super(Opinion, self).__init__(context=self.opinionContext,
prefix="marl",
*args, *args,
**kwargs) **kwargs)
if polarityValue is not None: if polarityValue is not None:
...@@ -74,13 +92,12 @@ class Opinion(Leaf): ...@@ -74,13 +92,12 @@ class Opinion(Leaf):
class EmotionSet(Leaf): class EmotionSet(Leaf):
emotionContext = { emotionContext = {}
"@vocab": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#"
}
def __init__(self, emotions=None, *args, **kwargs): def __init__(self, emotions=None, *args, **kwargs):
if not emotions: if not emotions:
emotions = [] emotions = []
super(EmotionSet, self).__init__(context=self.emotionContext, super(EmotionSet, self).__init__(context=self.emotionContext,
prefix="onyx",
*args, *args,
**kwargs) **kwargs)
self.emotions = emotions or [] self.emotions = emotions or []
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment