/// <summary> /// Loads into memory the default models for language detection /// </summary> /// <returns></returns> public Return<object> LoadDefaultModels() { Return<object> _answer = new Return<object>(); try { if (defaultLanguageModels == null) {//we don't have default models, we need to load it WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelCastilianOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelCatalanOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelEnglishOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelDutchOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelFrenchOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelGermanOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelItalianOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelPortugueseOrdered = null; WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> _modelLatinOrdered = null; if (loadCastilianModel) {//castilian WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetCastilianModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelCastilianOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelCastilianOrdered.Item2.Add(m.combinedString, m)); }//good model }//castilian if (!_answer.theresError && loadCatalanModel) {//catalan WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetCatalanModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelCatalanOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelCatalanOrdered.Item2.Add(m.combinedString, m)); }//good model }//catalan if (!_answer.theresError && loadEnglishModel) {//english WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetEnglishModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelEnglishOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelEnglishOrdered.Item2.Add(m.combinedString, m)); }//good model }//english if (!_answer.theresError && loadDutchModel) {//dutch WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetDutchModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelDutchOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelDutchOrdered.Item2.Add(m.combinedString, m)); }//good model }//dutch if (!_answer.theresError && loadFrenchModel) {//french WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetFrenchModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelFrenchOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelFrenchOrdered.Item2.Add(m.combinedString, m)); }//good model }//french if (!_answer.theresError && loadGermanModel) {//german WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetGermanModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelGermanOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelGermanOrdered.Item2.Add(m.combinedString, m)); }//good model }//german if (!_answer.theresError && loadItalianModel) {//italian WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetItalianModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelItalianOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelItalianOrdered.Item2.Add(m.combinedString, m)); }//good model }//italian if (!_answer.theresError && loadPortugueseModel) {//portuguese WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetPortugueseModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelPortugueseOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelPortugueseOrdered.Item2.Add(m.combinedString, m)); }//good model }//portuguese if (!_answer.theresError && loadLatinModel) {//latin WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _model = GetLatinModel(); Return<string> _answerIsAValidModel = IsAValidModel(_model.Item2); if (_answerIsAValidModel.theresError) { _answer.theresError = true; _answer.error = _answerIsAValidModel.error; } else if (!string.IsNullOrWhiteSpace(_answerIsAValidModel.data)) { _answer.theresError = true; _answer.error = Utility.GetError(new Exception(_answerIsAValidModel.data), this.GetType()); } else {//good model int _sizeInitialString = _model.Item2.First().initialString.Length; int _sizeFinalString = _model.Item2.Where(m => !string.IsNullOrEmpty(m.finalString)).First().finalString.Length; _modelLatinOrdered = new WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int> (_model.Item1, new SafeSortedList<string, LanguageIdentificationModelWrapper>(), _sizeInitialString, _sizeFinalString); _model.Item2.ForEach(m => _modelLatinOrdered.Item2.Add(m.combinedString, m)); }//good model }//latin if (!_answer.theresError) { defaultLanguageModels = new List<WritableTuple<EnumLanguage, SafeSortedList<string, LanguageIdentificationModelWrapper>, int, int>>(); if (loadCastilianModel) defaultLanguageModels.Add(_modelCastilianOrdered); if (loadCatalanModel) defaultLanguageModels.Add(_modelCatalanOrdered); if (loadEnglishModel) defaultLanguageModels.Add(_modelEnglishOrdered); if (loadDutchModel) defaultLanguageModels.Add(_modelDutchOrdered); if (loadFrenchModel) defaultLanguageModels.Add(_modelFrenchOrdered); if (loadGermanModel) defaultLanguageModels.Add(_modelGermanOrdered); if (loadItalianModel) defaultLanguageModels.Add(_modelItalianOrdered); if (loadPortugueseModel) defaultLanguageModels.Add(_modelPortugueseOrdered); if (loadLatinModel) defaultLanguageModels.Add(_modelLatinOrdered); } }//we don't have default models, we need to load it } catch (Exception _ex) { _answer.theresError = true; _answer.error = Utility.GetError(_ex, this.GetType()); } return _answer; }
private WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> GetCastilianModel() { WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>> _answer = null; List<string> _resourceFileNames = new List<string>() { "LanguageIdentificationModelMarkovCastilian_ab_abc__2.1.0.1_0000000000" //"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000000" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000001" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000002" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000003" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000004" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000005" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000006" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000007" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000008" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000009" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000010" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000011" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000012" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000013" //,"LanguageIdentificationModelMarkovCastilian_abc_abcd__2.1.0.1_0000000014" }; string _resourceDirectoryPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"Resources"); List<LanguageIdentificationModelWrapper> _wrappers = new List<LanguageIdentificationModelWrapper>(); foreach (string _resourceFileName in _resourceFileNames) {//file to file string _fileName = string.Format("{0}.resources", _resourceFileName) , _filePath = Path.Combine(_resourceDirectoryPath, _fileName); if (!File.Exists(_filePath)) { string _message = string.Format("Cannot find the file{0}{1}", Environment.NewLine, _filePath); throw new Exception(_message); } else { ResourceManager _resourceManager = System.Resources.ResourceManager.CreateFileBasedResourceManager(_resourceFileName, _resourceDirectoryPath, null); List<LanguageIdentificationModelWrapper> _list = (List<LanguageIdentificationModelWrapper>)_resourceManager.GetObject(MLConstants.PREFIX_LANGUAGE_CASTILIAN); _wrappers.AddRange(_list); } }//file to file _answer = new WritableTuple<EnumLanguage, List<LanguageIdentificationModelWrapper>>(EnumLanguage.castilian, _wrappers); return _answer; }
private Return<object> CalculateProbabilitiesChangeStateDirtyHands(ProbabilityChangeStateWrapper wrapper) { Return<object> _answer = new Return<object>(); if (wrapper == null) { _answer.theresError = true; _answer.error = Utility.GetError(new ArgumentNullException("wrapper"), this.GetType()); } else { try { foreach (LanguageIdentificationModelWrapper _fragment in wrapper.fragmentsOfModel) { WritableTuple<string, int> _combinedTuple = null; if (!wrapper.countCombinedStrings.ContainsKey(_fragment.combinedString)) throw new Exception(string.Format("Was impossible to find a combination for {0}", _fragment.combinedString)); else _combinedTuple = new WritableTuple<string, int>(_fragment.combinedString, wrapper.countCombinedStrings[_fragment.combinedString]); WritableTuple<string, int> _initialTuple = null; if (!wrapper.countInitialStrings.ContainsKey(_fragment.initialString)) throw new Exception(string.Format("Was impossible to find a tuple for {0}", _fragment.initialString)); else _initialTuple = new WritableTuple<string, int>(_fragment.initialString, wrapper.countInitialStrings[_fragment.initialString]); int _numberOfOccurrencesOfCombination = _combinedTuple.Item2, _numberOfOccurrencesOfInitialString = _initialTuple.Item2; decimal _Pabc_abcd = SOFT_PROBABILITY_0; if (_numberOfOccurrencesOfCombination != 0 && _numberOfOccurrencesOfInitialString != 0) _Pabc_abcd = (decimal)_numberOfOccurrencesOfCombination / (decimal)_numberOfOccurrencesOfInitialString; _fragment.probability = _Pabc_abcd; } } catch (Exception _ex) { _answer.theresError = true; _answer.error = Utility.GetError(_ex, this.GetType()); } } return _answer; }