Пример #1
0
        /// <summary>
        /// Main method. Call as follows:
        /// <para>
        /// java ModelTrainer dataFile modelFile
        /// </para>
        /// </summary>
        public static void Main(string[] args)
        {
            int    ai     = 0;
            bool   real   = false;
            string type   = "maxent";
            int    maxit  = 100;
            int    cutoff = 1;
            double sigma  = 1.0;

            if (args.Length == 0)
            {
                usage();
            }
            while (args[ai].StartsWith("-", StringComparison.Ordinal))
            {
                if (args[ai].Equals("-real"))
                {
                    real = true;
                }
                else if (args[ai].Equals("-perceptron"))
                {
                    type = "perceptron";
                }
                else if (args[ai].Equals("-maxit"))
                {
                    maxit = Convert.ToInt32(args[++ai]);
                }
                else if (args[ai].Equals("-cutoff"))
                {
                    cutoff = Convert.ToInt32(args[++ai]);
                }
                else if (args[ai].Equals("-sigma"))
                {
                    sigma = Convert.ToDouble(args[++ai]);
                }
                else
                {
                    Console.Error.WriteLine("Unknown option: " + args[ai]);
                    usage();
                }
                ai++;
            }
            string dataFileName  = args[ai++];
            string modelFileName = args[ai];

            try
            {
                FileReader  datafr = new FileReader(new Jfile(dataFileName));
                EventStream es;
                if (!real)
                {
                    es = new BasicEventStream(new PlainTextByLineDataStream(datafr), ",");
                }
                else
                {
                    es = new RealBasicEventStream(new PlainTextByLineDataStream(datafr));
                }

                Jfile outputFile = new Jfile(modelFileName);

                AbstractModelWriter writer;

                AbstractModel model;
                if (type.Equals("maxent"))
                {
                    GIS.SMOOTHING_OBSERVATION = SMOOTHING_OBSERVATION;

                    if (!real)
                    {
                        model = GIS.trainModel(es, maxit, cutoff, sigma);
                    }
                    else
                    {
                        model = GIS.trainModel(maxit, new OnePassRealValueDataIndexer(es, cutoff), USE_SMOOTHING);
                    }

                    writer = new SuffixSensitiveGISModelWriter(model, outputFile);
                }
                else if (type.Equals("perceptron"))
                {
                    //System.err.println("Perceptron training");
                    model = (new PerceptronTrainer()).trainModel(maxit, new OnePassDataIndexer(es, cutoff), cutoff);

                    writer = new SuffixSensitivePerceptronModelWriter(model, outputFile);
                }
                else
                {
                    throw new Exception("Unknown model type: " + type);
                }

                writer.persist();
            }
            catch (Exception e)
            {
                Console.Write("Unable to create model due to exception: ");
                Console.WriteLine(e);
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
        }
Пример #2
0
 public static MaxentModel train(EventStream events, int cutoff)
 {
     return(GIS.trainModel(events, 100, cutoff));
 }