-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathword_counter.go
56 lines (50 loc) · 1.1 KB
/
word_counter.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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"unicode"
"unicode/utf8"
)
func findAllTextFiles(outputDir string) (files []string, err error) {
files, err = filepath.Glob(filepath.Join(outputDir, "*.txt"))
return
}
func findUnicodeWords(file string, script *unicode.RangeTable, ch chan string) {
f, err := os.Open(file)
if err != nil {
fmt.Printf("Error while opening file %s\n", file)
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := scanner.Text()
if isUnicodeWord(word, script) {
ch <- word
}
}
}
func isUnicodeWord(word string, script *unicode.RangeTable) bool {
status := true
for len(word) > 0 {
r, size := utf8.DecodeRuneInString(word)
if !unicode.Is(script, r) || unicode.IsNumber(r) {
status = false
}
word = word[size:]
}
return status
}
func genUnicodeWordFiles(outputDir string, script *unicode.RangeTable, ch chan string) {
defer close(ch)
files, err := findAllTextFiles(outputDir)
if err != nil {
fmt.Println(err)
}
for _, f := range files {
findUnicodeWords(f, script, ch)
}
}