public async Task <IEnumerable <string> > Audios([FromBody] TextBody textBody) { var tokenizer = new TextTokenizer(); var tokens = tokenizer.GetWords(textBody.Input); var audios = new List <string>(); foreach (var token in tokens) { var composition = new Composition { Return = new ContentSegment { Url = $"https://forvo.com/word/{token.Value.RemoveAccents()}/#ru", Select = "span.play" } }; var elements = await composition.Return.DocumentElement(); foreach (var element in elements) { var onclick = element.GetAttribute("onclick"); var onclickParts = onclick.Split(','); if (onclickParts.Count() >= 5) { audios.Add(Encoding.UTF8.GetString(Convert.FromBase64String(onclickParts[4].Trim(new[] { '\'', '"' })))); } if (audios.Count >= 5) { break; } } } return(audios); }
public async Task <Translation> Translation([FromBody] TextBody textBody) { var translation = new Translation(); using (var service = new TranslateService(new BaseClientService.Initializer { ApiKey = "AIzaSyBiPTuvLggID2YrmBshuHBZhij6HeFOxko", ApplicationName = "Project Name" })) { var input = textBody.Input; if (input.StartsWith("en:")) { var russianResponse = await service.Translations.List(new string[] { input.Replace("en:", "") }, "ru").ExecuteAsync(); input = russianResponse.Translations[0].TranslatedText; } var tokenizer = new TextTokenizer(); var blocks = tokenizer.GetBlocks(input); var blockResponse = await service.Translations.List(blocks.Select(t => t.OriginalText).ToArray(), "en").ExecuteAsync(); for (int i = 0; i < blockResponse.Translations.Count; i++) { blocks[i].TranslatedText = blockResponse.Translations[i].TranslatedText; var words = tokenizer.GetWords(blocks[i].OriginalText); var wordResponse = await service.Translations.List(words.Select(t => t.Value).ToArray(), "en").ExecuteAsync(); for (int j = 0; j < wordResponse.Translations.Count; j++) { words[j].Translation = wordResponse.Translations[j].TranslatedText; blocks[i].Words.Add(words[j]); } translation.Blocks.Add(blocks[i]); } return(translation); } }