I'm a bit new to PHP and I'm still learning about namespaces. Right now I'm trying to impliment "PHP-ML" a machine learning project from github http://php-ml.readthedocs.io/en/v0.1.0/ It has an example included right in the index file itself, but for some reason, I couldn't get it to return anything. It didn't error out, but it didn't return anything.
Not realizing what was wrong, I continued on to a public tutorial,
https://www.sitepoint.com/how-to-analyze-tweet-sentiments-with-php-machine-learning/
And followed the steps to recreate the program given, but it returned the error:
Undefined variable: classifier
from the line $predictedLabels = $classifier->predict($testSamples);
in classifyTweets.php.
After doing a ton of experimenting, it seems clear to me that the namespaces just aren't being recognized somehow. I can call the files with include __DIR__ . '/src/classification/SentimentAnalysis.php';
but when I try to use use PhpmlExercise\Classification\SentimentAnalysis;
(which is what the tutorial calls for) Nothing happens
It doesn't even error out when I try to call it from the namespace, it just doesn't do anything. Until you try and use the public function "predict" from SentimentAnalysis.php, THEN it throws you an error, saying it doesn't exist.
I'm following all the steps perfectly, I thought. I don't understand what's going wrong.
--EDIT: added some of the basic code structure SentimentAnalysis.php
<?php
namespace PhpmlExercise;
use PhpmlExercise\Classification\SentimentAnalysis;
require __DIR__ . '/vendor/autoload.php';
// steps 1-4 have not given me any trouble, so I'll leave them out
//STEP 5 : Test the classifier accuracy
$predictedLabels = $classifier->predict($testSamples);
That last line is where I get an error saying classifier is an undefined variable. Which isn't wrong. But I thought SentimentAnalysis.php was supposed to handle that. At least, I set it all up in the way the tutorial instructed. $testSamples is created in step 4. "predict" is a public function of SentimentAnalysis, which is shown below
<?php
namespace PhpmlExercise\Classification;
use Phpml\Classification\NaiveBayes;
/**
* Class SentimentAnalysis
* @package PhpmlExercise\Classification
*/
class SentimentAnalysis
{
protected $classifier;
public function __construct()
{
$this->classifier = new NaiveBayes();
}
public function predict($samples)
{
return $this->classifier->predict($samples);
}
}
And yes, the composer.json in my root folder does contain
"autoload": {
"psr-4": {"PhpmlExercise\\": "src/"}
},
If you need any more code, let me know.
--OLD--
Here's a copy of the code I'm using.
https://www.dropbox.com/s/l4hb29g4o01ge88/phpml%20sample.zip?dl=0
Some of it is a bit messy. But it's current state is exactly the same as the tutorial's. you're going to have to take my word for it.
If you need me to post anything else, let me know.