将文本识别为简体与繁体中文

phi*_*reo 6 php unicode cjk language-detection

给定一个已知为中文并以UTF-8编码的文本块,有没有办法确定它是简化还是传统?

Mar*_*ker 4

我不知道这是否有效,但我会尝试使用 iconv 来查看它是否能在字符集之间正确转换,并将相同转换的结果与 //TRANSLIT 和 //IGNORE 进行比较。如果两个结果匹配,则字符集转换没有遇到任何无法翻译的字符,因此应该有一个匹配。

$test1 = iconv("UTF-8", "big5//TRANSLIT", $text);
$test2 = iconv("UTF-8", "big5//IGNORE", $text);
if ($test1 == $test2) {
   echo 'traditional';
} else {
   $test3 = iconv("UTF-8", "gb2312//TRANSLIT", $text);
   $test4 = iconv("UTF-8", "gb2312//IGNORE", $text);
   if ($test3 == $test4) {
      echo 'simplified';
   } else {
      echo 'Failed to match either traditional or simplified';
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 其中有一些 z 变体字符不在基本 GB-2312 中,但在 GB-18030 中。尝试“gb18030”而不是“gb2312”。或者,如果您的输入是面向 Windows 的,您可能更喜欢“cp936”(和“cp950”而不是“big5”)。 (4认同)