Plex plugin to to play various online streams (mostly Latvian).

eucjpprober.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 . import constants
  29. from .mbcharsetprober import MultiByteCharSetProber
  30. from .codingstatemachine import CodingStateMachine
  31. from .chardistribution import EUCJPDistributionAnalysis
  32. from .jpcntx import EUCJPContextAnalysis
  33. from .mbcssm import EUCJPSMModel
  34. class EUCJPProber(MultiByteCharSetProber):
  35. def __init__(self):
  36. MultiByteCharSetProber.__init__(self)
  37. self._mCodingSM = CodingStateMachine(EUCJPSMModel)
  38. self._mDistributionAnalyzer = EUCJPDistributionAnalysis()
  39. self._mContextAnalyzer = EUCJPContextAnalysis()
  40. self.reset()
  41. def reset(self):
  42. MultiByteCharSetProber.reset(self)
  43. self._mContextAnalyzer.reset()
  44. def get_charset_name(self):
  45. return "EUC-JP"
  46. def feed(self, aBuf):
  47. aLen = len(aBuf)
  48. for i in range(0, aLen):
  49. # PY3K: aBuf is a byte array, so aBuf[i] is an int, not a byte
  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._mContextAnalyzer.feed(self._mLastChar, charLen)
  66. self._mDistributionAnalyzer.feed(self._mLastChar, charLen)
  67. else:
  68. self._mContextAnalyzer.feed(aBuf[i - 1:i + 1], charLen)
  69. self._mDistributionAnalyzer.feed(aBuf[i - 1:i + 1],
  70. charLen)
  71. self._mLastChar[0] = aBuf[aLen - 1]
  72. if self.get_state() == constants.eDetecting:
  73. if (self._mContextAnalyzer.got_enough_data() and
  74. (self.get_confidence() > constants.SHORTCUT_THRESHOLD)):
  75. self._mState = constants.eFoundIt
  76. return self.get_state()
  77. def get_confidence(self):
  78. contxtCf = self._mContextAnalyzer.get_confidence()
  79. distribCf = self._mDistributionAnalyzer.get_confidence()
  80. return max(contxtCf, distribCf)