charsetgroupprober.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # The Original Code is Mozilla Communicator client 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. from . import constants
  28. import sys
  29. from .charsetprober import CharSetProber
  30. class CharSetGroupProber(CharSetProber):
  31. def __init__(self):
  32. CharSetProber.__init__(self)
  33. self._mActiveNum = 0
  34. self._mProbers = []
  35. self._mBestGuessProber = None
  36. def reset(self):
  37. CharSetProber.reset(self)
  38. self._mActiveNum = 0
  39. for prober in self._mProbers:
  40. if prober:
  41. prober.reset()
  42. prober.active = True
  43. self._mActiveNum += 1
  44. self._mBestGuessProber = None
  45. def get_charset_name(self):
  46. if not self._mBestGuessProber:
  47. self.get_confidence()
  48. if not self._mBestGuessProber:
  49. return None
  50. # self._mBestGuessProber = self._mProbers[0]
  51. return self._mBestGuessProber.get_charset_name()
  52. def feed(self, aBuf):
  53. for prober in self._mProbers:
  54. if not prober:
  55. continue
  56. if not prober.active:
  57. continue
  58. st = prober.feed(aBuf)
  59. if not st:
  60. continue
  61. if st == constants.eFoundIt:
  62. self._mBestGuessProber = prober
  63. return self.get_state()
  64. elif st == constants.eNotMe:
  65. prober.active = False
  66. self._mActiveNum -= 1
  67. if self._mActiveNum <= 0:
  68. self._mState = constants.eNotMe
  69. return self.get_state()
  70. return self.get_state()
  71. def get_confidence(self):
  72. st = self.get_state()
  73. if st == constants.eFoundIt:
  74. return 0.99
  75. elif st == constants.eNotMe:
  76. return 0.01
  77. bestConf = 0.0
  78. self._mBestGuessProber = None
  79. for prober in self._mProbers:
  80. if not prober:
  81. continue
  82. if not prober.active:
  83. if constants._debug:
  84. sys.stderr.write(prober.get_charset_name()
  85. + ' not active\n')
  86. continue
  87. cf = prober.get_confidence()
  88. if constants._debug:
  89. sys.stderr.write('%s confidence = %s\n' %
  90. (prober.get_charset_name(), cf))
  91. if bestConf < cf:
  92. bestConf = cf
  93. self._mBestGuessProber = prober
  94. if not self._mBestGuessProber:
  95. return 0.0
  96. return bestConf
  97. # else:
  98. # self._mBestGuessProber = self._mProbers[0]
  99. # return self._mBestGuessProber.get_confidence()