Replace local matcher with core package
This commit is contained in:
parent
47141cf4c0
commit
699279888a
5 changed files with 47 additions and 85 deletions
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
require __DIR__ . '/../autoload.php';
|
require __DIR__ . '/../autoload.php';
|
||||||
|
|
||||||
|
use Lvl0\AnagramFinder\Core\Matcher;
|
||||||
use Myrmidex\AnagramFinder\ArgHandler;
|
use Myrmidex\AnagramFinder\ArgHandler;
|
||||||
use Myrmidex\AnagramFinder\Matcher;
|
|
||||||
use Myrmidex\AnagramFinder\Output;
|
use Myrmidex\AnagramFinder\Output;
|
||||||
|
|
||||||
const DICTIONARY = __DIR__ . '/../assets/words.txt';
|
const DICTIONARY = __DIR__ . '/../assets/words.txt';
|
||||||
|
|
@ -23,4 +23,3 @@ if ($top10) {
|
||||||
$matches = $matches->slice(0, 10);
|
$matches = $matches->slice(0, 10);
|
||||||
}
|
}
|
||||||
$matches->each(fn(string $word) => Output::line($word . " (" . strlen($word) . ")"));
|
$matches->each(fn(string $word) => Output::line($word . " (" . strlen($word) . ")"));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
{
|
{
|
||||||
"name": "myrmidex/anagram-finder",
|
"name": "anagram-finder/tui",
|
||||||
"description": "An anagram finder in PHP",
|
"description": "A TUI anagram finder in PHP",
|
||||||
"type": "project",
|
"type": "project",
|
||||||
"require": {
|
"require": {
|
||||||
|
"anagram-finder/core": "^0.1.0",
|
||||||
"illuminate/collections": "^13.26"
|
"illuminate/collections": "^13.26"
|
||||||
},
|
},
|
||||||
"license": "AGPL-3.0-only",
|
"license": "AGPL-3.0-only",
|
||||||
|
|
@ -21,5 +22,11 @@
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^13",
|
"phpunit/phpunit": "^13",
|
||||||
"friendsofphp/php-cs-fixer": "^3.95"
|
"friendsofphp/php-cs-fixer": "^3.95"
|
||||||
|
},
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"type": "vcs",
|
||||||
|
"url": "ssh://git@forge.lvl0.xyz:2222/anagram-finder/core.git"
|
||||||
}
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
composer.lock
generated
36
composer.lock
generated
|
|
@ -4,8 +4,42 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "fda7b586750778a7250234cef7e8462d",
|
"content-hash": "c36dc53654ea02e67b64da57d7bc91f3",
|
||||||
"packages": [
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "anagram-finder/core",
|
||||||
|
"version": "v0.1.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "ssh://git@forge.lvl0.xyz:2222/anagram-finder/core.git",
|
||||||
|
"reference": "4b8ce74729fa9a6d0ca91c5d661256ca256e5291"
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"illuminate/collections": "^13.26"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"friendsofphp/php-cs-fixer": "^3.95",
|
||||||
|
"phpunit/phpunit": "^13"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Lvl0\\AnagramFinder\\Core\\": "src/",
|
||||||
|
"Lvl0\\AnagramFinder\\Core\\Tests\\": "tests/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"license": [
|
||||||
|
"AGPL-3.0-only"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "myrmidex",
|
||||||
|
"email": "myrmidex@myrmidex.net"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "AnagramFinder core",
|
||||||
|
"time": "2026-08-22T17:50:27+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "illuminate/collections",
|
"name": "illuminate/collections",
|
||||||
"version": "v13.26.1",
|
"version": "v13.26.1",
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Myrmidex\AnagramFinder;
|
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Illuminate\Support\LazyCollection;
|
|
||||||
|
|
||||||
class Matcher
|
|
||||||
{
|
|
||||||
public static function findWords(string $anagram, int $minLength = 3): Collection
|
|
||||||
{
|
|
||||||
$matches = self::getWordList()
|
|
||||||
->filter(fn (string $word) => strlen($word) >= $minLength)
|
|
||||||
->filter(fn (string $word) => ctype_alpha($word))
|
|
||||||
->filter(fn (string $word) => $word === strtolower($word))
|
|
||||||
->filter(fn (string $word) => self::check($anagram, $word))
|
|
||||||
->toArray();
|
|
||||||
|
|
||||||
return new Collection($matches)
|
|
||||||
->sort(fn ($a, $b) => strlen($b) - strlen($a));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function check(string $anagram, string $word): bool
|
|
||||||
{
|
|
||||||
$pool = array_count_values(str_split($anagram));
|
|
||||||
|
|
||||||
foreach (str_split($word) as $char) {
|
|
||||||
if (($pool[$char] ?? 0) === 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$pool[$char]--;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function getWordList(): LazyCollection
|
|
||||||
{
|
|
||||||
return LazyCollection::make(function () {
|
|
||||||
$fh = fopen(DICTIONARY, 'rb');
|
|
||||||
try {
|
|
||||||
while (($line = fgets($fh)) !== false) {
|
|
||||||
yield rtrim($line, "\r\n");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
fclose($fh);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Myrmidex\AnagramFinder\Tests\Unit;
|
|
||||||
|
|
||||||
use Myrmidex\AnagramFinder\Matcher;
|
|
||||||
use PHPUnit\Framework\Attributes\DataProvider;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
|
|
||||||
final class MatcherTest extends TestCase
|
|
||||||
{
|
|
||||||
#[DataProvider('checkProvider')]
|
|
||||||
public function testCheck(string $anagram, string $word, bool $isExpectedMatch)
|
|
||||||
{
|
|
||||||
$this->assertEquals($isExpectedMatch, Matcher::check($anagram, $word));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function checkProvider(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'valid1' => [ "eamstoxil", "steam", true ],
|
|
||||||
'valid2' => [ "wordneoxs", "downers", true ],
|
|
||||||
'valid3' => [ "ioevpmoef", "movie", true ],
|
|
||||||
'invalid_basic' => [ "eamstoxil", "smear", false],
|
|
||||||
'invalid_double_e' => [ "eamstexil", "smear", false],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue