public async Task ImportPhrasesAsync()
        {
            await Task.Run(() =>
            {
                using (var sr = new StringReader(Resources.Hsk6))
                    using (var csv = new CsvReader(sr))
                    {
                        while (csv.Read())
                        {
                            string chineseWord       = GetColumn(csv, "Word");
                            string pinyin            = GetColumn(csv, "Pronunciation");
                            string englishDefinition = GetColumn(csv, "Definition");
                            int hskLevel             = ParseHskLevel(csv);

                            string[] splitEnglish = englishDefinition.Split(new[] { "CL:" }, StringSplitOptions.None);
                            string english        = splitEnglish[0].Trim();
                            var phrase            = new HskPhrase {
                                Hanzi = chineseWord, Pinyin = pinyin, English = english, HskLevel = hskLevel
                            };

                            List <MeasureWord> measureWords = FindMeasureWords(splitEnglish).ToList();

                            using (var context = new LanguageLearningModel())
                            {
                                phrase.Tags         = new List <Tag>();
                                phrase.MeasureWords = new List <MeasureWord>();

                                Log.DebugFormat("Adding phrase [{0}]", phrase.Pinyin);

                                AddPhrase(context, phrase);

                                var tag = new Tag {
                                    Name = $"HSK{hskLevel}"
                                };

                                AddElementToSet(context, tag, phrase.Tags, x => x.Name == tag.Name);

                                foreach (MeasureWord measureWord in measureWords)
                                {
                                    AddElementToSet(context, measureWord, phrase.MeasureWords, mw => mw.Hanzi == measureWord.Hanzi);

                                    context.SaveChanges();
                                }

                                context.SaveChanges();
                            }
                        }
                    }
            });
        }
Esempio n. 2
0
        public static void AddSentence(string english, string pinyin, string chinese, List <Phrase> relatedPhrases, List <MeasureWord> relatedMeasureWords)
        {
            var sentence = new Sentence
            {
                Pinyin              = pinyin,
                English             = english,
                Hanzi               = chinese,
                RelatedPhrases      = relatedPhrases,
                RelatedMeasureWords = relatedMeasureWords
            };

            using (var context = new LanguageLearningModel())
            {
                // Phrases and measure words must already exist.
                foreach (Phrase relatedPhrase in relatedPhrases)
                {
                    context.Phrases.Attach(relatedPhrase);
                }
                foreach (MeasureWord relatedMeasureWord in relatedMeasureWords)
                {
                    context.MeasureWords.Attach(relatedMeasureWord);
                }

                Log.InfoFormat("Adding sentence [{0}] with {1} phrases and {2} measure words", sentence.Pinyin, relatedPhrases.Count, relatedMeasureWords.Count);

                context.Sentences.Add(sentence);
                context.SaveChanges();
                Log.InfoFormat("Sentence [{0}] added", sentence.Pinyin);
            }
        }