mbcharsetprober.py 3.2 KB

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