decode
Code to detect and fix semi official and unofficial encodings.
(Northern) sami character eight bit encodings have been semi or non official standards and have been converted to the various systems' internal encodings. This module has functions that revert the damage done.
decode_para(position, text)
Decode the text given to this function.
Replace letters in text with the ones from the dict at position position in CTYPES
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
str
|
an encoding name |
required |
text
|
str
|
the text to decode |
required |
Returns:
| Type | Description |
|---|---|
str
|
The decoded text |
Source code in corpustools/decode.py
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 | |
default_decoder(position, text)
The default decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
str
|
|
required |
text
|
str
|
The string that should be decoded. |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in corpustools/decode.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
fix_macsami_cp1252(instring)
Fix instring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instring
|
str
|
A bytestring that originally was encoded as macsami but has been decoded to unicode as if it was cp1252. |
required |
Returns:
| Type | Description |
|---|---|
str
|
str with fixed encoding. |
Source code in corpustools/decode.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
fix_macsami_latin1(instring)
Fix instring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instring
|
str
|
A bytestring that originally was encoded as macsami but has been decoded to unicode as if it was latin1. |
required |
Returns:
| Type | Description |
|---|---|
str
|
a string with fixed encoding. |
Source code in corpustools/decode.py
49 50 51 52 53 54 55 56 57 58 59 60 | |
fix_macsami_mac(instring)
Fix instring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instring
|
str
|
A bytestring that originally was encoded as macsami but has been decoded to unicode as if it was macroman. |
required |
Returns:
| Type | Description |
|---|---|
str
|
a string with fixed encoding. |
Source code in corpustools/decode.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
fix_meadowmari_cp1252(instring)
Fix instring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instring
|
str
|
A bytestring that originally was encoded as meadowmari but has been decoded to unicode as if it was cp1252. |
required |
Returns:
| Type | Description |
|---|---|
str
|
a string with fixed encoding. |
Source code in corpustools/decode.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
fix_winsami2_cp1252(instring)
Fix instring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instring
|
str
|
A bytestring that originally was encoded as winsami2 but has been decoded to unicode as if it was cp1252. |
required |
Returns:
| Type | Description |
|---|---|
str
|
a string with fixed encoding. |
Source code in corpustools/decode.py
80 81 82 83 84 85 86 87 88 89 90 91 | |
guess_body_encoding(content, mainlang)
Guess the encoding of the string content.
First get the frequencies of the "sami letters" Then get the frequencies of the letters in the encodings in CTYPES
If "sami letters" that the encoding tries to fix exist in "content", disregard the encoding
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
the content |
required |
mainlang
|
str
|
Three-letter language code |
required |
Returns:
| Type | Description |
|---|---|
str
|
A codec name, as given in the keys of CTYPES, or None if no codec could be determined |
Source code in corpustools/decode.py
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 | |
guess_file_encoding(filename, mainlang)
Guess the encoding of a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
the file to open |
required |
Returns:
| Type | Description |
|---|---|
str
|
A codec name, as given in the keys of CTYPES, or None if no codec could be determined |
Source code in corpustools/decode.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |