public function detect(string $str): LanguageResult
    {
        $str = mb_strtolower($str);

        $samples = $this->getNgrams($str);

        $result = [];

        if (count($samples) > 0)
        {
            foreach ($this->tokens as $lang => $value)
            {
                $index = $sum = 0;
                $value = array_flip($value);

                foreach ($samples as $v)
                {
                    if (isset($value[$v]))
                    {
                        $x = $index++ - $value[$v];
                        $y = $x >> (PHP_INT_SIZE * 8);
                        $sum += ($x + $y) ^ $y;
                        continue;
                    }

                    $sum += $this->maxNgrams;
                    ++$index;
                }

                $result[$lang] = 1 - ($sum / ($this->maxNgrams * $index));
            }

            arsort($result, SORT_NUMERIC);
        }

        return new LanguageResult($result);
    }
public function detect(string $str): LanguageResult
    {
        $str = mb_strtolower($str);

        $samples = $this->getNgrams($str);

        $result = [];

        if (count($samples) > 0)
        {
            foreach ($this->tokens as $lang => $value)
            {
                $index = $sum = 0;
                $value = array_flip($value);

                foreach ($samples as $v)
                {
                    if (isset($value[$v]))
                    {
                        $x = $index++ - $value[$v];
                        $y = $x >> (PHP_INT_SIZE * 8);
                        $sum += ($x + $y) ^ $y;
                        continue;
                    }

                    $sum += $this->maxNgrams;
                    ++$index;
                }

                $result[$lang] = 1 - ($sum / ($this->maxNgrams * $index));
            }

            arsort($result, SORT_NUMERIC);
        }

        return new LanguageResult($result);
    }
public function detect ( string $ str ) : LanguageResult { $ str = mb_strtolower ( $ str ) ; $ samples = $ this -> getNgrams ( $ str ) ; $ result = [ ] ; if ( count ( $ samples ) > 0 ) { foreach ( $ this -> tokens as $ lang => $ value ) { $ index = $ sum = 0 ; $ value = array_flip ( $ value ) ; foreach ( $ samples as $ v ) { if ( isset ( $ value [ $ v ] ) ) { $ x = $ index ++ - $ value [ $ v ] ; $ y = $ x >> ( PHP_INT_SIZE * 8 ) ; $ sum += ( $ x + $ y ) ^ $ y ; continue ; } $ sum += $ this -> maxNgrams ; ++ $ index ; } $ result [ $ lang ] = 1 - ( $ sum / ( $ this -> maxNgrams * $ index ) ) ; } arsort ( $ result , SORT_NUMERIC ) ; } return new LanguageResult ( $ result ) ; }
Detects the language from a given text string

@param string $str
@return LanguageResult
Detects the language from a given text string