mirror of https://github.com/gsi-upm/senpy
Python 3 compatible
There are also some slight changes to the JSON schemas and the use of JSON-LD.pull/8/head
parent
a79df7a3da
commit
14c9f61864
@ -1,42 +0,0 @@
|
|||||||
{
|
|
||||||
"dc": "http://purl.org/dc/terms/",
|
|
||||||
"dc:subject": {
|
|
||||||
"@type": "@id"
|
|
||||||
},
|
|
||||||
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
|
||||||
"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#",
|
|
||||||
"emotions": {
|
|
||||||
"@container": "@set",
|
|
||||||
"@id": "onyx:hasEmotionSet"
|
|
||||||
},
|
|
||||||
"opinions": {
|
|
||||||
"@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": {
|
|
||||||
"@container": "@set",
|
|
||||||
"@reverse": "nif:hasContext"
|
|
||||||
},
|
|
||||||
"date":
|
|
||||||
{
|
|
||||||
"@id": "dc:date",
|
|
||||||
"@type": "xsd:dateTime"
|
|
||||||
},
|
|
||||||
"text": { "@id": "nif:isString" },
|
|
||||||
"wnaffect": "http://www.gsi.dit.upm.es/ontologies/wnaffect#",
|
|
||||||
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
|
||||||
"senpy": "http://www.gsi.dit.upm.es/ontologies/senpy/ns#",
|
|
||||||
"@vocab": "http://www.gsi.dit.upm.es/ontologies/senpy/ns#"
|
|
||||||
}
|
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/Analysis"
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
{
|
||||||
|
"@vocab": "http://www.gsi.dit.upm.es/ontologies/senpy#",
|
||||||
|
"dc": "http://dublincore.org/2012/06/14/dcelements#",
|
||||||
|
"me": "http://www.mixedemotions-project.eu/ns/model#",
|
||||||
|
"prov": "http://www.w3.org/ns/prov#",
|
||||||
|
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
|
||||||
|
"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#",
|
||||||
|
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx#",
|
||||||
|
"wnaffect": "http://www.gsi.dit.upm.es/ontologies/wnaffect#",
|
||||||
|
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
||||||
|
"topics": {
|
||||||
|
"@id": "dc:subject"
|
||||||
|
},
|
||||||
|
"entities": {
|
||||||
|
"@id": "me:hasEntities"
|
||||||
|
},
|
||||||
|
"suggestions": {
|
||||||
|
"@id": "me:hasSuggestions"
|
||||||
|
},
|
||||||
|
"emotions": {
|
||||||
|
"@id": "onyx:hasEmotionSet"
|
||||||
|
},
|
||||||
|
"sentiments": {
|
||||||
|
"@id": "marl:hasOpinion"
|
||||||
|
},
|
||||||
|
"entries": {
|
||||||
|
"@id": "prov:used"
|
||||||
|
},
|
||||||
|
"analysis": {
|
||||||
|
"@id": "prov:wasGeneratedBy"
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,161 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"Results": {
|
||||||
|
"title": "Results",
|
||||||
|
"description": "The results of an analysis",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"@context": {
|
||||||
|
"$ref": "#/Context"
|
||||||
|
},
|
||||||
|
"@id": {
|
||||||
|
"description": "ID of the analysis",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"analysis": {
|
||||||
|
"type": "array",
|
||||||
|
"default": [],
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/Analysis"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"entries": {
|
||||||
|
"type": "array",
|
||||||
|
"default": [],
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/Entry"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
"required": ["@id", "analysis", "entries"]
|
||||||
|
},
|
||||||
|
"Context": {
|
||||||
|
"description": "JSON-LD Context",
|
||||||
|
"type": ["array", "string", "object"]
|
||||||
|
},
|
||||||
|
"Analysis": {
|
||||||
|
"description": "Senpy analysis",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"@id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"@type": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Type of the analysis. e.g. marl:SentimentAnalysis"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["@id", "@type"]
|
||||||
|
},
|
||||||
|
"Entry": {
|
||||||
|
"properties": {
|
||||||
|
"@id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"@type": {
|
||||||
|
"enum": [["nif:RFC5147String", "nif:Context"]]
|
||||||
|
},
|
||||||
|
"nif:isString": {
|
||||||
|
"description": "String contained in this Context",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"sentiments": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/Sentiment" }
|
||||||
|
},
|
||||||
|
"emotions": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/EmotionSet" }
|
||||||
|
},
|
||||||
|
"entities": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/Entity" }
|
||||||
|
},
|
||||||
|
"topics": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/Topic" }
|
||||||
|
},
|
||||||
|
"suggestions": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/Suggestion" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["@id", "nif:isString"]
|
||||||
|
},
|
||||||
|
"Sentiment": {
|
||||||
|
"properties": {
|
||||||
|
"@id": {"type": "string"},
|
||||||
|
"nif:beginIndex": {"type": "integer"},
|
||||||
|
"nif:endIndex": {"type": "integer"},
|
||||||
|
"nif:anchorOf": {
|
||||||
|
"description": "Piece of context that contains the Sentiment",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"marl:hasPolarity": {
|
||||||
|
"enum": ["marl:Positive", "marl:Negative", "marl:Neutral"]
|
||||||
|
},
|
||||||
|
"marl:polarityValue": {
|
||||||
|
"type": "number"
|
||||||
|
},
|
||||||
|
"prov:wasGeneratedBy": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The ID of the analysis that generated this Sentiment. The full object should be included in the \"analysis\" property of the root object"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["@id", "prov:wasGeneratedBy"]
|
||||||
|
},
|
||||||
|
"EmotionSet": {
|
||||||
|
"properties": {
|
||||||
|
"@id": {"type": "string"},
|
||||||
|
"nif:beginIndex": {"type": "integer"},
|
||||||
|
"nif:endIndex": {"type": "integer"},
|
||||||
|
"nif:anchorOf": {
|
||||||
|
"description": "Piece of context that contains the Sentiment",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"onyx:hasEmotion": {
|
||||||
|
"$ref": "#/Emotion"
|
||||||
|
},
|
||||||
|
"prov:wasGeneratedBy": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The ID of the analysis that generated this Emotion. The full object should be included in the \"analysis\" property of the root object"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["@id", "prov:wasGeneratedBy", "onyx:hasEmotion"]
|
||||||
|
},
|
||||||
|
"Emotion": {
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"Entity": {
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"Topic": {
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"Suggestion": {
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"Plugins": {
|
||||||
|
"properties": {
|
||||||
|
"plugins": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/Plugin"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Plugin": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["@id"],
|
||||||
|
"properties": {
|
||||||
|
"@id": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"type": "object"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/Emotion"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/EmotionSet"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/Entry"
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"$ref": "definitions.json#/Plugin"
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"$ref": "definitions.json#/Plugins"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/Response"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/Results"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/Sentiment"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"$ref": "definitions.json#/Suggestion"
|
||||||
|
}
|
@ -1,2 +1,4 @@
|
|||||||
[metadata]
|
[metadata]
|
||||||
description-file = README.rst
|
description-file = README.rst
|
||||||
|
[aliases]
|
||||||
|
test=pytest
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
{
|
|
||||||
"dc": "http://purl.org/dc/terms/",
|
|
||||||
"dc:subject": {
|
|
||||||
"@type": "@id"
|
|
||||||
},
|
|
||||||
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
|
||||||
"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#",
|
|
||||||
"emotions": {
|
|
||||||
"@container": "@set",
|
|
||||||
"@id": "onyx:hasEmotionSet"
|
|
||||||
},
|
|
||||||
"opinions": {
|
|
||||||
"@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": {
|
|
||||||
"@container": "@set",
|
|
||||||
"@reverse": "nif:hasContext"
|
|
||||||
},
|
|
||||||
"date":
|
|
||||||
{
|
|
||||||
"@id": "dc:date",
|
|
||||||
"@type": "xsd:dateTime"
|
|
||||||
},
|
|
||||||
"text": { "@id": "nif:isString" },
|
|
||||||
"wnaffect": "http://www.gsi.dit.upm.es/ontologies/wnaffect#",
|
|
||||||
"xsd": "http://www.w3.org/2001/XMLSchema#"
|
|
||||||
}
|
|
@ -1,8 +1,8 @@
|
|||||||
from senpy.plugins import SentimentPlugin
|
from senpy.plugins import SentimentPlugin
|
||||||
from senpy.models import Response
|
from senpy.models import Results
|
||||||
|
|
||||||
|
|
||||||
class DummyPlugin(SentimentPlugin):
|
class DummyPlugin(SentimentPlugin):
|
||||||
|
|
||||||
def analyse(self, *args, **kwargs):
|
def analyse(self, *args, **kwargs):
|
||||||
return Response()
|
return Results()
|
||||||
|
@ -1,81 +0,0 @@
|
|||||||
import os
|
|
||||||
import logging
|
|
||||||
|
|
||||||
try:
|
|
||||||
import unittest.mock as mock
|
|
||||||
except ImportError:
|
|
||||||
import mock
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
from unittest import TestCase
|
|
||||||
from senpy.models import Response, Entry
|
|
||||||
from senpy.plugins import SenpyPlugin
|
|
||||||
|
|
||||||
|
|
||||||
class ModelsTest(TestCase):
|
|
||||||
|
|
||||||
def test_response(self):
|
|
||||||
r = Response(context=os.path.normpath(
|
|
||||||
os.path.join(__file__, "..", "..", "context.jsonld")))
|
|
||||||
assert("@context" in r)
|
|
||||||
assert(r._frame)
|
|
||||||
logging.debug("Default frame: %s", r._frame)
|
|
||||||
assert("marl" in r.context)
|
|
||||||
assert("entries" in r.context)
|
|
||||||
|
|
||||||
r2 = Response(context=json.loads('{"test": "roger"}'))
|
|
||||||
assert("test" in r2.context)
|
|
||||||
|
|
||||||
r3 = Response(context=None)
|
|
||||||
del r3.context
|
|
||||||
assert("@context" not in r3)
|
|
||||||
assert("entries" in r3)
|
|
||||||
assert("analysis" in r3)
|
|
||||||
|
|
||||||
r4 = Response()
|
|
||||||
assert("@context" in r4)
|
|
||||||
assert("entries" in r4)
|
|
||||||
assert("analysis" in r4)
|
|
||||||
|
|
||||||
dummy = SenpyPlugin({"name": "dummy", "version": 0})
|
|
||||||
r5 = Response({"dummy": dummy}, context=None, frame=None)
|
|
||||||
logging.debug("Response 5: %s", r5)
|
|
||||||
assert("dummy" in r5)
|
|
||||||
assert(r5["dummy"].name == "dummy")
|
|
||||||
js = r5.jsonld(context={}, frame={})
|
|
||||||
logging.debug("jsonld 5: %s", js)
|
|
||||||
assert("dummy" in js)
|
|
||||||
assert(js["dummy"].name == "dummy")
|
|
||||||
|
|
||||||
r6 = Response()
|
|
||||||
r6.entries.append(Entry(text="Just testing"))
|
|
||||||
logging.debug("Reponse 6: %s", r6)
|
|
||||||
assert("@context" in r6)
|
|
||||||
assert("marl" in r6.context)
|
|
||||||
assert("entries" in r6.context)
|
|
||||||
js = r6.jsonld()
|
|
||||||
logging.debug("jsonld: %s", js)
|
|
||||||
assert("entries" in js)
|
|
||||||
assert("entries" in js)
|
|
||||||
assert("analysis" in js)
|
|
||||||
resp = r6.flask()
|
|
||||||
received = json.loads(resp.data.decode())
|
|
||||||
logging.debug("Response: %s", js)
|
|
||||||
assert(received["entries"])
|
|
||||||
assert(received["entries"][0]["text"] == "Just testing")
|
|
||||||
assert(received["entries"][0]["text"] != "Not testing")
|
|
||||||
|
|
||||||
def test_opinions(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_plugins(self):
|
|
||||||
p = SenpyPlugin({"name": "dummy", "version": 0})
|
|
||||||
c = p.jsonld()
|
|
||||||
assert "info" not in c
|
|
||||||
assert "repo" not in c
|
|
||||||
assert "params" not in c
|
|
||||||
logging.debug("Framed: %s", c)
|
|
||||||
assert "extra_params" in c
|
|
||||||
|
|
||||||
def test_frame_response(self):
|
|
||||||
pass
|
|
@ -0,0 +1,97 @@
|
|||||||
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import jsonschema
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from unittest import TestCase
|
||||||
|
from senpy.models import Response, Entry, Results, Sentiment, EmotionSet, Error
|
||||||
|
from senpy.plugins import SenpyPlugin
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
|
||||||
|
class ModelsTest(TestCase):
|
||||||
|
|
||||||
|
def test_jsonld(self):
|
||||||
|
ctx = os.path.normpath(os.path.join(__file__, "..", "..", "..", "senpy", "schemas", "context.jsonld"))
|
||||||
|
prueba = {"@id": "test",
|
||||||
|
"analysis": [],
|
||||||
|
"entries": []}
|
||||||
|
r = Results(**prueba)
|
||||||
|
|
||||||
|
print("Response's context: ")
|
||||||
|
pprint(r.context)
|
||||||
|
|
||||||
|
j = r.jsonld(with_context=True)
|
||||||
|
print("As JSON:")
|
||||||
|
pprint(j)
|
||||||
|
assert("@context" in j)
|
||||||
|
assert("marl" in j["@context"])
|
||||||
|
assert("entries" in j["@context"])
|
||||||
|
|
||||||
|
r6 = Results(**prueba)
|
||||||
|
r6.entries.append(Entry({"@id":"ohno", "nif:isString":"Just testing"}))
|
||||||
|
logging.debug("Reponse 6: %s", r6)
|
||||||
|
assert("marl" in r6.context)
|
||||||
|
assert("entries" in r6.context)
|
||||||
|
j6 = r6.jsonld(with_context=True)
|
||||||
|
logging.debug("jsonld: %s", j6)
|
||||||
|
assert("@context" in j6)
|
||||||
|
assert("entries" in j6)
|
||||||
|
assert("analysis" in j6)
|
||||||
|
resp = r6.flask()
|
||||||
|
received = json.loads(resp.data.decode())
|
||||||
|
logging.debug("Response: %s", j6)
|
||||||
|
assert(received["entries"])
|
||||||
|
assert(received["entries"][0]["nif:isString"] == "Just testing")
|
||||||
|
assert(received["entries"][0]["nif:isString"] != "Not testing")
|
||||||
|
|
||||||
|
def test_entries(self):
|
||||||
|
e = Entry()
|
||||||
|
self.assertRaises(jsonschema.ValidationError, e.validate)
|
||||||
|
e.nif__isString = "this is a test"
|
||||||
|
e.nif__beginIndex = 0
|
||||||
|
e.nif__endIndex = 10
|
||||||
|
e.validate()
|
||||||
|
|
||||||
|
def test_sentiment(self):
|
||||||
|
s = Sentiment()
|
||||||
|
self.assertRaises(jsonschema.ValidationError, s.validate)
|
||||||
|
s.nif__anchorOf = "so much testing"
|
||||||
|
s.prov__wasGeneratedBy = ""
|
||||||
|
s.validate()
|
||||||
|
|
||||||
|
def test_emotion_set(self):
|
||||||
|
e = EmotionSet()
|
||||||
|
self.assertRaises(jsonschema.ValidationError, e.validate)
|
||||||
|
e.nif__anchorOf = "so much testing"
|
||||||
|
e.prov__wasGeneratedBy = ""
|
||||||
|
self.assertRaises(jsonschema.ValidationError, e.validate)
|
||||||
|
e.onyx__hasEmotion = {}
|
||||||
|
e.validate()
|
||||||
|
|
||||||
|
def test_results(self):
|
||||||
|
r = Results()
|
||||||
|
e = Entry()
|
||||||
|
e.nif__isString = "Results test"
|
||||||
|
r.entries.append(e)
|
||||||
|
r.id = ":test_results"
|
||||||
|
r.validate()
|
||||||
|
|
||||||
|
def test_sentiments(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_plugins(self):
|
||||||
|
self.assertRaises(Error, SenpyPlugin)
|
||||||
|
p = SenpyPlugin({"name": "dummy", "version": 0})
|
||||||
|
c = p.jsonld()
|
||||||
|
assert "info" not in c
|
||||||
|
assert "repo" not in c
|
||||||
|
assert "params" in c
|
||||||
|
logging.debug("Framed:")
|
||||||
|
logging.debug(c)
|
||||||
|
p.validate()
|
||||||
|
|
||||||
|
def test_frame_response(self):
|
||||||
|
pass
|
Loading…
Reference in New Issue