Skip to content

Commit

Permalink
Flip tokens only once
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored and patrickschur committed Aug 18, 2023
1 parent 5a93199 commit b8da335
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/LanguageDetection/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class Language extends NgramParser
{
/**
* @var array
* @var array<string, array<string, int>>
*/
protected $tokens = [];

Expand All @@ -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);
}
}

/**
Expand All @@ -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)
{
Expand Down

0 comments on commit b8da335

Please sign in to comment.