Textrics API Stack


Textrics API Communication with Client Database

Using this API one can perform different types of analysis on open end data e.g. a customer review, magazine article, comment or a sentence. The analysis accomplished using this API are Sentiment, Intent, Emotion, Tagging, Offence Check and NER (Named Entity Recognition).

Endpoint

https://api.textrics.ai/sentence_analysis_api.php

We support POST method to communicate with API.

Parameters

1. user_text - This parameter is required
This is the text (sentence) which you wish to send to application to analyse.

2. api_key - This parameter is required
This is used to identify the user and the active subscription plan.

3. api_token - This parameter is required
This is used to authenticate the above user account.

API Script

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $text = trim($_POST['user_text']);			// input variable containing text //
    $text=str_replace("'","’",$text);
    if($text!='' && is_numeric($text)!=1){
	$apiUrl = 'https://api.textrics.ai/sentence_analysis_api.php';		// API URL //
    $args['user_text'] = $text;
	$args['api_key'] = 'A32NBK55K3'; 		// user specific key to validate the account //
	$args['api_token'] = 'P4lwpqT0YZYSVefSh44ADkvlJ8pesZDI';   // respective token to validate user credentials //

    $options=[
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POSTFIELDS => $args,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_HTTPHEADER => ['Content-Type: multipart/form-data']
      ];
    $ch = curl_init($apiUrl);
    curl_setopt_array($ch, $options);
    $response1 = curl_exec($ch);
    echo $err = curl_error($ch); 
    curl_close($ch);	
    $response =  json_decode($response1,true);		// API output is stored in $response variable //
	}
	else
	{
        $message= 'Error: Input Text is incorrect.';
	}
}

?>

Sample of retrieving the output


// Sample of sending the output from $response variable to a database table

// Case #1: Client wants to create a new database table for this analysis.

/* Database table with the following structure need to be created

Table Name: textrics_analysis
Column Name Datatype
textrics_analysis_id(PK) (integer, Auto Incremental)
user_input text/varchar(5000)
sentiment_text text/varchar(100)
sentiment_score float/decimal
intent_text text/varchar(100)
intent_score float/decimal
emotion_text text/varchar(100)
emotion_score float/decimal
tagging_text text/varchar(500)
offence_text text/varchar(100)
offence_score float/decimal
ner_text text/varchar(5500)
*/ <?php if(!empty($response['data'])) { //Establish the database connection by passing the name of already created connection object and comment the line of Creating New Connection, or create a new connection by passing required parameters and comment the line of Using Existing Connection. // Using Existing Connection $mysqli = ConnectionObjectName //Creating New Connection $mysqli = new mysqli("localhost","Database Username","Password","Database Name"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } $tags=implode(",",$response['data'][1]['Prediction']); $ner=explode("[",$response['data'][5]['Prediction']); $mysqli -> query("INSERT INTO textrics_analysis (user_input, sentiment_text, sentiment_score, tagging_text, intent_text, intent_score, emotion_text, emotion_score, offence_text, offence_score, ner_text) VALUES ('".$text."', '".$response['data'][0]['Prediction']."', '".number_format($response['data'][0]['Confidence'], 2)."', '".$tags."','".$response['data'][4]['Prediction']."', '".number_format($response['data'][4]['Confidence'], 2)."','".$response['data'][2]['Prediction']."', '".number_format($response['data'][2]['Confidence'], 2)."','".$response['data'][3]['Prediction']."', '".number_format($response['data'][3]['Confidence'], 2)."','".$ner[0]."')"); $message= "Response submitted successfully."; } ?> // Case #2: Clients want to use their existing database table for this analysis. /* The already existing table should have the following columns in it
Column Name Datatype
user_input text/varchar(5000)
sentiment_text text/varchar(100)
sentiment_score float/decimal
intent_text text/varchar(100)
intent_score float/decimal
emotion_text text/varchar(100)
emotion_score float/decimal
tagging_text text/varchar(500)
offence_text text/varchar(100)
offence_score float/decimal
ner_text text/varchar(5500)
*/ <?php if(!empty($response['data'])) { //Establish the database connection by passing the name of already created connection object and comment the line of Creating New Connection, or create a new connection by passing required parameters and comment the line of Using Existing Connection. // Using Existing Connection $mysqli = ConnectionObjectName //Creating New Connection $mysqli = new mysqli("localhost","Database Username","Password","Database Name"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } $txt_api_tablename=''; // pass the database table name $txt_api_pkname=''; // pass the primary key name $txt_api_pkvalue=''; // pass the primary key value $tags=implode(",",$response['data'][1]['Prediction']); $ner=explode("[",$response['data'][5]['Prediction']); $mysqli -> query("Update ".$txt_api_tablename." Set user_input = '".$text."',sentiment_text = '".$response['data'][0]['Prediction']."',sentiment_score ='".number_format($response['data'][0]['Confidence'], 2)."' ,intent_text = '".$response['data'][4]['Prediction']."',intent_score = '".number_format($response['data'][4]['Confidence'], 2)."',emotion_text = '".$response['data'][2]['Prediction']."',emotion_score = '".number_format($response['data'][2]['Confidence'], 2)."',tagging_text = '".$tags."', offence_text = '".$response['data'][3]['Prediction']."',offence_score ='".number_format($response['data'][3]['Confidence'], 2)."' ,ner_text = '".$ner[0]."' Where ".$txt_api_pkname." = ".$txt_api_pkvalue.""); $message= "Response updated successfully."; } ?>

Response Format

The response is structured in JSON as follows:

Array
(
    [response] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [Confidence] => 43.7504692078
                    [Module] => Sentiments
                    [Prediction] => Negative
                )
            [1] => Array
                (
                    [Confidence] => 100
                    [Module] => Tags
                    [Prediction] => Array
                        (
                            [0] => You
                            [1] => passed
                            [2] => exam
                        )
                )
            [2] => Array
                (
                    [Confidence] => 46.4759216309
                    [Module] => Emotions
                    [Prediction] => Happy
                )
            [3] => Array
                (
                    [Confidence] => 73.3554458618
                    [Module] => Abusive or non Abuisve
                    [Prediction] => Non Abusive
                )
            [4] => Array
                (
                    [Confidence] => 93.8117675781
                    [Module] => Intent
                    [Prediction] => News
                )
            [5] => Array
                (
                    [Confidence] => NA
                    [Module] => NER
                    [Prediction] => 
You would have passed your exam if you had worked harder.
[]
                )
        )
    [message] => success
)

API Response Codes

Code Interpretation
1 Success
Exception Codes
10 Authentication Exception
11 Invalid Input Exception
12 Language Exception
13 File Format Error
14 Request Timeout
15 Other Exception