~anna-g/micronaet/anna

« back to all changes in this revision

Viewing changes to mws_amazon/mws/requests/packages/chardet2/eucjpprober.py

  • Committer: Anna Micronaet
  • Date: 2013-07-18 09:08:36 UTC
  • Revision ID: anna@micronaet.it-20130718090836-ssmst48rrnvcd69w
Tolti tutti i moduli

Show diffs side-by-side

added added

removed removed

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