@@ -14,6 +14,9 @@ type Result struct {
14
14
Language string
15
15
// Confidence of the Result. Scale from 1 to 100. The bigger, the more confident.
16
16
Confidence int
17
+
18
+ // used for sorting internally
19
+ order int
17
20
}
18
21
19
22
// Detector implements charset detection.
@@ -87,13 +90,13 @@ var (
87
90
func (d * Detector ) DetectBest (b []byte ) (r * Result , err error ) {
88
91
input := newRecognizerInput (b , d .stripTag )
89
92
outputChan := make (chan recognizerOutput )
90
- for _ , r := range d .recognizers {
91
- go matchHelper (r , input , outputChan )
93
+ for i , r := range d .recognizers {
94
+ go matchHelper (r , input , outputChan , i )
92
95
}
93
96
var output Result
94
97
for i := 0 ; i < len (d .recognizers ); i ++ {
95
98
o := <- outputChan
96
- if output .Confidence < o .Confidence {
99
+ if output .Confidence < o .Confidence || ( output . Confidence == o . Confidence && o . order < output . order ) {
97
100
output = Result (o )
98
101
}
99
102
}
@@ -107,8 +110,8 @@ func (d *Detector) DetectBest(b []byte) (r *Result, err error) {
107
110
func (d * Detector ) DetectAll (b []byte ) ([]Result , error ) {
108
111
input := newRecognizerInput (b , d .stripTag )
109
112
outputChan := make (chan recognizerOutput )
110
- for _ , r := range d .recognizers {
111
- go matchHelper (r , input , outputChan )
113
+ for i , r := range d .recognizers {
114
+ go matchHelper (r , input , outputChan , i )
112
115
}
113
116
outputs := make (recognizerOutputs , 0 , len (d .recognizers ))
114
117
for i := 0 ; i < len (d .recognizers ); i ++ {
@@ -136,12 +139,14 @@ func (d *Detector) DetectAll(b []byte) ([]Result, error) {
136
139
return dedupOutputs , nil
137
140
}
138
141
139
- func matchHelper (r recognizer , input * recognizerInput , outputChan chan <- recognizerOutput ) {
140
- outputChan <- r .Match (input )
142
+ func matchHelper (r recognizer , input * recognizerInput , outputChan chan <- recognizerOutput , order int ) {
143
+ outputChan <- r .Match (input , order )
141
144
}
142
145
143
146
type recognizerOutputs []recognizerOutput
144
147
145
- func (r recognizerOutputs ) Len () int { return len (r ) }
146
- func (r recognizerOutputs ) Less (i , j int ) bool { return r [i ].Confidence > r [j ].Confidence }
147
- func (r recognizerOutputs ) Swap (i , j int ) { r [i ], r [j ] = r [j ], r [i ] }
148
+ func (r recognizerOutputs ) Len () int { return len (r ) }
149
+ func (r recognizerOutputs ) Less (i , j int ) bool {
150
+ return r [i ].Confidence > r [j ].Confidence || (r [i ].Confidence == r [j ].Confidence && r [i ].order < r [j ].order )
151
+ }
152
+ func (r recognizerOutputs ) Swap (i , j int ) { r [i ], r [j ] = r [j ], r [i ] }
0 commit comments