public void TestSerializationModel()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                var trainer = new QNTrainer();

                var model = trainer.TrainModel(Iterations, di);

                Assert.NotNull(model);

                QNModel deserialized;

                using (var mem = new MemoryStream()) {
                    using (var modelWriter = new GenericModelWriter(model, new UnclosableStream(mem))) {
                        modelWriter.Persist();
                        modelWriter.Close();
                    }

                    mem.Flush();
                    mem.Seek(0, SeekOrigin.Begin);


                    using (var modelReader = new GenericModelReader(new BinaryFileDataReader(mem)))
                        deserialized = modelReader.GetModel() as QNModel;
                }
                Assert.NotNull(deserialized);

                Assert.True(model.Equals(deserialized));

                var features2Classify = new [] {
                    "feature2", "feature3", "feature3",
                    "feature3", "feature3", "feature3",
                    "feature3", "feature3", "feature3",
                    "feature3", "feature3", "feature3"
                };

                var eval01 = model.Eval(features2Classify);
                var eval02 = deserialized.Eval(features2Classify);

                Assert.AreEqual(eval01.Length, eval02.Length);
                for (var i = 0; i < eval01.Length; i++)
                {
                    Assert.AreEqual(eval01[i], eval02[i], 0.00000001);
                }
            }
        }
Exemple #2
0
        public static void WriteModel(AbstractModel model, Stream outStream)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (outStream == null)
            {
                throw new ArgumentNullException(nameof(outStream));
            }

            if (!outStream.CanWrite)
            {
                throw new ArgumentException("Stream was not writeable.");
            }

            var writer = new GenericModelWriter(model, outStream);

            writer.Persist();
        }