コード例 #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
        /// <summary>
        /// Main method. Call as follows:
        /// <para>
        /// java ModelApplier modelFile dataFile
        /// </para>
        /// </summary>
        public static void Main(string[] args)
        {
            string dataFileName, modelFileName;
            bool   real = false;
            string type = "maxent";
            int    ai   = 0;

            if (args.Length == 0)
            {
                usage();
            }

            if (args.Length > 0)
            {
                while (args[ai].StartsWith("-", StringComparison.Ordinal))
                {
                    if (args[ai].Equals("-real"))
                    {
                        real = true;
                    }
                    else if (args[ai].Equals("-perceptron"))
                    {
                        type = "perceptron";
                    }
                    else
                    {
                        usage();
                    }
                    ai++;
                }

                modelFileName = args[ai++];
                dataFileName  = args[ai++];

                ModelApplier predictor = null;
                try
                {
                    MaxentModel m = (new GenericModelReader(new Jfile(modelFileName))).Model;
                    predictor = new ModelApplier(m);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    Environment.Exit(0);
                }

                try
                {
                    EventStream es =
                        new BasicEventStream(new PlainTextByLineDataStream(new FileReader(new Jfile(dataFileName))), ",");

                    while (es.hasNext())
                    {
                        predictor.eval(es.next(), real);
                    }

                    return;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unable to read from specified file: " + modelFileName);
                    Console.WriteLine();
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                }
            }
        }