入力フィールドを介して入力されているデータの言語を検出する方法はありますか?
10 に答える
うーん、私はDimaKrasunの機能の改良版を提供するかもしれません:
functoin is_arabic($string) {
if($string === 'arabic') {
return true;
}
return false;
}
大丈夫、十分な冗談です!
google translate apiを使用するというPekkasの提案は良いものです!しかし、あなたは常により複雑な外部サービスに依存しています。
Rushyosのアプローチは良いと思います!それほど簡単ではありません。私はあなたのために次の関数を書きましたが、テストされていませんが、動作するはずです...
<?
function uniord($u) {
// i just copied this function fron the php.net comments, but it should work fine!
$k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8');
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));
return $k2 * 256 + $k1;
}
function is_arabic($str) {
if(mb_detect_encoding($str) !== 'UTF-8') {
$str = mb_convert_encoding($str,mb_detect_encoding($str),'UTF-8');
}
/*
$str = str_split($str); <- this function is not mb safe, it splits by bytes, not characters. we cannot use it
$str = preg_split('//u',$str); <- this function woulrd probably work fine but there was a bug reported in some php version so it pslits by bytes and not chars as well
*/
preg_match_all('/.|\n/u', $str, $matches);
$chars = $matches[0];
$arabic_count = 0;
$latin_count = 0;
$total_count = 0;
foreach($chars as $char) {
//$pos = ord($char); we cant use that, its not binary safe
$pos = uniord($char);
echo $char ." --> ".$pos.PHP_EOL;
if($pos >= 1536 && $pos <= 1791) {
$arabic_count++;
} else if($pos > 123 && $pos < 123) {
$latin_count++;
}
$total_count++;
}
if(($arabic_count/$total_count) > 0.6) {
// 60% arabic chars, its probably arabic
return true;
}
return false;
}
$arabic = is_arabic('عربية إخبارية تعمل على مدار اليوم. يمكنك مشاهدة بث القناة من خلال الموقع');
var_dump($arabic);
?>
最終的な考え:たとえばラテン語のカウンターを追加したように、範囲は単なるダミーの数字ですが、この方法で文字セット(ヘブライ語、ラテン語、アラビア語、ヒンディー語、中国語など)を検出できます。
また、最初にいくつかの文字を削除することもできます...多分@、スペース、改行、スラッシュなど... preg_split関数のPREG_SPLIT_NO_EMPTYフラグは便利ですが、バグのため、ここでは使用しませんでした。
すべての文字セットのカウンターを用意して、もちろんどれが最も多いかを確認することもできます...
そして最後に、200文字か何かの後で文字列を切り落とすことを検討する必要があります。これは、使用されている文字セットを示すのに十分なはずです。
そして、あなたはいくつかのエラー処理をしなければなりません!ゼロ除算、空の文字列など!それを忘れないでください...質問はありますか?コメント!
文字列の言語を検出する場合は、単語に分割して、いくつかの事前定義されたテーブルで単語を確認する必要があります。完全な辞書は必要ありません。最も一般的な単語だけで、問題なく機能するはずです。トークン化/正規化も必須です!とにかくそのためのライブラリがあり、これはあなたが求めたものではありません:)ただそれについて言及したかっただけです
これにより、文字列がアラビア語であるか、アラビア語のテキストであるかがチェックされます
テキストはUNICODEである必要があります(例:UTF-8)
$str = "بسم الله";
if (preg_match('/[اأإء-ي]/ui', $str)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
短くて簡単な答えのために正規表現を使用する
$is_arabic = preg_match('/\p{Arabic}/u', $text);
これにより、アラビア語の文字列の場合はtrue(1)が返され、非アラビア語の文字列の場合は0が返されます。
99%の場合、文字列にアラビア文字が含まれていて、すべてが含まれているわけではないことを確認するだけで十分だと思います。
私の主な仮定は、少なくとも2つまたは3つのアラビア文字が含まれている場合、読者はそれを読む方法を知っている必要があるということです。
簡単な関数を使用できます。
<?php
/**
* Return`s true if string contains only arabic letters.
*
* @param string $string
* @return bool
*/
function contains_arabic($string)
{
return (preg_match("/^\p{Arabic}/i", $string) > 0);
}
または、正規表現クラスが機能しない場合:
function contains_arabic($subject)
{
return (preg_match("/^[\x0600-\x06FF]/i", $subject) > 0);
}
正規表現を使用してアラビア文字の数を取得し、それを文字列の全長と比較します。たとえば、テキストが少なくとも60%のアラビア語の文字である場合、私はそれを主にアラビア語と見なし、RTLフォーマットを適用します。
/**
* Is the given text mainly Arabic language?
*
* @param string $text string to be tested if it is arabic. :-)
* @return bool
*/
function ct_is_arabic_text($text) {
$text = preg_replace('/[ 0-9\(\)\.\,\-\:\n\r_]/', '', $text); // Remove spaces, numbers, punctuation.
$total_count = mb_strlen($text); // Length of text
if ($total_count==0)
return false;
$arabic_count = preg_match_all("/[اأإء-ي]/ui", $text, $matches); // Number of Arabic characters
if(($arabic_count/$total_count) > 0.6) { // >60% Arabic chars, its probably Arabic languages
return true;
}
return false;
}
インラインRTLフォーマットには、CSSを使用します。クラスの例:
.embed-rtl {
direction: rtl;
unicode-bidi: normal;
text-align: right;
}
私はこのためのPHPソリューションを知りません。
ただし、Google TranslateAjaxAPIが適している場合があります。
APIドキュメントからこのJavascriptスニペットをチェックしてください:例:言語検出
Unicode文字列を参照していると思います...その場合は、文字列内のコードがU + 0600〜U + 06FF(1536〜1791)の文字が存在するかどうかを確認してください。
public static function isArabic($string){
if(preg_match('/\p{Arabic}/u', $string))
return true;
return false;
}
PHP Text_LanguageDetectライブラリは、52の言語を検出できます。ユニットテストが行われ、composerとPEARを介してインストールできます。
この関数は、入力された行/文がアラビア語であるかどうかをチェックします。最初にトリミングしてから、単語ごとにチェックして、両方の合計数を計算しました。
function isArabic($string){
// Initializing count variables with zero
$arabicCount = 0;
$englishCount = 0;
// Getting the cleanest String without any number or Brackets or Hyphen
$noNumbers = preg_replace('/[0-9]+/', '', $string);
$noBracketsHyphen = array('(', ')', '-');
$clean = trim(str_replace($noBracketsHyphen , '', $noNumbers));
// After Getting the clean string, splitting it by space to get the total entered words
$array = explode(" ", $clean); // $array contain the words that was entered by the user
for ($i=0; $i <= count($array) ; $i++) {
// Checking either word is Arabic or not
$checkLang = preg_match('/\p{Arabic}/u', $array[$i]);
if($checkLang == 1){
++$arabicCount;
} else{
++$englishCount;
}
}
if($arabicCount >= $englishCount){
// Return 1 means TRUE i-e Arabic
return 1;
} else{
// Return 0 means FALSE i-e English
return 0;
}
}