Source file owl_nlp_vocabulary.ml
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# 1 "src/owl/nlp/owl_nlp_vocabulary.ml"
(** NLP: Vocabulary module *)
type t = {
mutable w2i : (string, int) Hashtbl.t;
mutable i2w : (int, string) Hashtbl.t;
mutable i2f : (int, int) Hashtbl.t
}
let get_w2i d = d.w2i
let get_i2w d = d.i2w
let exits_w d w = Hashtbl.mem d.w2i w
let exits_i d i = Hashtbl.mem d.i2w i
let word2index d w = Hashtbl.find d.w2i w
let index2word d i = Hashtbl.find d.i2w i
let length d = Hashtbl.length d.w2i
let freq_i d i = Hashtbl.find d.i2f i
let freq_w d w = w |> word2index d |> freq_i d
let copy d = {
w2i = Hashtbl.copy d.w2i;
i2w = Hashtbl.copy d.i2w;
i2f = Hashtbl.copy d.i2f;
}
let re_index d =
let w2i = Hashtbl.create (length d) in
let i2w = Hashtbl.create (length d) in
let i2f = Hashtbl.create (length d) in
let i = ref 0 in
Hashtbl.iter (fun w f ->
Hashtbl.add w2i w !i;
Hashtbl.add i2w !i w;
Hashtbl.add i2f !i f;
i := !i + 1;
) d.w2i;
{ w2i; i2w; i2f }
let _trim_percent_w2f lo hi h =
let n = Hashtbl.length h in
let all_freq = Array.make n 0 in
let i = ref 0 in
Hashtbl.iter (fun _ freq ->
all_freq.(!i) <- freq;
i := !i + 1;
) h;
Array.sort Stdlib.compare all_freq;
let l0 = float_of_int n *. lo |> int_of_float in
let h0 = float_of_int n *. hi |> int_of_float in
let lo = all_freq.(l0) in
let hi = all_freq.(h0) in
Hashtbl.filter_map_inplace (fun _ freq ->
match freq >= lo, freq <= hi with
| true, true -> Some freq
| _ -> None
) h
let trim_percent ~lo ~hi vocab =
let d = copy vocab in
_trim_percent_w2f lo hi d.i2f;
Hashtbl.filter_map_inplace (fun i w ->
match Hashtbl.mem d.i2f i with
| true -> Some w
| false -> Hashtbl.remove d.w2i w; None
) d.i2w;
re_index d
let _trim_count_w2f lo hi h =
Hashtbl.filter_map_inplace (fun _ freq ->
if freq >= lo && freq <= hi then Some freq
else None
) h
let trim_count ~lo ~hi vocab =
let d = copy vocab in
_trim_count_w2f lo hi d.i2f;
Hashtbl.filter_map_inplace (fun i w ->
match Hashtbl.mem d.i2f i with
| true -> Some w
| false -> Hashtbl.remove d.w2i w; None
) d.i2w;
re_index d
let remove_stopwords sw h =
Hashtbl.filter_map_inplace (fun w v ->
match Hashtbl.mem sw w with
| true -> None
| false -> Some v
) h
let _build_word_proc w2f s =
Str.split Owl_nlp_utils.regexp_split s
|> List.iter (fun w ->
match Hashtbl.mem w2f w with
| true -> (
let freq = Hashtbl.find w2f w in
Hashtbl.replace w2f w (freq + 1)
)
| false -> Hashtbl.add w2f w 1
)
let _build_alphabet_proc w2f s =
String.iter (fun c ->
let w = String.make 1 c in
match Hashtbl.mem w2f w with
| true -> (
let freq = Hashtbl.find w2f w in
Hashtbl.replace w2f w (freq + 1)
)
| false -> Hashtbl.add w2f w 1
) s
let build ?(lo=0.) ?(hi=1.) ?(alphabet=false) ?stopwords fname =
let w2f = Hashtbl.create (64 * 1024) in
let f = match alphabet with
| true -> _build_alphabet_proc w2f
| false -> _build_word_proc w2f
in
Owl_io.iteri_lines_of_file (fun _ s -> f s) fname;
if lo <> 0. || hi <> 1. then
_trim_percent_w2f lo hi w2f;
(
match stopwords with
| Some sw -> remove_stopwords sw w2f
| None -> ()
);
let w2i = Hashtbl.(create (length w2f)) in
let i2w = Hashtbl.(create (length w2f)) in
let i2f = Hashtbl.(create (length w2f)) in
let i = ref 0 in
Hashtbl.iter (fun w f ->
Hashtbl.add w2i w !i;
Hashtbl.add i2w !i w;
Hashtbl.add i2f !i f;
i := !i + 1;
) w2f;
{ w2i; i2w; i2f }
let build_from_string ?(lo=0.) ?(hi=1.) ?(alphabet=false) ?stopwords s =
let w2f = Hashtbl.create (64 * 1024) in
(
if alphabet then _build_alphabet_proc w2f s
else _build_word_proc w2f s
);
if lo <> 0. || hi <> 1. then
_trim_percent_w2f lo hi w2f;
(
match stopwords with
| Some sw -> remove_stopwords sw w2f
| None -> ()
);
let w2i = Hashtbl.(create (length w2f)) in
let i2w = Hashtbl.(create (length w2f)) in
let i2f = Hashtbl.(create (length w2f)) in
let i = ref 0 in
Hashtbl.iter (fun w f ->
Hashtbl.add w2i w !i;
Hashtbl.add i2w !i w;
Hashtbl.add i2f !i f;
i := !i + 1;
) w2f;
{ w2i; i2w; i2f }
let sort_freq ?(inc=true) d =
let all_freq = Array.make (length d) (0, 0) in
let i = ref 0 in
Hashtbl.iter (fun j freq ->
all_freq.(!i) <- (j, freq);
i := !i + 1;
) d.i2f;
let f = match inc with
| true -> fun a b -> Stdlib.compare (snd a) (snd b)
| false -> fun a b -> Stdlib.compare (snd b) (snd a)
in
Array.sort f all_freq;
all_freq
let top d k =
let all_freq = sort_freq ~inc:false d in
assert (length d >= k);
Array.sub all_freq 0 k |>
Array.map (fun (i, freq) -> (index2word d i, freq))
let bottom d k =
let all_freq = sort_freq ~inc:true d in
Array.sub all_freq 0 k |>
Array.map (fun (i, freq) -> (index2word d i, freq))
let w2i_to_tuples d = Hashtbl.fold (fun w i a -> (w,i) :: a) d.w2i []
let tokenise v s =
Array.init (String.length s) (fun i ->
word2index v (String.make 1 s.[i])
)
let save d fname = Owl_io.marshal_to_file d fname
let load fname : t = Owl_io.marshal_from_file fname
let save_txt d fname =
let fh = open_out fname in
let vl = w2i_to_tuples d in
List.fast_sort (fun x y -> String.compare (fst x) (fst y)) vl
|> List.iter (fun (w,i) ->
let s = Printf.sprintf "%s %i\n" w i in
output_string fh s
);
close_out fh
let to_string x =
let topk = top x 5 |> Array.fold_left (fun acc (s,i) ->
Printf.sprintf "%s(%s,%i) " acc s i
) ""
in
Printf.sprintf "length: %i; tokens:%s..." (length x) topk
let pp_vocab formatter x =
Format.open_box 0;
Format.fprintf formatter "%s" (to_string x);
Format.close_box ()
let to_array x =
let s = Owl_utils.Stack.make () in
Hashtbl.iter (fun i w -> Owl_utils.Stack.push s (i,w)) x.i2w;
Owl_utils.Stack.to_array s
let of_array x =
let n = Array.length x in
let w2i = Hashtbl.create n in
let i2w = Hashtbl.create n in
let i2f = Hashtbl.create n in
Array.iter (fun (i,w) ->
Hashtbl.add w2i w i;
Hashtbl.add i2w i w;
Hashtbl.add i2f i 1;
) x;
{ w2i; i2w; i2f }