sjisprober.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import sys
  28. from .mbcharsetprober import MultiByteCharSetProber
  29. from .codingstatemachine import CodingStateMachine
  30. from .chardistribution import SJISDistributionAnalysis
  31. from .jpcntx import SJISContextAnalysis
  32. from .mbcssm import SJISSMModel
  33. from . import constants
  34. class SJISProber(MultiByteCharSetProber):
  35. def __init__(self):
  36. MultiByteCharSetProber.__init__(self)
  37. self._mCodingSM = CodingStateMachine(SJISSMModel)
  38. self._mDistributionAnalyzer = SJISDistributionAnalysis()
  39. self._mContextAnalyzer = SJISContextAnalysis()
  40. self.reset()
  41. def reset(self):
  42. MultiByteCharSetProber.reset(self)
  43. self._mContextAnalyzer.reset()
  44. def get_charset_name(self):
  45. return self._mContextAnalyzer.get_charset_name()
  46. def feed(self, aBuf):
  47. aLen = len(aBuf)
  48. for i in range(0, aLen):
  49. codingState = self._mCodingSM.next_state(aBuf[i])
  50. if codingState == constants.eError:
  51. if constants._debug:
  52. sys.stderr.write(self.get_charset_name()
  53. + ' prober hit error at byte ' + str(i)
  54. + '\n')
  55. self._mState = constants.eNotMe
  56. break
  57. elif codingState == constants.eItsMe:
  58. self._mState = constants.eFoundIt
  59. break
  60. elif codingState == constants.eStart:
  61. charLen = self._mCodingSM.get_current_charlen()
  62. if i == 0:
  63. self._mLastChar[1] = aBuf[0]
  64. self._mContextAnalyzer.feed(self._mLastChar[2 - charLen:],
  65. charLen)
  66. self._mDistributionAnalyzer.feed(self._mLastChar, charLen)
  67. else:
  68. self._mContextAnalyzer.feed(aBuf[i + 1 - charLen:i + 3
  69. - charLen], charLen)
  70. self._mDistributionAnalyzer.feed(aBuf[i - 1:i + 1],
  71. charLen)
  72. self._mLastChar[0] = aBuf[aLen - 1]
  73. if self.get_state() == constants.eDetecting:
  74. if (self._mContextAnalyzer.got_enough_data() and
  75. (self.get_confidence() > constants.SHORTCUT_THRESHOLD)):
  76. self._mState = constants.eFoundIt
  77. return self.get_state()
  78. def get_confidence(self):
  79. contxtCf = self._mContextAnalyzer.get_confidence()
  80. distribCf = self._mDistributionAnalyzer.get_confidence()
  81. return max(contxtCf, distribCf)