void AddButtons()
        {
            int NumberOfAnalogue = 2;

            PhraseOfCollectMode CurrentPhrase = new PhraseOfCollectMode(LearningItem.CurrentSub);

            // finds analogue
            int WordsNumber = CurrentPhrase.Words.Count;
            List<PhraseOfCollectMode> analogue = LearningItem.MainSubs.PhrasesList.FindAll(x => x.Words.Count == WordsNumber);
            if (analogue.Count < (NumberOfAnalogue + 10))
            {
                analogue = LearningItem.MainSubs.PhrasesList.FindAll(
                    x => ((x.Words.Count >= WordsNumber - 1) && (x.Words.Count <= WordsNumber + 1)));
                    if (analogue.Count < (NumberOfAnalogue + 10))
                    {
                        analogue = LearningItem.MainSubs.PhrasesList;
                    }
             }

            // a list of phrases 
            Random rnd = new Random();
            Dictionary<int, PhraseOfCollectMode> PhrasesForShow = new Dictionary<int, PhraseOfCollectMode>();
            PhrasesForShow.Add(rnd.Next(0, 32000), CurrentPhrase);

            for (int i = 0; i < 100; i++)
            {
                if (PhrasesForShow.Count == AppSetting.NumberOfPrasesForUndestandingTest)  break;

                PhraseOfCollectMode rndphrase = analogue[rnd.Next(0, analogue.Count - 1)];

                if (string.IsNullOrEmpty(rndphrase.SubtitleItem.Text2)) continue;

                // Find duplicate
                bool IsThisDuplicate = false;
                foreach(var phr_ in PhrasesForShow)
                {
                    if (PhraseOfCollectMode.IsPhraseEqualent(phr_.Value,rndphrase))
                    {
                        IsThisDuplicate = true;
                        break;
                    }
                }
                if (IsThisDuplicate) continue;
                //var tlist = PhrasesForShow.Where(x => PhraseOfCollectMode.IsPhraseEqualent(x.Value, rndphrase));
                //if (tlist.Count() > 0) continue;

                PhrasesForShow.Add(rnd.Next(0, 32000), rndphrase);
            }

            if(PhrasesForShow.Count < AppSetting.NumberOfPrasesForUndestandingTest)
            {
                DialogService.Message(Loc.T("UnderstandingTest.Messages.NotEnoughPhrases"),Loc.T("Common.Titles.Error"));
                return;
            }

            // Add to panel
            ButtonsCollection.Clear();
            foreach (var elm in PhrasesForShow.OrderBy(x=>x.Key).ToList())
            {
                ButtonsCollection.Add(new ButtonModel() {Text = elm.Value.SubtitleItem.Text2,IsItTrue = elm.Value == CurrentPhrase});
            }
        }
Esempio n. 2
0
        public static FrequencyDictionary LoadRussianFrequencyDictionary()
        {

            string url = "http://dict.ruslang.ru/";
            FrequencyDictionary fd =  EFDbContext.DataBase.Table<FrequencyDictionary>().FirstOrDefault(x=>x.Url == url);
            if (fd == null)
            {
                fd.Url = url;
            }

            if (fd.Items.Any())
            {
                fd.Items.Clear();
                EFDbContext.SaveChgs();
            }

            Dictionary<string, SpeechParts> dict = new Dictionary<string, SpeechParts>();
            dict.Add("s",SpeechParts.Noun);
            dict.Add("spro",SpeechParts.Pronoun);
            dict.Add("v",SpeechParts.Verb);
            dict.Add("a",SpeechParts.Adjective);

            dict.Add("pr",null);// SpeechParts.Preposition
            dict.Add("adv",null);// SpeechParts.Adverb
            dict.Add("conj",null);// SpeechParts.Conjuction
            dict.Add("intj",null);// SpeechParts.Interjection
            dict.Add("num",null);//SpeechParts.Numeral
            dict.Add("part",null);//SpeechParts.Part
            dict.Add("s.prop",null);// SpeechParts.NounProp
            dict.Add("advpro", null);
            dict.Add("anum", null);
            dict.Add("apro", null);

            using (TextReader reader = File.OpenText("Resources/russian_freq_dict.txt"))
            {
                // header
                string line = reader.ReadLine();
                int number = 1;
                while (true)
                {
                    line = reader.ReadLine();
                    if (line == null) break;

                    string[] res = line.Split('\t');

                    FrequencyDictionary.Item item = new FrequencyDictionary.Item();
                    item.Lemma = res[0];

                    item.freq = float.Parse(res[2]);
                    if (item.freq < 40) continue;

                    item.speechpart = dict[res[1].ToLower()];
                    if (item.speechpart == null) continue;

                    item.Number = number++;    

                    fd.Items.Add(item);
                    //fd.Items.Add(item);
                }


                fd.Count = fd.Items.Count();
                EFDbContext.SaveChgs();

                return fd;

                // блок нужно переработать кривое использование id
                /*
                foreach(var grp in items.GroupBy(x => x.speechpart))
                {
                    FrequencyDictionary.RangeOfSpeechParts range = new FrequencyDictionary.RangeOfSpeechParts();
                    range.speechpart = grp.Key;
                    range.Count = grp.Count();
                    fd.Range.Add(range);

                    FrequencyDictionary.Item first = null;

                    var list2 = grp.OrderByDescending(x => x.freq);
                    foreach(var elm in list2)
                    {
                        fd.Items.Add(elm);
                        if (first == null) first = elm;
                    }
                    DataBase.SaveChanges();
                    range.StartId = first.id;
                    if (fd.StartId == 0) fd.StartId = first.id;
                }
                */

                //var lst = items.OrderBy(x => x.speechpart).Select(x=>x.speechpart);
                /*
                int id_ = 1;
                SpeechParts CurSpeechPart = null;
                foreach(var elm in lst)
                {
                    if()
                }
                */
            }
        }