-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.go
74 lines (62 loc) · 1.46 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"unicode/utf8"
)
type dict struct {
words []string
letter_set []rune
}
func find_dict(options options) (file *os.File, err error) {
for _, dir := range strings.Split(options.dict_path, ":") {
file, err = os.Open(dir + "/" + options.language)
if err == nil {
return
}
}
return nil, fmt.Errorf("could not find language \"%s\"", options.language)
}
func parse_dict(options options, file *os.File) (dict dict) {
scanner := bufio.NewScanner(file)
letters := make(map[rune]bool)
for scanner.Scan() {
line := strings.ToUpper(scanner.Text())
// pick words of the right length
if utf8.RuneCountInString(line) == options.chars {
// reject words with diacritics and similar
// if force-ascii is enabled
use_word := true
if options.force_ascii {
for _, char := range line {
if char < 'A' || char > 'Z' {
use_word = false
break
}
}
}
// if the word is used, then append it to the dict,
// and update the used characters list
if use_word {
dict.words = append(dict.words, line)
for _, char := range line {
letters[char] = true
}
}
}
}
// convert map of letters to a slice
dict.letter_set = make([]rune, len(letters))
i := 0
for key := range letters {
dict.letter_set[i] = key
i++
}
sort.Strings(dict.words)
sort.Slice(dict.letter_set,
func(i, j int) bool { return dict.letter_set[i] < dict.letter_set[j] })
return
}