Product

Text Analysis and Go: Getting Started with AYLIEN Text Analysis API and Go

Our “Getting started with AYLIEN Text Analysis API” blogs, focus on working with the API using different programming languages. We have previously published code snippets and getting started guides for C#, node.js, Powershell, Python, Java ,Ruby and PHP.

Today we’re going to look at using the API with Go. We’re going to run through some basic Text Analysis functions like analyzing the sentiment of a piece of text, detecting what language a piece of text is written in, classifying an article and generating hashtags for a URL.

To begin we’ll look at the code in action. Then we’ll go through it, section by section, to investigate each of the endpoints used in the code snippet and the results.

Here’s what we’re going to do:

Note: The getting started page on our website has a range of code snippets in other programming languages and links to our SDKs. We are making use of the GO SDK in this blog.

Overview of the code in action

The complete code snippet is given at the end of this blog for you to copy and paste. Copy and paste the snippet into a text editor of your choice. Make sure you replace the YOUR_APP_ID and YOUR_APP_KEY placeholders in the code with your own application id and application key. You would have been sent these when you signed up as an API user. If you haven’t signed up you can make your way to our sign up page to register for free.

Save the file as TextAPISample.go. Type “go run TextAPISample.go” to see the code snippet in action.

Once you have ran the code, you should receive the following output:

Language Detection Results
Text: What language is this sentence written in?
Language: en (0.999998)

Sentiment Analysis Results
Text: John is a very good football player!
Sentiment: positive (0.999999)
Subjectivity: Objective
Subjectivity Confidence: 0.989682

Classification Analysis Results:
Label : disease - heart disease
IPTC code : 07001005
Confidence : 1.000000

Hashtag Suggestion Results:
#Yoga
#HeartDisease
#Hypertension
#Cholesterol
#Oxygen
#BritishHeartFoundation
#ErasmusUniversityRotterdam
#Rotterdam
#Netherlands
#HathaYoga

In this case we have detected that the first piece of text is written in English and the sentiment or polarity of the second statement is positive. We have also generated hashtags for the URL and classified the content.

We’ll now go through the code snippet in more detail.

Language Detection

Using the Language Detection endpoint you can analyze a piece of text or a URL and automatically determine what language it is written in. In the piece of code we have used in this blog, the “languageParams ” variable controls whether the call is made specifying the text directly or as a URL.

languageParams := &textapi.LanguageParams{Text: "What language is this sentence written in?"}
lang, err := client.Language(languageParams)

In this case we have specified that, we want to analyze the following text “What language is this sentence written in?” and as you can see from the output below, it determined that the text is written in English and it gave a 0.999998 confidence score that the language was detected correctly. For all of the endpoints, the API returns the text which was analyzed for reference and we have included it in the results in each case.

Result:

Text            :   What language is this sentence written in?
Language        :   en
Confidence      :   0.999998

Sentiment Analysis

Similarly, the Sentiment Analysis endpoint takes a piece of text or a URL and analyzes it to determine whether it is positive, negative or even neutral it also provides a confidence score in the results.

sentimentParams := &textapi.SentimentParams{Text: "John is a very good football player!"}
sentiment, err := client.Sentiment(sentimentParams)

Analyzing the following text; “John is a very good football player!”. The API has determined that the sentiment of the piece of text is positive, we can also be pretty sure it’s correct based on the confidence score returned of 0.999999. It also detected that statement was Objective with a confidence of 0.989682.

Result:

Text            :   John is a very good football player!
Sentiment Polarity  :   positive
Polarity Confidence  :   0.999999
Subjectivity  : objective
Subjectivity Confidence  :   0.989682

Hashtag Suggestions

The Hashtag Suggestion endpoint, analyses a URL and generates a list of hashtag suggestions which can be used to ensure that your content or URL’s are optimally shared on social media:

hashtagsParams := &textapi.HashtagsParams{URL: "http://www.bbc.com/news/health-30475999"}
hashtags, err := client.Hashtags(hashtagsParams)

