-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathNormalizer.php
More file actions
48 lines (38 loc) · 1.41 KB
/
Copy pathNormalizer.php
File metadata and controls
48 lines (38 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace Codewithkyrian\Transformers\Normalizers;
use Codewithkyrian\Transformers\Transformers;
/**
* A base class for text normalization.
*/
abstract class Normalizer
{
public function __construct(protected array $config) {}
public static function fromConfig(?array $config): ?self
{
if ($config === null) {
return null;
}
$logger = Transformers::getLogger();
$logger->debug('Creating normalizer', ['type' => $config['type'] ?? 'unknown']);
return match ($config['type'] ?? null) {
'BertNormalizer' => new BertNormalizer($config),
'Precompiled' => new Precompiled($config),
'Sequence' => new NormalizerSequence($config),
'Replace' => new Replace($config),
'NFC' => new NFC($config),
'NFKC' => new NFKC($config),
'NFKD' => new NFKD($config),
'Strip' => new StripNormalizer($config),
'StripAccents' => new StripAccents($config),
'Lowercase' => new Lowercase($config),
'Prepend' => new Prepend($config),
default => throw new \InvalidArgumentException('Unknown normalizer type: ' . $config['type'] ?? null),
};
}
abstract public function normalize(string $text): string;
public function __invoke(): string
{
return $this->normalize(...func_get_args());
}
}