@@ -2,6 +2,7 @@ package chardet
2
2
3
3
import (
4
4
"errors"
5
+ "sort"
5
6
)
6
7
7
8
type Result struct {
@@ -34,10 +35,6 @@ func (d *Detector) DetectBest(b []byte, stripTag bool, declaredCharset string) (
34
35
return
35
36
}
36
37
37
- func matchHelper (r recognizer , input * recognizerInput , outputChan chan <- recognizerOutput ) {
38
- outputChan <- r .Match (input )
39
- }
40
-
41
38
func (d * Detector ) DetectAll (b []byte , stripTag bool , declaredCharset string ) ([]Result , error ) {
42
39
input := newRecognizerInput (b , stripTag , declaredCharset )
43
40
outputChan := make (chan recognizerOutput )
@@ -46,7 +43,35 @@ func (d *Detector) DetectAll(b []byte, stripTag bool, declaredCharset string) ([
46
43
}
47
44
outputs := make ([]recognizerOutput , 0 , len (recognizers ))
48
45
for i := 0 ; i < len (recognizers ); i ++ {
49
- outputs = append (outputs , <- outputChan )
46
+ o := <- outputChan
47
+ if o .Confidence > 0 {
48
+ outputs = append (outputs , <- outputChan )
49
+ }
50
+ }
51
+ if len (outputs ) == 0 {
52
+ return nil , NotDetectedError
53
+ }
54
+
55
+ sort .Sort (recognizerOutputs (outputs ))
56
+ dedupOutputs := make ([]Result , 0 , len (outputs ))
57
+ foundCharsets := make (map [string ]struct {}, len (outputs ))
58
+ for _ , o := range outputs {
59
+ if _ , found := foundCharsets [o .Charset ]; ! found {
60
+ dedupOutputs = append (dedupOutputs , Result (o ))
61
+ foundCharsets [o .Charset ] = struct {}{}
62
+ }
50
63
}
51
- return nil , NotDetectedError
64
+ if len (dedupOutputs ) == 0 {
65
+ return nil , NotDetectedError
66
+ }
67
+ return dedupOutputs , nil
68
+ }
69
+
70
+ func matchHelper (r recognizer , input * recognizerInput , outputChan chan <- recognizerOutput ) {
71
+ outputChan <- r .Match (input )
52
72
}
73
+
74
+ type recognizerOutputs []recognizerOutput
75
+ func (r recognizerOutputs ) Len () int { return len (r ) }
76
+ func (r recognizerOutputs ) Less (i , j int ) bool { return r [i ].Confidence > r [j ].Confidence }
77
+ func (r recognizerOutputs ) Swap (i , j int ) { r [i ], r [j ] = r [j ], r [i ] }
0 commit comments