mirror of
https://github.com/gsi-upm/senpy
synced 2025-09-18 12:32:21 +00:00
V 0.2.2 - Better plugins
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
class SenpyPlugin(object):
|
||||
def __init__(self, name=None, version=None, params=None):
|
||||
self.name = name
|
||||
self.version = version
|
||||
self.params = params or []
|
||||
|
||||
def analyse(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def activate(self):
|
||||
pass
|
||||
|
||||
def deactivate(self):
|
||||
pass
|
||||
|
||||
def jsonable(self, parameters=False):
|
||||
resp = {
|
||||
"@id": "{}_{}".format(self.name, self.version),
|
||||
}
|
||||
if parameters:
|
||||
resp["parameters"] = self.params,
|
||||
return resp
|
||||
|
||||
class SentimentPlugin(SenpyPlugin):
|
||||
def __init__(self,
|
||||
minPolarityValue=0,
|
||||
maxPolarityValue=1,
|
||||
**kwargs):
|
||||
super(SentimentPlugin, self).__init__(**kwargs)
|
||||
self.minPolarityValue = minPolarityValue
|
||||
self.maxPolarityValue = maxPolarityValue
|
||||
|
||||
def jsonable(self, *args, **kwargs):
|
||||
resp = super(SentimentPlugin, self).jsonable(*args, **kwargs)
|
||||
resp["marl:maxPolarityValue"] = self.maxPolarityValue
|
||||
resp["marl:minPolarityValue"] = self.minPolarityValue
|
||||
return resp
|
||||
|
||||
class EmotionPlugin(SenpyPlugin):
|
||||
def __init__(self,
|
||||
minEmotionValue=0,
|
||||
maxEmotionValue=1,
|
||||
emotionCategory=None,
|
||||
**kwargs):
|
||||
super(EmotionPlugin, self).__init__(**kwargs)
|
||||
self.minEmotionValue = minEmotionValue
|
||||
self.maxEmotionValue = maxEmotionValue
|
||||
self.emotionCategory = emotionCategory
|
||||
|
||||
def jsonable(self, *args, **kwargs):
|
||||
resp = super(EmotionPlugin, self).jsonable(*args, **kwargs)
|
||||
resp["onyx:minEmotionValue"] = self.minEmotionValue
|
||||
resp["onyx:maxEmotionValue"] = self.maxEmotionValue
|
||||
return resp
|
||||
|
@@ -4,26 +4,31 @@ import json
|
||||
import sys
|
||||
|
||||
print(sys.path)
|
||||
from senpy.plugin import SentimentPlugin
|
||||
from senpy.plugins import SentimentPlugin
|
||||
from senpy.models import Response, Opinion, Entry
|
||||
|
||||
class Sentiment140Plugin(SentimentPlugin):
|
||||
parameters = {
|
||||
"language": {"aliases": ["language", "l"],
|
||||
"required": False,
|
||||
"options": ["es", "en", "auto"],
|
||||
}
|
||||
}
|
||||
def __init__(self, **kwargs):
|
||||
super(Sentiment140Plugin, self).__init__(name="Sentiment140",
|
||||
version="1.0",
|
||||
**kwargs)
|
||||
|
||||
def analyse(self, **params):
|
||||
lang = params.get("language", "auto")
|
||||
res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
|
||||
json.dumps({
|
||||
"language": "auto",
|
||||
"language": lang,
|
||||
"data": [{"text": params["input"]}]}
|
||||
))
|
||||
|
||||
|
||||
response = {"analysis": [{}], "entries": []}
|
||||
response["analysis"][0].update({ "marl:algorithm": "SimpleAlgorithm",
|
||||
"marl:minPolarityValue": 0,
|
||||
"marl:maxPolarityValue": 100})
|
||||
response = Response()
|
||||
polarityValue = int(res.json()["data"][0]["polarity"]) * 25
|
||||
polarity = "marl:Neutral"
|
||||
if polarityValue > 50:
|
||||
@@ -31,17 +36,12 @@ class Sentiment140Plugin(SentimentPlugin):
|
||||
elif polarityValue < 50:
|
||||
polarity = "marl:Negative"
|
||||
|
||||
response["entries"] = [
|
||||
{
|
||||
"isString": params["input"],
|
||||
"opinions": [{
|
||||
"marl:polarityValue": polarityValue,
|
||||
"marl:hasPolarity": polarity
|
||||
|
||||
}]
|
||||
}
|
||||
]
|
||||
|
||||
entry = Entry(text=params["input"])
|
||||
opinion = Opinion(polarity=polarity, polarityValue=polarityValue)
|
||||
entry.opinions.append(opinion)
|
||||
entry.language = lang
|
||||
response.entries.append(entry)
|
||||
return response
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user