Exemple #1
0
        public void AddMutualPhrasesToVocab(int userId, int maxCount)
        {
            var allWords            = _usersWordsService.GetAllWords(userId).Select(s => s.ToLower().Trim()).ToHashSet();
            var allWordsForLearning = _usersWordsService.GetAllUserWordsForLearning(userId);

            List <int> allPhrasesIdForUser = new List <int>();

            foreach (var word in allWordsForLearning)
            {
                var phrases = word.GetPhrasesId();
                allPhrasesIdForUser.AddRange(phrases);
            }

            var allPhrases = _dictionaryService.FindSeverealPrasesById(allPhrasesIdForUser.ToArray());

            List <Phrase> searchedPhrases = new List <Phrase>();
            int           endings         = 0;

            foreach (var phrase in allPhrases)
            {
                var phraseText  = phrase.EnPhrase;
                int count       = 0;
                int endingCount = 0;
                foreach (var word in phraseText.Split(new[] { ' ', ',' }))
                {
                    var lowerWord = word.Trim().ToLower();
                    if (allWords.Contains(lowerWord))
                    {
                        count++;
                    }
                    else if (word.EndsWith('s'))
                    {
                        var withoutEnding = lowerWord.Remove(lowerWord.Length - 1);
                        if (allWords.Contains(withoutEnding))
                        {
                            endingCount++;
                        }
                    }
                    else if (word.EndsWith("ed"))
                    {
                        var withoutEnding = lowerWord.Remove(lowerWord.Length - 2);

                        if (allWords.Contains(withoutEnding))
                        {
                            endingCount++;
                        }
                    }
                    else if (word.EndsWith("ing"))
                    {
                        var withoutEnding = lowerWord.Remove(lowerWord.Length - 3);

                        if (allWords.Contains(withoutEnding))
                        {
                            endingCount++;
                        }
                    }

                    if (count + endingCount > 1)
                    {
                        searchedPhrases.Add(phrase);
                        if (endingCount > 0)
                        {
                            endings++;
                        }

                        if (count + endingCount > 2)
                        {
                            Console.WriteLine(phraseText);
                        }
                        break;
                    }
                }
            }

            var firstPhrases = searchedPhrases.Randomize().Take(maxCount);

            foreach (var phrase in firstPhrases)
            {
                Console.WriteLine("Adding " + phrase.EnPhrase);

                _usersWordsService.AddPhraseAsWordToUserCollection(phrase);
                //cv    _usersWordsService.RemovePhrase(phrase.Id,userId);
            }

            Console.WriteLine($"Found: {searchedPhrases.Count}+{endings}");
        }