Exemplo n.º 1
0
        public IActionResult GetExplain(string Word, long UserId)
        {
            IList <Dictionary>          dictionaries   = db.Dictionaries.ToList();
            IList <WordsHistory>        wordsHistories = db.WordsHistories.ToList();
            WordsHistory                wordsHistory   = new WordsHistory();
            Dictionary <string, string> first_result   = new Dictionary <string, string>();
            Dictionary <string, string> second_result  = new Dictionary <string, string>();

            foreach (var item in dictionaries)
            {
                if (GetSimilarity(item.Word, Word) > 0.85)
                {
                    first_result.Add(item.Word, item.Explain);
                    if (wordsHistories.Where(s => s.Word == item.Word).Count() == 0)
                    {
                        wordsHistory.UserId     = UserId;
                        wordsHistory.Word       = item.Word;
                        wordsHistory.Explain    = item.Explain;
                        wordsHistory.SearchTime = DateTime.Now;
                        db.WordsHistories.Add(wordsHistory);
                        db.SaveChanges();
                    }
                    break;
                }
                if (GetSimilarity(item.Word, Word) > 0.6)
                {
                    second_result.Add(item.Word, item.Explain);
                    if (wordsHistories.Where(s => s.Word == item.Word).Count() == 0)
                    {
                        wordsHistory.UserId     = UserId;
                        wordsHistory.Word       = item.Word;
                        wordsHistory.Explain    = item.Explain;
                        wordsHistory.SearchTime = DateTime.Now;
                        db.WordsHistories.Add(wordsHistory);
                        db.SaveChanges();
                    }
                }
            }

            if (first_result.Count > 0)
            {
                if (wordsHistories.Where(s => s.Word == first_result.First().Key).Count() > 0)
                {
                    wordsHistory                 = wordsHistories.Where(s => s.Word == first_result.First().Key).FirstOrDefault();
                    wordsHistory.SearchTime      = DateTime.Now;
                    db.Entry(wordsHistory).State = EntityState.Modified;
                    db.SaveChanges();
                }
                return(Json(new
                {
                    code = 200,
                    Word = first_result.First().Key,
                    Explain = first_result.First().Value,
                }));
            }

            if (second_result.Count > 0)
            {
                if (wordsHistories.Where(s => s.Word == second_result.First().Key).Count() > 0)
                {
                    wordsHistory                 = wordsHistories.Where(s => s.Word == second_result.First().Key).FirstOrDefault();
                    wordsHistory.SearchTime      = DateTime.Now;
                    db.Entry(wordsHistory).State = EntityState.Modified;
                    db.SaveChanges();
                }
                return(Json(new
                {
                    code = 200,
                    Word = second_result.First().Key,
                    Explain = second_result.First().Value,
                }));
            }

            return(Json(new
            {
                code = 400,
                data = "啥也没有"
            }));
        }
Exemplo n.º 2
0
        public IActionResult GetExplain(string Word, int UserId)
        {
            List <Dictionary> dictionaries                 = db.Dictionaries.ToList();
            Dictionary <Dictionary, double> results        = new Dictionary <Dictionary, double>();
            List <SearchHistory>            histories      = db.SearchHistories.ToList();
            List <SearchHistory>            repetitions    = new List <SearchHistory>();
            List <WordsHistory>             wordsHistories = db.WordsHistories.ToList();
            WordsHistory        wordsHistory               = null;
            List <WordsHistory> repeat        = new List <WordsHistory>();
            SearchHistory       searchHistory = null;

            for (int i = 0; i < histories.Count(); i++)
            {
                if (GetSimilarity(Word, histories[i].HistoricalText) > 0.85)
                {
                    repetitions.Add(histories[i]);
                }
            }
            for (int j = 0; j < wordsHistories.Count(); j++)
            {
                if (GetSimilarity(Word, wordsHistories[j].Word) > 0.85)
                {
                    repeat.Add(wordsHistories[j]);
                }
            }
            if (db.SearchHistories.Where(s => s.HistoricalText == Word).Count() == 0 && db.SearchHistories.Where(s => s.HistoricalText.Contains(Word)).Count() == 0 && repetitions.Count() == 0)
            {
                searchHistory                = new SearchHistory();
                searchHistory.SearchTime     = DateTime.Now;
                searchHistory.HistoricalText = Word;
                searchHistory.UserId         = UserId;
                searchHistory.User           = db.Users.FirstOrDefault(s => s.Id == UserId);
            }
            if (db.WordsHistories.Where(s => s.Word.Equals(Word)).Count() == 0 && db.WordsHistories.Where(s => s.Word.Contains(Word)).Count() == 0 && repeat.Count() == 0)
            {
                wordsHistory = new WordsHistory();
            }
            for (int i = 0; i < dictionaries.Count(); i++)
            {
                if (GetSimilarity(Word, dictionaries[i].Word) > 0.65)
                {
                    results.Add(dictionaries[i], GetSimilarity(Word, dictionaries[i].Word));
                }
            }
            Dictionary <Dictionary, double> list = results.OrderByDescending(s => s.Value).ToDictionary(s => s.Key, s => s.Value);

            if (list.Count() > 0)
            {
                if (searchHistory != null)
                {
                    searchHistory.Answer = list.First().Key.Explain;
                    db.SearchHistories.Add(searchHistory);
                    db.SaveChanges();
                }
                if (wordsHistory != null)
                {
                    wordsHistory.UserId     = UserId;
                    wordsHistory.Explain    = list.First().Key.Explain;
                    wordsHistory.Word       = list.First().Key.Word;
                    wordsHistory.SearchTime = DateTime.Now;
                    db.WordsHistories.Add(wordsHistory);
                    db.SaveChanges();
                }
                if (list.First().Value == 1)
                {
                    return(Json(new
                    {
                        datas = list.Take(1).Select(s => new
                        {
                            s.Key.Word,
                            s.Key.Explain
                        })
                    }));
                }
                else if (list.Count() > 2)
                {
                    return(Json(new
                    {
                        datas = list.Take(3).Select(s => new
                        {
                            s.Key.Word,
                            s.Key.Explain
                        })
                    }));
                }
                else if (list.Count() <= 2 && list.Count() > 0)
                {
                    return(Json(new
                    {
                        datas = list.Select(s => new
                        {
                            s.Key.Word,
                            s.Key.Explain
                        })
                    }));
                }
            }
            return(Json(new { }));
        }