-
Notifications
You must be signed in to change notification settings - Fork 0
/
misspell_parser.go
45 lines (39 loc) · 897 Bytes
/
misspell_parser.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
package spell
import (
"bufio"
"io"
"os"
)
type MisspellParser struct {
}
func NewMisspellParser() *MisspellParser {
return &MisspellParser{}
}
func (parser *MisspellParser) ParseFromFile(fileName string) ([]Misspell, error) {
fp, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer fp.Close()
return parser.Parse(fp)
}
func (parser *MisspellParser) Parse(reader io.Reader) ([]Misspell, error) {
scanner := bufio.NewScanner(reader)
misspells := make([]Misspell, 0, 1000)
for scanner.Scan() {
line := scanner.Text()
if line[0] == '$' {
term := line[1:]
misspells = append(misspells, Misspell{
Term: term,
Misspells: make([]string, 0, 4),
})
} else {
misspells[len(misspells)-1].Misspells = append(misspells[len(misspells)-1].Misspells, line)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return misspells, nil
}