-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.jl
268 lines (226 loc) · 6.65 KB
/
utils.jl
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
"""
Check whether the predictions are correct.
"""
function iscorrect(gold_ind, pred_ind)
gold_ind == pred_ind
end
"""
Display prediction nicely.
"""
function display_pred(
preds,
i2f,
grams,
tokenized = false,
sep_token = nothing,
start_end_token = "#",
output_sep_token = "",
)
if length(preds) == 0
println("No prediction for this utterance")
else
for i = 1:length(preds)
println("Predict $i")
println("support: $(preds[i].support)")
pred = translate(
preds[i].ngrams_ind,
i2f,
grams,
tokenized,
sep_token,
start_end_token,
output_sep_token,
)
println("prediction: $pred")
end
end
end
"""
Translate indices into words or utterances
"""
function translate(
ngrams_ind,
i2f,
grams,
tokenized,
sep_token,
start_end_token,
output_sep_token,
)
if isnothing(output_sep_token)
output_sep_token = ""
end
if tokenized && !isnothing(sep_token)
s1 = join(
[split(i2f[i], sep_token)[1] for i in ngrams_ind],
output_sep_token,
)
s2 = join(
split(i2f[ngrams_ind[end]], sep_token)[2:end],
output_sep_token,
)
out = replace(
s1 * output_sep_token * s2,
start_end_token * sep_token => "",
)
out = replace(out, sep_token * start_end_token => "")
else
s1 = join([split(i2f[i], "")[1] for i in ngrams_ind], output_sep_token)
s2 = join(split(i2f[ngrams_ind[end]], "")[2:end], output_sep_token)
out = replace(s1 * output_sep_token * s2, start_end_token => "")
end
out
end
"""
Append indices together to form a path
"""
function translate_path(ngrams_ind, i2f; sep_token = ":")
join([i2f[i] for i in ngrams_ind], sep_token)
end
"""
Check whether a matrix is truly sparse regardless its format, where M is originally a sparse matrix format.
"""
function is_truly_sparse(M::SparseMatrixCSC; threshold = 0.05, verbose = false)
verbose && println("Sparsity: $(length(M.nzval)/M.m/M.n)")
return threshold > (length(M.nzval) / M.m / M.n)
end
"""
Check whether a matrix is truly sparse regardless its format, where M is originally a dense matrix format.
"""
function is_truly_sparse(M::Matrix; threshold = 0.05, verbose = false)
M = sparse(M)
verbose && println("Sparsity: $(length(M.nzval)/M.m/M.n)")
return threshold > (length(M.nzval) / M.m / M.n)
end
"""
Check whether a gram can attach to another gram.
"""
function isattachable(a, b)
a[2:end] == b[1:end-1]
end
"""
Check whether a gram can attach to another gram.
"""
function isattachable(a, c, A)
convert(Bool, A[a[end], c])
end
"""
Check whether a path is complete.
"""
function iscomplete(a, i2f; tokenized = false, sep_token = nothing, start_end_token = "#")
ngram = i2f[a[end]]
if tokenized && !isnothing(sep_token)
last_w = split(ngram, sep_token)[end]
else
last_w = split(ngram, "")[end]
end
last_w == start_end_token
end
"""
Check whether a gram can start a path.
"""
function isstart(c, i2f; tokenized = false, sep_token = nothing, start_end_token = "#")
ngram = i2f[c]
if tokenized && !isnothing(sep_token)
start_w = split(ngram, sep_token)[1]
else
start_w = split(ngram, "")[1]
end
start_w == start_end_token
end
"""
Check whether a predicted path is in training data.
"""
function isnovel(gold_ind, pred_ngrams_ind)
!(pred_ngrams_ind in gold_ind)
end
"""
Check whether there are tokens already used in dataset as n-gram components.
"""
function check_used_token(data, target_col, token, token_name)
data_columns = data[:, target_col]
res = filter(x -> !isnothing(findfirst(token, x)), data_columns)
if length(res) > 0
throw(ArgumentError("$token_name \"$token\" is already used in the dataset"))
end
end
"""
function cal_max_timestep(
data_train::DataFrame,
data_val::DataFrame,
target_col::Union{String, Symbol};
tokenized::Bool = false,
sep_token::Union{Nothing, String, Char} = "",
)
Calculate the max timestep given training and validation datasets.
# Obligatory Arguments
- `data_train::DataFrame`: the training dataset
- `data_val::DataFrame`: the validation dataset
- `target_col::Union{String, Symbol}`: the column with the target word forms
# Optional Arguments
- `tokenized::Bool = false`: Whether the word forms in the `target_col` are already tokenized
- `sep_token::Union{Nothing, String, Char} = ""`: The token with which the word forms are tokenized
# Examples
```julia
JudiLing.cal_max_timestep(latin_train, latin_val, target_col=:Word)
```
"""
function cal_max_timestep(
data_train::DataFrame,
data_val::DataFrame,
target_col::Union{String, Symbol};
tokenized::Bool = false,
sep_token::Union{Nothing, String, Char} = "",
)
words_train = data_train[:, target_col]
words_val = data_val[:, target_col]
if tokenized && !isnothing(sep_token)
max_l_words_train =
maximum(x -> length(split(x, sep_token)), words_train)
max_l_words_val = maximum(x -> length(split(x, sep_token)), words_val)
else
max_l_words_train = maximum(x -> length(split(x, "")), words_train)
max_l_words_val = maximum(x -> length(split(x, "")), words_val)
end
maximum([max_l_words_train, max_l_words_val]) + 1
end
"""
function cal_max_timestep(
data::DataFrame,
target_col::Union{String, Symbol};
tokenized::Bool = false,
sep_token::Union{Nothing, String, Char} = "",
)
Calculate the max timestep given training dataset.
# Obligatory Arguments
- `data::DataFrame`: the dataset
- `target_col::Union{String, Symbol}`: the column with the target word forms
# Optional Arguments
- `tokenized::Bool = false`: Whether the word forms in the `target_col` are already tokenized
- `sep_token::Union{Nothing, String, Char} = ""`: The token with which the word forms are tokenized
# Examples
```julia
JudiLing.cal_max_timestep(latin, target_col=:Word)
```
"""
function cal_max_timestep(
data::DataFrame,
target_col::Union{String, Symbol};
tokenized::Bool = false,
sep_token::Union{Nothing, String, Char} = "",
)
words = data[:, target_col]
if tokenized && !isnothing(sep_token)
max_l_words = maximum(x -> length(split(x, sep_token)), words)
else
max_l_words = maximum(x -> length(split(x, "")), words)
end
max_l_words + 1
end
function inlinestring2string(s)
if s isa InlineString
return String(s)
else
return s
end
end