diff options
author | Mario <mario@mariovavti.com> | 2023-11-25 17:12:28 +0100 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2023-11-25 17:12:28 +0100 |
commit | 0fd8e02a884a2b040dca62ab5d9674db5f6a070b (patch) | |
tree | 586ee43f32f6f14368c09026f21dcd3244ea24b6 /vendor/patrickschur/language-detection/src/LanguageDetection/Trainer.php | |
parent | 82e704ec5b107823c09f1387e9091adee53a4c2d (diff) | |
parent | 55c4bfb67009c598f25b1a8189604bfffa73dfbb (diff) | |
download | volse-hubzilla-0fd8e02a884a2b040dca62ab5d9674db5f6a070b.tar.gz volse-hubzilla-0fd8e02a884a2b040dca62ab5d9674db5f6a070b.tar.bz2 volse-hubzilla-0fd8e02a884a2b040dca62ab5d9674db5f6a070b.zip |
Merge branch '8.8RC'8.8
Diffstat (limited to 'vendor/patrickschur/language-detection/src/LanguageDetection/Trainer.php')
-rw-r--r-- | vendor/patrickschur/language-detection/src/LanguageDetection/Trainer.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/patrickschur/language-detection/src/LanguageDetection/Trainer.php b/vendor/patrickschur/language-detection/src/LanguageDetection/Trainer.php new file mode 100644 index 000000000..2bc5e6761 --- /dev/null +++ b/vendor/patrickschur/language-detection/src/LanguageDetection/Trainer.php @@ -0,0 +1,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)) + ); + } + } +} |