1<?php
2
3declare(strict_types = 1);
4
5namespace LanguageDetection;
6
7/**
8 * Class Trainer
9 *
10 * @copyright Patrick Schur
11 * @license https://opensource.org/licenses/mit-license.html MIT
12 * @author Patrick Schur <patrick_schur@outlook.de>
13 * @package LanguageDetection
14 */
15class Trainer extends NgramParser
16{
17    /**
18     * Generates language profiles for all language files
19     *
20     * @param string $dirname Name of the directory where the translations files are located
21     * @return void
22     */
23    public function learn(string $dirname = '')
24    {
25        if (empty($dirname))
26        {
27            $dirname = __DIR__ . '/../../resources/*/*.txt';
28        }
29        else if (!\is_dir($dirname) || !\is_readable($dirname))
30        {
31            throw new \InvalidArgumentException('Provided directory could not be found or is not readable');
32        }
33        else
34        {
35            $dirname = \rtrim($dirname, '/');
36            $dirname .= '/*/*.txt';
37        }
38
39        /** @var \GlobIterator $txt */
40        foreach (new \GlobIterator($dirname) as $txt)
41        {
42            $content = \mb_strtolower(\file_get_contents($txt->getPathname()));
43
44            \file_put_contents(
45                \substr_replace($txt->getPathname(), 'php', -3),
46                \sprintf("<?php\n\nreturn %s;\n", var_export([ $txt->getBasename('.txt') => $this->getNgrams($content) ], true))
47            );
48        }
49    }
50}
51