public static List <ResultItem> SearchSubQueries(Db db, Db db_words, string query) { var results = new List <ResultItem>(); var words = query.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Take(5).ToList(); var other_queries = new List <string>(); for (int i = 0; i < words.Count(); i++) { var new_list = new List <string>(); for (int j = 0; j < words.Count(); j++) { if (i == j) { continue; } new_list.Add(words[j]); } var nq = new_list.Aggregate((a, b) => a + " " + b); other_queries.Add(nq); } foreach (var q in other_queries) { var tmp = SearchLogic.SearchTitles(db, db_words, q); results.AddRange(tmp); } foreach (var r in results) { r.Score += -50000; } return(results); }
public static List <string> GetPossibleQueries(string query) { Init(); var res = SearchLogic.GetAllPossibleQueries(db, query); var list = res.Item1; list.AddRange(res.Item2); return(list); }
public static List <ResultItem> SearchFragments(Db db, /*Db db_post,*/ Db db_words, string query) { var results = new List <ResultItem>(); var interm = new List <IntermResult>(); int max_step = db.Table <PostFragment>().LastStep(); int step = 50; Stopwatch sp = new Stopwatch(); sp.Start(); for (int i = 0; i <= max_step; i += step) { var res = db.Table <PostFragment>().Search(f => f.Text, query, i, step) .Select(f => new { f.Id, f.Text, f.QuestionId }) .Select(f => new IntermResult { Id = f.Id, Text = f.Text, QuestionId = f.QuestionId }); interm.AddRange(res); if (sp.ElapsedMilliseconds > 1000 || interm.GroupBy(f => f.QuestionId).Count() >= 1000) { break; } } foreach (var inter in interm) { inter.Score += /*5000 +*/ GetFragmentScore(inter.Text, query); } //tfidf var dic = new Dictionary <string, Dictionary <int, short> >(); foreach (var w in query.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { var d = db_words.Table <WordTfIdfData>().Where(f => f.Word == w).SelectEntity().FirstOrDefault(); if (d == null) { continue; } var data = Utils.TfIdfFromData(d.Data); dic[w] = data.Item1; } foreach (var inter in interm) { foreach (var w in query.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { if (dic.ContainsKey(w) && dic[w].ContainsKey(inter.Id)) { inter.Score += dic[w][inter.Id]; } } } foreach (var r in interm.GroupBy(f => f.QuestionId).OrderByDescending(f => f.OrderByDescending(z => z.Score).First().Score).Take(5)) { var q = r.OrderByDescending(z => z.Score).First(); var first_id = q.QuestionId; var item = new ResultItem() { Fragment = q.Text, Id = q.QuestionId, Title = db.Table <WholePost>().Where(f => f.Id == first_id).Select(f => new { f.Title }).First().Title, Score = q.Score }; results.Add(item); } if (!results.Any()) { //return SearchLogic.SearchPosts(db_post, db, query); return(SearchLogic.SearchSubQueries(db, db_words, query)); } else { return(results); } }
public static List <ResultItem> SearchMain(string query) { Init(); return(SearchLogic.SearchFragments(db, /*db_post,*/ db_words, query)); }
public static List <ResultItem> SearchTitles(string query) { Init(); return(SearchLogic.SearchTitles(db, db_words, query)); }