Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new event array based on the outcomes predicted by the specified parameters for the specified sequence.
        /// </summary>
        /// <param name="sequence">The sequence to be evaluated.</param>
        /// <param name="model">The model.</param>
        /// <returns>The event array.</returns>
        public Event[] UpdateContext(Sequence sequence, AbstractModel model)
        {
            var tagger = new POSTaggerME(new POSModel("x-unspecified", model, null, new POSTaggerFactory()));
            var sample = sequence.GetSource <POSSample>();
            var tags   = tagger.Tag(sample.Sentence);

            return(POSSampleEventStream.GenerateEvents(
                       sample.Sentence,
                       tags,
                       Array.ConvertAll(sample.AdditionalContext, input => (object)input),
                       contextGenerator).ToArray());
        }
Ejemplo 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);

            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));
        }