chardet 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/COPYING +504 -0
  2. data/README +12 -0
  3. data/lib/Big5Freq.rb +913 -0
  4. data/lib/Big5Prober.rb +48 -0
  5. data/lib/CharDistributionAnalysis.rb +245 -0
  6. data/lib/CharSetGroupProber.rb +114 -0
  7. data/lib/CharSetProber.rb +70 -0
  8. data/lib/CodingStateMachine.rb +74 -0
  9. data/lib/ESCSM.rb +242 -0
  10. data/lib/EUCJPProber.rb +97 -0
  11. data/lib/EUCKRFreq.rb +600 -0
  12. data/lib/EUCKRProber.rb +48 -0
  13. data/lib/EUCTWFreq.rb +432 -0
  14. data/lib/EUCTWProber.rb +48 -0
  15. data/lib/EscCharSetProber.rb +94 -0
  16. data/lib/GB2312Freq.rb +475 -0
  17. data/lib/GB2312Prober.rb +48 -0
  18. data/lib/HebrewProber.rb +292 -0
  19. data/lib/JISFreq.rb +573 -0
  20. data/lib/JapaneseContextAnalysis.rb +234 -0
  21. data/lib/LangBulgarianModel.rb +231 -0
  22. data/lib/LangCyrillicModel.rb +332 -0
  23. data/lib/LangGreekModel.rb +229 -0
  24. data/lib/LangHebrewModel.rb +202 -0
  25. data/lib/LangHungarianModel.rb +228 -0
  26. data/lib/LangThaiModel.rb +203 -0
  27. data/lib/Latin1Prober.rb +160 -0
  28. data/lib/MBCSGroupProber.rb +57 -0
  29. data/lib/MBCSSM.rb +513 -0
  30. data/lib/MultiByteCharSetProber.rb +94 -0
  31. data/lib/SBCSGroupProber.rb +71 -0
  32. data/lib/SJISProber.rb +99 -0
  33. data/lib/SingleByteCharSetProber.rb +131 -0
  34. data/lib/UTF8Prober.rb +91 -0
  35. data/lib/UniversalDetector.rb +209 -0
  36. data/python-docs/css/chardet.css +299 -0
  37. data/python-docs/faq.html +107 -0
  38. data/python-docs/how-it-works.html +113 -0
  39. data/python-docs/images/caution.png +0 -0
  40. data/python-docs/images/important.png +0 -0
  41. data/python-docs/images/note.png +0 -0
  42. data/python-docs/images/permalink.gif +0 -0
  43. data/python-docs/images/tip.png +0 -0
  44. data/python-docs/images/warning.png +0 -0
  45. data/python-docs/index.html +73 -0
  46. data/python-docs/license.html +62 -0
  47. data/python-docs/supported-encodings.html +86 -0
  48. data/python-docs/usage.html +107 -0
  49. metadata +86 -0
