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