Ejemplo n.º 1
0
        private static bool TrainModel(KeywordPredictorFileMapping mapping)
        {
            IKeywordPredictor predictor = mapping.KeywordPredictorType.GetMethod("GetGlobalModel").Invoke(null, null) as IKeywordPredictor;
            var keywordData             = new FileSystemDataSource().LoadKeywordTrainingExamples();
            var X = KeywordPredictorTrainingUtils.GenerateKeywordTrainingData(keywordData);
            var Y = KeywordPredictorTrainingUtils.GenerateKeywordTargetData(keywordData);

            predictor.Train(X, Y);
            AnsEncoderStream stream;

            try
            {
                stream = new AnsEncoderStream(
                    new FileStream(mapping.DefaultFileLocation, FileMode.Create, FileAccess.Write),
                    1048576,
                    4096);
            } catch (IOException)
            {
                return(false);
            }
            using (stream)
            {
                bool res = predictor.Save(stream);
                stream.Flush();
                return(res);
            }
        }
Ejemplo n.º 2
0
        private bool RestoreKeywordPredictor()
        {
            var examplesIn = DataSource.LoadKeywordTrainingExamples();
            var X          = KeywordPredictorTrainingUtils.GenerateKeywordTrainingData(examplesIn);
            var Y          = KeywordPredictorTrainingUtils.GenerateKeywordTargetData(examplesIn);

            KeywordPredictor.Train(X, Y);
            try
            {
                AnsEncoderStream stream = new AnsEncoderStream(
                    new FileStream(DefaultModelFileLocations.NAIVE_BAYES_KEYWORD_PREDICTOR_FILE, FileMode.Create, FileAccess.Write),
                    1048576,
                    4096);
                KeywordPredictor.Save(stream);
                stream.Flush();
                stream.Close();
            } catch (IOException)
            {
                return(false);
            }
            return(true);
        }