-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
typos.go
38 lines (30 loc) · 903 Bytes
/
typos.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
package helpers
import "unicode/utf8"
type TypoDetector struct {
oneCharTypos map[string]string
}
func MakeTypoDetector(valid []string) TypoDetector {
detector := TypoDetector{oneCharTypos: make(map[string]string)}
// Add all combinations of each valid word with one character missing
for _, correct := range valid {
if len(correct) > 3 {
for i, ch := range correct {
detector.oneCharTypos[correct[:i]+correct[i+utf8.RuneLen(ch):]] = correct
}
}
}
return detector
}
func (detector TypoDetector) MaybeCorrectTypo(typo string) (string, bool) {
// Check for a single deleted character
if corrected, ok := detector.oneCharTypos[typo]; ok {
return corrected, true
}
// Check for a single misplaced character
for i, ch := range typo {
if corrected, ok := detector.oneCharTypos[typo[:i]+typo[i+utf8.RuneLen(ch):]]; ok {
return corrected, true
}
}
return "", false
}