/
LSA.java
235 lines (193 loc) · 6.97 KB
/
LSA.java
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
/*
* This file is part of JaTeCS.
*
* JaTeCS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JaTeCS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JaTeCS. If not, see <http://www.gnu.org/licenses/>.
*
* The software has been mainly developed by (in alphabetical order):
* - Andrea Esuli (andrea.esuli@isti.cnr.it)
* - Tiziano Fagni (tiziano.fagni@isti.cnr.it)
* - Alejandro Moreo Fernández (alejandro.moreo@isti.cnr.it)
* Other past contributors were:
* - Giacomo Berardi (giacomo.berardi@isti.cnr.it)
*/
package it.cnr.jatecs.representation.lsa;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import it.cnr.jatecs.indexes.DB.generic.GenericIndex;
import it.cnr.jatecs.indexes.DB.interfaces.IIndex;
import it.cnr.jatecs.indexes.DB.troveCompact.TroveContentDBBuilder;
import it.cnr.jatecs.indexes.DB.troveCompact.TroveWeightingDBBuilder;
import it.cnr.jatecs.representation.randomprojections.IProjectionMethod;
import it.cnr.jatecs.representation.vector.SparseMatrix;
import it.cnr.jatecs.representation.vector.SparseMatrix.XY;
import it.cnr.jatecs.utils.JatecsLogger;
import it.cnr.jatecs.utils.iterators.IntArrayIterator;
import it.cnr.jatecs.utils.iterators.interfaces.IIntIterator;
import Jama.Matrix;
/**
* Implementation of Latent Semantic Analysis, wrapping the SVDlibc software
* (which might be obtained externally from {@code https://tedlab.mit.edu/~dr/SVDLIBC/}).
* For an overview, see e.g., {@code Deerwester, S., Dumais, S. T., Furnas, G. W., Landauer,
* T. K., & Harshman, R. (1990). Indexing by latent semantic analysis. Journal of the American
* society for information science, 41(6), 391.}
* */
public class LSA implements IProjectionMethod{
private IIndex _index;
private boolean _computed;
private Matrix Ut;
private Matrix S;
private Matrix Vt;
private IIndex _latentTraining;
private SVDlibcCustomizer _customizer;
private int _k;
public LSA(IIndex index, SVDlibcCustomizer customizer) {
_index = index;
_computed = false;
_customizer=customizer;
_k=_customizer.getK();
_latentTraining = null;
}
public void project() {
JatecsLogger.status().println("Start Latent Semantic Analysis indexes generation");
try {
JatecsLogger.status().println("Start singular value decomposition");
SVDlibc SVD = new SVDlibc(_index, _customizer);
Ut=SVD.getU_t();
S=SVD.getS();
Vt=SVD.getVt();
_k=SVD.getRank();
} catch (IOException e) {
JatecsLogger.execution().println("An error ocurred while runing svdlibc");
e.printStackTrace();
}
JatecsLogger.status().println("Start projecting Training Index into Latent Space");
Matrix docslatent = S.times(Vt);
JatecsLogger.status().println("\tcreating Jatecs Index");
_latentTraining = buildIndexFromMatrix(docslatent, _index);
_computed = true;
}
@Override
public IIndex getLatentTrainindex() {
if (!_computed) {
project();
}
return _latentTraining;
}
@Override
public IIndex getLatentTestindex(IIndex testindex) {
if (!_computed) {
project();
}
JatecsLogger.status().println("Start generating Latent Testing Index");
SparseMatrix testlatent = transformIndex(testindex);
Matrix testlatentDense = timesDenseSparse(Ut, testlatent);
JatecsLogger.status().println("\tcreating Jatecs Index");
IIndex latentTesting = buildIndexFromMatrix(testlatentDense, testindex);
return latentTesting;
}
private Matrix timesDenseSparse(Matrix D, SparseMatrix S) {
if(D.getColumnDimension()!=S.getRowsDimension()){
System.err.println("Dimensions must agree: exit"); System.exit(0);
}
int f=D.getRowDimension();
int c=S.getColumnDimensions();
List<Set<Integer>> colsNonZeroDims=new ArrayList<Set<Integer>>();
for(int i = 0; i < c; i++)
colsNonZeroDims.add(new HashSet<Integer>());
for(XY xy:S.getNonZeroPositions()){
int col=xy.y;
int dim=xy.x;
colsNonZeroDims.get(col).add(dim);
}
JatecsLogger.status().println("Creating a "+f+"x"+c+" matrix...");
Matrix R=new Matrix(f, c);
for(int i=0; i < f; i ++){
for(int j = 0; j < c; j++){
double dot=0;
for(int k:colsNonZeroDims.get(j)){
dot+=D.get(i, k)*S.get(k, j);
}
R.set(i, j, dot);
}
}
return R;
}
private IIndex buildIndexFromMatrix(Matrix latent, IIndex origIndex) {
IIndex latentIndex = origIndex.cloneIndex();
// remove exceeding features
int nf = latentIndex.getFeatureDB().getFeaturesCount();
int ntoremove = nf - _k;
int[] toRemove = new int[ntoremove];
for (int i = 0; i < ntoremove; i++)
toRemove[i] = i;
latentIndex.getFeatureDB().removeFeatures(new IntArrayIterator(toRemove));
// modify feature names to "lantent_i" pseudo-names
// set the weights of document-features, and feature frequencies to 1 in the latent index
TroveContentDBBuilder content = new TroveContentDBBuilder(latentIndex.getDocumentDB(), latentIndex.getFeatureDB());
TroveWeightingDBBuilder weighting = new TroveWeightingDBBuilder(
content.getContentDB());
for (int l = 0; l < latent.getRowDimension(); l++) {// latent features
for (int d = 0; d < latent.getColumnDimension(); d++) {// documents
double weight = latent.get(l, d);
content.setDocumentFeatureFrequency(d, l, weight!=0? 1 : 0);
weighting.setDocumentFeatureWeight(d, l, weight);
}
}
latentIndex = new GenericIndex("Lantent index",
latentIndex.getFeatureDB(), latentIndex.getDocumentDB(),
latentIndex.getCategoryDB(), latentIndex.getDomainDB(),
content.getContentDB(), weighting.getWeightingDB(),
latentIndex.getClassificationDB());
return latentIndex;
}
public boolean isComputed() {
return _computed;
}
private SparseMatrix transformIndex(IIndex index) {
int d = index.getDocumentDB().getDocumentsCount();
int t = index.getFeatureDB().getFeaturesCount();
// term x documents matrix
SparseMatrix matrix=new SparseMatrix(t, d);
IIntIterator docit = index.getDocumentDB().getDocuments();
for (int col=0; docit.hasNext();col++) {
int doc = docit.next();
IIntIterator featit = index.getFeatureDB().getFeatures();
for (int fil=0; featit.hasNext(); fil++) {
int feat = featit.next();
double w = index.getWeightingDB().getDocumentFeatureWeight(doc, feat);
matrix.set(fil, col, w);
}
}
return matrix;
}
@Override
public void clearResources() {
_index=null;
_computed=false;
Ut=null;
S=null;
Vt=null;
_latentTraining=null;
Runtime.getRuntime().freeMemory();
}
protected IIndex trainingIndex(){
return _index;
}
protected void setTrainingIndex(IIndex index){
_index = index;
}
}