How to detect the language of a string?

If the context of your code have internet access, you can try to use the Google API for language detection. http://code.google.com/apis/ajaxlanguage/documentation/ var text = “¿Dónde está el baño?”; google.language.detect(text, function(result) { if (!result.error) { var language=”unknown”; for (l in google.language.Languages) { if (google.language.Languages[l] == result.language) { language = l; break; } } var container = … Read more

Detecting programming language from a snippet [closed]

I think that the method used in spam filters would work very well. You split the snippet into words. Then you compare the occurences of these words with known snippets, and compute the probability that this snippet is written in language X for every language you’re interested in. http://en.wikipedia.org/wiki/Bayesian_spam_filtering If you have the basic mechanism … Read more

Detect language from string in PHP

I’ve used the Text_LanguageDetect pear package with some reasonable results. It’s dead simple to use, and it has a modest 52 language database. The downside is no detection of Eastern Asian languages. require_once ‘Text/LanguageDetect.php’; $l = new Text_LanguageDetect(); $result = $l->detect($text, 4); if (PEAR::isError($result)) { echo $result->getMessage(); } else { print_r($result); } results in: Array … Read more