divineomega/fuzzy-events

Perform actions based on fuzzy string matches

v1.0.0 2020-03-10 23:39 UTC

This package is auto-updated.

Last update: 2024-10-24 09:42:32 UTC


README

Build Status

Fuzzy events is a PHP package that allows you to perform actions based on a fuzzy string matches.

Installation

Install using the following Composer command.

composer require divineomega/fuzzy-events

Usage

See the following usage example.

class Greeting implements FuzzyListenerInterface
{

    public function handle(string $query)
    {
        return 'Hello there!';
    }
}
$listeners = [
    Greeting::class => [
        'Hello',
        'Hi',
        'Hey',
        'Greetings',
        'Howdy',
        'Hello there',
        'Hi there',
    ],
];

$confidenceThreshold = 75;

$dispatcher = new FuzzyDispatcher($listeners, $confidenceThreshold);

$response = $dispatcher->fire('Greetingz!');

// $response = 'Hello there!'

try {
    $dispatcher->fire('Goodbye!');
} catch (ConfidenceTooLowException $e) {
    // No matches within specified confidence threshold!
}

$confidences = $dispatcher->getConfidences('Hi!');

// $confidences = [
//    Greeting::class => 80
// ]