Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 495 lines (442 sloc) 17.845 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
#!/usr/bin/env python
# coding: utf-8

# nosetests --nocapture tests.py
from nose.tools import eq_

# http://docs.python.org/library/codecs.html
# https://bitbucket.org/medoc/uchardet-enhanced/src/85fc77c3e058/libcharsetdetect/README.md

# Support codecs
# Big5
# EUC-JP
# EUC-KR
# GB18030
# gb18030
# HZ-GB-2312
# IBM855
# IBM866
# ISO-2022-CN
# ISO-2022-JP
# ISO-2022-KR
# ISO-8859-2
# ISO-8859-5
# ISO-8859-7
# ISO-8859-8
# KOI8-R
# Shift_JIS
# TIS-620
# UTF-8
# UTF-16BE
# UTF-16LE
# UTF-32BE
# UTF-32LE
# windows-1250
# windows-1251
# windows-1252
# windows-1253
# windows-1255
# x-euc-tw
# X-ISO-10646-UCS-4-2143
# X-ISO-10646-UCS-4-3412
# x-mac-cyrillic
import cchardet

class TestCchardet():

    def test_detect_bg_iso88595(self):
        encoding = "ISO-8859-5"
        path = r"testdata/bg/ISO-8859-5/wikitop_bg_ISO-8859-5.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_bg_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/bg/UTF-8/wikitop_bg_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_bg_windows1251(self):
        encoding = "WINDOWS-1251"
        path = r"testdata/bg/WINDOWS-1251/wikitop_bg_WINDOWS-1251.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_cz_iso88592(self):
        encoding = "ISO-8859-2"
        path = r"testdata/cz/ISO-8859-2/wikitop_cz_ISO-8859-2.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_cz_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/cz/UTF-8/wikitop_cz_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_de_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/de/UTF-8/wikitop_de_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_de_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/de/WINDOWS-1252/wikitop_de_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_dk_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/dk/UTF-8/wikitop_dk_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_dk_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/dk/WINDOWS-1252/wikitop_dk_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_el_iso88597(self):
        encoding = "ISO-8859-7"
        path = r"testdata/el/ISO-8859-7/wikitop_el_ISO-8859-7.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_el_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/el/UTF-8/wikitop_el_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_en_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/en/UTF-8/wikitop_en_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_en_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/en/WINDOWS-1252/wikitop_en_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_es_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/es/UTF-8/wikitop_es_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_es_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/es/WINDOWS-1252/wikitop_es_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_fi_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/fi/UTF-8/wikitop_fi_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_fi_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/fi/WINDOWS-1252/wikitop_fi_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_fr_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/fr/UTF-8/wikitop_fr_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_fr_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/fr/WINDOWS-1252/wikitop_fr_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_he_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/he/UTF-8/wikitop_he_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_he_windows1255(self):
        encoding = "WINDOWS-1255"
        path = r"testdata/he/WINDOWS-1255/wikitop_he_WINDOWS-1255.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_hu_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/hu/UTF-8/wikitop_hu_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_hu_iso55892(self):
        encoding = "ISO-8859-2"
        path = r"testdata/hu/ISO-8859-2/wikitop_hu_ISO-8859-2.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_it_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/it/UTF-8/wikitop_it_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_it_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/it/WINDOWS-1252/wikitop_it_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_nl_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/nl/UTF-8/wikitop_nl_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_nl_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/nl/WINDOWS-1252/wikitop_nl_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_no_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/no/UTF-8/wikitop_no_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_no_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/no/WINDOWS-1252/wikitop_no_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_pl_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/pl/UTF-8/wikitop_pl_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_pl_iso88592(self):
        encoding = "ISO-8859-2"
        path = r"testdata/pl/ISO-8859-2/wikitop_pl_ISO-8859-2.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_pt_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/pt/UTF-8/wikitop_pt_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_pt_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/pt/WINDOWS-1252/wikitop_pt_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_ru_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/ru/UTF-8/wikitop_ru_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_ru_windows1251(self):
        encoding = "WINDOWS-1251"
        path = r"testdata/ru/WINDOWS-1251/wikitop_ru_WINDOWS-1251.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_ru_ibm855(self):
        encoding = "IBM855"
        path = r"testdata/ru/IBM855/wikitop_ru_IBM855.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_ru_koi8r(self):
        encoding = "KOI8-R"
        path = r"testdata/ru/KOI8-R/wikitop_ru_KOI8-R.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_ru_maccyrillic(self):
        encoding = "MAC-CYRILLIC"
        path = r"testdata/ru/X-MAC-CYRILLIC/wikitop_ru_MACCYRILLIC.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_se_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/se/UTF-8/wikitop_se_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_se_windows1252(self):
        encoding = "WINDOWS-1252"
        path = r"testdata/se/WINDOWS-1252/wikitop_se_WINDOWS-1252.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_th_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/th/UTF-8/wikitop_th_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_th_tis620_1(self):
        encoding = "TIS-620"
        path = r"testdata/th/TIS-620/utffool_th_TIS-620.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_th_tis620_2(self):
        encoding = "TIS-620"
        path = r"testdata/th/TIS-620/wikitop_th_TIS-620.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_tr_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/tr/UTF-8/wikitop_tr_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_tr_iso88599(self):
        encoding = "ISO-8859-9"
        path = r"testdata/tr/ISO-8859-9/wikitop_tr_ISO-8859-9.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_zh_utf8(self):
        encoding = "UTF-8"
        path = r"testdata/zh/UTF-8/wikitop_zh_UTF-8.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        print(detected_encoding)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

    def test_detect_zh_gb18030(self):
        encoding = "GB18030"
        path = r"testdata/zh/GB18030/wikitop_zh_GB18030.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        detected_encoding = cchardet.detect(msg)
        eq_(encoding.lower(),detected_encoding['encoding'].lower())

class TestCchardetSpeed():
    def test_speed(self):
        try:
            import chardet
            has_chardet = True
        except ImportError:
            has_chardet = False
        import time
        do_times = 5
        path = r"testdata/wikipediaJa_One_Thousand_and_One_Nights_SJIS.txt"
        with open(path, 'rb') as f:
            msg = f.read()
        # Test chardet
        if has_chardet:
            result_chardet = 0
            for i in range(do_times):
                start_chardet = time.time()
                chardet.detect(msg)
                result_chardet += (time.time() - start_chardet)
            print('chardet:',1/(result_chardet/do_times), 'call(s)/s')
        # Test cchardet
        result_cchardet = 0
        for i in range(do_times):
            start_cchardet = time.time()
            cchardet.detect(msg)
            result_cchardet += (time.time() - start_cchardet)
        print('cchardet:',1/(result_cchardet/do_times), 'call(s)/s')
Something went wrong with that request. Please try again.