public async Task<TranslateRequestResult> Translate(string sourceString) { TranslateRequestResult RequestResult = new TranslateRequestResult(sourceString); SourceExpressionManager sourceManager = new SourceExpressionManager(db); List<SourceExpression> sourceList = sourceManager.GetSourceExpressionCollection(sourceString, direction).ToList(); if (sourceList.Count > 0) { SourceDefinitionManager defManager = new SourceDefinitionManager(db); List<SourceDefinition> definitionsList = defManager.GetDefinitionCollection(sourceList[0].ID); TranslatedExpressionManager translatedManager = new TranslatedExpressionManager(db); var translatedList = translatedManager.GetListOfCoupleTranslatedExpressionAndFavorite(definitionsList); RequestResult.SetTranslateResult(createTranslateResult(sourceString, sourceList, definitionsList, translatedList)); } return RequestResult; }
private async Task<TranslateRequestResult> request(IRequestTranslateString service, string originalText) { string convertedSourceText = ConvertStrings.StringToOneLowerLineWithTrim(originalText); TranslateRequestResult result = new TranslateRequestResult(convertedSourceText); result = translaterFromCache.Translate(originalText).Result; if (result.TranslatedData.Definitions.Count == 0) { result = await service.Translate(originalText); /*if (!string.IsNullOrEmpty(result.errorDescription)) { //ToDo: сделать общий обработчик ошибок throw new Exception("Ошибка подключения к интернет:" + result.errorDescription); }*/ } return result; }
//ToDo:Нужен общий обработчик ошибок //ToDo:Тест! public void SaveResultToLocalCacheIfNotExist(TranslateRequestResult result) { if (string.IsNullOrEmpty(result.errorDescription)) { int sourceItemID = 0; string originalText = result.OriginalText; TranslateResultView resultView = result.TranslatedData; IEnumerable<SourceExpression> localCacheDataList = sourceExpressionManager.GetSourceExpressionCollection(originalText, direction); if (localCacheDataList.Count() == 0) { sourceItemID = writeSourceExpression(originalText, ref localCacheDataList); writeTranslatedExpression(sourceItemID, resultView); } } else throw new Exception(result.errorDescription); }
public async Task<TranslateRequestResult> Translate(string sourceString) { TranslateRequestResult RequestResult = new TranslateRequestResult(sourceString); string responseText = string.Empty; try { translater.SetSourceString(sourceString); responseText = await translater.GetResponse(string.Format("{0}-{1}", direction.LanguageFrom.NameShort, direction.LanguageTo.NameShort)); } catch (Exception e) { RequestResult.errorDescription = e.Message; } if(string.IsNullOrEmpty(RequestResult.errorDescription)) RequestResult.SetTranslateResult(translater.Parse(responseText)); return RequestResult; }
private void saveResultToLocalCache(TranslateRequestResult result, TranslateDirection direction) { CachedResultWriter localDBWriter = new CachedResultWriter(direction, db, new SourceExpressionManager(db)); localDBWriter.SaveResultToLocalCacheIfNotExist(result); }
private void createDBItemsFromResponse(TranslateRequestResult reqResult, IChatHistoryManager chatHistoryManager, ChatHistory defaultRobotItem, string delimiter) { var robotItem = defaultRobotItem; foreach (var definition in reqResult.TranslatedData.Definitions) { if (!string.IsNullOrEmpty(definition.Transcription)) robotItem.Transcription = string.Format("[{0}]", definition.Transcription); if (definition.Pos != DefinitionTypesEnum.translater) robotItem.Definition = definition.Pos.ToString(); StringBuilder builderTextFrom = new StringBuilder(); foreach (var textVariant in definition.TranslateVariants) { builderTextFrom.Append(textVariant.Text); builderTextFrom.Append(delimiter); } robotItem.TextTo = builderTextFrom.ToString().Remove(builderTextFrom.Length - delimiter.Length, delimiter.Length); robotItem.ChatID = defaultRobotItem.ChatID; robotItem.LanguageFrom = defaultRobotItem.LanguageFrom; robotItem.LanguageTo = defaultRobotItem.LanguageTo; var result = chatHistoryManager.SaveItem(robotItem); robotItem = new ChatHistory(); } }
private void addToDBRobotResponse(TranslateRequestResult reqResult) { ChatHistory defaultRobotItem = chatHistoryManager.GetLastRobotMessage(); defaultRobotItem.UpdateDate = DateTime.Now; defaultRobotItem.TextFrom = string.Empty; string delimiter = ", "; createDBItemsFromResponse(reqResult, chatHistoryManager, defaultRobotItem, delimiter); increaseChatUpdateDate(defaultRobotItem.ChatID); }
void updateListResults(TranslateRequestResult requestResult) { TranslateResultView resultView = requestResult.TranslatedData; if (resultView.Definitions.Count > 0) { //TextView splash = FindViewById<TextView>(Resource.Id.splashTextView); //splash.Visibility = ViewStates.Invisible; var scrollViewTranslateResultsDefs = FindViewById<ScrollView>(Resource.Id.scrollViewTranslateResultsDefs); scrollViewTranslateResultsDefs.RemoveAllViews(); scrollViewTranslateResultsDefs.AddView(new DynamicResultViewLayout(this, resultView.Definitions)); scrollViewTranslateResultsDefs.Visibility = ViewStates.Visible; } else { Toast.MakeText(this, Resource.String.msg_unknown_expression, ToastLength.Long).Show(); } }