Пример #1
0
        public void NormalizeTestWithSimpleLemma()
        {
            var simple     = "абв";
            var normalizer = new LemmaNormalizer(simple);

            Assert.AreEqual(simple, normalizer.Normalize());
        }
        private List <NameForm> Lookup(string nominativeSingular, CorrectionLanguage language)
        {
            var token = HttpContext.Current.Request.GetToken();

            if (token == null)
            {
                return(null);
            }

            var cache = (MorpherCacheObject)_morpherCache.Get(token.ToString().ToLowerInvariant());

            var correctionCache = (List <Name>)_correctionCache.Get(cache.UserId.ToString().ToLowerInvariant());

            if (correctionCache != null)
            {
                var result = correctionCache.FirstOrDefault(
                    name => name.Lemma == LemmaNormalizer.Normalize(nominativeSingular) &&
                    name.LanguageID == language.ToDatabaseLanguage())?.NameForms;

                if (result == null || !result.Any())
                {
                    return(null);
                }

                return(result.ToList());
            }

            // load to cache
            LoadToCache(cache.UserId.Value);
            return(Lookup(nominativeSingular, language));
        }
Пример #3
0
        public void NormalizeTestWithLetter_ё()
        {
            var complex    = "ёлка";
            var normalizer = new LemmaNormalizer(complex);

            Assert.AreEqual("елка", normalizer.Normalize());
        }
        private bool Remove(string nominativeSingular, CorrectionLanguage language)
        {
            var token = HttpContext.Current.Request.GetToken();

            if (token == null)
            {
                throw new TokenNotFoundException();
            }

            var cache = (MorpherCacheObject)_morpherCache.Get(token.ToString().ToLowerInvariant());

            _correctionCache.Remove(cache.UserId.ToString().ToLowerInvariant());

            using (UserCorrectionDataContext context = new UserCorrectionDataContext())
            {
                var query = (from name in context.Names
                             join userVote in context.UserVotes on name.ID equals userVote.NameID
                             where userVote.UserID == cache.UserId &&
                             name.Lemma == LemmaNormalizer.Normalize(nominativeSingular) &&
                             name.LanguageID == language.ToDatabaseLanguage()
                             select new { name, userVote }).FirstOrDefault();
                if (query == null)
                {
                    return(false);
                }

                foreach (var form in query.name.NameForms)
                {
                    context.NameForms.DeleteOnSubmit(form);
                }

                context.UserVotes.DeleteOnSubmit(query.userVote);
                context.SubmitChanges();
                return(true);
            }
        }
        private void Add(string nominativeSingular, List <NameForm> forms, CorrectionLanguage language)
        {
            var token = HttpContext.Current.Request.GetToken();

            if (token == null)
            {
                throw new TokenNotFoundException();
            }

            var cache = (MorpherCacheObject)_morpherCache.Get(token.ToString().ToLowerInvariant());

            string normalizedLemma = LemmaNormalizer.Normalize(nominativeSingular);

            using (UserCorrectionDataContext context = new UserCorrectionDataContext())
            {
                var userVote = context.UserVotes.FirstOrDefault(
                    vote => vote.UserID == cache.UserId &&
                    vote.Name.Lemma == normalizedLemma &&
                    vote.Name.LanguageID == language.ToDatabaseLanguage());

                Name name;
                if (userVote == null)
                {
                    var nameId = Guid.NewGuid();
                    name = new Name()
                    {
                        ID = nameId, LanguageID = language.ToDatabaseLanguage()
                    };
                    context.Names.InsertOnSubmit(name);
                    userVote = new UserVote()
                    {
                        Name = name, UserID = cache.UserId.Value
                    };
                    context.UserVotes.InsertOnSubmit(userVote);
                }
                else
                {
                    name = userVote.Name;
                }

                name.Lemma            = normalizedLemma;
                userVote.SubmittedUTC = DateTime.UtcNow;

                foreach (var nameForm in forms)
                {
                    var dbForm = name.NameForms.FirstOrDefault(
                        nf => nf.FormID == nameForm.FormID &&
                        nf.Plural == nameForm.Plural);
                    if (dbForm == null)
                    {
                        nameForm.Name = name;
                        context.NameForms.InsertOnSubmit(nameForm);
                    }
                    else
                    {
                        dbForm.AccentedText = nameForm.AccentedText;
                    }
                }

                context.SubmitChanges();
                _correctionCache.Remove(cache.UserId.ToString().ToLowerInvariant());
            }
        }