Esempio n. 1
0
        /// <summary>
        /// Trains a Part of Speech model with the given parameters.
        /// </summary>
        /// <param name="languageCode">The language code.</param>
        /// <param name="samples">The data samples.</param>
        /// <param name="parameters">The machine learnable parameters.</param>
        /// <param name="factory">The sentence detector factory.</param>
        /// <param name="monitor">
        /// A evaluation monitor that can be used to listen the messages during the training or it can cancel the training operation.
        /// This argument can be a <c>null</c> value.
        /// </param>
        /// <returns>The trained <see cref="POSModel"/> object.</returns>
        /// <exception cref="System.NotSupportedException">Trainer type is not supported.</exception>
        public static POSModel Train(string languageCode, IObjectStream <POSSample> samples, TrainingParameters parameters, POSTaggerFactory factory, Monitor monitor)
        {
            //int beamSize = trainParams.Get(Parameters.BeamSize, NameFinderME.DefaultBeamSize);

            var contextGenerator    = factory.GetPOSContextGenerator();
            var manifestInfoEntries = new Dictionary <string, string>();

            var trainerType = TrainerFactory.GetTrainerType(parameters);

            IMaxentModel posModel = null;

            ML.Model.ISequenceClassificationModel <string> seqPosModel = null;
            switch (trainerType)
            {
            case TrainerType.EventModelTrainer:
                var es      = new POSSampleEventStream(samples, contextGenerator);
                var trainer = TrainerFactory.GetEventTrainer(parameters, manifestInfoEntries, monitor);

                posModel = trainer.Train(es);
                break;

            case TrainerType.EventModelSequenceTrainer:
                var ss       = new POSSampleSequenceStream(samples, contextGenerator);
                var trainer2 = TrainerFactory.GetEventModelSequenceTrainer(parameters, manifestInfoEntries, monitor);

                posModel = trainer2.Train(ss);
                break;

            case TrainerType.SequenceTrainer:
                var trainer3 = TrainerFactory.GetSequenceModelTrainer(parameters, manifestInfoEntries, monitor);

                // TODO: This will probably cause issue, since the feature generator uses the outcomes array

                var ss2 = new POSSampleSequenceStream(samples, contextGenerator);
                seqPosModel = trainer3.Train(ss2);
                break;

            default:
                throw new NotSupportedException("Trainer type is not supported.");
            }

            if (posModel != null)
            {
                return(new POSModel(languageCode, posModel, manifestInfoEntries, factory));
            }

            return(new POSModel(languageCode, seqPosModel, manifestInfoEntries, factory));
        }
Esempio n. 2
0
        /// <summary>
        /// Trains a Part of Speech model with the given parameters.
        /// </summary>
        /// <param name="languageCode">The language code.</param>
        /// <param name="samples">The data samples.</param>
        /// <param name="parameters">The machine learnable parameters.</param>
        /// <param name="factory">The sentence detector factory.</param>
        /// <param name="monitor">
        /// A evaluation monitor that can be used to listen the messages during the training or it can cancel the training operation.
        /// This argument can be a <c>null</c> value.
        /// </param>
        /// <returns>The trained <see cref="POSModel"/> object.</returns>
        /// <exception cref="System.NotSupportedException">Trainer type is not supported.</exception>
        public static POSModel Train(string languageCode, IObjectStream<POSSample> samples, TrainingParameters parameters, POSTaggerFactory factory, Monitor monitor) {

            //int beamSize = trainParams.Get(Parameters.BeamSize, NameFinderME.DefaultBeamSize);

            var contextGenerator = factory.GetPOSContextGenerator();
            var manifestInfoEntries = new Dictionary<string, string>();

            var trainerType = TrainerFactory.GetTrainerType(parameters);


            switch (trainerType) {
                case TrainerType.EventModelTrainer:
                    var es = new POSSampleEventStream(samples, contextGenerator);
                    var trainer = TrainerFactory.GetEventTrainer(parameters, manifestInfoEntries, monitor);

                    var eventModel = trainer.Train(es);

                    return new POSModel(languageCode, eventModel, manifestInfoEntries, factory);

                case TrainerType.EventModelSequenceTrainer:
                    var ss = new POSSampleSequenceStream(samples, contextGenerator);
                    var trainer2 = TrainerFactory.GetEventModelSequenceTrainer(parameters, manifestInfoEntries, monitor);

                    var seqModel = trainer2.Train(ss);

                    return new POSModel(languageCode, seqModel, manifestInfoEntries, factory);

                case TrainerType.SequenceTrainer:
                    var trainer3 = TrainerFactory.GetSequenceModelTrainer(parameters, manifestInfoEntries, monitor);

                    // TODO: This will probably cause issue, since the feature generator uses the outcomes array

                    var ss2 = new POSSampleSequenceStream(samples, contextGenerator);
                    var seqPosModel = trainer3.Train(ss2);

                    return new POSModel(languageCode, seqPosModel, manifestInfoEntries, factory);
                default:
                    throw new NotSupportedException("Trainer type is not supported.");
            }
           

        }