Requires the langauge model database (lang.dat) that should have accompanied this class definition in order to be instantiated.
Example usage:
Text_LanguageDetect Text_LanguageDetect(
)
Will attempt to load the language database. If it fails, you will get a PEAR_Error object returned when you try to use detect()
array clusteredSearch(
string
$str)
WARNING: this method is EXPERIMENTAL. It is not recommended for common use, and it may disappear or its functionality may change in future releases without notice.
This compares the sample text to top the top level of clusters. If the sample is similar to the cluster it will drop down and compare it to the languages in the cluster, and so on until it hits a leaf node.
this should find the language in considerably fewer compares (the equivalent of a binary search), however clusterLanguages() is costly and the loss of accuracy from this technique is significant.
This method may need to be 'fuzzier' in order to become more accurate.
This function could be more useful if the universe of possible languages was very large, however in such cases some method of Bayesian inference might be more helpful.
array clusterLanguages(
)
WARNING: this method is EXPERIMENTAL. It is not recommended for common use, and it may disappear or its functionality may change in future releases without notice.
Uses a nearest neighbor technique to generate the maximum possible number of dendograms from the similarity data.
mixed detect(
string
$sample, [int
$limit = 0])
Calculates the statistical difference between the text and the trigrams for each language, normalizes the score then returns results for all languages in sorted order
If perl compatible, the score is 300-0, 0 being most similar. Otherwise, it's 0-1 with 1 being most similar.
The $sample text should be at least a few sentences in length; should be ascii-7 or utf8 encoded, if other and the mbstring extension is present it will try to detect and convert.
array detectConfidence(
string
$sample)
Confidence is a simple measure calculated from the similarity score minus the similarity score from the next most similar language divided by the highest possible score. Languages that have closely related cousins (e.g. Norwegian and Danish) should generally have lower confidence scores.
The similarity score answers the question "How likely is the text the returned language regardless of the other languages considered?" The confidence score is one way of answering the question "how likely is the text the detected language relative to the rest of the language model set?"
To see how similar languages are a priori, see languageSimilarity()
string detectSimple(
string
$sample)
Calls $this->detect() and returns only the top result
int getLanguageCount(
)
array getLanguages(
)
bool languageExists(
mixed
$lang)
If passed an array of names, will return true only if all exist
array languageSimilarity(
[string
$lang1 = null], [string
$lang2 = null])
Use this function to see how similar languages are to each other.
If passed 2 language names, will return just those languages compared. If passed 1 language name, will return that language compared to all others. If passed none, will return an array of every language model compared to every other one.
int omitLanguages(
mixed
$omit_list, [bool
$include_only = false])
Pass this function the name of or an array of names of languages that you don't want considered
If you're only expecting a limited set of languages, this can greatly speed up processing
void setPerlCompatible(
[bool
$setting = true])