-
Notifications
You must be signed in to change notification settings - Fork 96
/
dtm.jl
193 lines (160 loc) · 5.06 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
##############################################################################
#
# Basic DocumentTermMatrix type
#
##############################################################################
type DocumentTermMatrix
dtm::SparseMatrixCSC{Int, Int}
terms::Vector{UTF8String}
column_indices::Dict{UTF8String, Int}
end
##############################################################################
#
# Construct a DocumentTermMatrix from a Corpus
#
##############################################################################
function DocumentTermMatrix(crps::Corpus)
lex = lexicon(crps)
terms = sort(collect(keys(lex)))
column_indices = Dict{UTF8String, Int}()
for i in 1:length(terms)
term = terms[i]
column_indices[term] = i
end
rows = Array(Int, 0)
columns = Array(Int, 0)
values = Array(Int, 0)
for i in 1:length(crps)
doc = crps[i]
ngs = ngrams(doc)
for ngram in keys(ngs)
j = get(column_indices, ngram, 0)
v = ngs[ngram]
if j != 0
push!(rows, i)
push!(columns, j)
push!(values, v)
end
end
end
if length(rows) > 0
dtm = sparse(rows, columns, values)
else
dtm = spzeros(Int, length(crps), 0)
end
DocumentTermMatrix(dtm, terms, column_indices)
end
##############################################################################
#
# Access the DTM of a DocumentTermMatrix
#
##############################################################################
function dtm(d::DocumentTermMatrix, density::Symbol)
if density == :sparse
return d.dtm
else
return full(d.dtm)
end
end
function dtm(d::DocumentTermMatrix)
return d.dtm
end
function dtm(crps::Corpus)
dtm(DocumentTermMatrix(crps))
end
tdm(crps::DocumentTermMatrix, density::Symbol) = dtm(crps, density)' #'
tdm(crps::DocumentTermMatrix) = dtm(crps)' #'
tdm(crps::Corpus) = dtm(crps)' #'
##############################################################################
#
# Produce the signature of a DTM entry for a document
#
# TODO: Make this more efficieny by reusing column_indices
#
##############################################################################
function dtm_entries(d::AbstractDocument, lex::Dict{UTF8String, Int})
ngs = ngrams(d)
indices = Array(Int, 0)
values = Array(Int, 0)
terms = sort(collect(keys(lex)))
column_indices = Dict{UTF8String, Int}()
for i in 1:length(terms)
term = terms[i]
column_indices[term] = i
end
for ngram in keys(ngs)
if haskey(column_indices, ngram)
push!(indices, column_indices[ngram])
push!(values, ngs[ngram])
end
end
return (indices, values)
end
function dtv(d::AbstractDocument, lex::Dict{UTF8String, Int})
p = length(keys(lex))
row = zeros(Int, 1, p)
indices, values = dtm_entries(d, lex)
for i in 1:length(indices)
row[1, indices[i]] = values[i]
end
return row
end
function dtv(d::AbstractDocument)
error("Cannot construct a DTV without a pre-existing lexicon")
end
##############################################################################
#
# The hash trick: use a hash function instead of a lexicon to determine the
# columns of a DocumentTermMatrix-like encoding of the data
#
##############################################################################
function hash_dtv(d::AbstractDocument, h::TextHashFunction)
p = cardinality(h)
res = zeros(Int, 1, p)
ngs = ngrams(d)
for ng in keys(ngs)
res[1, index_hash(ng, h)] += ngs[ng]
end
return res
end
hash_dtv(d::AbstractDocument) = hash_dtv(d, TextHashFunction())
function hash_dtm(crps::Corpus, h::TextHashFunction)
n, p = length(crps), cardinality(h)
res = zeros(Int, n, p)
for i in 1:length(crps)
doc = crps[i]
res[i, :] = hash_dtv(doc, h)
end
return res
end
hash_dtm(crps::Corpus) = hash_dtm(crps, hash_function(crps))
hash_tdm(crps::Corpus) = hash_dtm(crps)' #'
##############################################################################
#
# Produce entries for on-line analysis when DTM would not fit in memory
#
##############################################################################
type EachDTV
crps::Corpus
end
start(edt::EachDTV) = 1
function next(edt::EachDTV, state::Int)
return (dtv(edt.crps.documents[state], lexicon(edt.crps)), state + 1)
end
done(edt::EachDTV, state::Int) = state > length(edt.crps.documents)
type EachHashDTV
crps::Corpus
end
start(edt::EachHashDTV) = 1
function next(edt::EachHashDTV, state::Int)
(hash_dtv(edt.crps.documents[state]), state + 1)
end
done(edt::EachHashDTV, state::Int) = state > length(edt.crps.documents)
each_dtv(crps::Corpus) = EachDTV(crps)
each_hash_dtv(crps::Corpus) = EachHashDTV(crps)
##
## getindex() methods
##
Base.getindex(dtm::DocumentTermMatrix, k::String) = dtm.dtm[:, dtm.column_indices[k]]
Base.getindex(dtm::DocumentTermMatrix, i::Any) = dtm.dtm[i]
Base.getindex(dtm::DocumentTermMatrix, i::Any, j::Any) = dtm.dtm[i, j]