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

sbcharsetprober.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #
  13. # This library is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU Lesser General Public
  15. # License as published by the Free Software Foundation; either
  16. # version 2.1 of the License, or (at your option) any later version.
  17. #
  18. # This library is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. # Lesser General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU Lesser General Public
  24. # License along with this library; if not, write to the Free Software
  25. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  26. # 02110-1301 USA
  27. ######################### END LICENSE BLOCK #########################
  28. import sys
  29. from . import constants
  30. from .charsetprober import CharSetProber
  31. from .compat import wrap_ord
  32. SAMPLE_SIZE = 64
  33. SB_ENOUGH_REL_THRESHOLD = 1024
  34. POSITIVE_SHORTCUT_THRESHOLD = 0.95
  35. NEGATIVE_SHORTCUT_THRESHOLD = 0.05
  36. SYMBOL_CAT_ORDER = 250
  37. NUMBER_OF_SEQ_CAT = 4
  38. POSITIVE_CAT = NUMBER_OF_SEQ_CAT - 1
  39. #NEGATIVE_CAT = 0
  40. class SingleByteCharSetProber(CharSetProber):
  41. def __init__(self, model, reversed=False, nameProber=None):
  42. CharSetProber.__init__(self)
  43. self._mModel = model
  44. # TRUE if we need to reverse every pair in the model lookup
  45. self._mReversed = reversed
  46. # Optional auxiliary prober for name decision
  47. self._mNameProber = nameProber
  48. self.reset()
  49. def reset(self):
  50. CharSetProber.reset(self)
  51. # char order of last character
  52. self._mLastOrder = 255
  53. self._mSeqCounters = [0] * NUMBER_OF_SEQ_CAT
  54. self._mTotalSeqs = 0
  55. self._mTotalChar = 0
  56. # characters that fall in our sampling range
  57. self._mFreqChar = 0
  58. def get_charset_name(self):
  59. if self._mNameProber:
  60. return self._mNameProber.get_charset_name()
  61. else:
  62. return self._mModel['charsetName']
  63. def feed(self, aBuf):
  64. if not self._mModel['keepEnglishLetter']:
  65. aBuf = self.filter_without_english_letters(aBuf)
  66. aLen = len(aBuf)
  67. if not aLen:
  68. return self.get_state()
  69. for c in aBuf:
  70. order = self._mModel['charToOrderMap'][wrap_ord(c)]
  71. if order < SYMBOL_CAT_ORDER:
  72. self._mTotalChar += 1
  73. if order < SAMPLE_SIZE:
  74. self._mFreqChar += 1
  75. if self._mLastOrder < SAMPLE_SIZE:
  76. self._mTotalSeqs += 1
  77. if not self._mReversed:
  78. i = (self._mLastOrder * SAMPLE_SIZE) + order
  79. model = self._mModel['precedenceMatrix'][i]
  80. else: # reverse the order of the letters in the lookup
  81. i = (order * SAMPLE_SIZE) + self._mLastOrder
  82. model = self._mModel['precedenceMatrix'][i]
  83. self._mSeqCounters[model] += 1
  84. self._mLastOrder = order
  85. if self.get_state() == constants.eDetecting:
  86. if self._mTotalSeqs > SB_ENOUGH_REL_THRESHOLD:
  87. cf = self.get_confidence()
  88. if cf > POSITIVE_SHORTCUT_THRESHOLD:
  89. if constants._debug:
  90. sys.stderr.write('%s confidence = %s, we have a'
  91. 'winner\n' %
  92. (self._mModel['charsetName'], cf))
  93. self._mState = constants.eFoundIt
  94. elif cf < NEGATIVE_SHORTCUT_THRESHOLD:
  95. if constants._debug:
  96. sys.stderr.write('%s confidence = %s, below negative'
  97. 'shortcut threshhold %s\n' %
  98. (self._mModel['charsetName'], cf,
  99. NEGATIVE_SHORTCUT_THRESHOLD))
  100. self._mState = constants.eNotMe
  101. return self.get_state()
  102. def get_confidence(self):
  103. r = 0.01
  104. if self._mTotalSeqs > 0:
  105. r = ((1.0 * self._mSeqCounters[POSITIVE_CAT]) / self._mTotalSeqs
  106. / self._mModel['mTypicalPositiveRatio'])
  107. r = r * self._mFreqChar / self._mTotalChar
  108. if r >= 1.0:
  109. r = 0.99
  110. return r