Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
Trainer | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
learn | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Localization\LanguageDetection |
8 | * @author Patrick Schur <patrick_schur@outlook.de> |
9 | * @copyright Patrick Schur |
10 | * @license https://opensource.org/licenses/mit-license.html MIT |
11 | * @link https://github.com/patrickschur/language-detection |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Localization\LanguageDetection; |
16 | |
17 | /** |
18 | * Langauge training class |
19 | * |
20 | * @package phpOMS\Localization\LanguageDetection |
21 | * @license https://opensource.org/licenses/mit-license.html MIT |
22 | * @link https://github.com/patrickschur/language-detection |
23 | * @since 1.0.0 |
24 | */ |
25 | class Trainer extends NgramParser |
26 | { |
27 | /** |
28 | * Generates language profiles for all language files |
29 | * |
30 | * @param string $dirname Name of the directory where the translations files are located |
31 | * |
32 | * @return void |
33 | * |
34 | * @throws \InvalidArgumentException |
35 | * |
36 | * @since 1.0.0 |
37 | */ |
38 | public function learn(string $dirname = '') : void |
39 | { |
40 | if (empty($dirname)) { |
41 | $dirname = __DIR__ . '/resources/*/*.txt'; |
42 | } elseif (!\is_dir($dirname) || !\is_readable($dirname)) { |
43 | throw new \InvalidArgumentException('Provided directory could not be found or is not readable'); |
44 | } else { |
45 | $dirname = \rtrim($dirname, '/'); |
46 | $dirname .= '/*/*.txt'; |
47 | } |
48 | |
49 | /** @var \GlobIterator $txt */ |
50 | foreach (new \GlobIterator($dirname) as $txt) { |
51 | $content = \file_get_contents($txt->getPathname()); |
52 | if ($content === false) { |
53 | break; |
54 | } |
55 | |
56 | $content = \mb_strtolower($content); |
57 | |
58 | \file_put_contents( |
59 | \substr_replace($txt->getPathname(), 'php', -3), |
60 | \sprintf("<?php\n\nreturn %s;\n", \var_export([$txt->getBasename('.txt') => $this->getNgrams($content)], true)) |
61 | ); |
62 | } |
63 | } |
64 | } |