鉴于此数据(两种语言的相对字母频率):
spanish => 'e' => 13.72, 'a' => 11.72, 'o' => 8.44, 's' => 7.20, 'n' => 6.83,
english => 'e' => 12.60, 't' => 9.37, 'a' => 8.34, 'o' => 7.70, 'n' => 6.80,
然后计算字符串“这是一个测试”的字母频率给了我:
"t"=>21.43, "s"=>14.29, "i"=>7.14, "r"=>7.14, "y"=>7.14, "'"=>7.14, "h"=>7.14, "e"=>7.14, "l"=>7.14
那么,将给定的字符串字母频率与语言匹配(并尝试检测语言)的好方法是什么?我已经看到(并测试过)一些使用 levenshtein distance 的示例,并且在添加更多语言之前它似乎工作正常。
"this is a test" gives (shortest distance:) [:english, 13] ...
"esto es una prueba" gives (shortest distance:) [:spanish, 13] ...