Textrics API Stack


Sentiment Analysis API Implementation

This API works on a sentence and tells us the sentiment of given text in the form of Positive, Negative, or Neutral. Given a short sentence, it returns a label representing the identified sentiment, along with a numeric confidence score.

Endpoint

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

We support POST method to communicate with API.

Parameters

1. dataset - This parameter is required
This is the upload file name 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.

4. language - This parameter is required
This is used to detect language of sentence in file.

API Script

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $cfile = new CURLFile(                                // input file  containing text //
        $_FILES['dataset']['tmp_name'],
        $_FILES['dataset']['type'],
        $_FILES['dataset']['name']
        );		           
	
    $apiUrl = 'https://api.textrics.ai/sentiment_file_analysis.php';		// API URL //
    $args['language'] = $_POST['language'];            // Pass language  used in file// 
	$args['dataset'] = $cfile;                                       // pass uploaded file object //
	$args['col_name'] = $_POST['col_name'];                // Pass Column Name used in file//
	$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 //
	
}

?>

Sample of retrieving the output

<?php
if(!empty($response))
{
    echo "<br/>";
	echo "Response";
	echo "<br/>";
	echo "<pre style='text-align:left;'>"; 
	print_r($response);				// Printing the $response output on page //
	echo "</pre>"; 
}
if(!empty($message))
{
	echo $message;
}

?>

Response Format

The response is structured in JSON as follows:

Array
(
    [response] => 1
    [data] => [
{"Confidence":47.141040802,"Sentence":"Our track record in developing innovations that are valued by the market is poor.  We spend too much time talking about our previous wins (innovations that were developed many years ago, sometimes over a decade ago).  There are often too many opinions \/ gatekeepers, design (of value propositions, pricing approach and sometimes even feature set) by committee is prevalent.  Highly collaborative \/ \"No blame\" culture has all but eliminated individual accountability on a major project level.","Sentiments":"Positive","wordcloud":["track","record","developing","innovations","valued","market","poor","spend","time","talking","previous","wins","innovations","developed","years","ago","decade","ago","opinions","gatekeepers","design","propositions","pricing","approach","feature","set","committee","prevalent","highly","collaborative","blame","culture","eliminated","individual","accountability","major","project","level"]},
{"Confidence":44.6266746521,"Sentence":"I am not aware of \"cost to serve\" models that have been developed; believe we could gain valuable information on if we spent some time truly understanding the direct costs by customer type to better direct human resources, financial resources, time and energy.","Sentiments":"Positive","wordcloud":["aware","cost","serve","models","developed",";","gain","valuable","information","spent","time","understanding","direct","costs","customer","type","direct","human","resources","financial","resources","time","energy"]},
{"Confidence":76.9427337646,"Sentence":"We have a significant weakness with large-scale production single family Builders, relative to our main facing competition - Moen\r\nThis hurts us overall as the Single Family Builder market has been particularly strong as of late.","Sentiments":"Negative","wordcloud":["significant","weakness","large","scale","production","single","family","builders","relative","main","facing","competition","moen","hurts","single","family","builder","market","strong","late"]},
{"Confidence":76.8086090088,"Sentence":"Areas to improve:  understanding the Pro segment who shops at retail.","Sentiments":"Positive","wordcloud":["areas","improve","understanding","pro","segment","shops","retail"]}
    ……. 
    ….
    ]
    [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 Column Name Mismatch
99 Other Exception