Product

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

Our “Getting up and running with AYLIEN Text Analysis API” blogs, focus on working with the API using different programming languages. Previously we published code snippets and getting started guides for C#, node.js, Powershell, Python, Java and Ruby. Today we’re going to focus on PHP.

As we did in our previous blogs, we’re going to perform 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. The idea, is to make it as easy as possible for developers to get up and running with our API and to showcase how easy it is to get started in your chosen programming language.

We’re first going to 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.

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.php in the docs folder of your web server. If you are using XAMPP as your web server the docs folder is typically at “xampp/htdocs”. Open a web browser and open the url “http://localhost/TextAPISample.php” to see the code snippet in action.

Once you open it, you should receive the following output:

Text: What language is this sentence written in?
Language: en (0.999996)

Text: John is a very good football player!
Sentiment: positive (0.999999)

Classification:
Label : natural science - geology
IPTC code : 13004001
Confidence : 1.000000

Hashtags:
#Moon
#Earth
#Theia
#MoonRock
#SolarSystem
#Fingerprint
#BBCNews
#ComputerSimulation
#Mars
#Oxygen
#Meteorite
#Venus
#AsteroidBelt
#Mercury
#Crust
#Sessility
#OpenUniversity
#SolarWind
#Meijer
#UniversityOfOxford
#Apollo
#Netherlands
#UniversityOfGroningen
#GreekLanguage
#CollisionTheory
#Selene
#Astronaut
#GiantImpactHypothesis

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 “parameter” variable controls whether the call is made specifying the text directly or as a URL.

$params = array('text' => 'What language is this sentence written in?');
$language = call_api('language', $params);

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.999996 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 (0.999996)

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.

$params = array('text' => 'John is a very good football player!');
$sentiment = call_api('sentiment', $params);

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.

Result:

Text: John is a very good football player!
Sentiment: positive (0.999999)

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:

$params = array('url' => $url);
$hashtags = call_api('hashtags', $params);

For hashtag suggestions, we have used an article published on the BBC news website about how the moon was formed when a planet called Theia collided with the Earth approximately 4.5 billion years ago. The url for the article is as follows “http://www.bbc.com/news/science-environment-27688511”. The hashtag suggestion endpoint first extracts the text from the URL and then analyzes that text and generates hashtag suggestions for it.

Result:

Hashtags:
#Moon
#Earth
#Theia
#MoonRock
#SolarSystem
#Fingerprint
#BBCNews
#ComputerSimulation
#Mars
#Oxygen
#Meteorite
#Venus
#AsteroidBelt
#Mercury
#Crust
#Sessility
#OpenUniversity
#SolarWind
#Meijer
#UniversityOfOxford
#Apollo
#Netherlands
#UniversityOfGroningen
#GreekLanguage
#CollisionTheory
#Selene
#Astronaut
#GiantImpactHypothesis

Article Classification

The Classification endpoint automatically assigns or tags an article or piece of text to one or more categories making it easier to manage and sort. The classification endpoint is based on IPTC International Subject News Codes and can identify up to 500 categories.

$params = array('url' => $url);
$classify = call_api('classify', $params);

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

Result:

Label : natural science - geology
IPTC code : 13004001
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. If you haven’t already done so you can get free access to our API on our sign up page.





Text Analysis API - Sign up




The Complete Code Snippet

<?php
define('APPLICATION_ID',    YOUR_APP_ID);
define('APPLICATION_KEY',  YOUR_APP_KEY');

function call_api($endpoint, $parameters) {
  $ch = curl_init('https://api.aylien.com/api/v1/' . $endpoint);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'X-AYLIEN-TextAPI-Application-Key: ' . APPLICATION_KEY,
    'X-AYLIEN-TextAPI-Application-ID: '. APPLICATION_ID
  ));
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  $response = curl_exec($ch);
  return json_decode($response);
}


$endpoints = array("language", "sentiment", "classify", "hashtags");
$url = "http://www.bbc.com/news/science-environment-27688511";

foreach($endpoints as $endpoint)
  {
    switch($endpoint){
      case "language":
      {
        $params = array('text' => 'What language is this sentence written in?');
        $language = call_api('language', $params);
        echo sprintf("Text: %s 
n", $language->text);
        echo sprintf("Language: %s (%F) 
n", $language->lang, $language->confidence);
        break;
      }
      case "sentiment":
      {
        $params = array('text' => 'John is a very good football player!');
        $sentiment = call_api('sentiment', $params);
        echo sprintf(" 
nText: %s 
n", $sentiment->text);
        echo sprintf("Sentiment: %s (%F) 
n", $sentiment->polarity, $sentiment->polarity_confidence);
        break;
      }
      case "classify":
      {
        echo sprintf("
nClassification:
n");
        $params = array('url' => $url);
        $classify = call_api('classify', $params);
        foreach($classify->categories as $val) {
          echo sprintf("
nLabel        :   %s     ", $val->label);
          echo sprintf("
nIPTC code    :   %s     ", $val->code);
          echo sprintf("
nConfidence   :   %F     ", $val->confidence);
        }
        break;
      }
      case "hashtags":
      {
        echo sprintf("
n
nHashtags:");
        $params = array('url' => $url);
        $hashtags = call_api('hashtags', $params);
        foreach($hashtags->hashtags as $val) {
          echo sprintf(" 
n %s", $val );
        }
        break;
      }
    }

  }
?>

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