1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 00:02:28 +00:00

JSON-LD contexts and prefixes

This commit is contained in:
J. Fernando Sánchez 2014-11-27 17:22:25 +01:00
parent 2834967026
commit 86f45f8147
2 changed files with 34 additions and 16 deletions

7
app.py
View File

@ -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

View File

@ -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, name): def __getattr__(self, key):
if name is not "context": return super(Leaf, self).__getitem__(self._get_key(key))
return super(Leaf, self).__getitem__(name)
return self["@context"]
def __setattr__(self, name, value): def __setattr__(self, key, value):
name = "@context" if name is "context" else name try:
self[name] = self.get_context(value) object.__getattr__(self, key)
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 __delattr__(self, name): def __delattr__(self, key):
return super(Leaf, self).__delitem__(name) return super(Leaf, self).__delitem__(self._get_key(key))
def _get_key(self, key):
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):
} }
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):
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 []