Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
senpy
senpy
Commits
28349670
Commit
28349670
authored
Nov 27, 2014
by
J. Fernando Sánchez
Browse files
Better jsonld support
parent
2f7a8d72
Changes
7
Show whitespace changes
Inline
Side-by-side
MANIFEST.in
View file @
28349670
include requirements.txt
include README.md
include senpy/context.jsonld
recursive-include *.senpy
app.py
View file @
28349670
...
...
@@ -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__
)
...
...
plugins/sentiment140/__init__.py
View file @
28349670
...
...
@@ -36,7 +36,7 @@ class Sentiment140Plugin(SentimentPlugin):
elif
polarity_value
<
50
:
polarity
=
"marl:Negative"
entry
=
Entry
(
text
=
params
[
"input"
])
opinion
=
Opinion
(
p
olarity
=
polarity
,
polarity
_v
alue
=
polarity_value
)
opinion
=
Opinion
(
hasP
olarity
=
polarity
,
polarity
V
alue
=
polarity_value
)
entry
.
opinions
.
append
(
opinion
)
entry
.
language
=
lang
response
.
entries
.
append
(
entry
)
...
...
senpy/context.jsonld
View file @
28349670
...
...
@@ -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:hasEmotionS
et",
"@
type
": "onyx:EmotionSet"
"@
container": "@s
et",
"@
id
": "onyx:
has
EmotionSet"
},
"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:hasContex
t",
"@
typ
e": "nif:
String
"
"@
container": "@se
t",
"@
revers
e": "nif:
hasContext
"
},
"date":
{
...
...
senpy/extensions.py
View file @
28349670
...
...
@@ -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
...
...
senpy/models.py
View file @
28349670
...
...
@@ -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
):
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
[]
setup.py
View file @
28349670
...
...
@@ -8,7 +8,7 @@ install_reqs = parse_requirements("requirements.txt")
# e.g. ['django==1.5.1', 'mezzanine==1.4.6']
reqs
=
[
str
(
ir
.
req
)
for
ir
in
install_reqs
]
VERSION
=
"0.2.
8
"
VERSION
=
"0.2.
9
"
print
(
reqs
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment