Analyse feedback on your product using PHP easily

Deepak Mohan Singh
4 min readJan 8, 2020

Text analysis is a much needed technique in this information filled internet. It is used in many aspects in many fields by many companies for many purposes.

Simply put, you developed a product, and now you need feedback on this product for effective build up. You may get feedback either by rating or words. If its words and if its coming from around hundreds of clients, will you keep reading it, or what if i tell you, that the Natural Language Processing(NLP) automatically reads every feedback and categorizes it into positive, negative and neutral feedback? Sounds great. Right? For example,

In love with Slack, I won’t be using anything to communicate else going forward. How did I survive without it?!” → Positive

I don’t agree with the hype, Slack failed to notify me of several important messages and that’s what a communication platform should be all about.” → Negative

“The UX is one of the best, it’s very intuitive and easy to use. However, we don’t have a budget for the high price Slack asks for its paid plans.” → Neutral

(Example taken from monkeylearn.com)

So this is what we are going to build as a simple project, “Feedback analysis”. In the previous blog, we saw how to setup our machine to analyse any one sentence. Now, not just one sentence, but accessing a file containing a set of sentences(feedback), analyzing them, and finally display the number of positive, negative and neutral feedback, by not reading even a single line of text!

Prerequisites:

  • A local web server
  • cURL installed
  • Compressor installed
  • Api of paralleldots

The complete setup of the prerequisites is explained in the previous blog. If you are following this series, you are ready! If not, i strongly recommend you to read the previous blog on installing and setting up the required server software and tools.

Start writing your code 🙂

1) A simple HTML form is needed to allow users upload their feedback file. In this case, we use a csv file.

First, ensure that PHP is configured to allow file uploads.

Open xampp->php->php.ini file, search for the file_uploads directive, and set it to On:

file_uploads = On

2) Create a new file and copy paste the code below, save it as “uploadfile.php” in your project folder inside “htdocs” directive.

<form action="analyse.php" method="POST" enctype="multipart/form-data"> <input type="file" name="file" required> <br><br> <input type="submit" name="submit" value="Upload"> </form>

3) Create a new file and copy paste the code below, save it as “analyse.php” in your project folder inside “htdocs” directive.

<?php 
$flag=0;
if(isset($_POST["submit"]))
{
require(__DIR__ . '/vendor/paralleldots/apis/autoload.php');
require('apikey.php');
$filepath = "files/".$_FILES['file']['name'];
$fileExtensions = ['csv'];
$fileName = $_FILES['file']['name'];
$fileSize = $_FILES['file']['size'];
$tmp = explode('.', $fileName);
$fileExtension = end($tmp);
if (!in_array($fileExtension,$fileExtensions))
{
echo "This file extension is not allowed. Please upload a CSV file"; }
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath))
{
$flag=1; $file = fopen($filepath,"r");
$line = fgetcsv($file);
$result="";
$positive=0;
$neutral=0;
$negative=0;
$nofeedback=0;
$request="[";
while (($line = fgetcsv($file)) !== FALSE)
{
//$line is an array of the csv elements
if(!isset($line[2]))
{
$nofeedback++;
continue;
}
$request.="\"";
$request.=$line[2];
$request.="\"";
$request.=",";
}
$req= rtrim($request, ',');
$req.="]";
fclose($file);
$response = sentiment_batch($req);
$result = json_decode($response);
foreach($result->sentiment as $sentiment)
{
if(isset($sentiment->positive) && isset($sentiment->neutral) && isset($sentiment->negative))
{
$ar = array("POSITIVE"=>$sentiment->positive, "NEUTRAL"=>$sentiment->neutral, "NEGATIVE"=>$sentiment->negative);
$keyOfhighestVal = array_keys($ar,max($ar));
$res = $keyOfhighestVal[0];
if(strcmp($res,"POSITIVE")==0)
$positive++;
else if(strcmp($res,"NEUTRAL")==0)
$neutral++;
else $negative++;
}
}
}
else
{
echo "File upload failed.";
}
}
}
else
{
echo "error";
}
if($flag==1)
{
echo "POSITIVE: ".$positive.'<br>';
echo "NEGATIVE: ".$negative.'<br>';
echo "NEUTRAL: ".$neutral.'<br>';
echo "NO FEEDBACK: ".$nofeedback.'<br>';
}
?>

3) Create an empty folder called “files” in your project folder in “htdocs”.

Run your code 🙂

1) Open Xampp server software and click the “start” button near Apache and MySql.

2) Open your browser and type the url:

localhost/YOUR_PROJECT_FOLDER_NAME/uploadfile.php

And your done!

NOTE 1: The code of the pages in the screenshots contains CSS to decorate the page. In your machine, you’ll get results in plain text format.

NOTE 2: Upload a csv file as this code works with csv files only. Your file can contain as many columns as you want. Give the column number decremented by one in lines 30 and 36 of “analyse.php”. That is, if you are collecting feedback and storing it in 1st column of your csv file, you need to give ‘0’ as array index in lines 30 and 36. In my case, i am collecting feedback in the third column. So i gave array index as ‘2’ in lines 30 and 36.

This is just a kick start tutorial to text analysis and once you start working with this, you can implement this concept in any field you wish! Hope you enjoyed learning and implementing this simple feedback analysis project. If you have any doubts or issues regarding this, do comment below and i’ll try to resolve them. Happy coding 🙂

Originally published at http://codingthing.wordpress.com on January 8, 2020.

--

--