/
dtm.jl
463 lines (396 loc) · 17.4 KB
/
dtm.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""
Basic Document-Term-Matrix (DTM) type.
# Fields
* `dtm::SparseMatriCSC{T,Int}` the actual DTM; rows represent terms
and columns represent documents
* `terms::Vector{String}` a list of terms that represent the lexicon of
the corpus associated with the DTM
* `row_indices::OrderedDict{String, Int}` a map between the `terms` and the
rows of the `dtm`
"""
struct DocumentTermMatrix{T}
dtm::SparseMatrixCSC{T, Int}
terms::Vector{String}
row_indices::OrderedDict{String, Int}
end
"""
rowindices(terms)
Returns a dictionary that maps each term from the vector `terms`
to a integer idex.
"""
rowindices(terms::Vector{String}) =
OrderedDict{String, Int}(term => i for (i,term) in enumerate(terms))
"""
columnindices(terms)
Identical to `rowindices`. Returns a dictionary that maps
each term from the vector `terms` to a integer idex.
"""
columnindices = rowindices
"""
DocumentTermMatrix{T}(docs [,terms] [; ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Auxiliary constructor(s) of the `DocumentTermMatrix` type. The type `T` has to be
a subtype of `Real`. The constructor(s) requires a corpus or vector of strings `docs`
and a `terms` structure representing the lexicon of the corpus. The latter
can be a `Vector{String}`, an `AbstractDict` where the keys are the lexicon, or can
be missing, in which case the `lexicon` field of the corpus is used.
"""
function DocumentTermMatrix{T}(docs::Union{Corpus, AbstractVector{S}},
terms::Vector{String};
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER
) where {S<:AbstractString,T<:Real}
row_indices = rowindices(terms)
m = length(terms)
n = length(docs)
rows = Vector{Int}(undef, 0)
columns = Vector{Int}(undef, 0)
values = Vector{T}(undef, 0)
for (i, doc) in enumerate(docs)
ngs = ngrams(doc, ngram_complexity, tokenizer=tokenizer)
for ngram in keys(ngs)
j = get(row_indices, ngram, 0)
v = ngs[ngram]
if j != 0
push!(columns, i)
push!(rows, j)
push!(values, v)
end
end
end
if length(rows) > 0
dtm = sparse(rows, columns, values, m, n)
else
dtm = spzeros(T, m, n)
end
return DocumentTermMatrix(dtm, terms, row_indices)
end
DocumentTermMatrix(docs::Union{Corpus, AbstractVector{S}},
terms::Vector{String};
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {S} =
DocumentTermMatrix{DEFAULT_DTM_TYPE}(docs, terms, ngram_complexity=ngram_complexity, tokenizer=tokenizer)
DocumentTermMatrix{T}(docs::Union{Corpus, AbstractVector{S}},
lex::AbstractDict;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {S,T<:Real} =
DocumentTermMatrix{T}(docs, collect(keys(lex)), ngram_complexity=ngram_complexity, tokenizer=tokenizer)
DocumentTermMatrix(docs::Union{Corpus, AbstractVector{S}},
lex::AbstractDict;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {S} =
DocumentTermMatrix{DEFAULT_DTM_TYPE}(docs, lex, ngram_complexity=ngram_complexity, tokenizer=tokenizer)
DocumentTermMatrix{T}(docs::Union{Corpus, AbstractVector{S}};
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {S,T<:Real} = begin
DocumentTermMatrix{T}(docs, create_lexicon(docs, ngram_complexity),
ngram_complexity=ngram_complexity, tokenizer=tokenizer)
end
DocumentTermMatrix(docs::Union{Corpus, AbstractVector{S}};
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {S} = begin
DocumentTermMatrix{DEFAULT_DTM_TYPE}(docs, create_lexicon(docs, ngram_complexity),
ngram_complexity=ngram_complexity, tokenizer=tokenizer)
end
DocumentTermMatrix(dtm::SparseMatrixCSC{T, Int}, terms::Vector{String}) where {T<:Real} =
DocumentTermMatrix(dtm, terms, rowindices(terms))
"""
dtm(d::DocumentTermMatrix)
Access the matrix of a `DocumentTermMatrix` `d`.
"""
dtm(d::DocumentTermMatrix) = d.dtm
"""
dtm(docs::Corpus, eltype::Type{T}=DEFAULT_DTM_TYPE [; ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Access the matrix of the DTM associated with the corpus `docs`. The
`DocumentTermMatrix{T}` will first have to be created in order for
the actual matrix to be accessed.
"""
dtm(docs::Union{Corpus, AbstractVector{S}},
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER
) where {S,T<:Real} =
dtm(DocumentTermMatrix{T}(docs, ngram_complexity=ngram_complexity, tokenizer=tokenizer))
# Produce the signature of a DTM entry for a document
function dtm_entries(d,
lex::OrderedDict{String, Int},
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER,
lex_is_row_indices::Bool=false) where {T<:Real}
ngs = ngrams(d, ngram_complexity, tokenizer=tokenizer)
indices = Vector{Int}(undef, 0)
values = Vector{T}(undef, 0)
local row_indices
if lex_is_row_indices
row_indices = lex
else
terms = collect(keys(lex))
row_indices = rowindices(terms)
end
for ngram in keys(ngs)
j = get(row_indices, ngram, 0)
v = ngs[ngram]
if j != 0
push!(indices, j)
push!(values, v)
end
end
return (indices, values)
end
"""
dtv(d, lex::OrderedDict{String,Int}, eltype::Type{T}=DEFAULT_DTM_TYPE [; ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Creates a document-term-vector with elements of type `T` for document `d`
using the lexicon `lex`. `d` can be an `AbstractString` or an `AbstractDocument`.
"""
function dtv(d,
lex::OrderedDict{String, Int},
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER,
lex_is_row_indices::Bool=false) where {T<:Real}
p = length(keys(lex))
column = spzeros(T, p)
indices, values = dtm_entries(d, lex, eltype,
ngram_complexity=ngram_complexity,
tokenizer=tokenizer,
lex_is_row_indices=lex_is_row_indices)
column[indices] = values
return column
end
"""
dtv(crps::Corpus, idx::Int, eltype::Type{T}=DEFAULT_DTM_TYPE [; ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Creates a document-term-vector with elements of type `T` for document `idx`
of the corpus `crps`.
"""
function dtv(crps::Corpus,
idx::Int,
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER,
lex_is_row_indices::Bool=false) where {T<:Real}
if idx >= length(crps.documents) || idx < 1
error("DTV requires the document index in [1,$(length(crps.documents))]")
end
if isempty(crps.lexicon)
lex = create_lexicon(crps, ngram_complexity)
else
lex = lexicon(crps)
end
return dtv(crps.documents[idx], lex, eltype,
ngram_complexity=ngram_complexity,
tokenizer=tokenizer,
lex_is_row_indices=lex_is_row_indices)
end
function dtv(d;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER,
lex_is_row_indices::Bool=false)
throw(ErrorException("Cannot construct a DTV without a pre-existing lexicon"))
end
# Document is a list of regular expressions in text form
function dtm_regex_entries(d,
lex::OrderedDict{String, Int},
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER,
lex_is_row_indices::Bool=false) where {T<:Real}
ngs = ngrams(d, ngram_complexity, tokenizer=tokenizer)
patterns = Regex.(keys(ngs))
indices = Vector{Int}(undef, 0)
terms = collect(keys(lex))
local row_indices
if lex_is_row_indices
row_indices = lex
else
row_indices = rowindices(terms)
end
for pattern in patterns
for term in terms
if occursin(pattern, term)
j = row_indices[term]
push!(indices, j)
end
end
end
values = ones(T, length(indices))
return (indices, values)
end
"""
dtv_regex(d, lex::OrderedDict{String,Int}, eltype::Type{T}=DEFAULT_DTM_TYPE [; ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Creates a document-term-vector with elements of type `T` for document `d`
using the lexicon `lex`. The tokens of document `d` are assumed to be regular
expressions in text format. `d` can be an `AbstractString` or an `AbstractDocument`.
# Examples
```
julia> dtv_regex(NGramDocument("a..b"), OrderedDict("aaa"=>1, "aaab"=>2, "accb"=>3, "bbb"=>4), Float32)
4-element Array{Float32,1}:
0.0
1.0
1.0
0.0
```
"""
function dtv_regex(d,
lex::OrderedDict{String, Int},
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER,
lex_is_row_indices::Bool=false) where {T<:Real}
p = length(keys(lex))
column = spzeros(T, p)
indices, values = dtm_regex_entries(d, lex, eltype,
ngram_complexity=ngram_complexity,
tokenizer=tokenizer,
lex_is_row_indices=lex_is_row_indices)
column[indices] = values
return column
end
"""
hash_dtv(d, h::TextHashFunction, eltype::Type{T}=DEFAULT_DTM_TYPE [; ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Creates a hashed document-term-vector with elements of type `T` for document `d`
using the hashing function `h`. `d` can be an `AbstractString` or an `AbstractDocument`.
"""
function hash_dtv(d,
h::TextHashFunction,
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {T<:Real}
p = cardinality(h)
res = spzeros(T, p)
ngs = ngrams(d, ngram_complexity, tokenizer=tokenizer)
for ng in keys(ngs)
res[index_hash(ng, h)] += ngs[ng]
end
return res
end
function hash_dtv(d;
cardinality::Int=DEFAULT_CARDINALITY,
eltype::Type{T}=DEFAULT_DTM_TYPE,
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {T<:Real}
hash_dtv(d, TextHashFunction(cardinality), eltype, ngram_complexity=ngram_complexity, tokenizer=tokenizer)
end
"""
hash_dtm(crps::Corpus [,h::TextHashFunction], eltype::Type{T}=DEFAULT_DTM_TYPE [; ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Creates a hashed DTM with elements of type `T` for corpus `crps` using the
the hashing function `h`. If `h` is missing, the hash function of the `Corpus`
is used.
"""
function hash_dtm(crps::Corpus,
h::TextHashFunction,
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {T<:Real}
n, p = length(crps), cardinality(h)
res = spzeros(T, p, n)
for (i, doc) in enumerate(crps)
res[:, i] = hash_dtv(doc, h, eltype, ngram_complexity=ngram_complexity, tokenizer=tokenizer)
end
return res
end
hash_dtm(crps::Corpus,
eltype::Type{T}=DEFAULT_DTM_TYPE;
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {T<:Real} =
hash_dtm(crps, hash_function(crps), eltype, ngram_complexity=ngram_complexity, tokenizer=tokenizer)
# Produce entries for on-line analysis when DTM would not fit in memory
struct EachDTV{U, S<:AbstractString, T<:AbstractDocument}
corpus::Corpus{S,T}
row_indices::OrderedDict{String, Int}
ngram_complexity::Int
tokenizer::Symbol
function EachDTV{U,S,T}(corpus::Corpus{S,T},
row_indices::OrderedDict{String, Int},
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER
) where {U, S<:AbstractString, T<:AbstractDocument}
@assert ngram_complexity >= 1 "Ngram complexity has to be >= 1"
@assert tokenizer in [:default,
:stringanalysis] "Tokenizer has to be either :default or :stringanalysis"
new(corpus, row_indices, ngram_complexity, tokenizer)
end
end
EachDTV{U}(crps::Corpus{S,T};
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {U,S,T} = begin
row_indices = rowindices(collect(keys(create_lexicon(crps, ngram_complexity))))
EachDTV{U,S,T}(crps, row_indices, ngram_complexity, tokenizer)
end
Base.iterate(edt::EachDTV, state=1) = begin
if state > length(edt.corpus)
return nothing
else
return next(edt, state)
end
end
next(edt::EachDTV{U,S,T}, state::Int) where {U,S,T} =
(dtv(edt.corpus.documents[state], edt.row_indices, U,
ngram_complexity=edt.ngram_complexity,
tokenizer=edt.tokenizer, lex_is_row_indices=true), state + 1)
"""
each_dtv(crps::Corpus [; eltype::Type{U}=DEFAULT_DTM_TYPE, ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Iterates through the columns of the DTM of the corpus `crps` without
constructing it. Useful when the DTM would not fit in memory.
`eltype` specifies the element type of the generated vectors.
"""
each_dtv(crps::Corpus;
eltype::Type{U}=DEFAULT_DTM_TYPE,
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where U<:Real =
EachDTV{U}(crps, ngram_complexity=ngram_complexity, tokenizer=tokenizer)
Base.eltype(::Type{EachDTV{U,S,T}}) where {U,S,T} = Vector{U}
Base.length(edt::EachDTV) = length(edt.corpus)
Base.size(edt::EachDTV) = (length(edt.corpus), edt.corpus.h.cardinality)
Base.show(io::IO, edt::EachDTV{U,S,T}) where {U,S,T} =
print(io, "DTV iterator, tokenizer is $(edt.tokenizer), "*
"$(length(edt)) elements of type $(eltype(edt)).")
struct EachHashDTV{U, S<:AbstractString, T<:AbstractDocument}
corpus::Corpus{S,T}
ngram_complexity::Int
tokenizer::Symbol
function EachHashDTV{U,S,T}(corpus::Corpus{S,T},
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where
{U, S<:AbstractString, T<:AbstractDocument}
@assert ngram_complexity >= 1 "Ngram complexity has to be >= 1"
@assert tokenizer in [:default,
:stringanalysis] "Tokenizer has to be either :default or :stringanalysis"
new(corpus, ngram_complexity, tokenizer)
end
end
EachHashDTV{U}(crps::Corpus{S,T};
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where {U,S,T} =
EachHashDTV{U,S,T}(crps, ngram_complexity, tokenizer)
Base.iterate(edt::EachHashDTV, state=1) = begin
if state > length(edt.corpus)
return nothing
else
return next(edt, state)
end
end
next(edt::EachHashDTV{U,S,T}, state::Int) where {U,S,T} =
(hash_dtv(edt.corpus.documents[state], edt.corpus.h, U,
ngram_complexity=edt.ngram_complexity,
tokenizer=edt.tokenizer), state + 1)
"""
each_hash_dtv(crps::Corpus [; eltype::Type{U}=DEFAULT_DTM_TYPE, ngram_complexity=DEFAULT_NGRAM_COMPLEXITY, tokenizer=DEFAULT_TOKENIZER])
Iterates through the columns of the hashed DTM of the corpus `crps` without
constructing it. Useful when the DTM would not fit in memory.
`eltype` specifies the element type of the generated vectors.
"""
each_hash_dtv(crps::Corpus;
eltype::Type{U}=DEFAULT_DTM_TYPE,
ngram_complexity::Int=DEFAULT_NGRAM_COMPLEXITY,
tokenizer::Symbol=DEFAULT_TOKENIZER) where U<:Real =
EachHashDTV{U}(crps, ngram_complexity=ngram_complexity, tokenizer=tokenizer)
Base.eltype(::Type{EachHashDTV{U,S,T}}) where {U,S,T} = Vector{U}
Base.length(edt::EachHashDTV) = length(edt.corpus)
Base.size(edt::EachHashDTV) = (length(edt.corpus), edt.corpus.h.cardinality)
Base.show(io::IO, edt::EachHashDTV{U,S,T}) where {U,S,T} =
print(io, "Hash-DTV iterator, tokenizer is $(edt.tokenizer) "*
"$(length(edt)) elements of type $(eltype(edt)).")
## getindex() methods
Base.getindex(dtm::DocumentTermMatrix, k::AbstractString) = dtm.dtm[dtm.row_indices[k], :]
Base.getindex(dtm::DocumentTermMatrix, i) = dtm.dtm[i]
Base.getindex(dtm::DocumentTermMatrix, i, j) = dtm.dtm[i, j]