Fri, 25 Apr 2014 22:07:19 +0200
updated CharDet to 2.2.1, updated changelog
3537
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
1 | ######################## BEGIN LICENSE BLOCK ######################## |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
2 | # The Original Code is Mozilla Universal charset detector code. |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
3 | # |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
4 | # The Initial Developer of the Original Code is |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
5 | # Netscape Communications Corporation. |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
6 | # Portions created by the Initial Developer are Copyright (C) 2001 |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
7 | # the Initial Developer. All Rights Reserved. |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
8 | # |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
9 | # Contributor(s): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
10 | # Mark Pilgrim - port to Python |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
11 | # Shy Shalom - original C code |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
12 | # Proofpoint, Inc. |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
13 | # |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
14 | # This library is free software; you can redistribute it and/or |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
15 | # modify it under the terms of the GNU Lesser General Public |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
16 | # License as published by the Free Software Foundation; either |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
17 | # version 2.1 of the License, or (at your option) any later version. |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
18 | # |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
19 | # This library is distributed in the hope that it will be useful, |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
22 | # Lesser General Public License for more details. |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
23 | # |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
24 | # You should have received a copy of the GNU Lesser General Public |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
25 | # License along with this library; if not, write to the Free Software |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
26 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
27 | # 02110-1301 USA |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
28 | ######################### END LICENSE BLOCK ######################### |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
29 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
30 | import sys |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
31 | from . import constants |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
32 | from .charsetprober import CharSetProber |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
33 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
34 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
35 | class MultiByteCharSetProber(CharSetProber): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
36 | def __init__(self): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
37 | CharSetProber.__init__(self) |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
38 | self._mDistributionAnalyzer = None |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
39 | self._mCodingSM = None |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
40 | self._mLastChar = [0, 0] |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
41 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
42 | def reset(self): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
43 | CharSetProber.reset(self) |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
44 | if self._mCodingSM: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
45 | self._mCodingSM.reset() |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
46 | if self._mDistributionAnalyzer: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
47 | self._mDistributionAnalyzer.reset() |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
48 | self._mLastChar = [0, 0] |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
49 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
50 | def get_charset_name(self): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
51 | pass |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
52 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
53 | def feed(self, aBuf): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
54 | aLen = len(aBuf) |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
55 | for i in range(0, aLen): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
56 | codingState = self._mCodingSM.next_state(aBuf[i]) |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
57 | if codingState == constants.eError: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
58 | if constants._debug: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
59 | sys.stderr.write(self.get_charset_name() |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
60 | + ' prober hit error at byte ' + str(i) |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
61 | + '\n') |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
62 | self._mState = constants.eNotMe |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
63 | break |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
64 | elif codingState == constants.eItsMe: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
65 | self._mState = constants.eFoundIt |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
66 | break |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
67 | elif codingState == constants.eStart: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
68 | charLen = self._mCodingSM.get_current_charlen() |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
69 | if i == 0: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
70 | self._mLastChar[1] = aBuf[0] |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
71 | self._mDistributionAnalyzer.feed(self._mLastChar, charLen) |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
72 | else: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
73 | self._mDistributionAnalyzer.feed(aBuf[i - 1:i + 1], |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
74 | charLen) |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
75 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
76 | self._mLastChar[0] = aBuf[aLen - 1] |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
77 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
78 | if self.get_state() == constants.eDetecting: |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
79 | if (self._mDistributionAnalyzer.got_enough_data() and |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
80 | (self.get_confidence() > constants.SHORTCUT_THRESHOLD)): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
81 | self._mState = constants.eFoundIt |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
82 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
83 | return self.get_state() |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
84 | |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
85 | def get_confidence(self): |
7662053c3906
updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
12
diff
changeset
|
86 | return self._mDistributionAnalyzer.get_confidence() |