For hashtag suggestions, we have used an article published on the BBC news website about how Yoga can help prevent Heart Disease. The URL for the article is as follows “http://www.bbc.com/news/health-30475999”. The hashtag suggestion endpoint extracts the text from the URL and then analyzes that text and generates hashtag suggestions for it.

Result:

Hashtag Suggestion Results
#Yoga
#HeartDisease
#Hypertension
#Cholesterol
#Oxygen
#BritishHeartFoundation
#ErasmusUniversityRotterdam
#Rotterdam
#Netherlands
#HathaYoga

Article Classification

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.

classifyParams := &textapi.ClassifyParams{URL: "http://www.bbc.com/news/health-30475999"}
class, err := client.Classify(classifyParams)

When we analyze the url pointing to the BBC news story, we receive the results as shown below. As you can see it has labeled the article as “disease- heart disease” with a corresponding IPTC code of 07001005 and a confidence of 1.

Result:

Classification Analysis Results
Classification Label        :   disease - heart disease
Classification Code         :   07001005
Classification Confidence   :   1.000000

For more getting started guides and code snippets to help you get up and running with our API, visit our getting started page on our website.

 

image

The Complete Code Snippet

package main

import (
    "fmt"
    textapi "github.com/AYLIEN/aylien_textapi_go"
)

func main() {
    auth := textapi.Auth{"YOUR_APP_ID ", "YOUR_APP_KEY"}
    client, err := textapi.NewClient(auth, true)
    if err != nil {
        panic(err)
    }


    endpoints := []string{"language", "sentiment","classify", "hashtags"};

    for _, endpoint := range endpoints {
        switch (endpoint) {
            case "language": {
                languageParams := &textapi.LanguageParams{Text: "What language is this sentence written in?"}
                lang, err := client.Language(languageParams)
                if err != nil {
                    panic(err)
                }
                fmt.Printf("nLanguage Detection Resultsn")
                fmt.Printf("Text            :   %sn", lang.Text)
                fmt.Printf("Language        :   %sn", lang.Language)
                fmt.Printf("Confidence      :   %fnn", lang.Confidence)
                break;
            }
            case "sentiment": {
                sentimentParams := &textapi.SentimentParams{Text: "John is a very good football player!"}
                sentiment, err := client.Sentiment(sentimentParams)
                if err != nil {
                    panic(err)
                }
                fmt.Printf("Sentiment Analysis Resultsn")
                fmt.Printf("Text            :   %sn", sentiment.Text)
                fmt.Printf("Sentiment Polarity  :   %sn", sentiment.Polarity)
                fmt.Printf("Polarity Confidence  :   %fn", sentiment.PolarityConfidence)
                fmt.Printf("Subjectivity  : %sn", sentiment.Subjectivity)
                fmt.Printf("Subjectivity Confidence  :   %fnn", sentiment.SubjectivityConfidence)
                break;
            }
            case "classify": {
                classifyParams := &textapi.ClassifyParams{URL: "http://www.bbc.com/news/health-30475999"}
                class, err := client.Classify(classifyParams)
                if err != nil {
                    panic(err)
                }
                fmt.Printf("Classification Analysis Resultsn")
                for _, v := range class.Categories {
                    fmt.Printf("Classification Label        :   %sn", v.Label)
                    fmt.Printf("Classification Code         :   %sn", v.Code)
                    fmt.Printf("Classification Confidence   :   %fnn", v.Confidence)
                }
                break;
            }
            case "hashtags": {
                hashtagsParams := &textapi.HashtagsParams{URL: "http://www.bbc.com/news/health-30475999"}
                hashtags, err := client.Hashtags(hashtagsParams)
                if err != nil {
                    panic(err)
                }
                fmt.Printf("Hashtag Suggestion Resultsn")
                for _, v := range hashtags.Hashtags {
                    fmt.Printf("%sn", v)
                }

            }
        }
    }

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