forked from f1monkey/spellchecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
51 lines (42 loc) · 863 Bytes
/
reader.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
package spellchecker
import (
"bufio"
"bytes"
"io"
"regexp"
)
type readData struct {
word string
err error
}
var wordSymbols = regexp.MustCompile(`[-\pL]+`)
func defaultSplitter(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanWords(data, atEOF)
if err != nil {
return
}
token = bytes.ToLower(token)
return advance, wordSymbols.Find(token), nil
}
func readInput(input io.Reader, splitter bufio.SplitFunc) <-chan readData {
if splitter == nil {
splitter = defaultSplitter
}
ch := make(chan readData)
scanner := bufio.NewScanner(input)
scanner.Split(splitter)
go func() {
defer close(ch)
for {
if !scanner.Scan() {
break
}
if err := scanner.Err(); err != nil {
ch <- readData{err: err}
return
}
ch <- readData{word: scanner.Text()}
}
}()
return ch
}