Esempio n. 1
0
        static void AddInitial()
        {
            var optionsBuilder = new DbContextOptionsBuilder <DictionaryContext>();

            optionsBuilder.UseSqlite("Data source=dict.db");
            var _context = new DictionaryContext(optionsBuilder.Options);

            using (var inputStream = new FileStream(@"Seeder\dictionary.dsl", FileMode.Open))
            {
                Parser p       = new Parser(inputStream);
                var    entries = p.ParseEntries();
                int    cnt     = 0;
                foreach (var entry in entries)
                {
                    _context.Add(entry);
                    Console.WriteLine($"Add {entry.Lemma}");
                    if (cnt % 1000 == 0)
                    {
                        _context.SaveChanges();
                    }
                    cnt++;
                }
                _context.SaveChanges();
            };
        }
Esempio n. 2
0
        public ActionResult Create([Bind(Include = "ID,Name")] Language language)
        {
            if (ModelState.IsValid)
            {
                db.Languages.Add(language);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(language));
        }
        public void RandomTest()
        {
            _context.Add(new DictionaryEntry()
            {
                Lemma      = "BabyPoeder",
                Definition = "Poeder voor baby's"
            });
            _context.SaveChanges();
            var entry = controller.Random().Value;

            Assert.AreEqual(entry.Lemma, "BabyPoeder");
        }
Esempio n. 4
0
        public int InsertUnit()
        {
            using (StreamReader reader = new StreamReader(Request.Body))
            {
                string jsonString = reader.ReadToEnd();

                Unit jsonObject = JsonConvert.DeserializeObject <Unit>(jsonString);
                Unit item       = new Unit(jsonObject.Word, jsonObject.Content);

                _context.Units.Add(item);

                return(_context.SaveChanges());
            }
        }
Esempio n. 5
0
        public virtual void Create(T entity)
        {
            if (_entities == null)
            {
                throw new ArgumentNullException("Tried to work with null entity set!");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("Tried to insert null entity!");
            }

            _entities.Add(entity);
            _context.SaveChanges();
        }
