Find unambiguous abbreviations for all words in a list.
Just like npm's abbrev, which is just like ruby's Abbrev.
Use composer:
composer install ifcanduela/abbrev
Feed a word list to the Abbrev
constructor and then call one of the three public methods.
Suggestions and matches are case-insensitive. The constructor accepts any number of strings or arrays of strings, arbitrarily nested. Once you have an instance it's ready to go.
use ifcanduela\abbrev\Abbrev;
$abbrev = new Abbrev(
'foo',
'bar',
['baz', 'foobar', ['barbaz']],
);
Get a matching word from the list, or false
if the input is ambiguous.
$abbrev = new Abbrev(['ape', 'aperture', 'apprentice'], 'albino', 'append');
$match = $abbrev->match('ap'); // there is not unambiguous match
// => false
$match = $abbrev->match('al'); // there is only one possible match
// => "albino"
Get a list of matching words from the list for an ambiguous or unambiguous input.
$abbrev = new Abbrev(['ape', 'aperture', 'apprentice'], 'albino', 'append');
$suggestions = $abbrev->suggest('app');
// append
// apprentice
Retrieve a list of all possible abbreviations.
$abbrev = new Abbrev(['ape', 'aperture', 'apprentice'], 'albino', 'append');
$suggestions = $abbrev->abbreviate();
// [
// 'alb' => 'albino',
// 'albi' => 'albino',
// 'albin' => 'albino',
// 'albino' => 'albino',
// 'ape' => 'ape',
// 'aper' => 'aperture',
// 'apert' => 'aperture',
// 'apertu' => 'aperture',
// 'apertur' => 'aperture',
// 'aperture' => 'aperture',
// 'appe' => 'append',
// 'appen' => 'append',
// 'append' => 'append',
// 'appr' => 'apprentice',
// 'appre' => 'apprentice',
// 'appren' => 'apprentice',
// 'apprent' => 'apprentice',
// 'apprenti' => 'apprentice',
// 'apprentic' => 'apprentice',
// 'apprentice' => 'apprentice',
// ]