public static async Task SaveSentence(string sentence, int level) { if (sentence.Length <= 2) { return; } var saved = await SentenceDao.GetByKeyAsync(sentence); if (saved != null) { await SentenceDao.DeleteAsync(saved.Id); await CommonKeywordMappingDao.DeleteAsync(saved.Id); } await SentenceDao.SaveAsync( new Sentence { CreatedTime = DateTime.Now, Key = sentence, LastUsedTime = DateTime.Now }); var savedSentence = await SentenceDao.GetByKeyAsync(sentence); var toSaves = ParticipleProcessor.ParticipleSentence(sentence, savedSentence.Id, level); await CommonKeywordMappingDao.SaveAsync(toSaves); }
public async Task TestKeywordMappingCountSmallThan() { await CommonKeywordMappingDao.SaveAsync(new List <KeywordMappingBase> { Helper.GetKeywordMappingInstance(Today, 1, 1), Helper.GetKeywordMappingInstance(Tommorow, 2, 1) }); var result = await CommonKeywordMappingDao.SearchKeywordMappingsAsync(new List <CommonMapping> { new CommonMapping { Count = 2, Key = Today }, new CommonMapping { Count = 1, Key = Tommorow } }); Assert.AreEqual(2, result.Length); var mapping = result.FirstOrDefault(i => !i.Any()); Assert.IsNotNull(mapping); var commonMappings = mapping as IList <CommonMapping> ?? mapping.ToList(); Assert.AreEqual(0, commonMappings.Count); }
public static async Task <IEnumerable <Sentence> > SearchBySentence(string sentence, int level) { var dictionary = Helper.GetDistinctCount(sentence.Trim().ToLowerInvariant().Split(' ')); // remove longer keyword PreProcessOnDictionary(ref dictionary, level); var searchMappings = dictionary.Select(pair => new CommonMapping { Count = pair.Value, Key = pair.Key }); // get keyword mapping result var allResult = await CommonKeywordMappingDao.SearchKeywordMappingsAsync(searchMappings); var allMappings = new List <CommonMapping>(); foreach (var tableResult in allResult) { allMappings.AddRange(tableResult); } // filter all sentence ids that do not contain all keywords. var firstFilter = allMappings.Select(mapping => mapping.Id).Distinct().ToList(); var secondFilter = new List <long>(); firstFilter.ForEach(id => { if (allMappings.Count(mapping => mapping.Id == id) == dictionary.Count) { secondFilter.Add(id); } }); var sentences = await SentenceDao.GetByIdsAsync(secondFilter); // sentence need to contain all longer keywords. var longerKeywords = dictionary.Where(pair => pair.Key.Length > level).ToList(); return(sentences.Where(s => { var notContained = false; foreach (var longerKeyword in longerKeywords) { notContained = !s.Key.Contains(longerKeyword.Key); } return !notContained; })); }
public async Task TestKeywordMappingDelete() { await CommonKeywordMappingDao.SaveAsync(new List <KeywordMappingBase> { Helper.GetKeywordMappingInstance(Today, 1, 1), Helper.GetKeywordMappingInstance(Tommorow, 2, 1) }); var result = await CommonKeywordMappingDao.SearchKeywordMappingsAsync(new List <CommonMapping> { new CommonMapping { Count = 1, Key = Today }, new CommonMapping { Count = 1, Key = Tommorow } }); Assert.AreEqual(result.Length, 2); await CommonKeywordMappingDao.DeleteAsync(1); await CommonKeywordMappingDao.DeleteAsync(2); var result2 = await CommonKeywordMappingDao.SearchKeywordMappingsAsync(new List <CommonMapping> { new CommonMapping { Count = 1, Key = Today }, new CommonMapping { Count = 1, Key = Tommorow } }); Assert.AreEqual(result.Length, 2); Assert.AreEqual(true, result2.All(i => !i.Any())); }