forked from jatecs/jatecs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexRCV1.java
More file actions
162 lines (132 loc) · 6.27 KB
/
IndexRCV1.java
File metadata and controls
162 lines (132 loc) · 6.27 KB
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
/*
* 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 apps.dataset;
import it.cnr.jatecs.indexes.DB.interfaces.ICategoryDB;
import it.cnr.jatecs.indexes.DB.interfaces.IIndex;
import it.cnr.jatecs.indexes.DB.troveCompact.TroveDependentIndexBuilder;
import it.cnr.jatecs.indexes.DB.troveCompact.TroveMainIndexBuilder;
import it.cnr.jatecs.indexes.DB.troveCompact.TroveReadWriteHelper;
import it.cnr.jatecs.indexing.corpus.BagOfWordsFeatureExtractor;
import it.cnr.jatecs.indexing.corpus.CorpusReader;
import it.cnr.jatecs.indexing.corpus.FeatureExtractor;
import it.cnr.jatecs.indexing.corpus.RCV1.RCV1CategoryReader;
import it.cnr.jatecs.indexing.corpus.RCV1.RCV1CorpusReader;
import it.cnr.jatecs.indexing.corpus.SetType;
import it.cnr.jatecs.indexing.module.FullIndexConstructor;
import it.cnr.jatecs.indexing.preprocessing.EnglishPorterStemming;
import it.cnr.jatecs.indexing.preprocessing.EnglishStopword;
import it.cnr.jatecs.indexing.weighting.IWeighting;
import it.cnr.jatecs.indexing.weighting.TfNormalizedIdf;
import it.cnr.jatecs.io.FileSystemStorageManager;
import it.cnr.jatecs.utils.Os;
import java.io.IOException;
public class IndexRCV1 {
/**
* This application reads the RCV1-v2 document collection, a more recent version of the precendet
* Reuters-21578 corpus, and creates an IIndex structure.
* See {@code Lewis, D. D., Yang, Y., Rose, T. G., & Li, F. (2004). Rcv1: A new
* benchmark collection for text categorization research. The Journal of Machine
* Learning Research, 5, 361-397.}.
* */
public static void main(String[] args) throws IOException {
if (args.length < 3 || args.length > 4) {
System.out
.println("Usage: IndexRCV1 <indexPath> <corpusPath> <categoryFile> [splitCorpusPath]");
return;
}
String indexPath = args[0];
String fullCorpusPath = args[1];
String categoriesFile = args[2];
String splittedCorpusPath = null;
if (args.length > 3) {
splittedCorpusPath = args[3];
}
// DOMAIN
RCV1CategoryReader categoriesReader = new RCV1CategoryReader(
categoriesFile);
ICategoryDB categoryDB = categoriesReader.getCategoryDB();
// CORPUS READER
RCV1CorpusReader corpusReader = new RCV1CorpusReader(categoryDB);
corpusReader.excludeDocumentsWithoutValidCategories(false);
corpusReader.setInputDir(fullCorpusPath);
FeatureExtractor extractor = new BagOfWordsFeatureExtractor();
extractor.disableEntitiesSubstitution();
extractor.disableSpecialTermsSubstitution();
extractor.disableSpellChecking();
extractor.disableTFFeatures();
extractor.enableStemming(new EnglishPorterStemming());
extractor.enableStopwordRemoval(new EnglishStopword());
// TRAINING INDEX
corpusReader.setDocumentSetType(SetType.TRAINING);
IIndex training = indexTraining(categoryDB, corpusReader, extractor);
FileSystemStorageManager storageManager = new FileSystemStorageManager(
indexPath, false);
storageManager.open();
TroveReadWriteHelper.writeIndex(storageManager, training,
"RCV1-training", true);
// TEST INDEX
corpusReader = new RCV1CorpusReader(categoryDB);
corpusReader.setDocumentSetType(SetType.TEST);
if (splittedCorpusPath != null) {
for (int i = 1; i < 13; ++i) {
corpusReader.setInputDir(splittedCorpusPath + "/" + i);
IIndex test = indexTest(corpusReader, extractor, training);
TroveReadWriteHelper.writeIndex(storageManager, test,
"RCV1-test" + Os.pathSeparator() + i, true);
}
} else {
corpusReader.setInputDir(fullCorpusPath);
IIndex test = indexTest(corpusReader, extractor, training);
TroveReadWriteHelper.writeIndex(storageManager, test, "RCV1-test",
true);
}
storageManager.close();
}
private static IIndex indexTraining(ICategoryDB categoryDB,
CorpusReader corpusReader, FeatureExtractor extractor) {
TroveMainIndexBuilder trainingIndexBuilder = new TroveMainIndexBuilder(
categoryDB);
FullIndexConstructor traningIndexConstructor = new FullIndexConstructor(
corpusReader, trainingIndexBuilder);
traningIndexConstructor.setFeatureExtractor(extractor);
traningIndexConstructor.exec();
IIndex index = traningIndexConstructor.index();
IWeighting weighting = new TfNormalizedIdf(index);
index = weighting.computeWeights(index);
return index;
}
private static IIndex indexTest(CorpusReader corpusReader,
FeatureExtractor extractor, IIndex training) {
TroveDependentIndexBuilder testIndexBuilder = new TroveDependentIndexBuilder(
training.getDomainDB());
FullIndexConstructor testIndexConstructor = new FullIndexConstructor(
corpusReader, testIndexBuilder);
testIndexConstructor.setFeatureExtractor(extractor);
testIndexConstructor.exec();
IIndex index = testIndexConstructor.index();
IWeighting weighting = new TfNormalizedIdf(training);
index = weighting.computeWeights(index);
return index;
}
}