overtrue / double-array-trie
A PHP implementation of Double Array Trie.
Fund package maintenance!
overtrue
Requires
- php: >=8.0.2
- ext-mbstring: *
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.8
- friendsofphp/php-cs-fixer: ^3.0
- jetbrains/phpstorm-attributes: ^1.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-12-21 06:10:25 UTC
README
A PHP implementation of Double Array Trie.
Installing
$ composer require overtrue/double-array-trie -vvv
Usage
Build a DoubleArrayTrie
build with a string array
use Overtrue\DoubleArrayTrie\Builder; $builder = new Builder(); $trie = $builder->build(['foo', 'bar', 'baz']); $trie->export()->toFile('trie.json'); $trie->export()->toFile('trie.php'); $trie->export()->toFile('trie.dat');
build with a key-value array
use Overtrue\DoubleArrayTrie\Builder; $builder = new Builder(); $trie = $builder->build([ '一举' => 'yi ju', '一举一动' => 'yi ju yi dong', ]);
Load a DoubleArrayTrie
use Overtrue\DoubleArrayTrie\Factory; $trie = Factory::loadFromFile('trie.json'); $trie = Factory::loadFromFile('trie.php'); $trie = Factory::loadFromFile('trie.dat');
Matching
use Overtrue\DoubleArrayTrie\Matcher; $trie = Factory::loadFromFile('trie.json'); $matcher = new Matcher($trie);
match a string no values:
// ['foo', 'bar', 'baz'] $matcher->match('foo'); // true $matcher->match('oo'); // false
match a string with values:
// ['一举' => 'yi ju', '一举一动' => 'yi ju yi dong'] $matcher->match('一举'); // 'yi ju' $matcher->match('一举一'); // false
prefix matching
// ['一举' => 'yi ju', '一举一动' => 'yi ju yi dong', '一举成名' => 'yi ju cheng ming',] $matcher->prefixMatch('一举一动都很奇怪'); // [ // '一举' => 'yi ju', // '一举一动' => 'yi ju yi dong' //]
Credits
- darts-java: Double-ARray Trie System Java implementation.
- DoubleArrayTrie: A PHP implementation of Double Array Trie.
- 双数组Trie树(DoubleArrayTrie)Java实现
❤️ Sponsor me
如果你喜欢我的项目并想支持它,点击这里 ❤️
Project supported by JetBrains
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
Contributing
You can contribute in one of three ways:
- File bug reports using the issue tracker.
- Answer questions or fix bugs on the issue tracker.
- Contribute new features or update the wiki.
The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.
License
MIT