Case 2. Review classification: "positive / negative"

Implementation in RubixML

In this case we build a simple RubixML pipeline: transform short reviews into TF–IDF features and train Naive Bayes to detect sentiment (positive/negative). It is a fast and practical baseline for customer feedback analysis.

Example of code:

 
<?php

use Rubix\ML\Classifiers\GaussianNB;
use 
Rubix\ML\Datasets\Labeled;
use 
Rubix\ML\Pipeline;
use 
Rubix\ML\Tokenizers\NGram;
use 
Rubix\ML\Transformers\TextNormalizer;
use 
Rubix\ML\Transformers\TfIdfTransformer;
use 
Rubix\ML\Transformers\WordCountVectorizer;

// Training data
$samples = [
    
'excellent product with no issues' => 'positive',
    
'excellent product highly recommended' => 'positive',
    
'excellent product worth the money' => 'positive',
    
'excellent product works perfectly' => 'positive',
    
'excellent product very satisfied' => 'positive',
    
'excellent product pleasant surprise' => 'positive',
    
'excellent product exactly what I needed' => 'positive',
    
'excellent product super smooth' => 'positive',
    
'excellent product top notch' => 'positive',
    
'excellent product really happy' => 'positive',
    
'excellent product delivery is very fast' => 'positive',
    
'excellent product the delivery is very fast and reliable' => 'positive',
    
'excellent product very fast delivery and great service' => 'positive',
    
'excellent delivery with no issues' => 'positive',
    
'excellent delivery highly recommended' => 'positive',
    
'excellent delivery worth the money' => 'positive',
    
'excellent delivery works perfectly' => 'positive',
    
'excellent delivery very satisfied' => 'positive',
    
'excellent delivery pleasant surprise' => 'positive',
    
'excellent delivery exactly what I needed' => 'positive',
    
'excellent delivery super smooth' => 'positive',
    
'excellent delivery top notch' => 'positive',
    
'excellent delivery really happy' => 'positive',
    
'excellent delivery delivery is very fast' => 'positive',
    
'excellent delivery the delivery is very fast and reliable' => 'positive',
    
'excellent delivery very fast delivery and great service' => 'positive',
    
'excellent app with no issues' => 'positive',
    
'excellent app highly recommended' => 'positive',
    
'excellent app worth the money' => 'positive',
    
'excellent app works perfectly' => 'positive',
    
'excellent app very satisfied' => 'positive',
    
'excellent app pleasant surprise' => 'positive',
    
'excellent app exactly what I needed' => 'positive',
    
'excellent app super smooth' => 'positive',
    
'excellent app top notch' => 'positive',
    
'excellent app really happy' => 'positive',
    
'excellent app delivery is very fast' => 'positive',
    
'excellent app the delivery is very fast and reliable' => 'positive',
    
'excellent app very fast delivery and great service' => 'positive',
    
'excellent store with no issues' => 'positive',
    
'excellent store highly recommended' => 'positive',
    
'excellent store worth the money' => 'positive',
    
'excellent store works perfectly' => 'positive',
    
'excellent store very satisfied' => 'positive',
    
'excellent store pleasant surprise' => 'positive',
    
'excellent store exactly what I needed' => 'positive',
    
'excellent store super smooth' => 'positive',
    
'excellent store top notch' => 'positive',
    
'excellent store really happy' => 'positive',
    
'excellent store delivery is very fast' => 'positive',
    
'excellent store the delivery is very fast and reliable' => 'positive',
    
'excellent store very fast delivery and great service' => 'positive',
    
'excellent experience with no issues' => 'positive',
    
'excellent experience highly recommended' => 'positive',
    
'excellent experience worth the money' => 'positive',
    
'excellent experience works perfectly' => 'positive',
    
'excellent experience very satisfied' => 'positive',
    
'excellent experience pleasant surprise' => 'positive',
    
'excellent experience exactly what I needed' => 'positive',
    
'excellent experience super smooth' => 'positive',
    
'excellent experience top notch' => 'positive',
    
'excellent experience really happy' => 'positive',
    
'excellent experience delivery is very fast' => 'positive',
    
'excellent experience the delivery is very fast and reliable' => 'positive',
    
'excellent experience very fast delivery and great service' => 'positive',
    
'excellent order with no issues' => 'positive',
    
'excellent order highly recommended' => 'positive',
    
'excellent order worth the money' => 'positive',
    
'excellent order works perfectly' => 'positive',
    
'excellent order very satisfied' => 'positive',
    
'excellent order pleasant surprise' => 'positive',
    
'excellent order exactly what I needed' => 'positive',
    
'excellent order super smooth' => 'positive',
    
'excellent order top notch' => 'positive',
    
'excellent order really happy' => 'positive',
    
'excellent order delivery is very fast' => 'positive',
    
'excellent order the delivery is very fast and reliable' => 'positive',
    
'excellent order very fast delivery and great service' => 'positive',
    
'excellent interface with no issues' => 'positive',
    
'excellent interface highly recommended' => 'positive',
    
'excellent interface worth the money' => 'positive',
    
'excellent interface works perfectly' => 'positive',
    
'excellent interface very satisfied' => 'positive',
    
'excellent interface pleasant surprise' => 'positive',
    
'excellent interface exactly what I needed' => 'positive',
    
'excellent interface super smooth' => 'positive',
    
'excellent interface top notch' => 'positive',
    
'excellent interface really happy' => 'positive',
    
'excellent interface delivery is very fast' => 'positive',
    
'excellent interface the delivery is very fast and reliable' => 'positive',
    
'excellent interface very fast delivery and great service' => 'positive',
    
'excellent quality with no issues' => 'positive',
    
'excellent quality highly recommended' => 'positive',
    
'excellent quality worth the money' => 'positive',
    
'terrible service do not recommend' => 'negative',
    
'terrible service waste of money' => 'negative',
    
'terrible service full of issues' => 'negative',
    
'terrible service never again' => 'negative',
    
'terrible service very frustrating' => 'negative',
    
'terrible service totally disappointed' => 'negative',
    
'terrible service quality is poor' => 'negative',
    
'terrible service support is useless' => 'negative',
    
'terrible service keeps failing' => 'negative',
    
'terrible service not worth it' => 'negative',
    
'terrible service the service is horrible' => 'negative',
    
'terrible service horrible service and poor quality' => 'negative',
    
'terrible service the support is horrible and useless' => 'negative',
    
'terrible product do not recommend' => 'negative',
    
'terrible product waste of money' => 'negative',
    
'terrible product full of issues' => 'negative',
    
'terrible product never again' => 'negative',
    
'terrible product very frustrating' => 'negative',
    
'terrible product totally disappointed' => 'negative',
    
'terrible product quality is poor' => 'negative',
    
'terrible product support is useless' => 'negative',
    
'terrible product keeps failing' => 'negative',
    
'terrible product not worth it' => 'negative',
    
'terrible product the service is horrible' => 'negative',
    
'terrible product horrible service and poor quality' => 'negative',
    
'terrible product the support is horrible and useless' => 'negative',
    
'terrible support do not recommend' => 'negative',
    
'terrible support waste of money' => 'negative',
    
'terrible support full of issues' => 'negative',
    
'terrible support never again' => 'negative',
    
'terrible support very frustrating' => 'negative',
    
'terrible support totally disappointed' => 'negative',
    
'terrible support quality is poor' => 'negative',
    
'terrible support support is useless' => 'negative',
    
'terrible support keeps failing' => 'negative',
    
'terrible support not worth it' => 'negative',
    
'terrible support the service is horrible' => 'negative',
    
'terrible support horrible service and poor quality' => 'negative',
    
'terrible support the support is horrible and useless' => 'negative',
    
'terrible delivery do not recommend' => 'negative',
    
'terrible delivery waste of money' => 'negative',
    
'terrible delivery full of issues' => 'negative',
    
'terrible delivery never again' => 'negative',
    
'terrible delivery very frustrating' => 'negative',
    
'terrible delivery totally disappointed' => 'negative',
    
'terrible delivery quality is poor' => 'negative',
    
'terrible delivery support is useless' => 'negative',
    
'terrible delivery keeps failing' => 'negative',
    
'terrible delivery not worth it' => 'negative',
    
'terrible delivery the service is horrible' => 'negative',
    
'terrible delivery horrible service and poor quality' => 'negative',
    
'terrible delivery the support is horrible and useless' => 'negative',
    
'terrible app do not recommend' => 'negative',
    
'terrible app waste of money' => 'negative',
    
'terrible app full of issues' => 'negative',
    
'terrible app never again' => 'negative',
    
'terrible app very frustrating' => 'negative',
    
'terrible app totally disappointed' => 'negative',
    
'terrible app quality is poor' => 'negative',
    
'terrible app support is useless' => 'negative',
    
'terrible app keeps failing' => 'negative',
    
'terrible app not worth it' => 'negative',
    
'terrible app the service is horrible' => 'negative',
    
'terrible app horrible service and poor quality' => 'negative',
    
'terrible app the support is horrible and useless' => 'negative',
    
'terrible store do not recommend' => 'negative',
    
'terrible store waste of money' => 'negative',
    
'terrible store full of issues' => 'negative',
    
'terrible store never again' => 'negative',
    
'terrible store quality is poor' => 'negative',
    
'terrible store support is useless' => 'negative',
    
'terrible store keeps failing' => 'negative',
    
'terrible store not worth it' => 'negative',
    
'terrible store the service is horrible' => 'negative',
    
'terrible store horrible service and poor quality' => 'negative',
    
'terrible store the support is horrible and useless' => 'negative',
    
'terrible experience do not recommend' => 'negative',
    
'terrible experience waste of money' => 'negative',
    
'terrible experience full of issues' => 'negative',
    
'terrible experience never again' => 'negative',
    
'terrible experience very frustrating' => 'negative',
    
'terrible experience totally disappointed' => 'negative',
    
'terrible experience quality is poor' => 'negative',
    
'terrible experience support is useless' => 'negative',
    
'terrible experience keeps failing' => 'negative',
    
'terrible experience not worth it' => 'negative',
    
'terrible experience the service is horrible' => 'negative',
    
'terrible experience horrible service and poor quality' => 'negative',
    
'terrible experience the support is horrible and useless' => 'negative',
    
'terrible quality do not recommend' => 'negative',
    
'terrible quality waste of money' => 'negative',
    
'terrible quality full of issues' => 'negative',
];

// Split text=>label pairs into Rubix samples and target labels.
$labels array_values($samples);
$samples array_map(static fn (string $text): array => [$text], array_keys($samples));

$dataset Labeled::build($samples$labels);

// Text classification pipeline.
$estimator = new Pipeline(
    [
        new 
TextNormalizer(),
        new 
WordCountVectorizer(tokenizer: new NGram(12)),
        new 
TfIdfTransformer(),
    ],
    new 
GaussianNB()
);

// Train model on prepared dataset.
$estimator->train($dataset);