Class: CharlockHolmes::EncodingDetector
- Inherits:
-
Object
- Object
- CharlockHolmes::EncodingDetector
- Defined in:
- lib/charlock_holmes/encoding_detector.rb,
ext/charlock_holmes/encoding_detector.c
Constant Summary collapse
- DEFAULT_BINARY_SCAN_LEN =
Default length for which to scan content for NULL bytes
1024*1024
- BINARY =
'binary'
Instance Attribute Summary collapse
-
#binary_scan_length ⇒ Object
Length for which to scan content for NULL bytes.
Class Method Summary collapse
-
.build_encoding_table ⇒ Object
Builds the ENCODING_TABLE hash by running through the list of supported encodings in the ICU detection API and trying to map them to supported encodings in Ruby.
-
.detect(str, hint_enc = nil) ⇒ Object
Attempt to detect the encoding of this string.
-
.detect_all(str, hint_enc = nil) ⇒ Object
Attempt to detect the encoding of this string, and return a list with all the possible encodings that match it.
- .encoding_table ⇒ Object
-
.detectable_encodings=(EncodingDetector) ⇒ Object
The list of detectable encodings supported by this library.
Instance Method Summary collapse
-
#detection_hash=(EncodingDetector) ⇒ Object
Attempt to detect the encoding of this string.
-
#detection_hash_array=(EncodingDetector) ⇒ Object
Attempt to detect the encoding of this string, and return a list with all the possible encodings that match it.
-
#initialize(scan_len = DEFAULT_BINARY_SCAN_LEN) ⇒ EncodingDetector
constructor
A new instance of EncodingDetector.
-
#true ⇒ Boolean
Attempt to detect if a string is binary or text.
-
#strip_tags? ⇒ Object
(also: #strip_tags?)
Returns whether or not the strip_tags flag is set on this detector.
-
#strip_tags=(true) ⇒ Object
Enable or disable the stripping of HTML/XML tags from the input before attempting any detection.
Constructor Details
#initialize(scan_len = DEFAULT_BINARY_SCAN_LEN) ⇒ EncodingDetector
Returns a new instance of EncodingDetector.
11 12 13 |
# File 'lib/charlock_holmes/encoding_detector.rb', line 11 def initialize(scan_len=DEFAULT_BINARY_SCAN_LEN) @binary_scan_length = scan_len end |
Instance Attribute Details
#binary_scan_length ⇒ Object
Length for which to scan content for NULL bytes
7 8 9 |
# File 'lib/charlock_holmes/encoding_detector.rb', line 7 def binary_scan_length @binary_scan_length end |
Class Method Details
.build_encoding_table ⇒ Object
Builds the ENCODING_TABLE hash by running through the list of supported encodings in the ICU detection API and trying to map them to supported encodings in Ruby. This is built dynamically so as to take advantage of ICU upgrades which may have support for more encodings in the future.
Returns nothing.
65 66 67 68 69 70 71 72 73 |
# File 'lib/charlock_holmes/encoding_detector.rb', line 65 def self.build_encoding_table supported_encodings.each do |name| @encoding_table[name] = begin ::Encoding.find(name).name rescue ArgumentError BINARY end end end |
.detect(str, hint_enc = nil) ⇒ Object
Attempt to detect the encoding of this string
NOTE: This will create a new CharlockHolmes::EncodingDetector instance on every call as well as use the default binary scan length
str - a String, what you want to detect the encoding of hint_enc - an optional String (like “UTF-8”), the encoding name which will
be used as an additional hint to the charset detector
Returns: a Hash with :encoding, :language, :type and :confidence
25 26 27 |
# File 'lib/charlock_holmes/encoding_detector.rb', line 25 def self.detect(str, hint_enc=nil) new.detect(str, hint_enc) end |
.detect_all(str, hint_enc = nil) ⇒ Object
Attempt to detect the encoding of this string, and return a list with all the possible encodings that match it.
NOTE: This will create a new CharlockHolmes::EncodingDetector instance on every call as well as use the default binary scan length
str - a String, what you want to detect the encoding of hint_enc - an optional String (like “UTF-8”), the encoding name which will
be used as an additional hint to the charset detector
Returns: an Array with zero or more Hashes, each one of them with with :encoding, :language, :type and :confidence
41 42 43 |
# File 'lib/charlock_holmes/encoding_detector.rb', line 41 def self.detect_all(str, hint_enc=nil) new.detect_all(str, hint_enc) end |
.encoding_table ⇒ Object
53 54 55 |
# File 'lib/charlock_holmes/encoding_detector.rb', line 53 def self.encoding_table @encoding_table end |
.detectable_encodings=(EncodingDetector) ⇒ Object
The list of detectable encodings supported by this library
Returns: an Array of Strings
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'ext/charlock_holmes/encoding_detector.c', line 317
static VALUE rb_get_supported_encodings(VALUE klass)
{
UCharsetDetector *csd;
UErrorCode status = U_ZERO_ERROR;
UEnumeration *encoding_list;
VALUE rb_encoding_list;
int32_t enc_count;
int32_t i;
const char *enc_name;
int32_t enc_name_len;
rb_encoding_list = rb_iv_get(klass, "encoding_list");
// lazily populate the list
if (NIL_P(rb_encoding_list)) {
csd = ucsdet_open(&status);
encoding_list = ucsdet_getAllDetectableCharsets(csd, &status);
rb_encoding_list = rb_ary_new();
enc_count = uenum_count(encoding_list, &status);
rb_ary_push(rb_encoding_list, charlock_new_str2("windows-1250"));
rb_ary_push(rb_encoding_list, charlock_new_str2("windows-1252"));
rb_ary_push(rb_encoding_list, charlock_new_str2("windows-1253"));
rb_ary_push(rb_encoding_list, charlock_new_str2("windows-1254"));
rb_ary_push(rb_encoding_list, charlock_new_str2("windows-1255"));
for(i=0; i < enc_count; i++) {
enc_name = uenum_next(encoding_list, &enc_name_len, &status);
rb_ary_push(rb_encoding_list, charlock_new_str(enc_name, enc_name_len));
}
rb_iv_set(klass, "encoding_list", rb_encoding_list);
ucsdet_close(csd);
}
return rb_encoding_list;
}
|
Instance Method Details
#detection_hash=(EncodingDetector) ⇒ Object
Attempt to detect the encoding of this string
str - a String, what you want to detect the encoding of hint_enc - an optional String (like “UTF-8”), the encoding name which will
be used as an additional hint to the charset detector
Returns: a Hash with :encoding, :language, :type and :confidence
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'ext/charlock_holmes/encoding_detector.c', line 179
static VALUE rb_encdec_detect(int argc, VALUE *argv, VALUE self)
{
UErrorCode status = U_ZERO_ERROR;
charlock_detector_t *detector;
VALUE rb_str;
VALUE rb_enc_hint;
rb_scan_args(argc, argv, "11", &rb_str, &rb_enc_hint);
Check_Type(rb_str, T_STRING);
TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector);
// first lets see if this is binary content
if (detect_binary_content(self, rb_str)) {
return rb_encdec_binarymatch();
}
// if we got here - the data doesn't look like binary
// lets try to figure out what encoding the text is in
ucsdet_setText(detector->csd, RSTRING_PTR(rb_str), (int32_t)RSTRING_LEN(rb_str), &status);
if (!NIL_P(rb_enc_hint)) {
Check_Type(rb_enc_hint, T_STRING);
ucsdet_setDeclaredEncoding(detector->csd, RSTRING_PTR(rb_enc_hint), (int32_t)RSTRING_LEN(rb_enc_hint), &status);
}
return rb_encdec_buildmatch(ucsdet_detect(detector->csd, &status));
}
|
#detection_hash_array=(EncodingDetector) ⇒ Object
Attempt to detect the encoding of this string, and return a list with all the possible encodings that match it.
str - a String, what you want to detect the encoding of hint_enc - an optional String (like “UTF-8”), the encoding name which will
be used as an additional hint to the charset detector
Returns: an Array with zero or more Hashes,
each one of them with with :encoding, :language, :type and :confidence
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'ext/charlock_holmes/encoding_detector.c', line 223
static VALUE rb_encdec_detect_all(int argc, VALUE *argv, VALUE self)
{
UErrorCode status = U_ZERO_ERROR;
charlock_detector_t *detector;
const UCharsetMatch **csm;
VALUE rb_ret;
int i, match_count;
VALUE rb_str;
VALUE rb_enc_hint;
VALUE binary_match;
rb_scan_args(argc, argv, "11", &rb_str, &rb_enc_hint);
Check_Type(rb_str, T_STRING);
TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector);
rb_ret = rb_ary_new();
// first lets see if this is binary content
binary_match = Qnil;
if (detect_binary_content(self, rb_str)) {
binary_match = rb_encdec_binarymatch();
}
ucsdet_setText(detector->csd, RSTRING_PTR(rb_str), (int32_t)RSTRING_LEN(rb_str), &status);
if (!NIL_P(rb_enc_hint)) {
Check_Type(rb_enc_hint, T_STRING);
ucsdet_setDeclaredEncoding(detector->csd, RSTRING_PTR(rb_enc_hint), (int32_t)RSTRING_LEN(rb_enc_hint), &status);
}
csm = ucsdet_detectAll(detector->csd, &match_count, &status);
for (i = 0; i < match_count; ++i) {
rb_ary_push(rb_ret, rb_encdec_buildmatch(csm[i]));
}
if (!NIL_P(binary_match))
rb_ary_unshift(rb_ret, binary_match);
return rb_ret;
}
|
#true ⇒ Boolean
Attempt to detect if a string is binary or text
str - a String, what you want to perform the binary check on
Returns: true or false
160 161 162 163 164 165 166 |
# File 'ext/charlock_holmes/encoding_detector.c', line 160
static VALUE rb_encdec_is_binary(VALUE self, VALUE str)
{
if (detect_binary_content(self, str))
return Qtrue;
else
return Qfalse;
}
|
#strip_tags? ⇒ Object Also known as:
Returns whether or not the strip_tags flag is set on this detector
Returns: Boolean
273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'ext/charlock_holmes/encoding_detector.c', line 273
static VALUE rb_get_strip_tags(VALUE self)
{
charlock_detector_t *detector;
UBool val;
VALUE rb_val;
TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector);
val = ucsdet_isInputFilterEnabled(detector->csd);
rb_val = val == 1 ? Qtrue : Qfalse;
return rb_val;
}
|
#strip_tags=(true) ⇒ Object
Enable or disable the stripping of HTML/XML tags from the input before attempting any detection
Returns: Boolean, the value passed
296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'ext/charlock_holmes/encoding_detector.c', line 296
static VALUE rb_set_strip_tags(VALUE self, VALUE rb_val)
{
charlock_detector_t *detector;
UBool val;
TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector);
val = rb_val == Qtrue ? 1 : 0;
ucsdet_enableInputFilter(detector->csd, val);
return rb_val;
}
|