File tree 2 files changed +25
-4
lines changed
2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -85,11 +85,22 @@ var (
85
85
86
86
// DetectBest returns the Result with highest Confidence.
87
87
func (d * Detector ) DetectBest (b []byte ) (r * Result , err error ) {
88
- var all []Result
89
- if all , err = d .DetectAll (b ); err == nil {
90
- r = & all [0 ]
88
+ input := newRecognizerInput (b , d .stripTag )
89
+ outputChan := make (chan recognizerOutput )
90
+ for _ , r := range d .recognizers {
91
+ go matchHelper (r , input , outputChan )
92
+ }
93
+ var output Result
94
+ for i := 0 ; i < len (d .recognizers ); i ++ {
95
+ o := <- outputChan
96
+ if output .Confidence < o .Confidence {
97
+ output = Result (o )
98
+ }
99
+ }
100
+ if output .Confidence == 0 {
101
+ return nil , NotDetectedError
91
102
}
92
- return
103
+ return & output , nil
93
104
}
94
105
95
106
// DetectAll returns all Results which have non-zero Confidence. The Results are sorted by Confidence in descending order.
Original file line number Diff line number Diff line change 1
1
package chardet_test
2
2
3
3
import (
4
+ "bytes"
4
5
"github.com/gogs/chardet"
5
6
"io"
6
7
"os"
@@ -58,3 +59,12 @@ func TestDetector(t *testing.T) {
58
59
}
59
60
}
60
61
}
62
+
63
+ func BenchmarkDetectBest (b * testing.B ) {
64
+ textDetector := chardet .NewTextDetector ()
65
+ aaaa := bytes .Repeat ([]byte ("A" ), 1024 )
66
+ b .ReportAllocs ()
67
+ for i := 0 ; i < b .N ; i ++ {
68
+ textDetector .DetectBest (aaaa )
69
+ }
70
+ }
You can’t perform that action at this time.
0 commit comments