-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellcheck.go
More file actions
60 lines (47 loc) · 863 Bytes
/
spellcheck.go
File metadata and controls
60 lines (47 loc) · 863 Bytes
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
package spellcheck
import (
"bufio"
"os"
"strings"
)
var (
wordlist []string
)
// CheckWord checks if the input is in a dataset of every english word.
func CheckWord(input string) bool {
initModel()
input = strings.ToLower(input)
if contains(wordlist, input) {
return true
}
return false
}
func initModel() {
if len(wordlist) == 0 {
lines, err := readFile("wordlist.txt")
if err != nil {
panic(err)
}
wordlist = lines
}
}
func readFile(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func contains(list []string, search string) bool {
for _, word := range list {
if word == search {
return true
}
}
return false
}