如何从字符串中检测语言,如python 3中的php函数
我想用Python3编写如下php代码,从刮取的页面的标题或描述中检测英语、波斯语和阿拉伯语如何从字符串中检测语言,如python 3中的php函数,python,regex,python-3.x,Python,Regex,Python 3.x,我想用Python3编写如下php代码,从刮取的页面的标题或描述中检测英语、波斯语和阿拉伯语 function language_detection($title,$description,$type){ $languageType=""; $keywordExist=0; $string=$title." ".$description; $metaTitle=explode(" ",$string); $metaTit
function language_detection($title,$description,$type){
$languageType="";
$keywordExist=0;
$string=$title." ".$description;
$metaTitle=explode(" ",$string);
$metaTitle=str_ireplace(" ","",$metaTitle);
$metaSplittedKeywords="";
$fa=0;
$faKeywordExist=0;
$en=0;
$enKeywordExist=0;
foreach($metaTitle as $splittedTitle){
if(!preg_match("/^[آ ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی]/", $splittedTitle)){
$en=1;
}
if(preg_match("/^[آ ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی]/", $splittedTitle )){
$fa=1;
}
}
$enInt=0;
$faInt=0;
if($en==1 && $fa==1){$languageType="fa";$faInt=1;}
else if($en==1 && $fa==0){$languageType="en";$enInt=1;}
else if($en==0 && $fa==1){$languageType="fa";$faInt=1;}
return $languageType;
}
非常感谢您的帮助。您可能可以使用python中的语言检测库,而不是使用正则表达式匹配。这是to langdetect,一个目前支持55种语言的语言检测库。为什么不把PHP翻译成Python呢?亲爱的@DYZ,我不知道怎么做。