Exemple #1
0
        protected async Task ProcessRecordAsync()
        {
            try
            {
                List <Word>          wordList = new List <Word>();
                IEnumerable <string> sourceWords;
                IWordLoader          wordLoader     = new WordLoader();
                IWordTranslator      wordTranslator = new WordTranslator();
                // Load word list from file
                WriteObject("Load word from the word file.");
                string fileType = _path.Substring(_path.LastIndexOf('.') + 1);
                if (fileType == "txt")
                {
                    sourceWords = await wordLoader.LoadWordFromFileAsync(_path);
                }
                else if (fileType == "xlsx")
                {
                    sourceWords = wordLoader.LoadWordFromExcelAsync(_path);
                }
                else
                {
                    throw new Exception($"{_path} is type invalid), Supported type includes xlsx, txt.");
                }

                var sortedSourceWord = from word in sourceWords
                                       orderby word ascending
                                       select word;
                // Translate word
                WriteObject("Translate word...");
                foreach (string word in sortedSourceWord)
                {
                    wordList.Add(await wordTranslator.TranslateAsync(word));
                }
                wordList.ForEach(item => item.LocalAudio = _storageFolder + $"\\audio\\" + item.EnglishLanguage + ".mp3");
                // Storage translated result to file.
                await wordLoader.StorageTranslatedWordAsync(wordList, Split, _storageFolder, Name);

                // Storage transfated result to excel file.
                if (IsExcel)
                {
                    await wordLoader.StorageTranslatedWordToExcelAsync(wordList, Split, _storageFolder + $@"\{Name}.xlsx", Name);
                }
                // Get andt storge mp3 file.

                if (IsAudio)
                {
                    WriteObject("Download mp3 file from web site.");
                    foreach (var item in wordList)
                    {
                        await wordLoader.GetWordMp3FileAsync(item);
                    }
                }
                WriteObject("Translated end");
            }
            catch
            {
                throw;
            }
        }
Exemple #2
0
        static async Task Main(string[] args)
        {
            //string subFolder = "level4-05";
            IWordLoader          wordLoader      = new WordLoader();
            IWordTranslator      wordTranslator  = new WordTranslator();
            List <Word>          wordList        = new List <Word>();
            string               storageFolder   = @"D:\Lucas\EnglishVocabulary\level4";
            IEnumerable <string> engWordTemplate = wordLoader.LoadWordFromExcelAsync(@"C:\Users\v-diya\Desktop\EnglishLevel4.xlsx");//(await wordLoader.LoadWordFromFileAsync(@$"D:\Lucas\git\EnglishVocabulary\resources\Level4Vocabulary\{subFolder}.txt")).ToList();

            var engWord = from word in engWordTemplate
                          orderby word ascending
                          select word;


            //int skipDebug = 0;
            foreach (string word in engWord)
            {
                //if (skipDebug > 57) break;
                wordList.Add(await wordTranslator.TranslateAsync(word));
                //skipDebug++;
            }

            wordList.ForEach(item => item.LocalAudio = storageFolder + $"\\audio\\" + item.EnglishLanguage + ".mp3");

            await wordLoader.StorageTranslatedWordAsync(wordList, 100, storageFolder, "level4");

            await wordLoader.StorageTranslatedWordToExcelAsync(wordList, 100, storageFolder + @"\level4.xlsx", "level4");

            try
            {
                foreach (var item in wordList)
                {
                    await wordLoader.GetWordMp3FileAsync(item);
                }
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }