Product

Getting Started with AYLIEN’s Text Analysis SDK for Python

This is the second in our series of blogs on getting started with Aylien’s various SDKs. There are SDKs available for Node.js, Python, Ruby, PHP, GO, Java and .Net (C#). Last week’s blog focused on the node.js SDK. This week we will focus on Python.

If you are new to AYLIEN and don’t have an account you can take a look at our blog on getting started with the API or alternatively you can go directly to the Getting Started page on the website which will take you through the signup process. We have a free plan available which allows you to make up to 1,000 calls to the API per day for free.

Downloading and Installing the Python SDK

All of our SDK repositories are hosted on Github. You can find the the python repository here. The simplest way to install the repository is with the python package installer pip. Simply run the following from a command line tool.


$ pip install --upgrade aylien-apiclient

The following libraries will be installed when you install the client library:

  • httplib2
  • Once you have installed the SDK you’re ready to start coding! The Sandbox area of the website has a number of sample applications in node.js which help to demonstrate what the API can do. For the remainder of this blog we will walk you through making calls to three of the API endpoints using the Python SDK.

    Utilizing the SDK with your AYLIEN credentials

    Once you have received your AYLIEN credentials and have downloaded the SDK you can start making calls by adding the following code to your python script.

    
    from aylienapiclient import textapi
    c = textapi.Client("YourApplicationID", "YourApplicationKey")
    

    When calling the various endpoints you can specify a piece of text directly for analysis or you can pass a URL linking to the text or article you wish to analyze.

    Language Detection

    First let’s take a look at the language detection endpoint. We’re going to detect the language of the sentence “’What language is this sentence written in?’

    You can do this by simply running the following piece of code:

    
    language = c.Language({'text': 'What Language is this sentence written in?'})
    print ("Language Detection Results:nn")
    print("Language: %sn" % (language["lang"]))
    print("Confidence: %Fn" %  (language["confidence"]))
    

    You should receive an output very similar to the one shown below which shows that the language detected was English and the confidence that it was detected correctly (a number between 0 and 1) is very close to 1 indicating that you can be pretty sure it is correct.

    Language Detection Results:

    
    Language	: en
    Confidence	: 0.9999984486883192
    

    Sentiment Analysis

    Next we will look at analyzing the sentence “John is a very good football player” to determine it’s sentiment i.e. whether it’s positive, neutral or negative. The API will also determine if the text is subjective or objective. You can call the endpoint with the following piece of code

    
    sentiment = c.Sentiment({'text': 'John is a very good football player!'})
    print ("nnSentiment Analysis Results:nn")
    print("Polarity: %sn" % sentiment["polarity"])
    print("Polarity Confidence: %sn" %  (sentiment["polarity_confidence"]))
    print("Subjectivity: %sn" % sentiment["subjectivity"])
    print("Subjectivity Confidence: %sn" %  (sentiment["subjectivity_confidence"]))
    

    You should receive an output similar to the one shown below which indicates that the sentence is positive and objective, both with a high degree of confidence.

    Sentiment Analysis Results:

    
    Polarity: positive
    Polarity Confidence: 0.9999988272764874
    Subjectivity: objective
    Subjectivity Confidence: 0.9896821594138254
    

    Article Classification

    Next we will take a look at the classification endpoint. The Classification Endpoint automatically assigns an article or piece of text to one or more categories making it easier to manage and sort. The classification is based on IPTC International Subject News Codes and can identify up to 500 categories. The code below analyzes a BBC news article about the first known picture of an oceanic shark giving birth.

    
    category = c.Classify({'url': 'http://www.bbc.com/news/science-environment-30747971'} )
    print("nArticle Classification Results:nn")
    print("Label	: %sn" % category["categories"][0]["label"])
    print("Code	: %sn" % category["categories"][0]["code"])
    print("Confidence	: %sn" % category["categories"][0]["confidence"])
    
    

    When you run this code you should receive an output similar to that shown below, which assigns the article an IPTC label of “science and technology – animal science” with an IPTC code of 13012000.

    Article Classification Results:

    
    Label   : science and technology - animal science
    Code    : 13012000
    Confidence      : 0.9999999999824132
    

    If python is not your preferred language then check out our SDKs for node.js, Ruby, PHP, GO, Java and .Net (C#). For more information regarding the APIs go to the documentation section of our website.

    We will be publishing ‘getting started’ blogs for the remaining languages over the coming weeks so keep an eye out for them. If you haven’t already done so, you can get free access to our API on our sign up page.

    Happy Hacking!

    image

    Author


    Avatar

    Mike Waldron

    Head of Marketing & Sales @ AYLIEN A legal convert with a masters degree from Smurfit Business School, Mike runs our Sales and Marketing at AYLIEN. Mike gathered his Sales and Marketing experience with technology companies in Sydney and Dublin before getting the startup itch and joining the team at AYLIEN. Twitter: @MikeWallly