-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhzgb2312.go
80 lines (75 loc) · 1.06 KB
/
hzgb2312.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
package chardet
type hzgb2312 struct {
byte
rune
hold [80]int
ttls int
}
func (h hzgb2312) String() string {
return "hz-gb2312"
}
func (h *hzgb2312) Feed(x byte) bool {
switch h.byte {
case 0:
if x == '~' {
h.byte = 1
return true
}
if x >= 0x00 && x <= 0x7F {
return true
}
case 1:
if x == '~' {
h.byte = 0
return true
}
if x == '{' {
h.byte = 2
return true
}
case 2:
if x == '~' {
h.byte = 3
return true
}
if x >= 0x21 && x <= 0x77 {
h.byte = 4
h.rune = (rune(x) | 0x80) << 8
return true
}
case 3:
if x == '}' {
h.byte = 0
return true
}
case 4:
if x >= 0x21 && x <= 0x7E {
h.byte = 2
h.rune |= rune(x) | 0x80
h.count()
return true
}
}
return false
}
func (h *hzgb2312) Priority() float64 {
if h.ttls == 0 {
return 0
}
f := 0.0
for i, x := range h.hold {
k := 100*float64(x)/float64(h.ttls) - freq_ch[i]
if k >= 0 {
f += k
} else {
f -= k
}
}
return 100 - f
}
func (h *hzgb2312) count() {
if i, ok := dict_gb[uint32(h.rune)]; ok {
h.hold[i]++
h.ttls++
}
}