1
######################## BEGIN LICENSE BLOCK ########################
2
# The Original Code is mozilla.org code.
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.
10
# Mark Pilgrim - port to Python
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.
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.
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
26
######################### END LICENSE BLOCK #########################
29
from constants import eStart, eError, eItsMe
30
from mbcharsetprober import MultiByteCharSetProber
31
from codingstatemachine import CodingStateMachine
32
from chardistribution import EUCJPDistributionAnalysis
33
from jpcntx import EUCJPContextAnalysis
34
from mbcssm import EUCJPSMModel
36
class EUCJPProber(MultiByteCharSetProber):
38
MultiByteCharSetProber.__init__(self)
39
self._mCodingSM = CodingStateMachine(EUCJPSMModel)
40
self._mDistributionAnalyzer = EUCJPDistributionAnalysis()
41
self._mContextAnalyzer = EUCJPContextAnalysis()
45
MultiByteCharSetProber.reset(self)
46
self._mContextAnalyzer.reset()
48
def get_charset_name(self):
53
for i in range(0, aLen):
54
codingState = self._mCodingSM.next_state(aBuf[i])
55
if codingState == eError:
57
sys.stderr.write(self.get_charset_name() + ' prober hit error at byte ' + str(i) + '\n')
58
self._mState = constants.eNotMe
60
elif codingState == eItsMe:
61
self._mState = constants.eFoundIt
63
elif codingState == eStart:
64
charLen = self._mCodingSM.get_current_charlen()
66
self._mLastChar[1] = aBuf[0]
67
self._mContextAnalyzer.feed(self._mLastChar, charLen)
68
self._mDistributionAnalyzer.feed(self._mLastChar, charLen)
70
self._mContextAnalyzer.feed(aBuf[i-1:i+1], charLen)
71
self._mDistributionAnalyzer.feed(aBuf[i-1:i+1], charLen)
73
self._mLastChar[0] = aBuf[aLen - 1]
75
if self.get_state() == constants.eDetecting:
76
if self._mContextAnalyzer.got_enough_data() and \
77
(self.get_confidence() > constants.SHORTCUT_THRESHOLD):
78
self._mState = constants.eFoundIt
80
return self.get_state()
82
def get_confidence(self):
83
contxtCf = self._mContextAnalyzer.get_confidence()
84
distribCf = self._mDistributionAnalyzer.get_confidence()
85
return max(contxtCf, distribCf)