mirror of
https://github.com/gsi-upm/senpy
synced 2024-12-22 13:08:13 +00:00
Better jsonld support
This commit is contained in:
parent
2f7a8d7267
commit
2834967026
@ -1,3 +1,4 @@
|
||||
include requirements.txt
|
||||
include README.md
|
||||
include senpy/context.jsonld
|
||||
recursive-include *.senpy
|
||||
|
2
app.py
2
app.py
@ -22,6 +22,8 @@ This class shows how to use the nif_server module to create custom services.
|
||||
import config
|
||||
from flask import Flask
|
||||
from senpy.extensions import Senpy
|
||||
import logging
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
@ -36,7 +36,7 @@ class Sentiment140Plugin(SentimentPlugin):
|
||||
elif polarity_value < 50:
|
||||
polarity = "marl:Negative"
|
||||
entry = Entry(text=params["input"])
|
||||
opinion = Opinion(polarity=polarity, polarity_value=polarity_value)
|
||||
opinion = Opinion(hasPolarity=polarity, polarityValue=polarity_value)
|
||||
entry.opinions.append(opinion)
|
||||
entry.language = lang
|
||||
response.entries.append(entry)
|
||||
|
@ -8,25 +8,26 @@
|
||||
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
|
||||
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
|
||||
"emotions": {
|
||||
"@id": "onyx:hasEmotionSet",
|
||||
"@type": "onyx:EmotionSet"
|
||||
"@container": "@set",
|
||||
"@id": "onyx:hasEmotionSet"
|
||||
},
|
||||
"opinions": {
|
||||
"@container": "@list",
|
||||
"@id": "marl:hasOpinion",
|
||||
"@type": "marl:Opinion"
|
||||
"@container": "@set",
|
||||
"@id": "marl:hasOpinion"
|
||||
},
|
||||
"prov": "http://www.w3.org/ns/prov#",
|
||||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
|
||||
"analysis": {
|
||||
"@container": "@set",
|
||||
"@id": "prov:wasInformedBy"
|
||||
},
|
||||
"entries": {
|
||||
"@container": "@set",
|
||||
"@id": "prov:generated"
|
||||
},
|
||||
"strings": {
|
||||
"@reverse": "nif:hasContext",
|
||||
"@type": "nif:String"
|
||||
"@container": "@set",
|
||||
"@reverse": "nif:hasContext"
|
||||
},
|
||||
"date":
|
||||
{
|
||||
|
@ -75,7 +75,7 @@ class Senpy(object):
|
||||
@property
|
||||
def default_plugin(self):
|
||||
candidates = self.filter_plugins(enabled=True)
|
||||
if len(candidates) > 1:
|
||||
if len(candidates) > 0:
|
||||
candidate = candidates.keys()[0]
|
||||
logger.debug("Default: {}".format(candidate))
|
||||
return candidate
|
||||
@ -169,4 +169,4 @@ class Senpy(object):
|
||||
def sentiment_plugins(self):
|
||||
""" Return only the sentiment plugins """
|
||||
return {p: plugin for p, plugin in self.plugins.items() if
|
||||
isinstance(plugin, SentimentPlugin)}
|
||||
isinstance(plugin, SentimentPlugin)}
|
||||
|
@ -4,35 +4,48 @@ from collections import defaultdict
|
||||
|
||||
|
||||
class Leaf(defaultdict):
|
||||
def __init__(self, ofclass=list):
|
||||
def __init__(self, context=None, ofclass=list):
|
||||
super(Leaf, self).__init__(ofclass)
|
||||
if context:
|
||||
self.context = context
|
||||
|
||||
def __getattr__(self, name):
|
||||
return super(Leaf, self).__getitem__(name)
|
||||
if name is not "context":
|
||||
return super(Leaf, self).__getitem__(name)
|
||||
return self["@context"]
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
self[name] = value
|
||||
name = "@context" if name is "context" else name
|
||||
self[name] = self.get_context(value)
|
||||
|
||||
def __delattr__(self, name):
|
||||
return super(Leaf, self).__delitem__(name)
|
||||
|
||||
@staticmethod
|
||||
def get_context(context):
|
||||
if isinstance(context, list):
|
||||
contexts = []
|
||||
for c in context:
|
||||
contexts.append(Response.get_context(c))
|
||||
return contexts
|
||||
elif isinstance(context, dict):
|
||||
return context
|
||||
elif isinstance(context, basestring):
|
||||
try:
|
||||
with open(context) as f:
|
||||
return json.loads(f.read())
|
||||
except IOError:
|
||||
return context
|
||||
|
||||
|
||||
class Response(Leaf):
|
||||
def __init__(self, context=None):
|
||||
super(Response, self).__init__()
|
||||
self["analysis"] = []
|
||||
self["entries"] = []
|
||||
def __init__(self, context=None, *args, **kwargs):
|
||||
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, str) or isinstance(context, unicode):
|
||||
try:
|
||||
with open(context) as f:
|
||||
self["@context"] = json.loads(f.read())
|
||||
except IOError:
|
||||
self["@context"] = context
|
||||
super(Response, self).__init__(*args, context=context, **kwargs)
|
||||
self["analysis"] = []
|
||||
self["entries"] = []
|
||||
|
||||
|
||||
class Entry(Leaf):
|
||||
@ -47,17 +60,27 @@ class Entry(Leaf):
|
||||
|
||||
|
||||
class Opinion(Leaf):
|
||||
def __init__(self, polarity_value=None, polarity=None, **kwargs):
|
||||
super(Opinion, self).__init__(**kwargs)
|
||||
if polarity_value is not None:
|
||||
self.polarity_value = polarity_value
|
||||
if polarity is not None:
|
||||
self.polarity = polarity
|
||||
opinionContext = {
|
||||
"@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"
|
||||
}
|
||||
def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
|
||||
super(Opinion, self).__init__(context=self.opinionContext,
|
||||
*args,
|
||||
**kwargs)
|
||||
if polarityValue is not None:
|
||||
self.polarityValue = polarityValue
|
||||
if hasPolarity is not None:
|
||||
self.hasPolarity = hasPolarity
|
||||
|
||||
|
||||
class EmotionSet(Leaf):
|
||||
def __init__(self, emotions=None, **kwargs):
|
||||
emotionContext = {
|
||||
"@vocab": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#"
|
||||
}
|
||||
def __init__(self, emotions=None, *args, **kwargs):
|
||||
if not emotions:
|
||||
emotions = []
|
||||
super(EmotionSet, self).__init__(**kwargs)
|
||||
super(EmotionSet, self).__init__(context=self.emotionContext,
|
||||
*args,
|
||||
**kwargs)
|
||||
self.emotions = emotions or []
|
||||
|
Loading…
Reference in New Issue
Block a user