@@ -0,0 +1,107 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
+ <html lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>Frequently asked questions [Universal Encoding Detector]</title>
6
+ <link rel="stylesheet" href="css/chardet.css" type="text/css">
7
+ <link rev="made" href="mailto:mark@diveintomark.org">
8
+ <meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
9
+ <meta name="keywords" content="character, set, encoding, detection, Python, XML, feed">
10
+ <link rel="start" href="index.html" title="Documentation">
11
+ <link rel="up" href="index.html" title="Documentation">
12
+ <link rel="prev" href="index.html" title="Documentation">
13
+ <link rel="next" href="supported-encodings.html" title="Supported encodings">
14
+ </head>
15
+ <body id="chardet-feedparser-org" class="docs">
16
+ <div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
17
+ <div class="s" id="pageHeader">
18
+ <h1><a href="/">Universal Encoding Detector</a></h1>
19
+ <p>Character encoding auto-detection in Python. As smart as your browser. Open source.</p>
20
+ </div>
21
+ <div class="s" id="quickSummary"><ul>
22
+ <li class="li1">
23
+ <a href="http://chardet.feedparser.org/download/">Download</a> ·</li>
24
+ <li class="li2">
25
+ <a href="index.html">Documentation</a> ·</li>
26
+ <li class="li3"><a href="faq.html" title="Frequently Asked Questions">FAQ</a></li>
27
+ </ul></div>
28
+ </div></div></div>
29
+ <div id="main"><div id="mainInner">
30
+ <p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <span class="thispage">Frequently asked questions</span></p>
31
+ <div class="section" lang="en">
32
+ <div class="titlepage">
33
+ <div><div><h2 class="title">
34
+ <a name="faq" class="skip" href="#faq" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Frequently asked questions</h2></div></div>
35
+ <div></div>
36
+ </div>
37
+ <div class="section" lang="en">
38
+ <div class="titlepage">
39
+ <div><div><h3 class="title">
40
+ <a name="faq.intro" class="skip" href="#faq.intro" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> What is character encoding?</h3></div></div>
41
+ <div></div>
42
+ </div>
43
+ <p>When you think of “<span class="quote">text</span>”, you probably think of “<span class="quote">characters and symbols I see on my computer screen</span>”. But computers don't deal in characters and symbols; they deal in bits and bytes. Every piece of text you've ever seen on a computer screen is actually stored in a particular <span class="emphasis"><em>character encoding</em></span>. There are many different character encodings, some optimized for particular languages like Russian or Chinese or English, and others that can be used for multiple languages. Very roughly speaking, the character encoding provides a mapping between the stuff you see on your screen and the stuff your computer actually stores in memory and on disk.</p>
44
+ <p>In reality, it's more complicated than that. Many characters are common to multiple encodings, but each encoding may use a different sequence of bytes to actually store those characters in memory or on disk. So you can think of the character encoding as a kind of decryption key for the text. Whenever someone gives you a sequence of bytes and claims it's “<span class="quote">text</span>”, you need to know what character encoding they used so you can decode the bytes into characters and display them (or process them, or whatever).</p>
45
+ </div>
46
+ <div class="section" lang="en">
47
+ <div class="titlepage">
48
+ <div><div><h3 class="title">
49
+ <a name="faq.what" class="skip" href="#faq.what" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> What is character encoding auto-detection?</h3></div></div>
50
+ <div></div>
51
+ </div>
52
+ <p>It means taking a sequence of bytes in an unknown character encoding, and attempting to determine the encoding so you can read the text. It's like cracking a code when you don't have the decryption key.</p>
53
+ </div>
54
+ <div class="section" lang="en">
55
+ <div class="titlepage">
56
+ <div><div><h3 class="title">
57
+ <a name="faq.impossible" class="skip" href="#faq.impossible" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Isn't that impossible?</h3></div></div>
58
+ <div></div>
59
+ </div>
60
+ <p>In general, yes. However, some encodings are optimized for specific languages, and languages are not random. Some character sequences pop up all the time, while other sequences make no sense. A person fluent in English who opens a newspaper and finds “<span class="quote">txzqJv 2!dasd0a QqdKjvz</span>” will instantly recognize that that isn't English (even though it is composed entirely of English letters). By studying lots of “<span class="quote">typical</span>” text, a computer algorithm can simulate this kind of fluency and make an educated guess about a text's language.</p>
61
+ <p>In other words, encoding detection is really language detection, combined with knowledge of which languages tend to use which character encodings.</p>
62
+ </div>
63
+ <div class="section" lang="en">
64
+ <div class="titlepage">
65
+ <div><div><h3 class="title">
66
+ <a name="faq.who" class="skip" href="#faq.who" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Who wrote this detection algorithm?</h3></div></div>
67
+ <div></div>
68
+ </div>
69
+ <p>This library is a port of <a href="http://lxr.mozilla.org/seamonkey/source/extensions/universalchardet/src/base/">the auto-detection code in Mozilla</a>. I have attempted to maintain as much of the original structure as possible (mostly for selfish reasons, to make it easier to maintain the port as the original code evolves). I have also retained the original authors' comments, which are quite extensive and informative.</p>
70
+ <p>You may also be interested in the research paper which led to the Mozilla implementation, <a href="http://www.mozilla.org/projects/intl/UniversalCharsetDetection.html">A composite approach to language/encoding detection</a>.</p>
71
+ </div>
72
+ <div class="section" lang="en">
73
+ <div class="titlepage">
74
+ <div><div><h3 class="title">
75
+ <a name="faq.yippie" class="skip" href="#faq.yippie" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Yippie! Screw the standards, I'll just auto-detect everything!</h3></div></div>
76
+ <div></div>
77
+ </div>
78
+ <p>Don't do that. Virtually every format and protocol contains a method for specifying character encoding.</p>
79
+ <div class="itemizedlist"><ul>
80
+ <li>HTTP can define a <tt class="literal">charset</tt> parameter in the <tt class="literal">Content-type</tt> header.</li>
81
+ <li>HTML documents can define a <tt class="literal">&lt;meta http-equiv="content-type"&gt;</tt> element in the <tt class="literal">&lt;head&gt;</tt> of a web page.</li>
82
+ <li>XML documents can define an <tt class="literal">encoding</tt> attribute in the XML prolog.</li>
83
+ </ul></div>
84
+ <p>If text comes with explicit character encoding information, you should use it. If the text has no explicit information, but the relevant standard defines a default encoding, you should use that. (This is harder than it sounds, because standards can overlap. If you fetch an XML document over HTTP, you need to support both standards <span class="emphasis"><em>and</em></span> figure out which one wins if they give you conflicting information.)</p>
85
+ <p>Despite the complexity, it's worthwhile to follow standards and <a href="http://www.w3.org/2001/tag/doc/mime-respect">respect explicit character encoding information</a>. It will almost certainly be faster and more accurate than trying to auto-detect the encoding. It will also make the world a better place, since your program will interoperate with other programs that follow the same standards.</p>
86
+ </div>
87
+ <div class="section" lang="en">
88
+ <div class="titlepage">
89
+ <div><div><h3 class="title">
90
+ <a name="faq.why" class="skip" href="#faq.why" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Why bother with auto-detection if it's slow, inaccurate, and non-standard?</h3></div></div>
91
+ <div></div>
92
+ </div>
93
+ <p>Sometimes you receive text with verifiably inaccurate encoding information. Or text without any encoding information, and the specified default encoding doesn't work. There are also some poorly designed standards that have no way to specify encoding at all.</p>
94
+ <p>If following the relevant standards gets you nowhere, <span class="emphasis"><em>and</em></span> you decide that processing the text is more important than maintaining interoperability, then you can try to auto-detect the character encoding as a last resort. An example is my <a href="http://feedparser.org/">Universal Feed Parser</a>, which calls this auto-detection library <a href="http://feedparser.org/docs/character-encoding.html">only after exhausting all other options</a>.</p>
95
+ </div>
96
+ </div>
97
+ <div class="footernavigation">
98
+ <div style="float: left">← <a class="NavigationArrow" href="index.html">Documentation</a>
99
+ </div>
100
+ <div style="text-align: right">
101
+ <a class="NavigationArrow" href="supported-encodings.html">Supported encodings</a> →</div>
102
+ </div>
103
+ <hr>
104
+ <div id="footer"><p class="copyright">Copyright © 2006 Mark Pilgrim · <a href="mailto:mark@diveintomark.org">mark@diveintomark.org</a> · <a href="license.html">Terms of use</a></p></div>
105
+ </div></div>
106
+ </body>
107
+ </html>
@@ -0,0 +1,113 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
+ <html lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>How it works [Universal Encoding Detector]</title>
6
+ <link rel="stylesheet" href="css/chardet.css" type="text/css">
7
+ <link rev="made" href="mailto:mark@diveintomark.org">
8
+ <meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
9
+ <meta name="keywords" content="character, set, encoding, detection, Python, XML, feed">
10
+ <link rel="start" href="index.html" title="Documentation">
11
+ <link rel="up" href="index.html" title="Documentation">
12
+ <link rel="prev" href="usage.html" title="Usage">
13
+ <link rel="next" href="license.html" title="Terms of use">
14
+ </head>
15
+ <body id="chardet-feedparser-org" class="docs">
16
+ <div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
17
+ <div class="s" id="pageHeader">
18
+ <h1><a href="/">Universal Encoding Detector</a></h1>
19
+ <p>Character encoding auto-detection in Python. As smart as your browser. Open source.</p>
20
+ </div>
21
+ <div class="s" id="quickSummary"><ul>
22
+ <li class="li1">
23
+ <a href="http://chardet.feedparser.org/download/">Download</a> ·</li>
24
+ <li class="li2">
25
+ <a href="index.html">Documentation</a> ·</li>
26
+ <li class="li3"><a href="faq.html" title="Frequently Asked Questions">FAQ</a></li>
27
+ </ul></div>
28
+ </div></div></div>
29
+ <div id="main"><div id="mainInner">
30
+ <p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <span class="thispage">How it works</span></p>
31
+ <div class="section" lang="en">
32
+ <div class="titlepage">
33
+ <div>
34
+ <div><h2 class="title">
35
+ <a name="howitworks" class="skip" href="#howitworks" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> How it works</h2></div>
36
+ <div><div class="abstract">
37
+ <h3 class="title"></h3>
38
+ <p>This is a brief guide to navigating the code itself.</p>
39
+ </div></div>
40
+ </div>
41
+ <div></div>
42
+ </div>
43
+ <p>First, you should read <a href="http://www.mozilla.org/projects/intl/UniversalCharsetDetection.html">A composite approach to language/encoding detection</a>, which explains the detection algorithm and how it was derived. This will help you later when you stumble across the huge character frequency distribution tables like <tt class="filename">big5freq.py</tt> and language models like <tt class="filename">langcyrillicmodel.py</tt>.</p>
44
+ <p>The main entry point for the detection algorithm is <tt class="filename">universaldetector.py</tt>, which has one class, <tt class="classname">UniversalDetector</tt>. (You might think the main entry point is the <tt class="function">detect</tt> function in <tt class="filename">chardet/__init__.py</tt>, but that's really just a convenience function that creates a <tt class="classname">UniversalDetector</tt> object, calls it, and returns its result.)</p>
45
+ <p>There are 5 categories of encodings that <tt class="classname">UniversalDetector</tt> handles:</p>
46
+ <div class="orderedlist"><ol type="1">
47
+ <li>
48
+ <tt class="literal">UTF-n</tt> with a <acronym title="Byte Order Mark">BOM</acronym>. This includes <tt class="literal">UTF-8</tt>, both <acronym title="Big Endian">BE</acronym> and <acronym title="Little Endian">LE</acronym> variants of <tt class="literal">UTF-16</tt>, and all 4 byte-order variants of <tt class="literal">UTF-32</tt>.</li>
49
+ <li>Escaped encodings, which are entirely 7-bit <acronym>ASCII</acronym> compatible, where non-<acronym>ASCII</acronym> characters start with an escape sequence. Examples: <tt class="literal">ISO-2022-JP</tt> (Japanese) and <tt class="literal">HZ-GB-2312</tt> (Chinese).</li>
50
+ <li>Multi-byte encodings, where each character is represented by a variable number of bytes. Examples: <tt class="literal">Big5</tt> (Chinese), <tt class="literal">SHIFT_JIS</tt> (Japanese), <tt class="literal">EUC-KR</tt> (Korean), and <tt class="literal">UTF-8</tt> without a <acronym title="Byte Order Mark">BOM</acronym>.</li>
51
+ <li>Single-byte encodings, where each character is represented by one byte. Examples: <tt class="literal">KOI8-R</tt> (Russian), <tt class="literal">windows-1255</tt> (Hebrew), and <tt class="literal">TIS-620</tt> (Thai).</li>
52
+ <li>
53
+ <tt class="literal">windows-1252</tt>, which is used primarily on Microsoft Windows by middle managers who don't know a character encoding from a hole in the ground.</li>
54
+ </ol></div>
55
+ <div class="section" lang="en">
56
+ <div class="titlepage">
57
+ <div><div><h3 class="title">
58
+ <a name="how.bom" class="skip" href="#how.bom" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> <tt class="literal">UTF-n</tt> with a <acronym title="Byte Order Mark">BOM</acronym></h3></div></div>
59
+ <div></div>
60
+ </div>
61
+ <p>If the text starts with a <acronym title="Byte Order Mark">BOM</acronym>, we can reasonably assume that the text is encoded in <tt class="literal">UTF-8</tt>, <tt class="literal">UTF-16</tt>, or <tt class="literal">UTF-32</tt>. (The <acronym title="Byte Order Mark">BOM</acronym> will tell us exactly which one; that's what it's for.) This is handled inline in <tt class="classname">UniversalDetector</tt>, which returns the result immediately without any further processing.</p>
62
+ </div>
63
+ <div class="section" lang="en">
64
+ <div class="titlepage">
65
+ <div><div><h3 class="title">
66
+ <a name="how.esc" class="skip" href="#how.esc" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Escaped encodings</h3></div></div>
67
+ <div></div>
68
+ </div>
69
+ <p>If the text contains a recognizable escape sequence that might indicate an escaped encoding, <tt class="classname">UniversalDetector</tt> creates an <tt class="classname">EscCharSetProber</tt> (defined in <tt class="filename">escprober.py</tt>) and feeds it the text.</p>
70
+ <p><tt class="classname">EscCharSetProber</tt> creates a series of state machines, based on models of <tt class="literal">HZ-GB-2312</tt>, <tt class="literal">ISO-2022-CN</tt>, <tt class="literal">ISO-2022-JP</tt>, and <tt class="literal">ISO-2022-KR</tt> (defined in <tt class="filename">escsm.py</tt>). <tt class="classname">EscCharSetProber</tt> feeds the text to each of these state machines, one byte at a time. If any state machine ends up uniquely identifying the encoding, <tt class="classname">EscCharSetProber</tt> immediately returns the positive result to <tt class="classname">UniversalDetector</tt>, which returns it to the caller. If any state machine hits an illegal sequence, it is dropped and processing continues with the other state machines.</p>
71
+ </div>
72
+ <div class="section" lang="en">
73
+ <div class="titlepage">
74
+ <div><div><h3 class="title">
75
+ <a name="how.mb" class="skip" href="#how.mb" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Multi-byte encodings</h3></div></div>
76
+ <div></div>
77
+ </div>
78
+ <p>Assuming no <acronym title="Byte Order Mark">BOM</acronym>, <tt class="classname">UniversalDetector</tt> checks whether the text contains any high-bit characters. If so, it creates a series of “<span class="quote">probers</span>” for detecting multi-byte encodings, single-byte encodings, and as a last resort, <tt class="literal">windows-1252</tt>.</p>
79
+ <p>The multi-byte encoding prober, <tt class="classname">MBCSGroupProber</tt> (defined in <tt class="filename">mbcsgroupprober.py</tt>), is really just a shell that manages a group of other probers, one for each multi-byte encoding: <tt class="literal">Big5</tt>, <tt class="literal">GB2312</tt>, <tt class="literal">EUC-TW</tt>, <tt class="literal">EUC-KR</tt>, <tt class="literal">EUC-JP</tt>, <tt class="literal">SHIFT_JIS</tt>, and <tt class="literal">UTF-8</tt>. <tt class="classname">MBCSGroupProber</tt> feeds the text to each of these encoding-specific probers and checks the results. If a prober reports that it has found an illegal byte sequence, it is dropped from further processing (so that, for instance, any subsequent calls to <tt class="classname">UniversalDetector</tt>.<tt class="methodname">feed</tt> will skip that prober). If a prober reports that it is reasonably confident that it has detected the encoding, <tt class="classname">MBCSGroupProber</tt> reports this positive result to <tt class="classname">UniversalDetector</tt>, which reports the result to the caller.</p>
80
+ <p>Most of the multi-byte encoding probers are inherited from <tt class="classname">MultiByteCharSetProber</tt> (defined in <tt class="filename">mbcharsetprober.py</tt>), and simply hook up the appropriate state machine and distribution analyzer and let <tt class="classname">MultiByteCharSetProber</tt> do the rest of the work. <tt class="classname">MultiByteCharSetProber</tt> runs the text through the encoding-specific state machine, one byte at a time, to look for byte sequences that would indicate a conclusive positive or negative result. At the same time, <tt class="classname">MultiByteCharSetProber</tt> feeds the text to an encoding-specific distribution analyzer.</p>
81
+ <p>The distribution analyzers (each defined in <tt class="filename">chardistribution.py</tt>) use language-specific models of which characters are used most frequently. Once <tt class="classname">MultiByteCharSetProber</tt> has fed enough text to the distribution analyzer, it calculates a confidence rating based on the number of frequently-used characters, the total number of character,s and a language-specific distribution ratio. If the confidence is high enough, <tt class="classname">MultiByteCharSetProber</tt> returns the result to <tt class="classname">MBCSGroupProber</tt>, which returns it to <tt class="classname">UniversalDetector</tt>, which returns it to the caller.</p>
82
+ <p>The case of Japanese is more difficult. Single-character distribution analysis is not always sufficient to distinguish between <tt class="literal">EUC-JP</tt> and <tt class="literal">SHIFT_JIS</tt>, so the <tt class="classname">SJISProber</tt> (defined in <tt class="filename">sjisprober.py</tt>) also uses 2-character distribution analysis. <tt class="classname">SJISContextAnalysis</tt> and <tt class="classname">EUCJPContextAnalysis</tt> (both defined in <tt class="filename">jpcntx.py</tt> and both inheriting from a common <tt class="classname">JapaneseContextAnalysis</tt> class) check the frequency of Hiragana syllabary characters within the text. Once enough text has been processed, they return a confidence level to <tt class="classname">SJISProber</tt>, which checks both analyzers and returns the higher confidence level to <tt class="classname">MBCSGroupProber</tt>.</p>
83
+ </div>
84
+ <div class="section" lang="en">
85
+ <div class="titlepage">
86
+ <div><div><h3 class="title">
87
+ <a name="how.sb" class="skip" href="#how.sb" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Single-byte encodings</h3></div></div>
88
+ <div></div>
89
+ </div>
90
+ <p>The single-byte encoding prober, <tt class="classname">SBCSGroupProber</tt> (defined in <tt class="filename">sbcsgroupprober.py</tt>), is also just a shell that manages a group of other probers, one for each combination of single-byte encoding and language: <tt class="literal">windows-1251</tt>, <tt class="literal">KOI8-R</tt>, <tt class="literal">ISO-8859-5</tt>, <tt class="literal">MacCyrillic</tt>, <tt class="literal">IBM855</tt>, and <tt class="literal">IBM866</tt> (Russian); <tt class="literal">ISO-8859-7</tt> and <tt class="literal">windows-1253</tt> (Greek); <tt class="literal">ISO-8859-5</tt> and <tt class="literal">windows-1251</tt> (Bulgarian); <tt class="literal">ISO-8859-2</tt> and <tt class="literal">windows-1250</tt> (Hungarian); <tt class="literal">TIS-620</tt> (Thai); <tt class="literal">windows-1255</tt> and <tt class="literal">ISO-8859-8</tt> (Hebrew).</p>
91
+ <p><tt class="classname">SBCSGroupProber</tt> feeds the text to each of these encoding+language-specific probers and checks the results. These probers are all implemented as a single class, <tt class="classname">SingleByteCharSetProber</tt> (defined in <tt class="filename">sbcharsetprober.py</tt>), which takes a language model as an argument. The language model defines how frequently different 2-character sequences appear in typical text. <tt class="classname">SingleByteCharSetProber</tt> processes the text and tallies the most frequently used 2-character sequences. Once enough text has been processed, it calculates a confidence level based on the number of frequently-used sequences, the total number of characters, and a language-specific distribution ratio.</p>
92
+ <p>Hebrew is handled as a special case. If the text appears to be Hebrew based on 2-character distribution analysis, <tt class="classname">HebrewProber</tt> (defined in <tt class="filename">hebrewprober.py</tt>) tries to distinguish between Visual Hebrew (where the source text actually stored “<span class="quote">backwards</span>” line-by-line, and then displayed verbatim so it can be read from right to left) and Logical Hebrew (where the source text is stored in reading order and then rendered right-to-left by the client). Because certain characters are encoded differently based on whether they appear in the middle of or at the end of a word, we can make a reasonable guess about direction of the source text, and return the appropriate encoding (<tt class="literal">windows-1255</tt> for Logical Hebrew, or <tt class="literal">ISO-8859-8</tt> for Visual Hebrew).</p>
93
+ </div>
94
+ <div class="section" lang="en">
95
+ <div class="titlepage">
96
+ <div><div><h3 class="title">
97
+ <a name="how.windows1252" class="skip" href="#how.windows1252" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> windows-1252</h3></div></div>
98
+ <div></div>
99
+ </div>
100
+ <p>If <tt class="classname">UniversalDetector</tt> detects a high-bit character in the text, but none of the other multi-byte or single-byte encoding probers return a confident result, it creates a <tt class="classname">Latin1Prober</tt> (defined in <tt class="filename">latin1prober.py</tt>) to try to detect English text in a <tt class="literal">windows-1252</tt> encoding. This detection is inherently unreliable, because English letters are encoded in the same way in many different encodings. The only way to distinguish <tt class="literal">windows-1252</tt> is through commonly used symbols like smart quotes, curly apostrophes, copyright symbols, and the like. <tt class="classname">Latin1Prober</tt> automatically reduces its confidence rating to allow more accurate probers to win if at all possible.</p>
101
+ </div>
102
+ </div>
103
+ <div class="footernavigation">
104
+ <div style="float: left">← <a class="NavigationArrow" href="usage.html">Usage</a>
105
+ </div>
106
+ <div style="text-align: right">
107
+ <a class="NavigationArrow" href="license.html">Terms of use</a> →</div>
108
+ </div>
109
+ <hr>
110
+ <div id="footer"><p class="copyright">Copyright © 2006 Mark Pilgrim · <a href="mailto:mark@diveintomark.org">mark@diveintomark.org</a> · <a href="license.html">Terms of use</a></p></div>
111
+ </div></div>
112
+ </body>
113
+ </html>
@@ -0,0 +1,73 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
+ <html lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>Documentation [Universal Encoding Detector]</title>
6
+ <link rel="stylesheet" href="css/chardet.css" type="text/css">
7
+ <link rev="made" href="mailto:mark@diveintomark.org">
8
+ <meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
9
+ <meta name="keywords" content="character, set, encoding, detection, Python, XML, feed">
10
+ <link rel="start" href="index.html" title="Documentation">
11
+ <link rel="next" href="faq.html" title="Frequently asked questions">
12
+ </head>
13
+ <body id="chardet-feedparser-org" class="docs">
14
+ <div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
15
+ <div class="s" id="pageHeader">
16
+ <h1><a href="/">Universal Encoding Detector</a></h1>
17
+ <p>Character encoding auto-detection in Python. As smart as your browser. Open source.</p>
18
+ </div>
19
+ <div class="s" id="quickSummary"><ul>
20
+ <li class="li1">
21
+ <a href="http://chardet.feedparser.org/download/">Download</a> ·</li>
22
+ <li class="li2">
23
+ <a href="index.html">Documentation</a> ·</li>
24
+ <li class="li3"><a href="faq.html" title="Frequently Asked Questions">FAQ</a></li>
25
+ </ul></div>
26
+ </div></div></div>
27
+ <div id="main"><div id="mainInner">
28
+ <p id="breadcrumb">You are here: <span class="thispage">Documentation</span></p>
29
+ <div class="article" lang="en">
30
+ <div class="titlepage">
31
+ <div></div>
32
+ <div></div>
33
+ </div>
34
+ <div class="toc"><ul>
35
+ <li>
36
+ <span class="section"><a href="faq.html">Frequently asked questions</a></span><ul>
37
+ <li><span class="section"><a href="faq.html#faq.intro">What is character encoding?</a></span></li>
38
+ <li><span class="section"><a href="faq.html#faq.what">What is character encoding auto-detection?</a></span></li>
39
+ <li><span class="section"><a href="faq.html#faq.impossible">Isn't that impossible?</a></span></li>
40
+ <li><span class="section"><a href="faq.html#faq.who">Who wrote this detection algorithm?</a></span></li>
41
+ <li><span class="section"><a href="faq.html#faq.yippie">Yippie! Screw the standards, I'll just auto-detect everything!</a></span></li>
42
+ <li><span class="section"><a href="faq.html#faq.why">Why bother with auto-detection if it's slow, inaccurate, and non-standard?</a></span></li>
43
+ </ul>
44
+ </li>
45
+ <li><span class="section"><a href="supported-encodings.html">Supported encodings</a></span></li>
46
+ <li>
47
+ <span class="section"><a href="usage.html">Usage</a></span><ul>
48
+ <li><span class="section"><a href="usage.html#usage.basic">Basic usage</a></span></li>
49
+ <li><span class="section"><a href="usage.html#usage.advanced">Advanced usage</a></span></li>
50
+ </ul>
51
+ </li>
52
+ <li>
53
+ <span class="section"><a href="how-it-works.html">How it works</a></span><ul>
54
+ <li><span class="section"><a href="how-it-works.html#how.bom">UTF-n with a BOM</a></span></li>
55
+ <li><span class="section"><a href="how-it-works.html#how.esc">Escaped encodings</a></span></li>
56
+ <li><span class="section"><a href="how-it-works.html#how.mb">Multi-byte encodings</a></span></li>
57
+ <li><span class="section"><a href="how-it-works.html#how.sb">Single-byte encodings</a></span></li>
58
+ <li><span class="section"><a href="how-it-works.html#how.windows1252">windows-1252</a></span></li>
59
+ </ul>
60
+ </li>
61
+ <li><span class="appendix"><a href="license.html">Terms of use</a></span></li>
62
+ </ul></div>
63
+ </div>
64
+ <div class="footernavigation">
65
+ <div style="float: left"></div>
66
+ <div style="text-align: right">
67
+ <a class="NavigationArrow" href="faq.html">Frequently asked questions</a> →</div>
68
+ </div>
69
+ <hr>
70
+ <div id="footer"><p class="copyright">Copyright © 2006 Mark Pilgrim · <a href="mailto:mark@diveintomark.org">mark@diveintomark.org</a> · <a href="license.html">Terms of use</a></p></div>
71
+ </div></div>
72
+ </body>
73
+ </html>
@@ -0,0 +1,62 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
+ <html lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>Terms of use [Universal Encoding Detector]</title>
6
+ <link rel="stylesheet" href="css/chardet.css" type="text/css">
7
+ <link rev="made" href="mailto:mark@diveintomark.org">
8
+ <meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
9
+ <meta name="keywords" content="character, set, encoding, detection, Python, XML, feed">
10
+ <link rel="start" href="index.html" title="Documentation">
11
+ <link rel="up" href="index.html" title="Documentation">
12
+ <link rel="prev" href="how-it-works.html" title="How it works">
13
+ </head>
14
+ <body id="chardet-feedparser-org" class="docs">
15
+ <div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
16
+ <div class="s" id="pageHeader">
17
+ <h1><a href="/">Universal Encoding Detector</a></h1>
18
+ <p>Character encoding auto-detection in Python. As smart as your browser. Open source.</p>
19
+ </div>
20
+ <div class="s" id="quickSummary"><ul>
21
+ <li class="li1">
22
+ <a href="http://chardet.feedparser.org/download/">Download</a> ·</li>
23
+ <li class="li2">
24
+ <a href="index.html">Documentation</a> ·</li>
25
+ <li class="li3"><a href="faq.html" title="Frequently Asked Questions">FAQ</a></li>
26
+ </ul></div>
27
+ </div></div></div>
28
+ <div id="main"><div id="mainInner">
29
+ <p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <span class="thispage">Terms of use</span></p>
30
+ <div class="appendix" lang="en">
31
+ <div class="titlepage">
32
+ <div><div><h2 class="title">
33
+ <a name="license" class="skip" href="#license" title="link to this appendix"><img src="images/permalink.gif" alt="[link]" title="link to this appendix" width="8" height="9"></a> Terms of use</h2></div></div>
34
+ <div></div>
35
+ </div>
36
+ <h3>
37
+ <a name="id4953178" class="skip" href="#id4953178" title="link to this bridgehead"><img src="images/permalink.gif" alt="[link]" title="link to this bridgehead" width="8" height="9"></a> Software</h3>
38
+ <p>The <span class="application">Universal Encoding Detector</span> library is copyright © 2006 Mark Pilgrim. All rights reserved.</p>
39
+ <p>Portions copyright © 1998-2001 Netscape Communications Corporation. All rights reserved.</p>
40
+ <p>The <span class="application">Universal Encoding Detector</span> library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.</p>
41
+ <p>This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.</p>
42
+ <p>You should have received a copy of the GNU Lesser General Public License along with this library (in a file named <tt class="literal">COPYING</tt>); if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA</p>
43
+ <h3>
44
+ <a name="id4953205" class="skip" href="#id4953205" title="link to this bridgehead"><img src="images/permalink.gif" alt="[link]" title="link to this bridgehead" width="8" height="9"></a> Documentation</h3>
45
+ <p>The <span class="application">Universal Encoding Detector</span> documentation is copyright © 2006 Mark Pilgrim. All rights reserved.</p>
46
+ <p>Redistribution and use in source (XML DocBook) and “<span class="quote">compiled</span>” forms (SGML, HTML, PDF, PostScript, RTF and so forth) with or without modification, are permitted provided that the following conditions are met:</p>
47
+ <div class="orderedlist"><ol type="1">
48
+ <li>Redistributions of source code (XML DocBook) must retain the above copyright notice, this list of conditions and the following disclaimer unmodified.</li>
49
+ <li>Redistributions in compiled form (transformed to other DTDs, converted to PDF, PostScript, RTF and other formats) must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
50
+ </ol></div>
51
+ <p>THIS DOCUMENTATION IS PROVIDED BY THE AUTHOR “<span class="quote">AS IS</span>” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
52
+ </div>
53
+ <div class="footernavigation">
54
+ <div style="float: left">← <a class="NavigationArrow" href="how-it-works.html">How it works</a>
55
+ </div>
56
+ <div style="text-align: right"></div>
57
+ </div>
58
+ <hr>
59
+ <div id="footer"><p class="copyright">Copyright © 2006 Mark Pilgrim · <a href="mailto:mark@diveintomark.org">mark@diveintomark.org</a> · <a href="license.html">Terms of use</a></p></div>
60
+ </div></div>
61
+ </body>
62
+ </html>
@@ -0,0 +1,86 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
+ <html lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>Supported encodings [Universal Encoding Detector]</title>
6
+ <link rel="stylesheet" href="css/chardet.css" type="text/css">
7
+ <link rev="made" href="mailto:mark@diveintomark.org">
8
+ <meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
9
+ <meta name="keywords" content="character, set, encoding, detection, Python, XML, feed">
10
+ <link rel="start" href="index.html" title="Documentation">
11
+ <link rel="up" href="index.html" title="Documentation">
12
+ <link rel="prev" href="faq.html" title="Frequently asked questions">
13
+ <link rel="next" href="usage.html" title="Usage">
14
+ </head>
15
+ <body id="chardet-feedparser-org" class="docs">
16
+ <div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
17
+ <div class="s" id="pageHeader">
18
+ <h1><a href="/">Universal Encoding Detector</a></h1>
19
+ <p>Character encoding auto-detection in Python. As smart as your browser. Open source.</p>
20
+ </div>
21
+ <div class="s" id="quickSummary"><ul>
22
+ <li class="li1">
23
+ <a href="http://chardet.feedparser.org/download/">Download</a> ·</li>
24
+ <li class="li2">
25
+ <a href="index.html">Documentation</a> ·</li>
26
+ <li class="li3"><a href="faq.html" title="Frequently Asked Questions">FAQ</a></li>
27
+ </ul></div>
28
+ </div></div></div>
29
+ <div id="main"><div id="mainInner">
30
+ <p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <span class="thispage">Supported encodings</span></p>
31
+ <div class="section" lang="en">
32
+ <div class="titlepage">
33
+ <div>
34
+ <div><h2 class="title">
35
+ <a name="encodings" class="skip" href="#encodings" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Supported encodings</h2></div>
36
+ <div><div class="abstract">
37
+ <h3 class="title"></h3>
38
+ <p><span class="application">Universal Encoding Detector</span> currently supports over two dozen character encodings.</p>
39
+ </div></div>
40
+ </div>
41
+ <div></div>
42
+ </div>
43
+ <div class="itemizedlist"><ul>
44
+ <li>
45
+ <tt class="literal">Big5</tt>, <tt class="literal">GB2312</tt>/<tt class="literal">GB18030</tt>, <tt class="literal">EUC-TW</tt>, <tt class="literal">HZ-GB-2312</tt>, and <tt class="literal">ISO-2022-CN</tt> (Traditional and Simplified Chinese)</li>
46
+ <li>
47
+ <tt class="literal">EUC-JP</tt>, <tt class="literal">SHIFT_JIS</tt>, and <tt class="literal">ISO-2022-JP</tt> (Japanese)</li>
48
+ <li>
49
+ <tt class="literal">EUC-KR</tt> and <tt class="literal">ISO-2022-KR</tt> (Korean)</li>
50
+ <li>
51
+ <tt class="literal">KOI8-R</tt>, <tt class="literal">MacCyrillic</tt>, <tt class="literal">IBM855</tt>, <tt class="literal">IBM866</tt>, <tt class="literal">ISO-8859-5</tt>, and <tt class="literal">windows-1251</tt> (Russian)</li>
52
+ <li>
53
+ <tt class="literal">ISO-8859-2</tt> and <tt class="literal">windows-1250</tt> (Hungarian)</li>
54
+ <li>
55
+ <tt class="literal">ISO-8859-5</tt> and <tt class="literal">windows-1251</tt> (Bulgarian)</li>
56
+ <li><tt class="literal">windows-1252</tt></li>
57
+ <li>
58
+ <tt class="literal">ISO-8859-7</tt> and <tt class="literal">windows-1253</tt> (Greek)</li>
59
+ <li>
60
+ <tt class="literal">ISO-8859-8</tt> and <tt class="literal">windows-1255</tt> (Visual and Logical Hebrew)</li>
61
+ <li>
62
+ <tt class="literal">TIS-620</tt> (Thai)</li>
63
+ <li>
64
+ <tt class="literal">UTF-32</tt> <acronym title="Big Endian">BE</acronym>, <acronym title="Little Endian">LE</acronym>, 3412-ordered, or 2143-ordered (with a <acronym title="Byte Order Mark">BOM</acronym>)</li>
65
+ <li>
66
+ <tt class="literal">UTF-16</tt> <acronym title="Big Endian">BE</acronym> or <acronym title="Little Endian">LE</acronym> (with a <acronym title="Byte Order Mark">BOM</acronym>)</li>
67
+ <li>
68
+ <tt class="literal">UTF-8</tt> (with or without a <acronym title="Byte Order Mark">BOM</acronym>)</li>
69
+ <li><acronym>ASCII</acronym></li>
70
+ </ul></div>
71
+ <a name="id4897088"></a><table class="caution" border="0" summary="">
72
+ <tr><td rowspan="2" align="center" valign="top" width="1%"><img src="images/caution.png" alt="Caution" title="" width="24" height="24"></td></tr>
73
+ <tr><td colspan="2" align="left" valign="top" width="99%">Due to inherent similarities between certain encodings, some encodings may be detected incorrectly. In my tests, the most problematic case was Hungarian text encoded as <tt class="literal">ISO-8859-2</tt> or <tt class="literal">windows-1250</tt> (encoded as one but reported as the other). Also, Greek text encoded as <tt class="literal">ISO-8859-7</tt> was often mis-reported as <tt class="literal">ISO-8859-2</tt>. Your mileage may vary.</td></tr>
74
+ </table>
75
+ </div>
76
+ <div class="footernavigation">
77
+ <div style="float: left">← <a class="NavigationArrow" href="faq.html">Frequently asked questions</a>
78
+ </div>
79
+ <div style="text-align: right">
80
+ <a class="NavigationArrow" href="usage.html">Usage</a> →</div>
81
+ </div>
82
+ <hr>
83
+ <div id="footer"><p class="copyright">Copyright © 2006 Mark Pilgrim · <a href="mailto:mark@diveintomark.org">mark@diveintomark.org</a> · <a href="license.html">Terms of use</a></p></div>
84
+ </div></div>
85
+ </body>
86
+ </html>