pythonapiibm-cloudalchemyapi

Trying to call the AlchemyLanguage API


I have written code for calling the AlchemyLanguage API of Bluemix in Python. I need the keywords and entities, but it is only showing the first keyword and first entity for the text file. Where am I going wrong?

import requests    
import urllib    
import urllib2

def call_alchemy_api(text, API_KEY):

    payload = {'outputMode':'json','extract':'entities,keywords','sentiment':'1','maxRetrieve':'1', 'url':'https://www.ibm.com/us-en/'}
    payload['apikey'] = API_KEY
    encoded_text = urllib.quote_plus(text)
    payload['text'] = text
    data = urllib.urlencode(payload)
    url = 'https://gateway-a.watsonplatform.net/calls/text/TextGetCombinedData'
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    return response

if __name__ == "__main__":

    api_key = 'xxxxxxxxxxxxxxxxxxxxxmyapi'
    f = open('in0.txt','r')
    text = f.read()

    print text
    response = call_alchemy_api(text, api_key)
    print response.read()

Solution

  • Change the maxRetrieve keyword's value.

    Example:

    payload = {'outputMode':'json','extract':'entities,keywords','sentiment':'1','maxRetrieve':'3', 'url':'https://www.ibm.com/us-en/'}
    

    API Link:

    http://www.ibm.com/watson/developercloud/alchemy-language/api/v1/