public static Phrase GetRandomPhrase()
 {
     using (var context = new LanguageLearningModel())
     {
         return(context.Phrases.OrderBy(phrase => Guid.NewGuid()).First());
     }
 }
Example #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);
            }
        }
 private static void AddPhrase(LanguageLearningModel context, Phrase phrase)
 {
     if (!context.Phrases.Any(p => p.Hanzi == phrase.Hanzi))
     {
         context.Phrases.Add(phrase);
     }
     else
     {
         Log.WarnFormat("Phrase {0} already exists. Not adding.", phrase);
     }
 }
 public static async Task <IEnumerable <Phrase> > GetAllPhrasesFromLevelAsync(int hskLevel)
 {
     return(await Task.Run(() =>
     {
         using (var context = new LanguageLearningModel())
         {
             Log.DebugFormat("Loading all HSK Phrases found for level {0} and under.", hskLevel);
             List <HskPhrase> results = context.HskPhrases.Include(x => x.MeasureWords).Where(x => x.HskLevel <= hskLevel).ToList();
             Log.DebugFormat("Completed loading {0} HSK Phrases found for level {1} and under.", results.Count, hskLevel);
             return results;
         }
     }));
 }
 public static async Task <IEnumerable <Phrase> > GetAllPhrasesAsync()
 {
     return(await Task.Run(() =>
     {
         using (var context = new LanguageLearningModel())
         {
             Log.Debug("Loading all Phrases");
             List <Phrase> results = context.Phrases.ToList();
             Log.DebugFormat("Completed loading {0} Phrases.", results.Count);
             return results;
         }
     }));
 }
 public static async Task <Phrase> GetRandomHskPhraseByLevel(int hskLevel)
 {
     return(await Task.Run(() =>
     {
         using (var context = new LanguageLearningModel())
         {
             Log.DebugFormat("Loading random HSK Phrases found for level {0}.", hskLevel);
             HskPhrase randomHskPhrase = context.HskPhrases.Include(x => x.MeasureWords).OrderBy(phrase => Guid.NewGuid()).FirstOrDefault(x => x.HskLevel == hskLevel);
             Log.DebugFormat("Completed loading random HSK Phrases found for level {0}.", hskLevel);
             return randomHskPhrase;
         }
     }));
 }
 public static async Task <IEnumerable <Sentence> > GetAllSentencesAsync()
 {
     return(await Task.Run(() =>
     {
         using (var context = new LanguageLearningModel())
         {
             Log.Debug("Loading all Sentences");
             List <Sentence> results = context.Sentences.Include(x => x.RelatedMeasureWords).Include(x => x.RelatedPhrases).ToList();
             Log.DebugFormat("Completed loading {0} Sentences.", results.Count);
             return results;
         }
     }));
 }
        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();
                            }
                        }
                    }
            });
        }