This repository was archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTrainingCorpusCreatorPR.java
400 lines (352 loc) · 12.3 KB
/
TrainingCorpusCreatorPR.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
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
/**
* Copyright 2010 DigitalPebble Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.digitalpebble.gate.textclassification;
import gate.Annotation;
import gate.AnnotationSet;
import gate.FeatureMap;
import gate.ProcessingResource;
import gate.Resource;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.ExecutionException;
import gate.creole.ResourceInstantiationException;
import gate.util.OffsetComparator;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.digitalpebble.classification.Document;
import com.digitalpebble.classification.FileTrainingCorpus;
import com.digitalpebble.classification.Learner;
import com.digitalpebble.classification.Lexicon;
import com.digitalpebble.classification.Parameters;
import com.digitalpebble.classification.TrainingCorpus;
import com.digitalpebble.classification.Parameters.WeightingMethod;
import com.digitalpebble.classification.RAMTrainingCorpus;
import com.digitalpebble.classification.util.CorpusUtils;
import com.digitalpebble.classification.util.scorers.AttributeScorer;
import com.digitalpebble.classification.util.scorers.logLikelihoodAttributeScorer;
import com.digitalpebble.classification.libsvm.Utils;
public class TrainingCorpusCreatorPR extends AbstractLanguageAnalyser
implements ProcessingResource {
/** * */
private Learner creator;
/**
* Input AnnotationSet name
*/
private String inputAnnotationSet;
/**
* Label Annotation Type (e.g. Sentence) For which a separate text
* classification document is created
*/
private String labelAnnotationType;
/**
* Label Annotation Value (e.g. lang) The value of this feature is used as a
* label
*/
private String labelAnnotationValue;
/**
* Attribute Annotation Type (e.g. token) The annotation type used for the ML attributes.
*/
private String attributeAnnotationType;
/**
* ComponentAnnotationValue (e.g. form) The feature value used for the ML attributes.
*/
private String attributeAnnotationValue;
/**
* Directory where lexicon, vector and raw model will be saved
*/
private URL directory;
/**
* FEature weighting scheme
*/
private String weightingScheme;
private Integer minFreq=1;
private Integer maxFreq=Integer.MAX_VALUE;
/**
* Run after prunning according to min and max freq
*/
private Integer keepNBestAttributes =0;
/**
* Compact the lexicon after prunning
*/
Boolean compactLexicon =true;
private Boolean reinitCorpus = true;
private FileTrainingCorpus trainingcorpus;
private String implementation = Learner.LibSVMModelCreator;
String pathDirectory;
private String libsvmVectorPath;
/*
* this method gets called whenever an object of this class is created
* either from GATE GUI or if initiated using Factory.createResource()
* method.
*/
public Resource init() throws ResourceInstantiationException {
// here initialize all required variables, and may
// be throw an exception if the value for any of the
// mandatory parameters is not provided
// check that a modelLocation has been selected
if (directory == null || "".equals(directory))
throw new ResourceInstantiationException(
"directory is required to store the data and cannot be null or empty");
// it is not null, check it is a file: URL
if (!"file".equals(directory.getProtocol())) {
throw new ResourceInstantiationException(
"directory must be a file: URL");
}
// initializes the modelCreator
pathDirectory = new File(URI.create(directory.toExternalForm()))
.getAbsolutePath();
if(libsvmVectorPath == null || libsvmVectorPath.isEmpty()){
libsvmVectorPath = pathDirectory+File.separator+"vector";
}
try {
this.creator = Learner.getLearner(pathDirectory, implementation,
reinitCorpus);
this.trainingcorpus = creator.getFileTrainingCorpus();
} catch (Exception e) {
throw new ResourceInstantiationException(e);
}
fireProcessFinished();
System.out.println("CorpusCreator reinitialised");
return this;
}
/* this method is called to reinitialize the resource */
public void reInit() throws ResourceInstantiationException {
init();
}
/**
* Called when user clicks on RUN button in GATE GUI
*/
public void execute() throws ExecutionException {
// reinitialise the model if necessary
int positionDoc = corpus.indexOf(document);
if (positionDoc == 0 && getReinitCorpus().booleanValue())
try {
reInit();
} catch (ResourceInstantiationException e1) {
throw new ExecutionException(e1);
}
// check parameters
checkParameters();
AnnotationSet inputAS = inputAnnotationSet == null
|| inputAnnotationSet.trim().length() == 0 ? document
.getAnnotations() : document.getAnnotations(inputAnnotationSet);
// obtain annotations of type textAnnotationType
AnnotationSet textAS = inputAS.get(labelAnnotationType);
if (textAS == null || textAS.isEmpty()) {
System.err.println("There are no annotations of type "
+ labelAnnotationType + " available in document!");
}
Iterator<Annotation> iterator = textAS.iterator();
while (iterator.hasNext()) {
Annotation annotation = iterator.next();
// find out the feature of type textAnnotationValue
// e.g a sentence
FeatureMap features = annotation.getFeatures();
String textAV = (String) features.get(labelAnnotationValue);
if (textAV == null) {
continue;
}
// obtain the components annotations
AnnotationSet set = inputAS.getContained(annotation.getStartNode()
.getOffset(), annotation.getEndNode().getOffset());
if (set.isEmpty())
continue;
AnnotationSet underlyingAS = set.get(attributeAnnotationType);
if (underlyingAS.isEmpty())
continue;
List<Annotation> list = new ArrayList<Annotation>();
Iterator<Annotation> iter = underlyingAS.iterator();
while (iter.hasNext()) {
Annotation annot = iter.next();
if (annot.getFeatures().containsKey(attributeAnnotationValue)) {
list.add(annot);
}
}
// make underlyingAS eligible for garbage collection
underlyingAS = null;
// sort them
Collections.sort(list, new OffsetComparator());
String[] values = new String[list.size()];
// else obtain the value of each feature (componentAnnotationValue)
for (int i = 0; i < list.size(); i++) {
Annotation annot = (Annotation) list.get(i);
values[i] = (String) annot.getFeatures().get(
attributeAnnotationValue);
}
if (values.length == 0)
continue;
// creates a simple document
Document newDocument = creator.createDocument(values, textAV);
try {
this.trainingcorpus.addDocument(newDocument);
} catch (IOException e) {
throw new ExecutionException(e);
}
}
// check if this document is the last of this corpus in which case
// we'll start the learning
fireProgressChanged((100 * positionDoc) / corpus.size());
// do we trigger the learning?
if (positionDoc == corpus.size() - 1) {
fireStatusChanged("Saving the Lexicon");
try {
WeightingMethod method = Parameters.WeightingMethod
.methodFromString(getWeightingScheme());
this.creator.setMethod(method);
trainingcorpus.close();
Lexicon lexicon = creator.getLexicon();
creator.saveLexicon();
//prune by frequency
lexicon.pruneTermsDocFreq(minFreq, maxFreq);
//further keep only the N best attributes
if (keepNBestAttributes >0) {
AttributeScorer scorer = logLikelihoodAttributeScorer.getScorer(
trainingcorpus, lexicon);
lexicon.setAttributeScorer(scorer);
lexicon.applyAttributeFilter(scorer, keepNBestAttributes);
}
// change the indices of the attributes to remove
// gaps between them
Map<Integer, Integer> equiv = null;
if (compactLexicon){
// create a new Lexicon object
equiv = lexicon.compact();
}
// save the modified lexicon file
lexicon.saveToFile(this.pathDirectory+"lexicon.compact");
Utils.writeExamples(trainingcorpus,lexicon,
this.libsvmVectorPath, equiv);
} catch (Exception e) {
throw new ExecutionException(e);
} finally {
fireProcessFinished();
}
}
}
/**
* Checks if values for the manadatory parameters provided.
*
* @throws ExecutionException
*/
private void checkParameters() throws ExecutionException {
if (document == null)
throw new ExecutionException("Document is null!");
if (labelAnnotationType == null
|| labelAnnotationType.trim().length() == 0)
throw new ExecutionException("TextAnnotationType is null!");
if (labelAnnotationValue == null
|| labelAnnotationValue.trim().length() == 0)
throw new ExecutionException("TextAnnotationValue is null!");
if (attributeAnnotationType == null
|| attributeAnnotationType.trim().length() == 0)
throw new ExecutionException("componentAnnotationType is null!");
if (attributeAnnotationValue == null
|| attributeAnnotationValue.trim().length() == 0)
throw new ExecutionException("componentAnnotationValue is null!");
// check weighting scheme
Parameters.WeightingMethod.methodFromString(getWeightingScheme());
}
public String getAttributeAnnotationType() {
return attributeAnnotationType;
}
public void setAttributeAnnotationType(String componentAnnotationType) {
this.attributeAnnotationType = componentAnnotationType;
}
public String getAttributeAnnotationValue() {
return attributeAnnotationValue;
}
public void setAttributeAnnotationValue(String componentAnnotationValue) {
this.attributeAnnotationValue = componentAnnotationValue;
}
public String getInputAnnotationSet() {
return inputAnnotationSet;
}
public void setInputAnnotationSet(String inputAnnotationSet) {
this.inputAnnotationSet = inputAnnotationSet;
}
public String getLabelAnnotationType() {
return labelAnnotationType;
}
public void setLabelAnnotationType(String textAnnotationType) {
this.labelAnnotationType = textAnnotationType;
}
public String getLabelAnnotationValue() {
return labelAnnotationValue;
}
public void setLabelAnnotationValue(String textAnnotationValue) {
this.labelAnnotationValue = textAnnotationValue;
}
public URL getDirectory() {
return directory;
}
public void setDirectory(URL directoryLocation) {
this.directory = directoryLocation;
}
public String getWeightingScheme() {
return weightingScheme;
}
public void setWeightingScheme(String weightingScheme) {
this.weightingScheme = weightingScheme;
}
public Boolean getReinitCorpus() {
return reinitCorpus;
}
public void setReinitCorpus(Boolean reinitCorpus) {
this.reinitCorpus = reinitCorpus;
}
public String getImplementation() {
return implementation;
}
public void setImplementation(String implementation) {
this.implementation = implementation;
}
public Learner getCreator() {
return creator;
}
public void setCreator(Learner creator) {
this.creator = creator;
}
public Integer getMinFreq() {
return minFreq;
}
public void setMinFreq(Integer minFreq) {
this.minFreq = minFreq;
}
public Integer getMaxFreq() {
return maxFreq;
}
public void setMaxFreq(Integer maxFreq) {
this.maxFreq = maxFreq;
}
public Integer getKeepNBestAttributes() {
return keepNBestAttributes;
}
public void setKeepNBestAttributes(Integer keepNBestAttributes) {
this.keepNBestAttributes = keepNBestAttributes;
}
public Boolean getCompactLexicon() {
return compactLexicon;
}
public void setCompactLexicon(Boolean compactLexicon) {
this.compactLexicon = compactLexicon;
}
}