0

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.

neubert
  • 15,947
  • 24
  • 120
  • 212
  • Did you install the library with composer? – Don't Panic Feb 05 '18 at 20:18
  • hi, hope you’re doing well with your learning. Eventhough haven’t look at your profile dropbox, I suggest you to use ‘strict’, you said nothing happens. It will give you an error once nothing happens, so at least you can google it and research furthermore. Good luck! – Vitali Pom Feb 05 '18 at 20:19
  • @Don’t Panic seems like a little bit too novice question for Composer to me :) – Vitali Pom Feb 05 '18 at 20:20
  • 1
    @VitaliPom the documentation for the ML library linked to in the question says that it should be installed with composer. In order for the classes to be autoloaded with a `use` statement, it's going to depend on the composer autoloader. The tutorial also linked to in the question does so. – Don't Panic Feb 05 '18 at 20:23
  • ooh okay, I let him respond then. Sorry about my misconception. – Vitali Pom Feb 05 '18 at 20:24
  • @Don'tPanic Yes, I installed it with composer to the root folder of the project. Everything on the Composer end has seemed to be working fine for me so far, as far as I'm aware. – Connor Kurschat Feb 05 '18 at 23:32
  • Great! Does your script include the composer autoloader? (Should be something like `require __DIR__ . '/vendor/autoload.php';`) Also, it will be much easier (possible) to answer your question if you can reduce your code to a [mcve] and include it here in the question rather than linking to it. – Don't Panic Feb 05 '18 at 23:34
  • @Don'tPanic Although, I just recalled, earlier in the first part of installing composer, it once gave me this error - Installing php-ai/php-ml Failed to download php-ai/php-ml from source: Failed to clone https://github.com/php-ai/php-ml.git, git was not found, check that it is installed and in your PATH env. But it only gave me that error once. I had googled a few solutions, including downloading the windows version of git, but it didn't seem to solve my problem (or prevent the error from showing up) – Connor Kurschat Feb 05 '18 at 23:48
  • @Don'tPanic Yes it does, and I have been experimenting with the autoloader myself because I suspected it might have something to do with it. And yes, thank you, I will do that. This is my first time posting a stackoverflow thread, and I wasn't really sure the best method to upload code. I will update the original post when I do so – Connor Kurschat Feb 05 '18 at 23:51
  • @Don'tPanic Is that the kind of edit you were hoping for? Should I typically add more or less code to examples like these? Also, I am starting to second guess myself as to if this problem is with namespaces or not, as I really don't see where $classified is supposed to be defined from. I guess I was only assuming the code the tutorial provided took that into account somewhere – Connor Kurschat Feb 06 '18 at 00:52

0 Answers0