50
50
import opennlp .tools .util .featuregen .StringPattern ;
51
51
52
52
/**
53
- * A {@link POSTagger part-of-speech tagger} that uses maximum entropy.
53
+ * A {@link POSTagger part-of-speech tagger} implementation that uses maximum entropy.
54
54
* <p>
55
- * Tries to predict whether words are nouns, verbs, or any of 70 other POS tags
55
+ * Tries to predict whether words are nouns, verbs, or any other {@link POSTagFormat POS tags}
56
56
* depending on their surrounding context.
57
+ *
58
+ * @see POSModel
59
+ * @see POSTagFormat
60
+ * @see POSTagger
57
61
*/
58
62
public class POSTaggerME implements POSTagger {
59
63
60
64
private static final Logger logger = LoggerFactory .getLogger (POSTaggerME .class );
61
65
66
+ /**
67
+ * The default beam size value is 3.
68
+ */
62
69
public static final int DEFAULT_BEAM_SIZE = 3 ;
63
70
64
71
private final POSModel modelPackage ;
65
72
66
73
/**
67
74
* The {@link POSContextGenerator feature context generator}.
68
75
*/
69
- protected final POSContextGenerator contextGen ;
76
+ protected final POSContextGenerator cg ;
70
77
71
78
/**
72
79
* {@link TagDictionary} used for restricting words to a fixed set of tags.
@@ -140,7 +147,7 @@ public POSTaggerME(POSModel model, POSTagFormat format) {
140
147
141
148
modelPackage = model ;
142
149
143
- contextGen = factory .getPOSContextGenerator (beamSize );
150
+ cg = factory .getPOSContextGenerator (beamSize );
144
151
tagDictionary = factory .getTagDictionary ();
145
152
size = beamSize ;
146
153
@@ -165,14 +172,20 @@ public String[] getAllPosTags() {
165
172
return model .getOutcomes ();
166
173
}
167
174
175
+ /**
176
+ * {@inheritDoc}
177
+ */
168
178
@ Override
169
179
public String [] tag (String [] sentence ) {
170
180
return this .tag (sentence , null );
171
181
}
172
182
183
+ /**
184
+ * {@inheritDoc}
185
+ */
173
186
@ Override
174
187
public String [] tag (String [] sentence , Object [] additionalContext ) {
175
- bestSequence = model .bestSequence (sentence , additionalContext , contextGen , sequenceValidator );
188
+ bestSequence = model .bestSequence (sentence , additionalContext , cg , sequenceValidator );
176
189
final List <String > t = bestSequence .getOutcomes ();
177
190
return convertTags (t );
178
191
}
@@ -186,7 +199,7 @@ public String[] tag(String[] sentence, Object[] additionalContext) {
186
199
*/
187
200
public String [][] tag (int numTaggings , String [] sentence ) {
188
201
Sequence [] bestSequences = model .bestSequences (numTaggings , sentence , null ,
189
- contextGen , sequenceValidator );
202
+ cg , sequenceValidator );
190
203
String [][] tags = new String [bestSequences .length ][];
191
204
for (int si = 0 ; si < tags .length ; si ++) {
192
205
List <String > t = bestSequences [si ].getOutcomes ();
@@ -204,18 +217,25 @@ private String[] convertTags(List<String> t) {
204
217
}
205
218
}
206
219
220
+ /**
221
+ * {@inheritDoc}
222
+ */
207
223
@ Override
208
224
public Sequence [] topKSequences (String [] sentence ) {
209
225
return this .topKSequences (sentence , null );
210
226
}
211
227
228
+ /**
229
+ * {@inheritDoc}
230
+ */
212
231
@ Override
213
232
public Sequence [] topKSequences (String [] sentence , Object [] additionalContext ) {
214
- return model .bestSequences (size , sentence , additionalContext , contextGen , sequenceValidator );
233
+ return model .bestSequences (size , sentence , additionalContext , cg , sequenceValidator );
215
234
}
216
235
217
236
/**
218
- * Populates the specified array with the probabilities for each tag of the last tagged sentence.
237
+ * Populates the specified {@code probs} array with the probabilities
238
+ * for each tag of the last tagged sentence.
219
239
*
220
240
* @param probs An array to put the probabilities into.
221
241
*/
@@ -239,7 +259,7 @@ public String[] getOrderedTags(List<String> words, List<String> tags, int index,
239
259
MaxentModel posModel = modelPackage .getArtifact (POSModel .POS_MODEL_ENTRY_NAME );
240
260
if (posModel != null ) {
241
261
242
- double [] probs = posModel .eval (contextGen .getContext (index , words .toArray (new String [0 ]),
262
+ double [] probs = posModel .eval (cg .getContext (index , words .toArray (new String [0 ]),
243
263
tags .toArray (new String [0 ]), null ));
244
264
245
265
String [] orderedTags = new String [probs .length ];
@@ -263,34 +283,44 @@ public String[] getOrderedTags(List<String> words, List<String> tags, int index,
263
283
}
264
284
}
265
285
266
- public static POSModel train (String languageCode ,
267
- ObjectStream <POSSample > samples , TrainingParameters trainParams ,
268
- POSTaggerFactory posFactory ) throws IOException {
269
-
270
- int beamSize = trainParams .getIntParameter (BeamSearch .BEAM_SIZE_PARAMETER , POSTaggerME .DEFAULT_BEAM_SIZE );
271
-
272
- POSContextGenerator contextGenerator = posFactory .getPOSContextGenerator ();
286
+ /**
287
+ * Starts a training of a {@link POSModel} with the given parameters.
288
+ *
289
+ * @param languageCode The ISO language code to train the model. Must not be {@code null}.
290
+ * @param samples The {@link ObjectStream} of {@link POSSample} used as input for training.
291
+ * @param mlParams The {@link TrainingParameters} for the context of the training process.
292
+ * @param posFactory The {@link POSTaggerFactory} for creating related objects as defined
293
+ * via {@code mlParams}.
294
+ *
295
+ * @return A valid, trained {@link POSModel} instance.
296
+ * @throws IOException Thrown if IO errors occurred.
297
+ */
298
+ public static POSModel train (String languageCode , ObjectStream <POSSample > samples ,
299
+ TrainingParameters mlParams , POSTaggerFactory posFactory )
300
+ throws IOException {
273
301
274
- Map <String , String > manifestInfoEntries = new HashMap <>();
302
+ final int beamSize = mlParams .getIntParameter (
303
+ BeamSearch .BEAM_SIZE_PARAMETER , POSTaggerME .DEFAULT_BEAM_SIZE );
275
304
276
- TrainerType trainerType = TrainerFactory .getTrainerType (trainParams );
305
+ final POSContextGenerator contextGenerator = posFactory .getPOSContextGenerator ();
306
+ final TrainerType trainerType = TrainerFactory .getTrainerType (mlParams );
307
+ final Map <String , String > manifestInfoEntries = new HashMap <>();
277
308
278
309
MaxentModel posModel = null ;
279
310
SequenceClassificationModel seqPosModel = null ;
280
311
if (TrainerType .EVENT_MODEL_TRAINER .equals (trainerType )) {
281
312
ObjectStream <Event > es = new POSSampleEventStream (samples , contextGenerator );
282
313
283
- EventTrainer trainer = TrainerFactory .getEventTrainer (trainParams ,
284
- manifestInfoEntries );
314
+ EventTrainer trainer = TrainerFactory .getEventTrainer (mlParams , manifestInfoEntries );
285
315
posModel = trainer .train (es );
286
316
} else if (TrainerType .EVENT_MODEL_SEQUENCE_TRAINER .equals (trainerType )) {
287
317
POSSampleSequenceStream ss = new POSSampleSequenceStream (samples , contextGenerator );
288
318
EventModelSequenceTrainer <POSSample > trainer =
289
- TrainerFactory .getEventModelSequenceTrainer (trainParams , manifestInfoEntries );
319
+ TrainerFactory .getEventModelSequenceTrainer (mlParams , manifestInfoEntries );
290
320
posModel = trainer .train (ss );
291
321
} else if (TrainerType .SEQUENCE_TRAINER .equals (trainerType )) {
292
322
SequenceTrainer trainer = TrainerFactory .getSequenceModelTrainer (
293
- trainParams , manifestInfoEntries );
323
+ mlParams , manifestInfoEntries );
294
324
295
325
// TODO: This will probably cause issue, since the feature generator uses the outcomes array
296
326
0 commit comments