mirror of https://github.com/gsi-upm/senpy
parent
a5e79bead3
commit
eaf65f0c6b
@ -0,0 +1,9 @@
|
|||||||
|
from senpy.plugins import SenpyPlugin
|
||||||
|
|
||||||
|
class Prueba(SenpyPlugin):
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super(Prueba, self).__init__(name="prueba",
|
||||||
|
version="4.0",
|
||||||
|
**kwargs)
|
||||||
|
|
||||||
|
plugin = Prueba()
|
@ -1,41 +0,0 @@
|
|||||||
'''
|
|
||||||
SENTIMENT140
|
|
||||||
=============
|
|
||||||
|
|
||||||
* http://www.sentiment140.com/api/bulkClassifyJson
|
|
||||||
* Method: POST
|
|
||||||
* Parameters: JSON Object (that is copied to the result)
|
|
||||||
* text
|
|
||||||
* query
|
|
||||||
* language
|
|
||||||
* topic
|
|
||||||
|
|
||||||
* Example response:
|
|
||||||
```json
|
|
||||||
{"data": [{"text": "I love Titanic.", "id":1234, "polarity": 4},
|
|
||||||
{"text": "I hate Titanic.", "id":4567, "polarity": 0}]}
|
|
||||||
```
|
|
||||||
'''
|
|
||||||
import requests
|
|
||||||
import json
|
|
||||||
|
|
||||||
ENDPOINT_URI = "http://www.sentiment140.com/api/bulkClassifyJson"
|
|
||||||
|
|
||||||
def analyse(texts):
|
|
||||||
parameters = {"data": []}
|
|
||||||
if isinstance(texts, list):
|
|
||||||
for text in texts:
|
|
||||||
parameters["data"].append({"text": text})
|
|
||||||
else:
|
|
||||||
parameters["data"].append({"text": texts})
|
|
||||||
|
|
||||||
res = requests.post(ENDPOINT_URI, json.dumps(parameters))
|
|
||||||
res.json()
|
|
||||||
return res.json()
|
|
||||||
|
|
||||||
def test():
|
|
||||||
print analyse("I love Titanic")
|
|
||||||
print analyse(["I love Titanic", "I hate Titanic"])
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test()
|
|
@ -0,0 +1,35 @@
|
|||||||
|
import os
|
||||||
|
import logging
|
||||||
|
try:
|
||||||
|
import unittest.mock as mock
|
||||||
|
except ImportError:
|
||||||
|
import mock
|
||||||
|
from senpy.extensions import Senpy
|
||||||
|
from flask import Flask
|
||||||
|
from flask.ext.testing import TestCase
|
||||||
|
|
||||||
|
def check_dict(indic, template):
|
||||||
|
return all(item in indic.items() for item in template.items())
|
||||||
|
|
||||||
|
class Blueprints_Test(TestCase):
|
||||||
|
def create_app(self):
|
||||||
|
self.app = Flask("test_extensions")
|
||||||
|
self.senpy = Senpy()
|
||||||
|
self.senpy.init_app(self.app)
|
||||||
|
self.dir = os.path.join(os.path.dirname(__file__), "..")
|
||||||
|
self.senpy.add_folder(self.dir)
|
||||||
|
return self.app
|
||||||
|
|
||||||
|
|
||||||
|
def test_home(self):
|
||||||
|
""" Calling with no arguments should ask the user for more arguments """
|
||||||
|
resp = self.client.get("/")
|
||||||
|
self.assert200(resp)
|
||||||
|
logging.debug(resp.json)
|
||||||
|
assert resp.json["status"] == "failed"
|
||||||
|
atleast = {
|
||||||
|
"status": "failed",
|
||||||
|
"message": "Missing or invalid parameters",
|
||||||
|
}
|
||||||
|
assert check_dict(resp.json, atleast)
|
||||||
|
|
@ -0,0 +1,3 @@
|
|||||||
|
from senpy.plugins import SenpyPlugin
|
||||||
|
|
||||||
|
plugin = SenpyPlugin("dummy")
|
@ -0,0 +1,70 @@
|
|||||||
|
import os
|
||||||
|
import logging
|
||||||
|
try:
|
||||||
|
import unittest.mock as mock
|
||||||
|
except ImportError:
|
||||||
|
import mock
|
||||||
|
from senpy.extensions import Senpy
|
||||||
|
from flask import Flask
|
||||||
|
from flask.ext.testing import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
class Extensions_Test(TestCase):
|
||||||
|
def create_app(self):
|
||||||
|
self.app = Flask("test_extensions")
|
||||||
|
self.senpy = Senpy()
|
||||||
|
self.senpy.init_app(self.app)
|
||||||
|
self.dir = os.path.join(os.path.dirname(__file__), "..")
|
||||||
|
self.senpy.add_folder(self.dir)
|
||||||
|
return self.app
|
||||||
|
|
||||||
|
def test_init(self):
|
||||||
|
""" Initialising the app with the extension. """
|
||||||
|
assert hasattr(self.app, "senpy")
|
||||||
|
tapp = Flask("temp app")
|
||||||
|
tsen = Senpy(tapp)
|
||||||
|
assert hasattr(tapp, "senpy")
|
||||||
|
|
||||||
|
def test_discovery(self):
|
||||||
|
""" Discovery of plugins in given folders. """
|
||||||
|
assert self.dir in self.senpy._search_folders
|
||||||
|
print self.senpy.plugins
|
||||||
|
assert "dummy" in self.senpy.plugins
|
||||||
|
|
||||||
|
|
||||||
|
def test_enabling(self):
|
||||||
|
""" Enabling a plugin """
|
||||||
|
self.senpy.enable_plugin("dummy")
|
||||||
|
assert self.senpy.plugins["dummy"].enabled
|
||||||
|
|
||||||
|
def test_disabling(self):
|
||||||
|
""" Disabling a plugin """
|
||||||
|
self.senpy.enable_plugin("dummy")
|
||||||
|
self.senpy.disable_plugin("dummy")
|
||||||
|
assert self.senpy.plugins["dummy"].enabled == False
|
||||||
|
|
||||||
|
def test_default(self):
|
||||||
|
""" Default plugin should be set """
|
||||||
|
assert self.senpy.default_plugin
|
||||||
|
assert self.senpy.default_plugin == "dummy"
|
||||||
|
|
||||||
|
def test_analyse(self):
|
||||||
|
""" Using a plugin """
|
||||||
|
with mock.patch.object(self.senpy.plugins["dummy"], "analyse") as mocked:
|
||||||
|
self.senpy.analyse(algorithm="dummy", input="tupni", output="tuptuo")
|
||||||
|
self.senpy.analyse(input="tupni", output="tuptuo")
|
||||||
|
mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="dummy")
|
||||||
|
mocked.assert_any_call(input="tupni", output="tuptuo")
|
||||||
|
for plug in self.senpy.plugins:
|
||||||
|
self.senpy.disable_plugin(plug)
|
||||||
|
resp = self.senpy.analyse(input="tupni")
|
||||||
|
logging.debug("Response: {}".format(resp))
|
||||||
|
assert resp["status"] == 400
|
||||||
|
|
||||||
|
def test_filtering(self):
|
||||||
|
""" Filtering plugins """
|
||||||
|
assert len(self.senpy.filter_plugins(name="dummy"))>0
|
||||||
|
assert not len(self.senpy.filter_plugins(name="notdummy"))
|
||||||
|
assert self.senpy.filter_plugins(name="dummy", enabled=True)
|
||||||
|
self.senpy.disable_plugin("dummy")
|
||||||
|
assert not len(self.senpy.filter_plugins(name="dummy", enabled=True))
|
Loading…
Reference in New Issue