-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathtext.go
189 lines (173 loc) · 4.93 KB
/
text.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2016 Salle, Alexandre <[email protected]>
* Author: Salle, Alexandre <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"bufio"
"io"
"math/rand"
"os"
"unicode/utf8"
)
func createScanner(reader io.Reader) *bufio.Scanner {
var s = bufio.NewScanner(bufio.NewReader(reader))
s.Split(scanWords)
return s
}
func openCorpus() *os.File {
if len(corpusPath) == 0 {
logln(errorLogLevel, "FATAL ERROR: corpus is a required argument")
os.Exit(1)
}
corpus, err := os.Open(corpusPath)
check(err)
return corpus
}
func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Skip leading spaces.
start := 0
for width := 0; start < len(data); start += width {
var r rune
r, width = utf8.DecodeRune(data[start:])
if r == '\n' || r == '.' {
return start + width, ctxbreakbytes, nil
}
if !isSpace(r) {
break
}
}
// Scan until space, marking end of word.
for width, i := 0, start; i < len(data); i += width {
var r rune
r, width = utf8.DecodeRune(data[i:])
if isSpace(r) {
if r == '\n' || r == '.' {
width = 0
}
return i + width, data[start:i], nil
}
}
// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
if atEOF && len(data) > start {
return len(data), data[start:], nil
}
// Request more data.
return start, nil, nil
}
// Identical to stdlib but allows treating periods as whitespace
func isSpace(r rune) bool {
if r <= '\u00FF' {
// Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
if periodIsWhitespace && r == '.' {
return true
}
switch r {
case ' ', '\t', '\n', '\v', '\f', '\r':
return true
case '\u0085', '\u00A0':
return true
}
return false
}
// High-valued ones.
if '\u2000' <= r && r <= '\u200a' {
return true
}
switch r {
case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
return true
}
return false
}
type windowerCallback func(w, c *word, pos int) bool
func windower(s *bufio.Scanner, randng *rand.Rand, includeTargetOnly bool, callback windowerCallback) {
var buf []*word
// Iterate over every token in the corpus.
for s.Scan() {
wordInStream := s.Text()
mapw, ok := vocab[wordInStream]
// If word is not in vocabulary, skip it.
if !ok {
continue
}
// Run subsampling, skipping word with probability subsampleP.
if mapw != ctxbreakw && subsample > 0 {
subsampleP := mapw.subsampleP(subsample, corpusSize)
if subsampleP > 0 {
bernoulliTrial := randng.Float64() // uniform dist 0.0 - 1.0
if bernoulliTrial <= subsampleP {
continue
}
}
}
// If we hit a context break (newline or period) or the buffer is full,
// run window over the buffer.
if mapw == ctxbreakw || len(buf) == maxSentenceLen {
// j is the position of target word within sliding window.
for j, target := range buf {
win := window
// If we are using weighted window like word2vec, uniformly sample window size.
if weightedWindow {
win = 1 + randng.Intn(window)
}
start := j - win
if start < 0 {
start = 0
}
end := j + win + 1
if end > len(buf) {
end = len(buf)
}
// Iterate over each word in window except for target word.
for i := start; i < end; i++ {
if i == j {
continue
}
mapc := buf[i]
// If using positional contexts, convert token to token_i
pos := i - j
posW := mapc.w
if positionalContexts {
posW = mapc.posW(pos)
}
mapc, ok = ctxVocab[posW]
if !ok {
logln(errorLogLevel, "ctx word %s not in vocab", posW)
}
if !callback(target, mapc, pos) {
return
}
}
if includeTargetOnly {
if !callback(target, nil, 0) {
return
}
}
}
// Clear the buffer so we can process the next sentence.
buf = nil
}
// If the current target word is not a context break, push it into the buffer.
if mapw != ctxbreakw {
buf = append(buf, mapw)
}
}
}