-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.go
49 lines (40 loc) · 919 Bytes
/
dict.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
39
40
41
42
43
44
45
46
47
48
49
package purify
import (
"bufio"
"errors"
"os"
)
// Dict implements a quasi hash map
type Dict struct {
Key rune
Value *Trie
}
const pathToWords = "data/bad.txt"
var (
errFileNotFound = errors.New("file could not be opened")
errScanningFile = errors.New("an error occurred while scanning")
)
// ParseDictionary returns a slice containing either good or bad words. Defaults to bad words
// if no argument is specified.
func ParseDictionary(path ...string) ([]string, error) {
words := make([]string, 0)
p := pathToWords
if len(path) > 0 {
p = path[0]
}
file, err := os.Open(p)
if err != nil {
// log.Printf("%v\n", err.Error())
return words, errFileNotFound
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
words = append(words, scanner.Text())
}
if err := scanner.Err(); err != nil {
// log.Printf("%v", err)
return words, err
}
return words, nil
}