/
detect.go
147 lines (137 loc) · 4.67 KB
/
detect.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
139
140
141
142
143
144
145
146
147
package chardet
/*
######################## BEGIN LICENSE BLOCK ########################
# The Original Code is mozilla.org 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 (
"bytes"
log "github.com/sirupsen/logrus"
"golang.org/x/net/html/charset"
"io/ioutil"
"strings"
"unicode/utf8"
)
// Detect detects the encoding of a byte slice
func Detect(data []byte) *Result {
detector := newUniversalDetector(LFAll)
detector.Feed(data)
return detector.Close()
}
// DetectAll detects all the possible encoding of a byte slice
// Outputs possible encodings only if their confidence is above a threshold
func DetectAll(data []byte) []*Result {
detector := newUniversalDetector(LFAll)
detector.Feed(data)
detector.Close()
if detector.inputState == UDSHighByte {
results := []*Result{}
for _, prober := range detector.charsetProbers {
if prober.getConfidence() <= UDMinimumThreshold {
continue
}
charsetName := prober.charsetName()
lowerCharsetName := strings.ToLower(charsetName)
// Use windows encoding name instead of ISO-8859
// if we saw any extra windows-specific bytes
if strings.HasPrefix(lowerCharsetName, "iso-8859") && detector.hasWinBytes {
charsetNameTmp, ok := UDIsoWinMap[lowerCharsetName]
if ok {
charsetName = charsetNameTmp
}
}
results = append(results, &Result{
Encoding: charsetName,
Confidence: prober.getConfidence(),
})
}
}
return []*Result{detector.Result}
}
// DetectShortestUTF8 detects the best encoding for a byte slice
// Given that the right encoding may not have the highest confidence.
// Heuristic is made with the fewer count of exotic UTF8 chars
// (neither letters, numbers, punctuation or spaces)
func DetectShortestUTF8(data []byte) *Result {
detector := newUniversalDetector(LFAll)
detector.Feed(data)
highConfidenceResult := detector.Close()
lowestConfidence := 2.0*highConfidenceResult.Confidence - 1.0 // conf - (1 - conf)
if highConfidenceResult.Confidence >= 0.99 {
return highConfidenceResult
}
currentBestResult := highConfidenceResult
currentBestScore := scoreFromResult(data, currentBestResult, -1)
if detector.inputState == UDSHighByte {
for _, prober := range detector.charsetProbers {
conf := prober.getConfidence()
if conf <= lowestConfidence || prober.getConfidence() <= UDMinimumThreshold {
break
}
charsetName := prober.charsetName()
lowerCharsetName := strings.ToLower(charsetName)
// Use windows encoding name instead of ISO-8859
// if we saw any extra windows-specific bytes
if strings.HasPrefix(lowerCharsetName, "iso-8859") && detector.hasWinBytes {
charsetNameTmp, ok := UDIsoWinMap[lowerCharsetName]
if ok {
charsetName = charsetNameTmp
}
}
newResult := &Result{
Encoding: charsetName,
Confidence: prober.getConfidence(),
}
newScore := scoreFromResult(data, newResult, currentBestScore)
if newScore < currentBestScore {
currentBestScore = newScore
currentBestResult = newResult
}
}
}
if highConfidenceResult.Encoding != currentBestResult.Encoding {
log.Debugf("Detected differing encoding from highest confidence : %v vs %v", highConfidenceResult, currentBestResult)
}
return currentBestResult
}
func intoCharset(b []byte, encoding string) ([]byte, error) {
reader, err := charset.NewReaderLabel(encoding, bytes.NewReader(b))
if err != nil {
return nil, err
}
decoded, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return decoded, nil
}
func scoreFromResult(data []byte, result *Result, currentBestScore int) int {
bestDecode, err := intoCharset(data, result.Encoding)
if err != nil {
return utf8.RuneCountInString(string(data))
}
return utf8.RuneCountInString(string(bestDecode))
}