/
charsetgroupprober.go
138 lines (129 loc) · 3.11 KB
/
charsetgroupprober.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package chardet
/*
######################## BEGIN LICENSE BLOCK ########################
# The Original Code is Mozilla Communicator client code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Mark Pilgrim - port to Python
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
######################### END LICENSE BLOCK #########################
*/
import (
//log "github.com/sirupsen/logrus"
)
// CharSetGroupProber is a group prober ???
type CharSetGroupProber struct {
CharSetProber
activeNum int
probers []Prober
bestGuessProber Prober
}
func newCharSetGroupProber(langFilter LanguageFilter) *CharSetGroupProber {
return &CharSetGroupProber{
*newCharSetProber(langFilter),
0,
[]Prober{},
nil,
}
}
func (c *CharSetGroupProber) reset() {
c.CharSetProber.reset()
c.activeNum = 0
for _, prober := range c.probers {
if prober != nil {
prober.reset()
prober.setActive(true)
c.activeNum++
}
}
c.bestGuessProber = nil
}
func (c *CharSetGroupProber) charsetName() string {
if c.bestGuessProber == nil {
c.getConfidence()
if c.bestGuessProber == nil {
return ""
}
}
return c.bestGuessProber.charsetName()
}
func (c *CharSetGroupProber) language() string {
if c.bestGuessProber == nil {
c.getConfidence()
if c.bestGuessProber == nil {
return ""
}
}
return c.bestGuessProber.language()
}
func (c *CharSetGroupProber) getConfidence() float64 {
state := c.state
if state == PSFound {
return 0.99
}
if state == PSNotMe {
return 0.01
}
var bestConf float64 = 0.0
c.bestGuessProber = nil
for _, prober := range c.probers {
if prober == nil {
continue
}
if !prober.getActive() {
continue
}
conf := prober.getConfidence()
if bestConf < conf {
bestConf = conf
c.bestGuessProber = prober
}
}
if c.bestGuessProber == nil {
return 0.0
}
return bestConf
}
func (c *CharSetGroupProber) feed(data []byte) ProbingState {
for _, prober := range c.probers {
if prober == nil || !prober.getActive() {
continue
}
state := prober.feed(data)
switch state {
case PSDetecting:
continue
case PSFound:
c.bestGuessProber = prober
c.state = PSFound
return c.state
case PSNotMe:
prober.setActive(false)
c.activeNum--
if c.activeNum <= 0 {
c.state = PSNotMe
return c.state
}
}
}
return c.state
}