-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from diego-ninja/feature/improved_detection
feat(detection): improves local checker detection
- Loading branch information
Showing
35 changed files
with
1,347 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Ninja\Censor\Contracts; | ||
|
||
interface DetectionStrategy | ||
{ | ||
/** | ||
* @param array<string> $words | ||
* @return array{ | ||
* clean: string, | ||
* matches: array<int, array{word: string, type: string}> | ||
* } | ||
*/ | ||
public function detect(string $text, array $words): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Ninja\Censor\Detection; | ||
|
||
use Ninja\Censor\Contracts\DetectionStrategy; | ||
|
||
final readonly class LevenshteinStrategy implements DetectionStrategy | ||
{ | ||
public function __construct( | ||
private string $replacer, | ||
private int $threshold | ||
) {} | ||
|
||
public function detect(string $text, array $words): array | ||
{ | ||
$matches = []; | ||
$clean = $text; | ||
|
||
$textWords = preg_split('/\s+/', $text); | ||
|
||
if ($textWords === false) { | ||
return [ | ||
'clean' => $clean, | ||
'matches' => $matches, | ||
]; | ||
} | ||
|
||
foreach ($textWords as $textWord) { | ||
$bestMatch = null; | ||
$shortestDistance = PHP_INT_MAX; | ||
|
||
foreach ($words as $badWord) { | ||
if (mb_strtolower($textWord) === mb_strtolower($badWord) || | ||
str_contains($badWord, ' ')) { | ||
continue; | ||
} | ||
|
||
$distance = levenshtein( | ||
mb_strtolower($textWord), | ||
mb_strtolower($badWord) | ||
); | ||
|
||
if ($distance <= $this->threshold && $distance < $shortestDistance) { | ||
$shortestDistance = $distance; | ||
$bestMatch = [ | ||
'word' => $textWord, | ||
'type' => 'levenshtein', | ||
'distance' => $distance, | ||
]; | ||
} | ||
} | ||
|
||
if ($bestMatch !== null) { | ||
$matches[] = [ | ||
'word' => $bestMatch['word'], | ||
'type' => $bestMatch['type'], | ||
]; | ||
$clean = str_replace( | ||
$bestMatch['word'], | ||
str_repeat($this->replacer, mb_strlen($bestMatch['word'])), | ||
$clean | ||
); | ||
} | ||
} | ||
|
||
return [ | ||
'clean' => $clean, | ||
'matches' => $matches, | ||
]; | ||
} | ||
} |
Oops, something went wrong.