blob: 2bc5e6761bd81e80d3e882cacdbc049c6c5a712f (
plain) (
blame)
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
|
<?php
declare(strict_types = 1);
namespace LanguageDetection;
/**
* Class Trainer
*
* @copyright Patrick Schur
* @license https://opensource.org/licenses/mit-license.html MIT
* @author Patrick Schur <patrick_schur@outlook.de>
* @package LanguageDetection
*/
class Trainer extends NgramParser
{
/**
* Generates language profiles for all language files
*
* @param string $dirname Name of the directory where the translations files are located
* @return void
*/
public function learn(string $dirname = '')
{
if (empty($dirname))
{
$dirname = __DIR__ . '/../../resources/*/*.txt';
}
else if (!\is_dir($dirname) || !\is_readable($dirname))
{
throw new \InvalidArgumentException('Provided directory could not be found or is not readable');
}
else
{
$dirname = \rtrim($dirname, '/');
$dirname .= '/*/*.txt';
}
/** @var \GlobIterator $txt */
foreach (new \GlobIterator($dirname) as $txt)
{
$content = \mb_strtolower(\file_get_contents($txt->getPathname()));
\file_put_contents(
\substr_replace($txt->getPathname(), 'php', -3),
\sprintf("<?php\n\nreturn %s;\n", var_export([ $txt->getBasename('.txt') => $this->getNgrams($content) ], true))
);
}
}
}
|