public void When_GivenNonExistentModelPath_Then_ShouldThrowFileNotFoundException()
        {
            // Arrange
            var testTarget = new ModelReader(
                _testModelPath + "some nonexistant garbage");

            // Act
            testTarget.LoadModel();
        }
        public void When_GivenValidModelPath_Then_ShouldLoadModel()
        {
            // Arrange
            var testTarget          = new ModelReader(_testModelPath);
            int expectedFeatureSize = 470517; // figured out based on model metadata

            // Act
            testTarget.LoadModel();

            // Assert
            Assert.AreEqual(testTarget.feature_size(), expectedFeatureSize);
        }
        public void When_GivenValidModelLoader_Then_ShouldLoadModel()
        {
            // Arrange
            Func <string, Stream> fakeLoader = modelName =>
                                               new MemoryStream(File.ReadAllBytes(modelName));
            var testTarget          = new ModelReader(fakeLoader, _testModelPath);
            int expectedFeatureSize = 470517; // figured out based on model metadata

            // Act
            testTarget.LoadModel();

            // Assert
            Assert.AreEqual(testTarget.feature_size(), expectedFeatureSize);
        }
Beispiel #4
0
        //Build feature set into indexed data
        public bool BuildFeatureSetIntoIndex(string filename, double max_slot_usage_rate_threshold, int debugLevel)
        {
            IList <string> keyList;
            IList <int>    valList;

            featureLexicalDict.GenerateLexicalIdList(out keyList, out valList);

            if (debugLevel > 0)
            {
                var filename_featureset_raw_format = filename + ".feature.raw_text";
                var sw = new StreamWriter(filename_featureset_raw_format);
                // save feature and its id into lists in raw format
                for (var i = 0; i < keyList.Count; i++)
                {
                    sw.WriteLine("{0}\t{1}", keyList[i], valList[i]);
                }
                sw.Close();
            }

            //Build feature index
            var filename_featureset = filename + ".feature";
            var da = new CRFLite.Utils.DoubleArrayTrieBuilder(thread_num_);

            if (da.build(keyList, valList, max_slot_usage_rate_threshold) == false)
            {
                return(false);
            }
            //Save indexed feature set into file
            da.save(filename_featureset);

            if (string.IsNullOrWhiteSpace(modelFileName))
            {
                //Clean up all data
                featureLexicalDict.Clear();
                featureLexicalDict = null;
                keyList            = null;
                valList            = null;

                GC.Collect();

                //Create weight matrix
                alpha_ = new double[feature_size() + 1];
            }
            else
            {
                //Create weight matrix
                alpha_ = new double[feature_size() + 1];
                var modelReader = new ModelReader(this.modelFileName);
                modelReader.LoadModel();

                if (modelReader.y_.Count == y_.Count)
                {
                    for (var i = 0; i < keyList.Count; i++)
                    {
                        var index = modelReader.get_id(keyList[i]);
                        if (index < 0)
                        {
                            continue;
                        }
                        var size = (keyList[i][0] == 'U' ? y_.Count : y_.Count * y_.Count);
                        for (var j = 0; j < size; j++)
                        {
                            alpha_[valList[i] + j + 1] = modelReader.GetAlpha(index + j);
                        }
                    }
                }
                else
                {
                }

                //Clean up all data
                featureLexicalDict.Clear();
                featureLexicalDict = null;
                keyList            = null;
                valList            = null;

                GC.Collect();
            }

            return(true);
        }
Beispiel #5
0
 /// <summary>
 /// Loads an encoded model using the specified delegate.
 /// Using this overload you can read the model e.g.
 /// from network, zipped archives or other locations, as you wish.
 /// </summary>
 /// <param name="modelLoader">
 /// Allows reading the model from arbitrary formats and sources.
 /// </param>
 /// <param name="modelFilename">
 /// The model file name, as used by the given <paramref name="modelLoader"/>
 /// for file resolution.
 /// </param>
 /// <returns></returns>
 public void LoadModel(Func <string, Stream> modelLoader, string modelFilename)
 {
     this._modelReader = new ModelReader(modelLoader, modelFilename);
     _modelReader.LoadModel();
 }
Beispiel #6
0
 /// <summary>
 /// Load encoded model from file
 /// </summary>
 /// <param name="modelFilename">
 /// The model path.
 /// </param>
 /// <returns></returns>
 public void LoadModel(string modelFilename)
 {
     _modelReader = new ModelReader(modelFilename);
     _modelReader.LoadModel();
 }
Beispiel #7
0
 //Load encoded model form file
 public bool LoadModel(string strModelFileName)
 {
     modelReader = new ModelReader();
     return(modelReader.LoadModel(strModelFileName));
 }
Beispiel #8
0
 /// <summary>
 /// Load encoded model from file
 /// </summary>
 /// <param name="strModelFileName"></param>
 /// <returns></returns>
 public void LoadModel(string strModelFileName)
 {
     modelReader = new ModelReader();
     modelReader.LoadModel(strModelFileName);
 }