-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorpus.go
144 lines (141 loc) · 2.95 KB
/
corpus.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package gortex
import (
"bufio"
"fmt"
"os"
"strings"
)
func CharSampleVisitor(file string, minLen uint, tokenizer Tokenizer, dictionary *Dictionary, visitor func(epoch int, sample []uint)) error {
f, e := os.Open(file)
if e != nil {
return e
}
epoch := 0
r := bufio.NewReader(f)
for {
line, e := r.ReadString('\n')
if e != nil {
f.Seek(0, 0)
line, e = r.ReadString('\n')
if e != nil {
break
}
epoch++
}
line = strings.TrimSpace(line)
if len(line) > 0 {
terms := tokenizer.Split(line)
sample := make([]uint, len(terms))
for i, term := range terms {
sample[i] = dictionary.IDByToken(term)
}
if len(sample) > int(minLen) {
visitor(epoch, sample)
}
}
}
f.Close()
return nil
}
func CharClassifierSampleVisitor(file string, minLen uint, rewind bool, tokenizer Tokenizer, dictionary *Dictionary, visitor func(sample []uint, label string)) error {
f, e := os.Open(file)
if e != nil {
return e
}
r := bufio.NewReader(f)
for {
line, e := r.ReadString('\n')
if e != nil {
if rewind {
f.Seek(0, 0)
line, e = r.ReadString('\n')
if e != nil {
break
}
} else {
break
}
}
line = strings.TrimSpace(line)
if len(line) > 0 {
index := strings.LastIndex(line, " __label__")
if index == -1 {
panic(fmt.Errorf("Wrong classifier sample [%s]", line))
}
label := line[index+1:]
terms := tokenizer.Split(line[:index])
sample := make([]uint, len(terms))
for i, term := range terms {
sample[i] = dictionary.IDByToken(term)
}
if len(sample) > int(minLen) {
visitor(sample, label)
}
}
}
f.Close()
return nil
}
func WordSampleVisitor(file string, tokenizer Tokenizer, dictionary *Dictionary, visitor func(epoch int, sample []uint)) error {
f, e := os.Open(file)
if e != nil {
return e
}
epoch := 0
r := bufio.NewReader(f)
for {
line, e := r.ReadString('\n')
if e != nil {
f.Seek(0, 0)
line, e = r.ReadString('\n')
if e != nil {
break
}
epoch++
}
line = strings.TrimSpace(line)
if len(line) > 0 {
terms := tokenizer.Split(line)
sample := make([]uint, len(terms))
for i, term := range terms {
sample[i] = dictionary.IDByToken(term)
}
visitor(epoch, sample)
}
}
f.Close()
return nil
}
func WordCharSampleVisitor(file string, tokenizer Tokenizer, dictionary *Dictionary, visitor func(epoch int, sample [][]uint)) error {
f, e := os.Open(file)
if e != nil {
return e
}
epoch := 0
r := bufio.NewReader(f)
for {
line, e := r.ReadString('\n')
if e != nil {
f.Seek(0, 0)
//r = bufio.NewReader(f)
line, e = r.ReadString('\n')
if e != nil {
break
}
epoch++
}
//line = strings.TrimSpace(line)
if len(line) > 0 {
terms := tokenizer.Split(line)
sample := make([][]uint, len(terms))
for i, term := range terms {
for _, r := range []rune(term) {
sample[i] = append(sample[i], dictionary.IDByToken(string(r)))
}
}
visitor(epoch, sample)
}
}
f.Close()
return nil
}