Add support for optional arguments to API

This commit is contained in:
Madison May
2014-12-18 16:10:36 -05:00
parent 8b9e0841a0
commit 6c23d0d7ee
11 changed files with 35 additions and 34 deletions
+2 -1
View File
@@ -14,4 +14,5 @@ v0.4.5, Thu Sep 25 -- Added interface to local indico server
v0.4.6, Fri Oct 27 -- Updated to point to new indico api servers, cleaner REST API
v0.4.8, Fri Nov 7 -- Updated API interface to include new text tags API
v0.4.11, Wed Dec 18 -- Updated tests for text tags
v0.4.12, Thu Dec 19 -- Added batch support interface
v0.4.12, Thu Dec 19 -- Added batch support interface
v0.4.13, Thu Dec 19 -- Added optional arguments to text tags API
+1 -1
View File
@@ -3,7 +3,7 @@ import indicoio.config as config
JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}
Version, version, __version__, VERSION = ('0.4.12',) * 4
Version, version, __version__, VERSION = ('0.4.13',) * 4
from indicoio.text.sentiment import political, posneg
from indicoio.text.sentiment import posneg as sentiment
+4 -4
View File
@@ -5,7 +5,7 @@ import numpy as np
from indicoio.utils import image_preprocess, api_handler
def facial_features(api_root, image, batch=False, auth=None):
def facial_features(api_root, image, batch=False, auth=None, **kwargs):
"""
Given an grayscale input image of a face, returns a 48 dimensional feature vector explaining that face.
Useful as a form of feature engineering for face oriented tasks.
@@ -27,9 +27,9 @@ def facial_features(api_root, image, batch=False, auth=None):
:type image: list of lists
:rtype: List containing feature responses
"""
return api_handler(image, api_root + "facialfeatures", batch=batch, auth=auth)
return api_handler(image, api_root + "facialfeatures", batch=batch, auth=auth, **kwargs)
def image_features(api_root, image, batch=False, auth=None):
def image_features(api_root, image, batch=False, auth=None, **kwargs):
"""
Given an input image, returns a 2048 dimensional sparse feature vector explaining that image.
Useful as a form of feature engineering for image oriented tasks.
@@ -60,4 +60,4 @@ def image_features(api_root, image, batch=False, auth=None):
:rtype: List containing features
"""
image = image_preprocess(image)
return api_handler(image, api_root + "imagefeatures", batch=batch, auth=auth)
return api_handler(image, api_root + "imagefeatures", batch=batch, auth=auth, **kwargs)
+2 -2
View File
@@ -4,7 +4,7 @@ import requests
import numpy as np
from indicoio.utils import api_handler
def fer(api_root, image, batch=False, auth=None):
def fer(api_root, image, batch=False, auth=None, **kwargs):
"""
Given a grayscale input image of a face, returns a probability distribution over emotional state.
Input should be in a list of list format, resizing will be attempted internally but for best
@@ -28,4 +28,4 @@ def fer(api_root, image, batch=False, auth=None):
:rtype: Dictionary containing emotion probability pairs
"""
return api_handler(image, api_root + "fer", batch=batch, auth=auth)
return api_handler(image, api_root + "fer", batch=batch, auth=auth, **kwargs)
+2 -2
View File
@@ -1,6 +1,6 @@
from indicoio.utils import api_handler
def language(api_root, text, batch=False, auth=None):
def language(api_root, text, batch=False, auth=None, **kwargs):
"""
Given input text, returns a probability distribution over 33 possible
languages of what language the text was written in.
@@ -23,4 +23,4 @@ def language(api_root, text, batch=False, auth=None):
:rtype: Dictionary of language probability pairs
"""
return api_handler(text, api_root + "language", batch=batch, auth=auth)
return api_handler(text, api_root + "language", batch=batch, auth=auth, **kwargs)
+4 -4
View File
@@ -1,7 +1,7 @@
from indicoio import JSON_HEADERS
from indicoio.utils import api_handler
def political(api_root, text, batch=False, auth=None):
def political(api_root, text, batch=False, auth=None, **kwargs):
"""
Given input text, returns a probability distribution over the political alignment of the speaker.
@@ -27,9 +27,9 @@ def political(api_root, text, batch=False, auth=None):
:rtype: Dictionary of party probability pairs
"""
return api_handler(text, api_root + "political", batch=batch, auth=auth)
return api_handler(text, api_root + "political", batch=batch, auth=auth, **kwargs)
def posneg(api_root, text, batch=False, auth=None):
def posneg(api_root, text, batch=False, auth=None, **kwargs):
"""
Given input text, returns a scalar estimate of the sentiment of that text.
Values are roughly in the range 0 to 1 with 0.5 indicating neutral sentiment.
@@ -50,4 +50,4 @@ def posneg(api_root, text, batch=False, auth=None):
:rtype: Float
"""
return api_handler(text, api_root + "sentiment", batch=batch, auth=auth)
return api_handler(text, api_root + "sentiment", batch=batch, auth=auth, **kwargs)
+2 -2
View File
@@ -1,6 +1,6 @@
from indicoio.utils import api_handler
def text_tags(api_root, text, batch=False, auth=None):
def text_tags(api_root, text, batch=False, auth=None, **kwargs):
"""
Given input text, returns a probability distribution over 100 document categories
@@ -22,4 +22,4 @@ def text_tags(api_root, text, batch=False, auth=None):
:rtype: Dictionary of class probability pairs
"""
return api_handler(text, api_root + "texttags", batch=batch, auth=None)
return api_handler(text, api_root + "texttags", batch=batch, auth=None, **kwargs)
+6 -4
View File
@@ -20,15 +20,17 @@ def auth_query():
return (email, password)
def api_handler(arg, url, batch=False, auth=None):
data_dict = json.dumps({'data': arg})
def api_handler(arg, url, batch=False, auth=None, **kwargs):
data = {'data': arg}
data.update(**kwargs)
json_data = json.dumps(data)
if batch:
url += "/batch"
if not auth:
auth = auth_query()
response = requests.post(url, data=data_dict, headers=JSON_HEADERS, auth=auth).json()
response = requests.post(url, data=json_data, headers=JSON_HEADERS, auth=auth).json()
results = response.get('results', False)
if not results:
if results is False:
error = response.get('error')
raise ValueError(error)
return results
+1 -1
View File
@@ -8,7 +8,7 @@ except ImportError:
setup(
name="IndicoIo",
version='0.4.12',
version='0.4.13',
packages=[
"indicoio",
"indicoio.text",
+6 -13
View File
@@ -22,22 +22,15 @@ class FullAPIRun(unittest.TestCase):
self.assertTrue(np.ptp(vector) > span)
def test_text_tags(self):
expected_keys = set(['fashion', 'art', 'energy', 'economics', 'entreprener',
'books', 'politics', 'gardening', 'nba', 'conservative',
'technology', 'startps', 'relationships', 'edcation',
'hmor', 'psychology', 'bicycling', 'investing', 'travel',
'cooking', 'christianity', 'environment', 'religion', 'health',
'hockey', 'pets', 'msic', 'soccer', 'gns', 'gaming', 'jobs',
'bsiness', 'natre', 'food', 'cars', 'photography', 'philosophy',
'geek', 'sports', 'baseball', 'news', 'television', 'entertainment',
'parenting', 'comics', 'science', 'nfl','programming',
'personalfinance', 'atheism', 'movies', 'anime', 'fitness',
'military', 'realestate', 'history'])
text = "On Monday, president Barack Obama will be..."
results = text_tags(text)
max_keys = sorted(results.keys(), key=lambda x:results.get(x), reverse=True)
assert 'politics' in max_keys[:3]
self.assertTrue(expected_keys == set(results.keys()))
assert 'political_discussion' in max_keys[:5]
results = text_tags(text, top_n=5)
assert len(results) is 5
results = text_tags(text, threshold=0.1)
for v in results.values():
assert v >= 0.1
def test_political(self):
political_set = set(['Libertarian', 'Liberal', 'Conservative', 'Green'])
+5
View File
@@ -27,6 +27,11 @@ class FullAPIRun(unittest.TestCase):
results = text_tags(text)
max_keys = sorted(results.keys(), key=lambda x:results.get(x), reverse=True)
assert 'political_discussion' in max_keys[:5]
results = text_tags(text, top_n=5)
assert len(results) is 5
results = text_tags(text, threshold=0.1)
for v in results.values():
assert v >= 0.1
def test_political(self):
political_set = set(['Libertarian', 'Liberal', 'Conservative', 'Green'])