Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从字符串中检测语言,如python 3中的php函数_Python_Regex_Python 3.x - Fatal编程技术网

如何从字符串中检测语言,如python 3中的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

我想用Python3编写如下php代码,从刮取的页面的标题或描述中检测英语、波斯语和阿拉伯语

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,我不知道怎么做。