-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalizerFactory.php
More file actions
62 lines (57 loc) · 2.51 KB
/
NormalizerFactory.php
File metadata and controls
62 lines (57 loc) · 2.51 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Codewithkyrian\Tokenizers\Factories;
use Codewithkyrian\Tokenizers\Contracts\NormalizerInterface;
use Codewithkyrian\Tokenizers\Normalizers\BertNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\LowercaseNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\NFCNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\NFKCNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\NFKDNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\NormalizerSequence;
use Codewithkyrian\Tokenizers\Normalizers\PrecompiledNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\PrependNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\ReplaceNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\StripAccentsNormalizer;
use Codewithkyrian\Tokenizers\Normalizers\StripNormalizer;
class NormalizerFactory
{
/**
* @param array<string, mixed> $config the normalizer configuration
*/
public static function create(array $config): NormalizerInterface
{
$type = $config['type'] ?? null;
return match ($type) {
'BertNormalizer' => new BertNormalizer(
cleanText: $config['clean_text'] ?? true,
handleChineseChars: $config['handle_chinese_chars'] ?? true,
stripAccents: $config['strip_accents'] ?? null,
lowercase: $config['lowercase'] ?? true,
),
'Lowercase' => new LowercaseNormalizer(),
'Sequence' => new NormalizerSequence(
array_map(static fn ($c) => self::create($c), $config['normalizers'] ?? [])
),
'Strip' => new StripNormalizer(
stripLeft: $config['strip_left'] ?? true,
stripRight: $config['strip_right'] ?? true
),
'Prepend' => new PrependNormalizer(
prepend: $config['prepend'] ?? ''
),
'Replace' => new ReplaceNormalizer(
regex: $config['pattern']['Regex'] ?? null,
subString: $config['pattern']['String'] ?? null,
replacement: $config['content'] ?? ''
),
'NFC' => new NFCNormalizer(),
'NFKC' => new NFKCNormalizer(),
'NFKD' => new NFKDNormalizer(),
'Precompiled' => new PrecompiledNormalizer(
precompiledCharsmap: $config['precompiled_charsmap']
),
'StripAccents' => new StripAccentsNormalizer(),
default => throw new \Exception("Unknown normalizer type: {$type}"),
};
}
}