Character set detection is the process of determining the character set, or encoding, of character data in an unknown format. This is, at best, an imprecise operation using statistics and heuristics. Because of this, detection works best if you supply at least a few hundred bytes of character data that’s mostly in a single language. In some cases, the language can be determined along with the encoding.
Several different techniques are used for character set detection. For multi-byte encodings, the sequence of bytes is checked for legal patterns. The detected characters are also check against a list of frequently used characters in that encoding. For single byte encodings, the data is checked against a list of the most commonly occurring three letter groups for each language that can be written using that encoding. The detection process can be configured to optionally ignore html or xml style markup, which can interfere with the detection process by changing the statistics.
The input data can either be a Java input stream, or an array of bytes. The output of the detection process is a list of possible character sets, with the most likely one first. For simplicity, you can also ask for a Java Reader that will read the data in the detected encoding.
There is another character set detection C++ library, the Compact Encoding Detector, that may have a lower error rate, particularly when working with short samples of text.
CharsetMatch
The CharsetMatch class holds the result of comparing the input data to a particular encoding. You can use an instance of this class to get the name of the character set, the language, and how good the match is. You can also use this class to decode the input data.
To find out how good the match is, you use the getConfidence() method to get a confidence value. This is an integer from 0 to 100. The higher the value, the more confidence there is in the match For example:
CharsetMatchmatch=...;intconfidence;confidence=match.getConfidence();if(confidence<50){// handle a poor match...}else{// handle a good match...}
In C, you can use the ucsdet_getConfidence(const UCharsetMatch *ucsm, UErrorCode *status) method to get a confidence value.
constUCharsetMatch*ucm;UErrorCodestatus=U_ZERO_ERROR;int32_tconfidence=ucsdet_getConfidence(ucm,&status);if(confidence<50){// handle a poor match...}else{// handle a good match...}
To get the name of the character set, which can be used as an encoding name in Java, you use the getName() method:
To get the three letter ISO code for the detected language, you use the getLanguage() method. If the language could not be determined, getLanguage() will return null. Note that language detection does not work with all charsets, and includes only a very small set of possible languages. It should not used if robust, reliable language detection is required.
CharsetMatchmatch=...;StringlanguageCode;languageCode=match.getLanguage();if(languageCode!=null){// handle the language code...}
The ucsdet_getLanguage(const UCharsetMatch *ucsm, UErrorCode *status) method can be used in C to get the language code. If the language could not be determined, the method will return an empty string.
The CharsetDetector class does the actual detection. It matches the input data against all character sets, and computes a list of CharsetMatch objects to hold the results. The input data can be supplied as an array of bytes, or as a java.io.InputStream.
To use a CharsetDetector object, first you construct it, and then you set the input data, using the setText() method. Because setting the input data is separate from the construction, it is easy to reuse a CharsetDetector object:
CharsetDetectordetector;byte[]byteData=...;InputStreamstreamData=...;detector=newCharsetDetector();detector.setText(byteData);// use detector with byte data...detector.setText(streamData);// use detector with stream data...
If you want to know which character set matches your input data with the highest confidence, you can use the detect() method, which will return a CharsetMatch object for the match with the highest confidence:
If you want to know which character set matches your input data in C, you can use the ucsdet_detect(UCharsetDetector *csd , UErrorCode *status) method.
UCharsetDetector*csd;constUCharsetMatch*ucm;staticcharbuffer[BUFFER_SIZE]={....};int32_tinputLength=...// length of the input textUErrorCodestatus=U_ZERO_ERROR;ucsdet_setText(csd,buffer,inputLength,&status);ucm=ucsdet_detect(csd,&status);
If you want to know all of the character sets that could match your input data with a non-zero confidence, you can use the detectAll() method, which will return an array of CharsetMatch objects sorted by confidence, from highest to lowest.:
CharsetDetectordetector;CharsetMatchmatches[];byte[]byteData=...;detector=newCharsetDetector();detector.setText(byteData);matches=detector.detectAll();for(intm=0;m<matches.length;m+=1){// process this match...}
Note: The ucsdet_detectALL(UCharsetDetector *csd , int32_t *matchesFound, UErrorCode *status) method can be used in C in order to detect all of the character sets where matchesFound is a pointer to a variable that will be set to the number of charsets identified that are consistent with the input data.
The CharsetDetector class also implements a crude input filter that can strip out html and xml style tags. If you want to enable the input filter, which is disabled when you construct a CharsetDetector, you use the enableInputFilter() method, which takes a boolean. Pass in true if you want to enable the input filter, and false if you want to disable it:
To enable an input filter in C, you can use ucsdet_enableInputFilter(UCharsetDetector *csd, UBool filter) function.
UCharsetDetector*csd;constUCharsetMatch*ucm;staticcharbuffer[BUFFER_SIZE]={....};int32_tinputLength=...// length of the input textUErrorCodestatus=U_ZERO_ERROR;ucsdet_setText(csd,buffer,inputLength,&status);ucsdet_enableInputFilter(csd,true);ucm=ucsdet_detect(csd,&status);
If you have more detailed knowledge about the structure of the input data, it is better to filter the data yourself before you pass it to CharsetDetector. For example, you might know that the data is from an html page that contains CSS styles, which will not be stripped by the input filter.
You can use the inputFilterEnabled() method to see if the input filter is enabled:
CharsetDetectordetector;detector=newCharsetDetector();// do a bunch of stuff with detector// which may or may not enable the input filter...if(detector.inputFilterEnabled()){// handle enabled input filter}else{// handle disabled input filter}
Note: The ICU4C API provide uscdet_isInputFilterEnabled(const UCharsetDetector* csd) function to check whether the input filter is enabled.
The CharsetDetector class also has two convenience methods that let you detect and convert the input data in one step: the getReader() and getString() methods:
Note: The second argument to the getReader() and getString() methods is a String called declaredEncoding, which is not currently used. There is also a setDeclaredEncoding() method, which is also not currently used.
The following code is equivalent to using the convenience methods:
The following table shows all the encodings that can be detected. You can get this list (without the languages) by calling the getAllDetectableCharsets() method:
Character Set
Languages
UTF-8
UTF-16BE
UTF-16LE
UTF-32BE
UTF-32LE
Shift_JIS
Japanese
ISO-2022-JP
Japanese
ISO-2022-CN
Simplified Chinese
ISO-2022-KR
Korean
GB18030
Chinese
Big5
Traditional Chinese
EUC-JP
Japanese
EUC-KR
Korean
ISO-8859-1
Danish, Dutch, English, French, German, Italian, Norwegian, Portuguese, Swedish
ISO-8859-2
Czech, Hungarian, Polish, Romanian
ISO-8859-5
Russian
ISO-8859-6
Arabic
ISO-8859-7
Greek
ISO-8859-8
Hebrew
ISO-8859-9
Turkish
windows-1250
Czech, Hungarian, Polish, Romanian
windows-1251
Russian
windows-1252
Danish, Dutch, English, French, German, Italian, Norwegian, Portuguese, Swedish