private Return<List<LanguageIdentificationModelWrapper>> CalculateProbabilitiesInitialState(int countSubprocesses, List<string> sentences, List<string> initialStrings, List<WritableTuple<string, int>> countBeginningOfSentence) { Return<List<LanguageIdentificationModelWrapper>> _answer = new Return<List<LanguageIdentificationModelWrapper>>() { data = new List<LanguageIdentificationModelWrapper>() }; { try { int _countSentences = sentences.Count; List<List<string>> _partitions = initialStrings.PartitionAccordingSizeOfPartitions(SIZE_PARTITION_STRINGS); List<ProbabilityInitialStateWrapper> _wrappers = _partitions.Select(p => new ProbabilityInitialStateWrapper() { countSentences = _countSentences , countBeginningOfSentence = countBeginningOfSentence , fragmentsOfModel = p.Select(pp => new LanguageIdentificationModelWrapper(pp, string.Empty, 1)).ToList() } ).ToList(); int _countSubprocesses = _wrappers.Count < countSubprocesses ? _wrappers.Count : countSubprocesses; errorsMultithreadedProcess.Clear(); Return<object> _answerProcess = new Return<object>(); using (Processor<ProbabilityInitialStateWrapper> _processor = new Processor<ProbabilityInitialStateWrapper>(_countSubprocesses, PAUSE_MILISECONDS_PROCESSOR)) { _processor.ErrorProcessEntity += processorCalculateProbabilitiesInitialState_ErrorProcessEntity; _processor.Progress += processorCalculateProbabilitiesInitialState_Progress; _answerProcess = _countSubprocesses == 1 ? _processor.ProcessSynchronously(_wrappers, CalculateProbabilitiesInitialStateDirtyHands) : _processor.Process(_wrappers, CalculateProbabilitiesInitialStateDirtyHands); _processor.Progress -= processorCalculateProbabilitiesInitialState_Progress; _processor.ErrorProcessEntity -= processorCalculateProbabilitiesInitialState_ErrorProcessEntity; } if (_answerProcess.theresError) { _answer.theresError = true; _answer.error = _answerProcess.error; } else if (errorsMultithreadedProcess.Any()) { _answer.theresError = true; _answer.error = errorsMultithreadedProcess.First().Item2; } else _answer.data = _wrappers.SelectMany(c => c.fragmentsOfModel).ToList(); } catch (Exception _ex) { _answer.theresError = true; _answer.error = Utility.GetError(_ex, this.GetType()); } } return _answer; }
/// <summary> /// Performs a count of occurrences of a string into a beginning of a sentence /// </summary> /// <param name="countSubprocesses"></param> /// <param name="text">The sentences where to search into</param> /// <param name="strings">The strings to search</param> /// <returns>searched text, count of occurrences</returns> private Return<List<WritableTuple<string, int>>> CountBeginningOfSentence(int countSubprocesses, List<string> sentences, List<string> strings) { Return<List<WritableTuple<string, int>>> _answer = new Return<List<WritableTuple<string, int>>>() { data = new List<WritableTuple<string, int>>() }; { try { List<List<string>> _partitions = strings.PartitionAccordingSizeOfPartitions(SIZE_PARTITION_STRINGS); List<CountWrapper> _wrappers = _partitions.Select(p => new CountWrapper() { sentences = sentences , textToFind = p.Select(s => new WritableTuple<string, int>(s, 0)).ToList() } ).ToList(); int _countSubprocesses = _wrappers.Count < countSubprocesses ? _wrappers.Count : countSubprocesses; errorsMultithreadedProcess.Clear(); Return<object> _answerProcess = new Return<object>(); using (Processor<CountWrapper> _processor = new Processor<CountWrapper>(_countSubprocesses, PAUSE_MILISECONDS_PROCESSOR)) { _processor.ErrorProcessEntity += processorCountExistingStrings_ErrorProcessEntity; _processor.Progress += processorCountExistingStrings_Progress; _answerProcess = _countSubprocesses == 1 ? _processor.ProcessSynchronously(_wrappers, CountBeginningOfSentenceDirtyHands) : _processor.Process(_wrappers, CountBeginningOfSentenceDirtyHands); _processor.Progress -= processorCountExistingStrings_Progress; _processor.ErrorProcessEntity -= processorCountExistingStrings_ErrorProcessEntity; } if (_answerProcess.theresError) { _answer.theresError = true; _answer.error = _answerProcess.error; } else if (errorsMultithreadedProcess.Any()) { _answer.theresError = true; _answer.error = errorsMultithreadedProcess.First().Item2; } else { _answer.data = _wrappers.SelectMany(c => c.textToFind).ToList(); } } catch (Exception _ex) { _answer.theresError = true; _answer.error = Utility.GetError(_ex, this.GetType()); } } return _answer; }