1420216e5SHanoh Haim######################## BEGIN LICENSE BLOCK ########################
2420216e5SHanoh Haim# The Original Code is Mozilla Communicator client code.
3420216e5SHanoh Haim#
4420216e5SHanoh Haim# The Initial Developer of the Original Code is
5420216e5SHanoh Haim# Netscape Communications Corporation.
6420216e5SHanoh Haim# Portions created by the Initial Developer are Copyright (C) 1998
7420216e5SHanoh Haim# the Initial Developer. All Rights Reserved.
8420216e5SHanoh Haim#
9420216e5SHanoh Haim# Contributor(s):
10420216e5SHanoh Haim#   Mark Pilgrim - port to Python
11420216e5SHanoh Haim#
12420216e5SHanoh Haim# This library is free software; you can redistribute it and/or
13420216e5SHanoh Haim# modify it under the terms of the GNU Lesser General Public
14420216e5SHanoh Haim# License as published by the Free Software Foundation; either
15420216e5SHanoh Haim# version 2.1 of the License, or (at your option) any later version.
16420216e5SHanoh Haim#
17420216e5SHanoh Haim# This library is distributed in the hope that it will be useful,
18420216e5SHanoh Haim# but WITHOUT ANY WARRANTY; without even the implied warranty of
19420216e5SHanoh Haim# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20420216e5SHanoh Haim# Lesser General Public License for more details.
21420216e5SHanoh Haim#
22420216e5SHanoh Haim# You should have received a copy of the GNU Lesser General Public
23420216e5SHanoh Haim# License along with this library; if not, write to the Free Software
24420216e5SHanoh Haim# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25420216e5SHanoh Haim# 02110-1301  USA
26420216e5SHanoh Haim######################### END LICENSE BLOCK #########################
27420216e5SHanoh Haim
28420216e5SHanoh Haimfrom . import constants
29420216e5SHanoh Haimimport sys
30420216e5SHanoh Haimfrom .charsetprober import CharSetProber
31420216e5SHanoh Haim
32420216e5SHanoh Haim
33420216e5SHanoh Haimclass CharSetGroupProber(CharSetProber):
34420216e5SHanoh Haim    def __init__(self):
35420216e5SHanoh Haim        CharSetProber.__init__(self)
36420216e5SHanoh Haim        self._mActiveNum = 0
37420216e5SHanoh Haim        self._mProbers = []
38420216e5SHanoh Haim        self._mBestGuessProber = None
39420216e5SHanoh Haim
40420216e5SHanoh Haim    def reset(self):
41420216e5SHanoh Haim        CharSetProber.reset(self)
42420216e5SHanoh Haim        self._mActiveNum = 0
43420216e5SHanoh Haim        for prober in self._mProbers:
44420216e5SHanoh Haim            if prober:
45420216e5SHanoh Haim                prober.reset()
46420216e5SHanoh Haim                prober.active = True
47420216e5SHanoh Haim                self._mActiveNum += 1
48420216e5SHanoh Haim        self._mBestGuessProber = None
49420216e5SHanoh Haim
50420216e5SHanoh Haim    def get_charset_name(self):
51420216e5SHanoh Haim        if not self._mBestGuessProber:
52420216e5SHanoh Haim            self.get_confidence()
53420216e5SHanoh Haim            if not self._mBestGuessProber:
54420216e5SHanoh Haim                return None
55420216e5SHanoh Haim#                self._mBestGuessProber = self._mProbers[0]
56420216e5SHanoh Haim        return self._mBestGuessProber.get_charset_name()
57420216e5SHanoh Haim
58420216e5SHanoh Haim    def feed(self, aBuf):
59420216e5SHanoh Haim        for prober in self._mProbers:
60420216e5SHanoh Haim            if not prober:
61420216e5SHanoh Haim                continue
62420216e5SHanoh Haim            if not prober.active:
63420216e5SHanoh Haim                continue
64420216e5SHanoh Haim            st = prober.feed(aBuf)
65420216e5SHanoh Haim            if not st:
66420216e5SHanoh Haim                continue
67420216e5SHanoh Haim            if st == constants.eFoundIt:
68420216e5SHanoh Haim                self._mBestGuessProber = prober
69420216e5SHanoh Haim                return self.get_state()
70420216e5SHanoh Haim            elif st == constants.eNotMe:
71420216e5SHanoh Haim                prober.active = False
72420216e5SHanoh Haim                self._mActiveNum -= 1
73420216e5SHanoh Haim                if self._mActiveNum <= 0:
74420216e5SHanoh Haim                    self._mState = constants.eNotMe
75420216e5SHanoh Haim                    return self.get_state()
76420216e5SHanoh Haim        return self.get_state()
77420216e5SHanoh Haim
78420216e5SHanoh Haim    def get_confidence(self):
79420216e5SHanoh Haim        st = self.get_state()
80420216e5SHanoh Haim        if st == constants.eFoundIt:
81420216e5SHanoh Haim            return 0.99
82420216e5SHanoh Haim        elif st == constants.eNotMe:
83420216e5SHanoh Haim            return 0.01
84420216e5SHanoh Haim        bestConf = 0.0
85420216e5SHanoh Haim        self._mBestGuessProber = None
86420216e5SHanoh Haim        for prober in self._mProbers:
87420216e5SHanoh Haim            if not prober:
88420216e5SHanoh Haim                continue
89420216e5SHanoh Haim            if not prober.active:
90420216e5SHanoh Haim                if constants._debug:
91420216e5SHanoh Haim                    sys.stderr.write(prober.get_charset_name()
92420216e5SHanoh Haim                                     + ' not active\n')
93420216e5SHanoh Haim                continue
94420216e5SHanoh Haim            cf = prober.get_confidence()
95420216e5SHanoh Haim            if constants._debug:
96420216e5SHanoh Haim                sys.stderr.write('%s confidence = %s\n' %
97420216e5SHanoh Haim                                 (prober.get_charset_name(), cf))
98420216e5SHanoh Haim            if bestConf < cf:
99420216e5SHanoh Haim                bestConf = cf
100420216e5SHanoh Haim                self._mBestGuessProber = prober
101420216e5SHanoh Haim        if not self._mBestGuessProber:
102420216e5SHanoh Haim            return 0.0
103420216e5SHanoh Haim        return bestConf
104420216e5SHanoh Haim#        else:
105420216e5SHanoh Haim#            self._mBestGuessProber = self._mProbers[0]
106420216e5SHanoh Haim#            return self._mBestGuessProber.get_confidence()
107