当前位置: 首页>>代码示例>>Python>>正文


Python codingstatemachine.CodingStateMachine类代码示例

本文整理汇总了Python中codingstatemachine.CodingStateMachine的典型用法代码示例。如果您正苦于以下问题:Python CodingStateMachine类的具体用法?Python CodingStateMachine怎么用?Python CodingStateMachine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了CodingStateMachine类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SJISProber

class SJISProber(MultiByteCharSetProber):
	def __init__(self):
		MultiByteCharSetProber.__init__(self)
		self._mCodingSM = CodingStateMachine(SJISSMModel)
		self._mDistributionAnalyzer = SJISDistributionAnalysis()
		self._mContextAnalyzer = SJISContextAnalysis()
		self.reset()

	def reset(self):
		MultiByteCharSetProber.reset(self)
		self._mContextAnalyzer.reset()

	def get_charset_name(self):
		return "SHIFT_JIS"

	def feed(self, aBuf):
		aLen = len(aBuf)
		for i in range(0, aLen):
			codingState = self._mCodingSM.next_state(aBuf[i])
			if codingState == eError:
				if constants._debug:
					sys.stderr.write(self.get_charset_name() + ' prober hit error at byte ' + str(i) + '\n')
				self._mState = constants.eNotMe
				break
			elif codingState == eItsMe:
				self._mState = constants.eFoundIt
				break
			elif codingState == eStart:
				charLen = self._mCodingSM.get_current_charlen()
				if i == 0:
					self._mLastChar[1] = aBuf[0]
					self._mContextAnalyzer.feed(self._mLastChar[2 - charLen :], charLen)
					self._mDistributionAnalyzer.feed(self._mLastChar, charLen)
				else:
					self._mContextAnalyzer.feed(aBuf[i + 1 - charLen : i + 3 - charLen], charLen)
					self._mDistributionAnalyzer.feed(aBuf[i - 1 : i + 1], charLen)

		self._mLastChar[0] = aBuf[aLen - 1]

		if self.get_state() == constants.eDetecting:
			if self._mContextAnalyzer.got_enough_data() and \
				   (self.get_confidence() > constants.SHORTCUT_THRESHOLD):
				self._mState = constants.eFoundIt

		return self.get_state()

	def get_confidence(self):
		contxtCf = self._mContextAnalyzer.get_confidence()
		distribCf = self._mDistributionAnalyzer.get_confidence()
		return max(contxtCf, distribCf)
开发者ID:3cky,项目名称:3,代码行数:50,代码来源:sjisprober.py

示例2: UTF8Prober

class UTF8Prober(CharSetProber):
    def __init__(self):
        CharSetProber.__init__(self)
        self._mCodingSM = CodingStateMachine(UTF8SMModel)
        self.reset()

    def reset(self):
        CharSetProber.reset(self)
        self._mCodingSM.reset()
        self._mNumOfMBChar = 0

    def get_charset_name(self):
        return "utf-8"

    def feed(self, aBuf):
        for c in aBuf:
            codingState = self._mCodingSM.next_state(c)
            if codingState == eError:
                self._mState = constants.eNotMe
                break
            elif codingState == eItsMe:
                self._mState = constants.eFoundIt
                break
            elif codingState == eStart:
                if self._mCodingSM.get_current_charlen() >= 2:
                    self._mNumOfMBChar += 1

        if self.get_state() == constants.eDetecting:
            if self.get_confidence() > constants.SHORTCUT_THRESHOLD:
                self._mState = constants.eFoundIt

        return self.get_state()

    def get_confidence(self):
        unlike = 0.99
        if self._mNumOfMBChar < 6:
            for i in range(0, self._mNumOfMBChar):
                unlike = unlike * ONE_CHAR_PROB
            return 1.0 - unlike
        else:
            return unlike
开发者ID:christofoo,项目名称:hard-way,代码行数:41,代码来源:ex41.py

示例3: __init__

	def __init__(self):
		MultiByteCharSetProber.__init__(self)
		self._mCodingSM = CodingStateMachine(SJISSMModel)
		self._mDistributionAnalyzer = SJISDistributionAnalysis()
		self._mContextAnalyzer = SJISContextAnalysis()
		self.reset()
开发者ID:3cky,项目名称:3,代码行数:6,代码来源:sjisprober.py

示例4: __init__

 def __init__(self):
     CharSetProber.__init__(self)
     self._mCodingSM = CodingStateMachine(UTF8SMModel)
     self.reset()
开发者ID:christofoo,项目名称:hard-way,代码行数:4,代码来源:ex41.py


注:本文中的codingstatemachine.CodingStateMachine类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。