-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikitfidf.go
289 lines (249 loc) · 7.08 KB
/
wikitfidf.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package wikitfidf
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"github.com/ebonetti/ctxutils"
"github.com/negapedia/wikitfidf/internal/assets"
"github.com/negapedia/wikitfidf/internal/badwords"
"github.com/negapedia/wikitfidf/internal/topicwords"
"github.com/negapedia/wikibrief"
"github.com/negapedia/wikitfidf/internal/dumpreducer"
"github.com/negapedia/wikitfidf/internal/tfidf"
"github.com/negapedia/wikitfidf/internal/wordmapper"
"github.com/pkg/errors"
)
// New ingests, processes and stores the desidered Wikipedia dump from the channel.
func New(ctx context.Context, lang string, in <-chan wikibrief.EvolvingPage, resultDir string, limits Limits, testMode bool) (exporter Exporter, err error) {
ctx, fail := ctxutils.WithFail(ctx)
wb := newBuilder(fail, lang, resultDir, limits, testMode).Preprocess(ctx, fail, in).Process(ctx, fail)
if err = fail(nil); err == nil {
exporter = Exporter{wb.ResultDir, wb.Lang}
}
return
}
//Limits represents limits at which data is cut off
type Limits struct {
WordsPages int
GlobalWords int
TopicWords int
Reverts int
}
//ReasonableLimits returns reasonable limits
func ReasonableLimits() Limits {
return Limits{
WordsPages: 50,
GlobalWords: 100,
TopicWords: 100,
Reverts: 10,
}
}
func newBuilder(fail func(error) error, lang string, resultDir string, limits Limits, testMode bool) (w builder) {
err := CheckAvailableLanguage(lang)
if err != nil {
fail(err)
return
}
if limits.WordsPages <= 0 || limits.GlobalWords <= 0 || limits.TopicWords <= 0 || limits.Reverts <= 0 {
fail(errors.New("Invalid limits"))
return
}
if resultDir, err = filepath.Abs(filepath.Join(resultDir, "TFIDF")); err != nil {
fail(errors.WithStack(err))
return
}
if err = os.MkdirAll(filepath.Join(resultDir, "Stem"), os.ModePerm); err != nil && !os.IsExist(err) {
fail(errors.WithStack(err))
return
}
logger := ioutil.Discard
if testMode {
logger = os.Stdout
}
return builder{Lang: lang, ResultDir: resultDir, Limits: limits, Logger: logger}
}
type builder struct {
Lang string
ResultDir string
Limits Limits
Logger io.Writer
}
// Preprocess given a wikibrief.EvolvingPage channel reduce the amount of information in pages and save them
func (wt builder) Preprocess(ctx context.Context, fail func(error) error, channel <-chan wikibrief.EvolvingPage) builder {
if ctx.Err() != nil {
return wt
}
fmt.Fprintln(wt.Logger, "Parse and reduction")
start := time.Now()
dumpreducer.DumpReducer(ctx, fail, channel, wt.ResultDir, wt.Limits.Reverts)
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
return wt
}
// Process is the main procedure where the data process happen. In this method page will be cleaned by wikitext,
// will be performed tokenization, stopwords cleaning and stemming, files aggregation and then files de-stemming
func (wt builder) Process(ctx context.Context, fail func(error) error) (wtOut builder) {
if ctx.Err() != nil {
return wt
}
fmt.Fprintln(wt.Logger, "WikiMarkup and Stopwords cleaning;")
start := time.Now()
err := assets.Run(ctx, "textnormalizer", ".", map[string]string{"RESULTDIR": wt.ResultDir, "LANG": wt.Lang})
if err != nil {
fail(errors.WithStack(err))
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
fmt.Fprintln(wt.Logger, "Word mapping by page")
start = time.Now()
err = wordmapper.ByPage(wt.ResultDir)
if err != nil {
fail(err)
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
if ctx.Err() != nil {
return wt
}
fmt.Fprintln(wt.Logger, "Processing GlobalWordMap file")
start = time.Now()
err = wordmapper.GlobalWordMapper(wt.ResultDir)
if err != nil {
fail(err)
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
fmt.Fprintln(wt.Logger, "Processing GlobalStem file")
start = time.Now()
err = wordmapper.StemRevAggregator(wt.ResultDir)
if err != nil {
fail(err)
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
if ctx.Err() != nil {
return wt
}
fmt.Fprintln(wt.Logger, "Processing GlobalPage file")
start = time.Now()
err = wordmapper.PageMapAggregator(wt.ResultDir)
if err != nil {
fail(err)
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
fmt.Fprintln(wt.Logger, "Processing TFIDF file")
start = time.Now()
err = tfidf.ComputeTFIDF(wt.ResultDir)
if err != nil {
fail(err)
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
fmt.Fprintln(wt.Logger, "Performing Destemming")
start = time.Now()
err = assets.Run(ctx, "destemmer", ".", map[string]string{"RESULTDIR": wt.ResultDir})
if err != nil {
fail(errors.WithStack(err))
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
fmt.Fprintln(wt.Logger, "Processing topic words")
start = time.Now()
err = topicwords.TopicWords(wt.ResultDir)
if err != nil {
fail(err)
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
fmt.Fprintln(wt.Logger, "Processing Badwords report")
start = time.Now()
err = badwords.BadWords(wt.Lang, wt.ResultDir)
if err != nil {
fail(err)
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
fmt.Fprintln(wt.Logger, "Processing top N words")
start = time.Now()
err = assets.Run(ctx, "topwordspageextractor", ".", map[string]string{
"RESULTDIR": wt.ResultDir,
"WORDSPAGELIMIT": strconv.Itoa(wt.Limits.WordsPages),
"GLOBALWORDSLIMIT": strconv.Itoa(wt.Limits.GlobalWords),
"TOPICWORDSLIMIT": strconv.Itoa(wt.Limits.TopicWords),
"LANG": wt.Lang,
})
if err != nil {
fail(errors.WithStack(err))
return wt
}
fmt.Fprintln(wt.Logger, "Done in", time.Now().Sub(start))
return wt
}
// CheckAvailableLanguage check if a language is handled
func CheckAvailableLanguage(lang string) error {
/*
languages := map[string]string{
"en": "english",
"ar": "arabic",
"da": "danish",
"nl": "dutch",
"fi": "finnish",
"fr": "french",
"de": "german",
"el": "greek",
"hu": "hungarian",
"id": "indonesian",
"it": "italian",
"kk": "kazakh",
"ne": "nepali",
"no": "norwegian",
"pt": "portuguese",
"ro": "romanian",
"ru": "russian",
"es": "spanish",
"sv": "swedish",
"tr": "turkish",
"hy": "armenian",
"az": "azerbaijani",
"eu": "basque",
"bn": "bengali",
"bg": "bulgarian",
"ca": "catalan",
"zh": "chinese",
"sh": "croatian",
"cs": "czech",
"gl": "galician",
"he": "hebrew",
"hi": "hindi",
"ga": "irish",
"ja": "japanese",
"ko": "korean",
"lv": "latvian",
"lt": "lithuanian",
"mr": "marathi",
"fa": "persian",
"pl": "polish",
"sk": "slovak",
"th": "thai",
"uk": "ukrainian",
"ur": "urdu",
"simple": "english",
"cr": "english", //for tests, 10K pages wiki
}
*/
if lang == "" {
return errors.New("Empty language")
}
/*
// @@ Commented out so to allow any language (test)
if _, isIn := languages[lang]; !isIn {
return errors.New(lang + " is not an available language")
}
*/
return nil
}