codewithkyrian/transformers
A Laravel-friendly transformers package for turning models, arrays, and API responses into consistent, reusable output. Define transformer classes, map fields, nest relations, and format data cleanly for JSON APIs, with minimal boilerplate and flexible customization.
Text Classification, including sentiment analysis, is a fundamental task in natural language processing (NLP) where a label or class is assigned to a given text. This classification depends on the labels defined during the model's training phase, meaning the final output is contingent upon the specific model used. The features can be anything from the words themselves to the context in which they are used.
text-classificationsentiment-analysisXenova/distilbert-base-uncased-finetuned-sst-2-english.Text Classification can be applied in various scenarios, including but not limited to:
Here's how to perform text classification or sentiment analysis using the pipeline:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$classifier = pipeline('sentiment-analysis');
$result = $classifier("I love TransformersPHP!");
::: details Click to view output
['label' => 'POSITIVE', 'score' => 0.9995358059835]
:::
When running the text-classification pipeline, you can the following options:
texts (string|array)The sentence(s) to classify. It's the first argument so there's no need to pass it as a named argument. You can pass a single string or an array of strings. When passing an array, the pipeline will return predictions for each sentence in the array.
$result = $classifier(['I love TransformersPHP!', 'I hate TransformersPHP!']);
topK (int)[Optional] The number of classification labels to return. By default, it returns the best classification. Set to a specific number to receive that many top classifications, or use -1 to obtain all classifications from the model.
$result = $classifier("I love TransformersPHP!", topK: 3);
::: details Click to view output
[
['label' => 'POSITIVE', 'score' => 0.9995358059835],
['label' => 'NEGATIVE', 'score' => 0.0004641940165],
['label' => 'NEUTRAL', 'score' => 0.0000000000000],
]
:::
The output of the pipeline is an array containing the classification label and the confidence score. The confidence score is a value between 0 and 1, with 1 being the highest confidence. Since the actual labels depend on the model, it's crucial to consult the model's documentation for the specific labels it uses. Here are examples demonstrating how outputs might differ:
Natural Language Inference (NLI) is a task that involves determining the logical relationship between two sentences. The relationship can be one of three types: entailment, contradiction, or neutral. Here's how to perform NLI using the pipeline:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$nli = pipeline('text-classification', 'Xenova/roberta-large-mnli');
$result = $nli('A person is eating, there is no food left');
::: details Click to view output
['label' => 'contradiction', 'score' => 0.9595358059835]
:::
Similar to NLI, Question Natural Language Inference (QNLI) involves determining the logical relationship between two sentences. However, QNLI is specifically designed for question-answering tasks. Here's how to perform QNLI using the pipeline:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$qnli = pipeline('text-classification', 'Xenova/qnli-electra-base');
$result = $qnli('Where is the capital of Nigeria?', 'The capital of Nigeria is Abuja.');
::: details Click to view output
['label' => 'entailment', 'score' => 0.9995358059835]
:::
Sentiment Analysis involves determining the emotional tone behind a series of words to gain an understanding of the attitudes, opinions, and emotions expressed. Here's how to perform sentiment analysis using the pipeline:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$sentiment = pipeline('sentiment-analysis');
$result = $sentiment('I love TransformersPHP!');
::: details Click to view output
['label' => 'POSITIVE', 'score' => 0.9995358059835]
:::
Grammatical Correctness involves evaluating the grammatical accuracy of text. The labels for grammatical correctness
are typically binary, with the options being acceptable and unacceptable. Here's how to perform grammatical
correctness
analysis using the pipeline:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$grammar = pipeline('text-classification', 'codewithkyrian/bert-base-uncased-rotten-tomatoes'); // convert to ONNX
$result = $grammar('I will not be able to attended the meeting because I am sick.');
::: details Click to view output
['label' => 'unacceptable', 'score' => 0.9995358059835]
:::
How can I help you explore Laravel packages today?