ZH
apps/winzik / src / Model / Language.php · commit 6576d6f
<?phpnamespace App\Model;/** * The language of lyrics, guessed cheaply (no network): the most frequent small words of each language counted. * Enough to know whether a song is French (no translation then) and what to tell the translator. */final class Language{    /** the languages WinZik translates to and from, with their French names (the option's list) */    public const NAMES = [        'fr' => 'Français', 'en' => 'Anglais', 'es' => 'Espagnol', 'de' => 'Allemand', 'it' => 'Italien', 'pt' => 'Portugais',        'nl' => 'Néerlandais', 'pl' => 'Polonais', 'ru' => 'Russe', 'uk' => 'Ukrainien', 'tr' => 'Turc', 'ja' => 'Japonais',        'zh' => 'Chinois', 'ko' => 'Coréen',    ];    private const WORDS = [        'fr' => ['je', 'tu', 'il', 'elle', 'nous', 'vous', 'les', 'des', 'est', 'et', 'un', 'une', 'le', 'la', 'que', 'qui', 'pas', 'dans', 'pour', 'mon', 'ma', 'mes', 'moi', 'toi', 'ce', 'sur', 'avec', 'plus', 'mais', 'du', 'au', "j'ai", "c'est", 'suis', 'comme', 'ne', 'on'],        'en' => ['the', 'you', 'and', 'i', 'to', 'my', 'me', 'it', 'is', 'in', 'of', 'your', 'that', 'we', 'on', 'for', 'be', 'all', 'just', 'so', "i'm", "don't", 'with', 'love', 'what', 'this', 'are', 'was', 'when', 'can', 'oh', 'no', 'baby', 'yeah', 'know'],        'es' => ['el', 'la', 'que', 'de', 'y', 'en', 'tu', 'te', 'mi', 'me', 'es', 'por', 'un', 'una', 'yo', 'no', 'lo', 'los', 'las', 'con', 'para', 'como', 'pero', 'más', 'quiero', 'amor', 'eres', 'estoy', 'corazón'],        'de' => ['ich', 'du', 'und', 'die', 'der', 'das', 'nicht', 'ist', 'ein', 'eine', 'mich', 'dich', 'mir', 'dir', 'wir', 'mit', 'auf', 'es', 'sie', 'zu', 'den', 'auch', 'noch', 'nur', 'wie', 'bin', 'liebe'],        'it' => ['il', 'che', 'di', 'e', 'la', 'non', 'mi', 'ti', 'un', 'una', 'per', 'io', 'tu', 'sei', 'è', 'come', 'sono', 'del', 'della', 'nel', 'con', 'ma', 'più', 'amore', 'cuore', 'questa', 'gli'],        'pt' => ['o', 'a', 'que', 'de', 'e', 'não', 'eu', 'você', 'um', 'uma', 'meu', 'minha', 'com', 'para', 'te', 'me', 'no', 'na', 'do', 'da', 'é', 'mais', 'amor', 'coração', 'vou', 'quero', 'sem'],        'nl' => ['ik', 'je', 'jij', 'de', 'het', 'een', 'en', 'niet', 'is', 'van', 'dat', 'mijn', 'met', 'voor', 'op', 'wat', 'zijn', 'maar', 'nog', 'ben', 'heb', 'mij'],    ];    /** The language of a text ('fr', 'en'...), '' when it cannot tell. */    public static function detect(string $text): string    {        $text = mb_strtolower($text);        // whole scripts first: Cyrillic, kana, hangul, han        $scripts = ['ja' => '/[\p{Hiragana}\p{Katakana}]/u', 'ko' => '/\p{Hangul}/u', 'zh' => '/\p{Han}/u', 'ru' => '/\p{Cyrillic}/u'];        $letters = max(1, (int)preg_match_all('/\p{L}/u', $text));        foreach ($scripts as $lang => $pattern) {            if ((int)preg_match_all($pattern, $text) > $letters * 0.3) {                return $lang === 'ru' && preg_match('/[іїєґ]/u', $text) ? 'uk' : $lang;            }        }        preg_match_all("/[\p{L}']+/u", str_replace('’', "'", $text), $m);        $words = $m[0];        if (count($words) < 3) {            return '';        }        $counts = array_count_values($words);        $best = '';        $bestScore = 0.0;        $second = 0.0;        foreach (self::WORDS as $lang => $list) {            $score = 0.0;            foreach ($list as $w) {                $score += $counts[$w] ?? 0;            }            $score /= count($words);            if ($score > $bestScore) {                [$second, $bestScore, $best] = [$bestScore, $score, $lang];            } elseif ($score > $second) {                $second = $score;            }        }        // a few small words at least, and clearly more than the next language        return $bestScore >= 0.08 && $bestScore > $second * 1.2 ? $best : '';    }    public static function name(string $code): string    {        return self::NAMES[$code] ?? strtoupper($code);    }}