Ejemplo n.º 1
0
        /// <summary>
        /// Adds the given synonyms to the thesaurus.
        /// </summary>
        /// <param name="synonyms">The synonyms to add.</param>
        public void AddSynonyms(IEnumerable <string> synonyms)
        {
            HashSet <int> foundSynonymGroupIDs = new HashSet <int>();
            List <string> unrecognizedWords    = new List <string>();

            foreach (string word in synonyms)
            {
                var wordsQuery = dbContext.Words.Where(w => w.Characters == word);
                if (wordsQuery.Any())
                {
                    foundSynonymGroupIDs.Add(wordsQuery.Select(p => p.SynonymGroupID).First());
                    continue;
                }
                else
                {
                    unrecognizedWords.Add(word);
                }
            }

            SynonymGroup synonymGroup = GetSynonymGroup(foundSynonymGroupIDs);

            foreach (string word in unrecognizedWords)
            {
                var newWord = new Word
                {
                    Characters   = word,
                    SynonymGroup = synonymGroup
                };

                dbContext.Words.Add(newWord);
            }

            dbContext.SaveChanges();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Maps synonyms with a meaning-ID and adds them to the MeaningGroup table.
        /// It also adds all synonyms to the Word-table if not already added. Only a-z and 0-9 are allowed.
        /// </summary>
        public void AddSynonyms(IEnumerable <string> synonyms)
        {
            try
            {
                if (synonyms == null)
                {
                    throw new ArgumentNullException("The 'synonyms' argument has a null-value");
                }
                if (synonyms.Count() < 2)
                {
                    throw new Exception("Requires at least two words, to be able to store synonyms");
                }
                // The meaningID that will be mapped with the synonyms
                int nextMeaningID = 1;

                // Generates a new meaningID-value if there are already stored MeaningGroups
                if (context.MeaningGroups.Any())
                {
                    nextMeaningID = context.MeaningGroups.Max(p => p.MeaningID) + 1;
                }

                foreach (string synonym in synonyms)
                {
                    Regex  onlyLettersAndDigits = new Regex("^[0-9a-z_]*$");
                    string synonymLowerCase     = synonym.ToLower();

                    // Make sure that the synonym does not contain illegal characters
                    if (!onlyLettersAndDigits.IsMatch(synonymLowerCase))
                    {
                        // words with illegal characters
                        throw new Exception($"The synonym {synonymLowerCase} has illegal characters");
                    }

                    if (context.Words.Find(synonymLowerCase) == null)
                    {
                        context.Words.Add(new Word
                        {
                            Name = synonymLowerCase,
                        });
                    }

                    // Maps the synonyms with the meaningID
                    context.MeaningGroups.Add(new MeaningGroup
                    {
                        WordName  = synonymLowerCase,
                        MeaningID = nextMeaningID
                    });
                }
                // Locks the database while editing
                lock (dbLock)
                {
                    // Stores the synonyms with the meaningID
                    context.SaveChanges();
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.ToString());
            }
        }