Esempio n. 6
0
        public void Query()
        {
            var map            = GetTranslationMap();
            var optionsBuilder = new DbContextOptionsBuilder <DictionaryContext>();

            optionsBuilder.UseSqlite("Data source=dict.db");
            var _context = new DictionaryContext(optionsBuilder.Options);
            var lastId   = _context.DictionaryEntries.OrderBy(e => e.Id).Last().Id;

            var last = 0;

            while (last < lastId)
            {
                var set = _context.DictionaryEntries
                          .Where(e => e.Id > last)
                          .OrderBy(e => e.Id)
                          .Take(1000);
                foreach (var entry in set)
                {
                    _context.Update(entry);
                    string translation;
                    if (map.TryGetValue(entry.Lemma, out translation))
                    {
                        entry.Translation = translation;
                    }
                    ;
                }
                last = set.Last().Id;
                _context.SaveChanges();
            }
        }
        public void AddCacheToRepository(IList <string> list, string word)
        {
            var resultList  = new List <int>();
            var anagramList = ReadAnagramIDS(list);
            var sortedword  = string.Concat(word.OrderBy(c => c));

            for (int i = 0; i < anagramList.IDList.Count(); i++)
            {
                if (string.Concat(anagramList.WordList[i].OrderBy(c => c)) == sortedword)
                {
                    resultList.Add(anagramList.IDList[i]);
                }
            }

            foreach (var item in resultList)
            {
                var cachedWords = new CachedWords()
                {
                    AnagramWordId = item,
                    SearchedWord  = word,
                };
                db.CachedWords.Add(cachedWords);
                db.SaveChanges();
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Создание и заполнение словаря предоставленным списком слов
 /// </summary>
 private static void CreateDictionary(DictionaryContext context, List <Word> words)
 {
     Console.WriteLine("Процесс создания словаря...");
     context.Words.AddRange(words);
     context.SaveChanges();
     Console.WriteLine("Словарь успешно создан!");
 }
        public async Task <IActionResult> Create(DefinitionViewModel model)
        {
            User user = await _userManager.FindByNameAsync(User.Identity.Name);

            if (ModelState.IsValid && user != null)
            {
                Definition definition = new Definition()
                {
                    Name           = model.Name,
                    Description    = model.Description,
                    Example        = model.Example,
                    UserId         = user.Id,
                    CreationDate   = DateTime.Now,
                    AuthorUsername = user.UserName,
                    Hashtags       = _hashtagParser.Parse(model.Hashtags)
                };
                await _context.Definitions.AddAsync(definition);

                user.Definitions.Add(definition);
                _context.SaveChanges();
            }
            else
            {
                return(RedirectToAction("Index", "Feed"));
            }
            return(RedirectToAction("Index", "Feed"));
        }
Esempio n. 10
0
        private int AddNewTerm(string word)
        {
            var term = _context.Terms.
                       FirstOrDefault(t => t.Name.Trim().ToLower().Equals(word.Trim().ToLower()));

            if (term == null)
            {
                term = new Term()
                {
                    Name = word.Trim().ToLower()
                };
                _context.Terms.Add(term);
                _context.SaveChanges();
            }
            return(term.TermId);
        }
Esempio n. 11
0
        public void Setup()
        {
            var optionsBuilder = new DbContextOptionsBuilder <DictionaryContext>();

            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Lingva;Trusted_Connection=True;MultipleActiveResultSets=true;");
            var _dbContext = new DictionaryContext(optionsBuilder.Options);

            _dbContext.Database.EnsureDeleted();
            _dbContext.Database.EnsureCreated();
            _groupList = new List <Group>()
            {
                new Group
                {
                    Name        = "Harry Potter",
                    Date        = DateTime.Now,
                    Description = "Description",
                    LanguageId  = 1,
                },
                new Group
                {
                    Name        = "Harry Potter",
                    Date        = DateTime.Now,
                    Description = "Description",
                    LanguageId  = 1,
                }
            };
            _dbContext.Set <Group>().AddRange(_groupList);
            _dbContext.SaveChanges();

            _groupRepository = new GroupRepository(_dbContext);
        }
Esempio n. 12
0
        public static void SetStressIndex()
        {
            var optionsBuilder = new DbContextOptionsBuilder <DictionaryContext>();

            optionsBuilder.UseSqlite("Data source=dict.db");
            var _context = new DictionaryContext(optionsBuilder.Options);

            using (var inputStream = new FileStream(@"C:\Users\Willem\Downloads\ru-ru_ozhegov_shvedova_cc_v2_0.dsl", FileMode.Open))
            {
                int          cnt    = 0;
                StreamReader reader = new StreamReader(inputStream);
                while (!reader.EndOfStream)
                {
                    while (char.IsWhiteSpace((char)reader.Peek()))
                    {
                        reader.ReadLine();
                    }
                    string          lemma = reader.ReadLine();
                    DictionaryEntry entry = _context.DictionaryEntries.Where(de => de.Lemma == lemma).FirstOrDefault();
                    if (entry != null)
                    {
                        string definition = reader.ReadLine();
                        if (definition.Contains("_"))
                        {
                            Match match = Regex.Match(definition, "[А-Я]+(?=_)");
                            if (match.Success)
                            {
                                _context.Update(entry);
                                entry.StressIndex = match.Value.Length;
                                cnt++;
                                if (cnt % 500 == 0)
                                {
                                    _context.SaveChanges();
                                }
                            }
                        }
                    }
                }
                _context.SaveChanges();
            }
        }
Esempio n. 13
0
 public bool Complete()
 {
     try
     {
         db.SaveChanges();
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Esempio n. 14
0
        public override bool Delete(int id)
        {
            Word w = GetOne(id);

            if (w.Translations != null)
            {
                w.Translations.Clear();
                _db.Entry(w).State = EntityState.Modified;
                _db.SaveChanges();
            }
            return(base.Delete(id));
        }
Esempio n. 15
0
        public void Seed()
        {
            var document  = XDocument.Load(new FileStream(_filePath, FileMode.Open));
            var sentences = document.Root
                            .Descendants("sentence");

            sentences = sentences.Where(s => IsCyrillic(s.Element("source").Value.First()));

            var tokens = sentences.Descendants("token").Descendants("l");
            int cnt    = 0;

            foreach (var token in tokens)
            {
                var normalizedToken = token.Attribute("t").Value;
                if (IsCyrillic(normalizedToken.First()))
                {
                    var entry = FindCorrespondingEntry(normalizedToken);
                    if (entry != null)
                    {
                        Usage u = new Usage()
                        {
                            Entry    = entry,
                            Sentence = token.Ancestors("sentence").FirstOrDefault().Element("source").Value,
                            Position = token.Ancestors("token").FirstOrDefault().ElementsBeforeSelf().Count() + 1
                        };
                        _ctx.Usages.Add(u);
                        cnt++;
                        if (cnt % 500 == 0)
                        {
                            _ctx.SaveChanges();
                            Console.WriteLine($"Saved changes (#{cnt}");
                        }
                    }
                }
            }
            _ctx.SaveChanges();
        }
Esempio n. 16
0
 public void SaveDictionary(ModelDictionary Dict)
 {
     if (Dict.Id == 0)
     {
         context.DictionaryList.Add(Dict);
     }
     else
     {
         ModelDictionary dbEntry = context.DictionaryList.Find(Dict.Id);
         if (dbEntry != null)
         {
             dbEntry.Type  = Dict.Type;
             dbEntry.Value = Dict.Value;
         }
     }
     context.SaveChanges();
 }
Esempio n. 17
0
        public override async void Execute(Message message, TelegramBotClient client)
        {
            Console.WriteLine("Executing " + Name + "Command");
            ChatId id = message.Chat.Id;

            //Splitting message "/add word definition"
            string[] messageArgs = message.Text.Split();

            //If input is not correct
            if (messageArgs.Length != 3)
            {
                string errorMessage = "Неправильный формат комманды" +
                                      "\nПроверьте, или комманда соответствует шаблону \"/add слово перевод\"";
                await client.SendTextMessageAsync(id, errorMessage);

                return;
            }

            //TODO: move user creating and db operations to separate class
            using (DictionaryContext db = new DictionaryContext())
            {
                //Checking if user already exists in database, creating new if not
                var user = db.Users.FirstOrDefault(x => x.Chat == id.Identifier);
                if (user == null)
                {
                    user = new BotUser {
                        Chat = id.Identifier
                    };
                    db.Users.Add(user);
                }

                //Adding word to users dictionary
                Word word = new Word(messageArgs[1], messageArgs[2]);
                user.Words.Add(word);

                //Updating data in database
                db.SaveChanges();
            }

            await Bot.Get().SendTextMessageAsync(id, "Новое слово добавлено в ваш словарь" +
                                                 "\nEnglish: " + messageArgs[1] +
                                                 "\nПеревод: " + messageArgs[2]);
        }
Esempio n. 18
0
        /// <summary>
        /// Добавление данных в Word, если база данных еще не создана, вызывается метод CreateDictionary и происходит её создание
        /// </summary>
        public static void UpdateDictionary(string pathToFile)
        {
            List <Word> words = new TextProcessor().GetWordsFromFile(pathToFile);

            if (words.Count == 0)
            {
                return;
            }

            try
            {
                using (DictionaryContext context = new DictionaryContext())
                {
                    if (!context.Database.Exists())
                    {
                        CreateDictionary(context, words);
                        return;
                    }

                    Console.WriteLine("Процесс обновления словаря...");
                    foreach (Word word in words)
                    {
                        Word dbElement = context.Words.Find(word.Text);
                        if (dbElement != null)
                        {
                            dbElement.Count += word.Count;
                        }
                        else
                        {
                            context.Words.Add(word);
                        }
                    }
                    context.SaveChanges();
                    Console.WriteLine("Словарь успешно обновлён!\n");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"\nПри обновлении словаря возникла непредвиденная ошибка:\n{e.Message} {e.StackTrace}");
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Удаление всех данных из таблицы Words
        /// </summary>
        public static void ClearDictionary()
        {
            try
            {
                using (DictionaryContext context = new DictionaryContext())
                {
                    if (!context.Database.Exists())
                    {
                        Console.WriteLine("Словарь не может быть очищен, так как его не существует!\n");
                        return;
                    }

                    Console.WriteLine("Процесс очистки словаря...");
                    context.Words.RemoveRange(context.Words);
                    context.SaveChanges();
                    Console.WriteLine("Словарь успешно очищен!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"\nПри очистке словаря возникла непредвиденная ошибка:\n{e.Message} {e.StackTrace}");
            }
        }
Esempio n. 20
0
 public void Create(Subtitles entity)
 {
     _context.Subtitles.Add(entity);
     _context.SaveChanges();
 }
Esempio n. 21
0
 public void Create(TEntity item)
 {
     _dbSet.Add(item);
     _context.SaveChanges();
 }
Esempio n. 22
0
 public void Create(Film entity)
 {
     _context.Films.Add(entity);
     _context.SaveChanges();
 }
Esempio n. 23
0
 public static int Insert(string _English, string _Vietnamese)
 {
     db.Words.Add(new Word(_English, _Vietnamese));
     return(db.SaveChanges());
 }
Esempio n. 24
0
 public void AddUserLog(UserLog userLog)
 {
     _dbContext.UserLog.Add(userLog);
     _dbContext.SaveChanges();
 }
 public void AddUserInfo(UserInfo userInfo)
 {
     _dbContext.UserInfo.Add(userInfo);
     _dbContext.SaveChanges();
 }
Esempio n. 26
0
 public void AddWord(Words words)
 {
     _dbContext.Words.Add(words);
     _dbContext.SaveChanges();
 }
Esempio n. 27
0
 public void AddCachedWords(CachedWords cachedWords)
 {
     _dbContext.CachedWords.Add(cachedWords);
     _dbContext.SaveChanges();
 }
Esempio n. 28
0
        private void SaveWords(List <Word> words)
        {
            var subDictionaries = new List <SubDictionary>();
            var wordClasses     = new List <WordClass>();

            SubDictionary GetExistedSubDictionary(SubDictionary subDict)
            {
                // Comparation is case insensitive in database!
                var reusedSubDict = subDictionaries.SingleOrDefault(s => s.Name.Equals(subDict.Name, StringComparison.OrdinalIgnoreCase));

                if (reusedSubDict != null)
                {
                    return(reusedSubDict);
                }

                reusedSubDict = context.SubDictionaries.SingleOrDefault(s => s.Name == subDict.Name);
                if (reusedSubDict != null)
                {
                    subDictionaries.Add(reusedSubDict);
                    return(reusedSubDict);
                }

                subDictionaries.Add(subDict);
                return(subDict);
            }

            WordClass GetExistedWordClass(WordClass wordClass)
            {
                var reusedWordClass = wordClasses.SingleOrDefault(w => w.Name.Equals(wordClass.Name, StringComparison.OrdinalIgnoreCase));

                if (reusedWordClass != null)
                {
                    return(reusedWordClass);
                }

                reusedWordClass = context.WordClasses.SingleOrDefault(w => w.Name == wordClass.Name);
                if (reusedWordClass != null)
                {
                    wordClasses.Add(reusedWordClass);
                    return(reusedWordClass);
                }

                wordClasses.Add(wordClass);
                return(wordClass);
            }

            foreach (var word in words)
            {
                // If a dictionary or a word class already exists then use it, do not create a new one
                if (word.Definitions != null)
                {
                    foreach (var def in word.Definitions)
                    {
                        def.SubDictionary = GetExistedSubDictionary(def.SubDictionary);
                        def.WordClass     = GetExistedWordClass(def.WordClass);
                    }
                }

                if (word.Phases != null)
                {
                    foreach (var p in word.Phases)
                    {
                        p.SubDictionary = GetExistedSubDictionary(p.SubDictionary);
                    }
                }
            }

            try
            {
                totalWordCount += words.Count;

                context.Words.AddRange(words);
                context.SaveChanges();

                successWordCount += words.Count;
                logger.Log(GetType(), Level.Info, $"{words.Count} word(s) '{string.Join(", ", words.Select(w => w.Content))}' was(were) registered successfully", null);
            }
            catch (DbUpdateException ex)
            {
                logger.Log(GetType(), Level.Info, $"Could not save {words.Count} word(s) '{string.Join(", ", words.Select(w => w.Content))}', trying to register each word individually", ex);

                // The words could not be saved, remove them from the context
                //context.Words.RemoveRange(words); // TODO is this enough?
                ResetContext();

                // Try to register each word separately
                foreach (var word in words)
                {
                    try
                    {
                        context.Words.Add(word);
                        context.SaveChanges();

                        successWordCount++;
                        logger.Log(GetType(), Level.Info, $"The word '{word.Content}' was registered successfully", null);
                    }
                    catch (DbUpdateException e)
                    {
                        ResetContext();
                        logger.Log(GetType(), Level.Error, $"Could not save the word '{word.Content}' to database", e);
                    }
                }
            }
        }