mirror of
https://github.com/wassname/IndicoIo-python.git
synced 2026-08-12 11:40:34 +08:00
ADD: keywords api support
This commit is contained in:
@@ -13,6 +13,7 @@ from indicoio.text.sentiment import political, posneg, sentiment_hq
|
||||
from indicoio.text.sentiment import posneg as sentiment
|
||||
from indicoio.text.lang import language
|
||||
from indicoio.text.tagging import text_tags
|
||||
from indicoio.text.keywords import keywords
|
||||
from indicoio.images.fer import fer
|
||||
from indicoio.images.features import facial_features
|
||||
from indicoio.images.features import image_features
|
||||
@@ -20,6 +21,7 @@ from indicoio.utils.multi import predict_image, predict_text
|
||||
|
||||
from indicoio.config import API_NAMES
|
||||
|
||||
|
||||
apis = dict((api, globals().get(api)) for api in API_NAMES)
|
||||
|
||||
for api in apis:
|
||||
|
||||
+2
-1
@@ -50,7 +50,8 @@ TEXT_APIS = [
|
||||
'political',
|
||||
'sentiment',
|
||||
'language',
|
||||
'sentiment_hq'
|
||||
'sentiment_hq',
|
||||
'keywords'
|
||||
]
|
||||
|
||||
IMAGE_APIS = [
|
||||
|
||||
@@ -26,7 +26,8 @@ def facial_features(image, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:rtype: List containing feature responses
|
||||
"""
|
||||
image = image_preprocess(image, batch=batch)
|
||||
return api_handler(image, cloud=cloud, api="facialfeatures", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(image, cloud=cloud, api="facialfeatures", url_params=url_params, **kwargs)
|
||||
|
||||
def image_features(image, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
"""
|
||||
@@ -59,4 +60,5 @@ def image_features(image, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:rtype: List containing features
|
||||
"""
|
||||
image = image_preprocess(image, batch=batch, size=(64,64))
|
||||
return api_handler(image, cloud=cloud, api="imagefeatures", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(image, cloud=cloud, api="imagefeatures", url_params=url_params, **kwargs)
|
||||
|
||||
@@ -28,4 +28,5 @@ def fer(image, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:rtype: Dictionary containing emotion probability pairs
|
||||
"""
|
||||
image = image_preprocess(image, batch=batch)
|
||||
return api_handler(image, cloud=cloud, api="fer", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(image, cloud=cloud, api="fer", url_params=url_params, **kwargs)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
from indicoio.utils.api import api_handler
|
||||
import indicoio.config as config
|
||||
|
||||
def keywords(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
"""
|
||||
Given input text, returns series of keywords and associated scores
|
||||
|
||||
Example usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> import indicoio
|
||||
>>> import numpy as np
|
||||
>>> text = 'Monday: Delightful with mostly sunny skies. Highs in the low 70s.'
|
||||
>>> keywords = indicoio.keywords(text, top_n=3)
|
||||
>>> print "The keywords are: "+str(keywords.keys())
|
||||
u'The keywords are ['delightful', 'highs', 'skies']
|
||||
|
||||
:param text: The text to be analyzed.
|
||||
:type text: str or unicode
|
||||
:rtype: Dictionary of feature score pairs
|
||||
"""
|
||||
url_params = {'batch': batch, 'api_key': api_key}
|
||||
return api_handler(text, cloud=cloud, api="keywords", url_params=url_params, **kwargs)
|
||||
@@ -23,5 +23,5 @@ def language(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:type text: str or unicode
|
||||
:rtype: Dictionary of language probability pairs
|
||||
"""
|
||||
|
||||
return api_handler(text, cloud=cloud, api="language", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(text, cloud=cloud, api="language", url_params=url_params, **kwargs)
|
||||
|
||||
@@ -25,8 +25,8 @@ def political(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:type text: str or unicode
|
||||
:rtype: Dictionary of party probability pairs
|
||||
"""
|
||||
|
||||
return api_handler(text, cloud=cloud, api="political", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(text, cloud=cloud, api="political", url_params=url_params, **kwargs)
|
||||
|
||||
def posneg(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
"""
|
||||
@@ -48,8 +48,8 @@ def posneg(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:type text: str or unicode
|
||||
:rtype: Float
|
||||
"""
|
||||
|
||||
return api_handler(text, cloud=cloud, api="sentiment", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(text, cloud=cloud, api="sentiment", url_params=url_params, **kwargs)
|
||||
|
||||
def sentiment_hq(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
"""
|
||||
@@ -71,5 +71,5 @@ def sentiment_hq(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:type text: str or unicode
|
||||
:rtype: Float
|
||||
"""
|
||||
|
||||
return api_handler(text, cloud=cloud, api="sentimenthq", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(text, cloud=cloud, api="sentimenthq", url_params=url_params, **kwargs)
|
||||
|
||||
@@ -22,5 +22,5 @@ def text_tags(text, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
:type text: str or unicode
|
||||
:rtype: Dictionary of class probability pairs
|
||||
"""
|
||||
|
||||
return api_handler(text, cloud=cloud, api="texttags", url_params={"batch":batch, "api_key":api_key}, **kwargs)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(text, cloud=cloud, api="texttags", url_params=url_params, **kwargs)
|
||||
|
||||
@@ -29,6 +29,8 @@ def api_handler(arg, cloud, api, url_params = {"batch":False, "api_key":None}, *
|
||||
if apis:
|
||||
url += "&apis=%s" % ",".join(apis)
|
||||
|
||||
print url
|
||||
|
||||
response = requests.post(url, data=json_data, headers=JSON_HEADERS)
|
||||
if response.status_code == 503 and cloud != None:
|
||||
raise IndicoError("Private cloud '%s' does not include api '%s'" % (cloud, api))
|
||||
|
||||
Reference in New Issue
Block a user