微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

评估字符串是否不是英语:最佳和最简单的做法?

我的字符串足够长(5000个字符),我需要检查它是否为英文.

经过简短的网络搜索,我找到了几种解决方案:

>使用PEAR Text_LanguageDetect(它看起来很吸引人,但我仍在避免不了解它们如何工作的解决方案)
>检查letters frequency(我在下面做了一些评论功能)
>检查国家字符的字符串(例如č,ß等)
>检查字符串中是否包含诸如“ is”,“ the”之类的标记

因此,函数如下:

function is_english($str){
    // Most used English chars frequencies
    $chars = array(
        array('e',12.702),
        array('t', 9.056),
        array('a', 8.167),
        array('o', 7.507),
        array('i', 6.966),
        array('n', 6.749),
        array('s', 6.327),
        array('h', 6.094),
        array('r', 5.987),
    );

    $str = strtolower($str);
    $sum = 0;
    foreach($chars as $key=>$char){
        $i = substr_count($str,$char[0]);
        $i = 100*$i/strlen($str);    // normalization
        $i = $i/$char[1];
        $sum += $i;
    }
    $avg = $sum/count($chars);

    // Calculation of mean square value
    $value = 0;
    foreach($chars as $char)
        $value += pow($char[2]-$avg,2);

    // Average value
    $value = $value / count($chars);
    return $value;
}

通常,此函数会估计字符频率并将其与给定模式进行比较.频率越接近模式,结果应越接近于0.

不幸的是,它的效果不佳:大多数情况下,我认为英语的结果为0.05或更低,而英语则不高.但是,有许多英语字符串具有很高的价值,而许多外国字符串(在我的情况下,大多数是德语)-低.

由于无法找到任何全面的字符集-外语标记,因此我无法实施Third解决方案.

第四个看起来很吸引人,但我不知道哪个标记最适合使用.

有什么想法吗?

PS经过一番讨论,佐德建议该问题与问题Regular expression to match non-English characters?重复,该问题仅部分回答.所以我想让这个问题独立.

解决方法:

我认为第四个解决方案可能是您最好的选择,但我会对其进行扩展以包括更广泛的词典.

您可以在以下位置找到一些综合列表:https://en.wikipedia.org/wiki/Most_common_words_in_English

在当前的实现中,您会遇到一些挫折,因为许多语言都使用标准的拉丁字母.甚至可以说,超出标准拉丁字母的语言通常也主要使用“符合英语的字符”.例如,句子“ Ich bin lustig”是德语,但仅使用拉丁字母字符.同样,“ Jeg er happy”是丹麦语,但仅使用拉丁字母字符.当然,在5000个字符的字符串中,您可能会看到一些非拉丁字符,但这并不能保证.此外,仅关注字符频率,您可能会发现使用拉丁字母的外语通常具有相似的字符出现频率,因此使现有解决方案无效.

通过使用英语词典查找英语单词的出现,您将能够查看字符串并确切确定英语中有多少单词,然后从那里计算英语单词数量的频率. (较高的百分比表示句子可能是英语.)

以下是一个可能的解决方案:

<?PHP
$testString = "Some long string of text that you would like to test.";

// Words from: https://en.wikipedia.org/wiki/Most_common_words_in_English
$common_english_words = array('time', 'person', 'year', 'way', 'day', 'thing', 'man', 'world', 'life', 'hand', 'part', 'child', 'eye', 'woman', 'place', 'work', 'week', 'case', 'point', 'government', 'company', 'number', 'group', 'problem', 'fact', 'be', 'have', 'do', 'say', 'get', 'make', 'go', 'kNow', 'take', 'see', 'come', 'think', 'look', 'want', 'give', 'use', 'find', 'tell', 'ask', 'seem', 'feel', 'try', 'leave', 'call', 'good', 'new', 'first', 'last', 'long', 'great', 'little', 'own', 'other', 'old', 'right', 'big', 'high', 'different', 'small', 'large', 'next', 'early', 'young', 'important', 'few', 'public', 'bad', 'same', 'able', 'to', 'of', 'in', 'for', 'on', 'with', 'at', 'by', 'from', 'up', 'about', 'into', 'over', 'after', 'beneath', 'under', 'above', 'the', 'and', 'a', 'that', 'i', 'it', 'not', 'he', 'as', 'you', 'this', 'but', 'his', 'they', 'her', 'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there', 'their', 'I', 'we', 'what', 'so', 'out', 'if', 'who', 'which', 'me', 'when', 'can', 'like', 'no', 'just', 'him', 'people', 'your', 'some', 'Could', 'them', 'than', 'then', 'Now', 'only', 'its', 'also', 'back', 'two', 'how', 'our', 'well', 'even', 'because', 'any', 'these', 'most', 'us');

/* you might also consider replacing "'s" with ' ', because 's is common in English
   as a contraction and simply removing the single quote Could throw off the frequency. */
$transformedTest = preg_replace('@\s+@', ' ', preg_replace("@[^a-zA-Z'\s]@", ' ', strtolower($testString)));

$splitTest = explode(' ', $transformedTest);

$matchCount = 0;
for($i=0;$i<count($splitTest);$i++){
    if(in_array($splitTest[$i], $common_english_words))
        $matchCount++;
}

echo "raw count: $matchCount\n<br>\nPercent: " . ($matchCount/count($common_english_words))*100 . "%\n<br>\n";
if(($matchCount/count($common_english_words)) > 0.5){
    echo "More than half of the test string is English. Text is likely English.";
}else{
    echo "Text is likely a foreign language.";
}
?>

您可以在此处看到一个示例,其中包含两个要测试的示例字符串(一个为德语,一个为英语):https://ideone.com/lfYcs2

在IDEOne代码中,在英语字符串上运行它时,您将看到结果与常见英语单词的匹配率大约为69.3%.在德语上运行时,匹配百分比仅与常见英语单词匹配的4.57%.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