From b8da335336c09fa6814fe0ca0d6d506c357cd7b9 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Fri, 18 Aug 2023 11:24:08 +0200 Subject: [PATCH] Flip tokens only once --- src/LanguageDetection/Language.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/LanguageDetection/Language.php b/src/LanguageDetection/Language.php index 00d2720..c369a33 100644 --- a/src/LanguageDetection/Language.php +++ b/src/LanguageDetection/Language.php @@ -15,7 +15,7 @@ class Language extends NgramParser { /** - * @var array + * @var array> */ protected $tokens = []; @@ -42,14 +42,19 @@ public function __construct(array $lang = [], string $dirname = '') } $isEmpty = empty($lang); + $tokens = []; foreach (\glob($dirname) as $file) { if ($isEmpty || \in_array(\basename($file, '.php'), $lang)) { - $this->tokens += require $file; + $tokens += require $file; } } + + foreach ($tokens as $lang => $value) { + $this->tokens[$lang] = \array_flip($value); + } } /** @@ -71,7 +76,6 @@ public function detect(string $str): LanguageResult foreach ($this->tokens as $lang => $value) { $index = $sum = 0; - $value = \array_flip($value); foreach ($samples as $v) {