Ejemplo n.º 1
0
        public void AddSynonyms(IEnumerable <string> synonyms)
        {
            try
            {
                if (synonyms.Count() > 0)
                {
                    // Checking for empty collection and not allowed symbols.

                    string collectAsString = string.Join(tempCollectionDelimiter, synonyms);

                    // TODO:
                    // what to do if synonyms collection contains a word with not allowed symbols?
                    if (!string.IsNullOrWhiteSpace(collectAsString) && regExp.IsMatch(collectAsString))
                    {
                        WordSynonims existedWord = db.GetOneIfExists(synonyms);

                        // if exists then update else create new.
                        if (existedWord != null)
                        {
                            var newSynCollection = existedWord.Synonims.Split(' ').ToList();

                            newSynCollection.AddRange(synonyms);
                            newSynCollection = newSynCollection.Distinct().ToList();
                            newSynCollection.Sort();

                            // TODO:
                            // escaping with spaces must be refactored!
                            // TODO:
                            string newSynonims = " " + String.Join(" ", newSynCollection.ToArray()) + " ";
                            db.UpdateSynonymsForId(existedWord.Id, newSynonims);
                        }
                        else
                        {
                            // TODO:
                            // check for description!
                            string newSynonyms = " " + String.Join(" ", synonyms.ToArray()) + " ";
                            db.InsertNewSynonyms(newSynonyms.Trim());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error($"AddSynonyms to db : {ex.Message}");
            }
        }
Ejemplo n.º 2
0
        public WordSynonims GetOneIfExists(IEnumerable <string> words)
        {
            List <string> list = words.ToList();

            WordSynonims synonyms = null;

            using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbStorage + "; Version=3;"))
            {
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    SQLiteCommand command = conn.CreateCommand();

                    foreach (var word in words)
                    {
                        command.CommandText = $"SELECT * FROM {synonymNounsTable} WHERE synonyms LIKE '% {word} %'";

                        SQLiteDataReader rdr = command.ExecuteReader();

                        while (rdr.Read() && synonyms == null)
                        {
                            int id = rdr.GetInt32(0);
                            if (id != 0)
                            {
                                synonyms = new WordSynonims
                                {
                                    Id          = id,
                                    Description = rdr.GetString(1),
                                    Synonims    = rdr.GetString(2)
                                };
                            }
                        }
                        rdr.Close();
                    }
                }
                conn.Close();
            }
            return(synonyms);
        }