diff --git a/indicoio/__init__.py b/indicoio/__init__.py index 4b0af6b..37fea7c 100644 --- a/indicoio/__init__.py +++ b/indicoio/__init__.py @@ -9,7 +9,7 @@ JSON_HEADERS = { 'version-number': VERSION } -from indicoio.text.sentiment import political, posneg +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 diff --git a/indicoio/config.py b/indicoio/config.py index 895c76a..0b24332 100644 --- a/indicoio/config.py +++ b/indicoio/config.py @@ -49,7 +49,8 @@ TEXT_APIS = [ 'text_tags', 'political', 'sentiment', - 'language' + 'language', + 'sentiment_hq' ] IMAGE_APIS = [ diff --git a/indicoio/text/sentiment.py b/indicoio/text/sentiment.py index 182908a..5172e40 100644 --- a/indicoio/text/sentiment.py +++ b/indicoio/text/sentiment.py @@ -50,3 +50,26 @@ def posneg(text, cloud=None, batch=False, api_key=None, **kwargs): """ return api_handler(text, cloud=cloud, api="sentiment", url_params={"batch":batch, "api_key":api_key}, **kwargs) + +def sentiment_hq(text, cloud=None, batch=False, api_key=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. + For reference, 0 suggests very negative sentiment and 1 suggests very positive sentiment. + + Example usage: + + .. code-block:: python + + >>> from indicoio import sentimenthq + >>> text = 'Thanks everyone for the birthday wishes!! It was a crazy few days ><' + >>> sentiment = sentimenthq(text) + >>> sentiment + 0.6210052967071533 + + :param text: The text to be analyzed. + :type text: str or unicode + :rtype: Float + """ + + return api_handler(text, cloud=cloud, api="sentimenthq", url_params={"batch":batch, "api_key":api_key}, **kwargs) diff --git a/indicoio/utils/api.py b/indicoio/utils/api.py index 71a43b5..e11abe3 100644 --- a/indicoio/utils/api.py +++ b/indicoio/utils/api.py @@ -17,10 +17,15 @@ def api_handler(arg, cloud, api, url_params = {"batch":False, "api_key":None}, * if cloud: host = "%s.indico.domains" % cloud + else: # default to indico public cloud host = config.PUBLIC_API_HOST + # error message for public cloud + if api == 'sentimenthq': + raise IndicoError("The high quality sentiment API is currently in private beta.") + url = config.url_protocol + "//%s/%s" % (host, api) url = url + "/batch" if url_params.get("batch", False) else url url += "?key=%s" % (url_params.get("api_key", None) or config.api_key)