From 60f417616a26341f81870e89a2b88c9324786cf5 Mon Sep 17 00:00:00 2001 From: Irma Date: Fri, 15 Sep 2023 15:29:30 +0200 Subject: [PATCH 1/4] addede --- .../Services/SynonymsServiceWithDirective.cs | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/SynonymsAPI/Services/SynonymsServiceWithDirective.cs b/SynonymsAPI/Services/SynonymsServiceWithDirective.cs index 97f09e1..a874545 100644 --- a/SynonymsAPI/Services/SynonymsServiceWithDirective.cs +++ b/SynonymsAPI/Services/SynonymsServiceWithDirective.cs @@ -233,6 +233,150 @@ public bool Add(string word, List synonyms) } + public bool SomeOtherMethod(string word, List synonyms) + { + try + { + if (word == "A") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "C") + { + + } + //Remove all empty strings, or synonyms that are equal to word, and get Distinct values + synonyms.RemoveAll(s => string.IsNullOrWhiteSpace(s) || s == word); + synonyms = synonyms.Distinct().ToList(); + + if (word == null || synonyms.Count == 0) return false; + + //If the word that is a synonym already exists, get the ID. If not, add it + var synonymsFromCache = _words.Where(x => synonyms.Contains(x.WordString.ToLower())).ToList(); + var synonymIds = synonymsFromCache.Select(x => x.Id).ToList(); + + //Get synonyms that are not already in cache + var synonymsNotInCache = synonyms.Where(s => !synonymsFromCache.Select(x => x.WordString).Contains(s)); + + //Add those synonyms + var (newSynonymIds, nextId) = AddWordsToCache(_words.Count + 1, synonymIds, synonymsNotInCache); + + //Now we have a list of all synonyms, new and old + var allSynonymIds = synonymIds.Concat(newSynonymIds).ToList(); + + //Check if word already exists + var existingWord = _words.FirstOrDefault(x => x.WordString == word.ToLower()); + + //If it doesn't, add the word and synonyms + if (existingWord == null) + { + //Add synonyms of synonyms + synonymsFromCache.ForEach(x => allSynonymIds.AddRange(x.SynonymIds)); + + //Add new word and bind synonyms + _words.Add(new Word() { Id = nextId, WordString = word.ToLower(), SynonymIds = allSynonymIds.ToList() }); + allSynonymIds.Add(nextId); + } + else + { + //If it does, hook only synonyms that aren't already in the list + HookTheNewSynonymsToWord(existingWord, newSynonymIds); + + //Add the word and its synonyms to all synonyms list + allSynonymIds.Add(existingWord.Id); + allSynonymIds.AddRange(existingWord.SynonymIds); + } + allSynonymIds = allSynonymIds.ToList(); + + //Update newly added synonyms to have the references to all words + UpdateAllSynonymsOfSynonyms(allSynonymIds); + + //Update the cache + _cache.Set(wordListCacheKey, _words); + return true; + + } + catch (Exception) + { + return false; + } + } + public bool AddTest(string word) { From 42996502ad2b0fb860029060fdf50092ee278e9f Mon Sep 17 00:00:00 2001 From: Irma Date: Fri, 15 Sep 2023 15:31:18 +0200 Subject: [PATCH 2/4] removed --- SynonymsAPI/Services/SynonymsServiceWithDirective.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/SynonymsAPI/Services/SynonymsServiceWithDirective.cs b/SynonymsAPI/Services/SynonymsServiceWithDirective.cs index a874545..4482464 100644 --- a/SynonymsAPI/Services/SynonymsServiceWithDirective.cs +++ b/SynonymsAPI/Services/SynonymsServiceWithDirective.cs @@ -87,7 +87,6 @@ private List GetSynonymWords(Word initialWord) return synonyms; } - // @CodeScene(disable:"Complex Method") public bool Add(string word, List synonyms) { try @@ -232,7 +231,7 @@ public bool Add(string word, List synonyms) } } - + // @CodeScene(disable:"Complex Method") public bool SomeOtherMethod(string word, List synonyms) { try From e5192143c385e0b2c4bf439c76a836e0dfc48cf4 Mon Sep 17 00:00:00 2001 From: Irma Date: Fri, 15 Sep 2023 15:35:56 +0200 Subject: [PATCH 3/4] added new service --- SynonymsAPI/Services/ServiceWithDirective.cs | 453 +++++++++++++++++++ 1 file changed, 453 insertions(+) create mode 100644 SynonymsAPI/Services/ServiceWithDirective.cs diff --git a/SynonymsAPI/Services/ServiceWithDirective.cs b/SynonymsAPI/Services/ServiceWithDirective.cs new file mode 100644 index 0000000..4482464 --- /dev/null +++ b/SynonymsAPI/Services/ServiceWithDirective.cs @@ -0,0 +1,453 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; +using SynonymsAPI.Interfaces; +using SynonymsDB; +using System.Linq; + +namespace SynonymsAPI.Services +{ + public class SynonymsService : ISynonymsService + { + private const string wordListCacheKey = "wordList"; + private readonly IMemoryCache _cache; + private List _words; + + public SynonymsService(IMemoryCache memoryCache) + { + _cache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache)); + if (!_cache.TryGetValue(wordListCacheKey, out List words)) + { + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSlidingExpiration(TimeSpan.FromSeconds(560)) + .SetAbsoluteExpiration(TimeSpan.FromSeconds(3600)) + .SetPriority(CacheItemPriority.Normal) + .SetSize(1024); + _cache.Set(wordListCacheKey, SeedData.initialWords, cacheEntryOptions); + _words = SeedData.initialWords; + } + else _words = words; + } + + public List Get() + { + var wordsDto = _words.Select(x => GetByWord(x.WordString)).OrderBy(x => x.WordString).ToList(); + return wordsDto; + } + + public WordDto? GetByWord(string word) + { + //Get all synonyms for that word + var wordWithSynonyms = _words.FirstOrDefault(x => x.WordString == word.ToLower()); + + //Word not found + if (wordWithSynonyms == null) return null; + var wordDto = new WordDto + { + Id = wordWithSynonyms.Id, + WordString = word, + //Get synonyms + Synonyms = _words.Where(x => wordWithSynonyms.SynonymIds.Contains(x.Id)).Select(x => x.WordString).ToList() + }; + + return wordDto; + } + + public List SearchByWord(string word) + { + //Get all synonyms for that word + var wordsWithSynonyms = _words.Where(x => x.WordString.ToLower().StartsWith(word.ToLower())); + var wordsList = new List(); + + //Words not found + if (!wordsWithSynonyms.Any()) return wordsList; + + //Get synonyms + foreach (var wordWithSynonyms in wordsWithSynonyms) + { + var wordDto = new WordDto + { + Id = wordWithSynonyms.Id, + WordString = wordWithSynonyms.WordString, + Synonyms = _words.Where(x => wordWithSynonyms.SynonymIds.Contains(x.Id)).Select(x => x.WordString).ToList() + }; + wordsList.Add(wordDto); + } + return wordsList.OrderBy(x => x.WordString).ToList(); + } + + private List GetSynonymWords(Word initialWord) + { + var synonymWords = _words.Where(w => initialWord.SynonymIds.Contains(w.Id)); + var synonyms = synonymWords.Select(x => x.WordString).ToList(); + + foreach (var word in synonymWords) + { + synonyms.AddRange(GetSynonymWords(word)); + } + return synonyms; + } + + public bool Add(string word, List synonyms) + { + try + { + if (word == "A") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "C") + { + + } + //Remove all empty strings, or synonyms that are equal to word, and get Distinct values + synonyms.RemoveAll(s => string.IsNullOrWhiteSpace(s) || s == word); + synonyms = synonyms.Distinct().ToList(); + + if (word == null || synonyms.Count == 0) return false; + + //If the word that is a synonym already exists, get the ID. If not, add it + var synonymsFromCache = _words.Where(x => synonyms.Contains(x.WordString.ToLower())).ToList(); + var synonymIds = synonymsFromCache.Select(x => x.Id).ToList(); + + //Get synonyms that are not already in cache + var synonymsNotInCache = synonyms.Where(s => !synonymsFromCache.Select(x => x.WordString).Contains(s)); + + //Add those synonyms + var (newSynonymIds, nextId) = AddWordsToCache(_words.Count + 1, synonymIds, synonymsNotInCache); + + //Now we have a list of all synonyms, new and old + var allSynonymIds = synonymIds.Concat(newSynonymIds).ToList(); + + //Check if word already exists + var existingWord = _words.FirstOrDefault(x => x.WordString == word.ToLower()); + + //If it doesn't, add the word and synonyms + if (existingWord == null) + { + //Add synonyms of synonyms + synonymsFromCache.ForEach(x => allSynonymIds.AddRange(x.SynonymIds)); + + //Add new word and bind synonyms + _words.Add(new Word() { Id = nextId, WordString = word.ToLower(), SynonymIds = allSynonymIds.ToList() }); + allSynonymIds.Add(nextId); + } + else + { + //If it does, hook only synonyms that aren't already in the list + HookTheNewSynonymsToWord(existingWord, newSynonymIds); + + //Add the word and its synonyms to all synonyms list + allSynonymIds.Add(existingWord.Id); + allSynonymIds.AddRange(existingWord.SynonymIds); + } + allSynonymIds = allSynonymIds.ToList(); + + //Update newly added synonyms to have the references to all words + UpdateAllSynonymsOfSynonyms(allSynonymIds); + + //Update the cache + _cache.Set(wordListCacheKey, _words); + return true; + + } + catch (Exception) + { + return false; + } + } + + // @CodeScene(disable:"Complex Method") + public bool SomeOtherMethod(string word, List synonyms) + { + try + { + if (word == "A") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "BA") + { + + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "BB") + { + + } + } + } + } + else if (word == "C") + { + + } + //Remove all empty strings, or synonyms that are equal to word, and get Distinct values + synonyms.RemoveAll(s => string.IsNullOrWhiteSpace(s) || s == word); + synonyms = synonyms.Distinct().ToList(); + + if (word == null || synonyms.Count == 0) return false; + + //If the word that is a synonym already exists, get the ID. If not, add it + var synonymsFromCache = _words.Where(x => synonyms.Contains(x.WordString.ToLower())).ToList(); + var synonymIds = synonymsFromCache.Select(x => x.Id).ToList(); + + //Get synonyms that are not already in cache + var synonymsNotInCache = synonyms.Where(s => !synonymsFromCache.Select(x => x.WordString).Contains(s)); + + //Add those synonyms + var (newSynonymIds, nextId) = AddWordsToCache(_words.Count + 1, synonymIds, synonymsNotInCache); + + //Now we have a list of all synonyms, new and old + var allSynonymIds = synonymIds.Concat(newSynonymIds).ToList(); + + //Check if word already exists + var existingWord = _words.FirstOrDefault(x => x.WordString == word.ToLower()); + + //If it doesn't, add the word and synonyms + if (existingWord == null) + { + //Add synonyms of synonyms + synonymsFromCache.ForEach(x => allSynonymIds.AddRange(x.SynonymIds)); + + //Add new word and bind synonyms + _words.Add(new Word() { Id = nextId, WordString = word.ToLower(), SynonymIds = allSynonymIds.ToList() }); + allSynonymIds.Add(nextId); + } + else + { + //If it does, hook only synonyms that aren't already in the list + HookTheNewSynonymsToWord(existingWord, newSynonymIds); + + //Add the word and its synonyms to all synonyms list + allSynonymIds.Add(existingWord.Id); + allSynonymIds.AddRange(existingWord.SynonymIds); + } + allSynonymIds = allSynonymIds.ToList(); + + //Update newly added synonyms to have the references to all words + UpdateAllSynonymsOfSynonyms(allSynonymIds); + + //Update the cache + _cache.Set(wordListCacheKey, _words); + return true; + + } + catch (Exception) + { + return false; + } + } + + + public bool AddTest(string word) + { + try + { + if (word == "A") + { + + } + else if (word.Contains("B")) + { + if (word == "BA") + { + + } + else if (word == "BB") + { + if (word == "A") + { + + } + } + } + else if (word == "C") + { + + } + return true; + } + catch (Exception) + { + return false; + } + } + + private void UpdateAllSynonymsOfSynonyms(List allSynonymIds) + { + _words.Where(x => allSynonymIds.Contains(x.Id)).Select(x => + { + //Add list of new synonyms to existing list of synonyms + var idsToAdd = x.SynonymIds.Concat(allSynonymIds.Where(id => id != x.Id)); + var synonymIds = idsToAdd.Distinct().ToList(); + x.SynonymIds = synonymIds; + return x; + }).ToList(); + } + + private void HookTheNewSynonymsToWord(Word existingWord, List newSynonymIds) + { + _words.Where(x => x.Id == existingWord.Id).Select(x => + { + x.SynonymIds = x.SynonymIds.Concat(newSynonymIds).ToList(); + return x; + }).ToList(); + } + + private (List, int) AddWordsToCache(int nextId, List synonymIds, IEnumerable synonymsNotInCache) + { + var newSynonymIds = new List(); + foreach (var synonym in synonymsNotInCache) + { + _words.Add(new Word() + { + Id = nextId, + WordString = synonym.ToLower(), + SynonymIds = synonymIds, + }); + newSynonymIds.Add(nextId++); + } + + return (newSynonymIds, nextId); + } + + } +} From 1f7ef28cdfa3371f116b8cbc671790fd96dde56a Mon Sep 17 00:00:00 2001 From: Irma Date: Fri, 15 Sep 2023 15:36:07 +0200 Subject: [PATCH 4/4] new service --- SynonymsAPI/Services/ServiceWithDirective.cs | 76 +------------------- 1 file changed, 1 insertion(+), 75 deletions(-) diff --git a/SynonymsAPI/Services/ServiceWithDirective.cs b/SynonymsAPI/Services/ServiceWithDirective.cs index 4482464..3121628 100644 --- a/SynonymsAPI/Services/ServiceWithDirective.cs +++ b/SynonymsAPI/Services/ServiceWithDirective.cs @@ -12,81 +12,7 @@ public class SynonymsService : ISynonymsService private readonly IMemoryCache _cache; private List _words; - public SynonymsService(IMemoryCache memoryCache) - { - _cache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache)); - if (!_cache.TryGetValue(wordListCacheKey, out List words)) - { - var cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSlidingExpiration(TimeSpan.FromSeconds(560)) - .SetAbsoluteExpiration(TimeSpan.FromSeconds(3600)) - .SetPriority(CacheItemPriority.Normal) - .SetSize(1024); - _cache.Set(wordListCacheKey, SeedData.initialWords, cacheEntryOptions); - _words = SeedData.initialWords; - } - else _words = words; - } - - public List Get() - { - var wordsDto = _words.Select(x => GetByWord(x.WordString)).OrderBy(x => x.WordString).ToList(); - return wordsDto; - } - - public WordDto? GetByWord(string word) - { - //Get all synonyms for that word - var wordWithSynonyms = _words.FirstOrDefault(x => x.WordString == word.ToLower()); - - //Word not found - if (wordWithSynonyms == null) return null; - var wordDto = new WordDto - { - Id = wordWithSynonyms.Id, - WordString = word, - //Get synonyms - Synonyms = _words.Where(x => wordWithSynonyms.SynonymIds.Contains(x.Id)).Select(x => x.WordString).ToList() - }; - - return wordDto; - } - - public List SearchByWord(string word) - { - //Get all synonyms for that word - var wordsWithSynonyms = _words.Where(x => x.WordString.ToLower().StartsWith(word.ToLower())); - var wordsList = new List(); - - //Words not found - if (!wordsWithSynonyms.Any()) return wordsList; - - //Get synonyms - foreach (var wordWithSynonyms in wordsWithSynonyms) - { - var wordDto = new WordDto - { - Id = wordWithSynonyms.Id, - WordString = wordWithSynonyms.WordString, - Synonyms = _words.Where(x => wordWithSynonyms.SynonymIds.Contains(x.Id)).Select(x => x.WordString).ToList() - }; - wordsList.Add(wordDto); - } - return wordsList.OrderBy(x => x.WordString).ToList(); - } - - private List GetSynonymWords(Word initialWord) - { - var synonymWords = _words.Where(w => initialWord.SynonymIds.Contains(w.Id)); - var synonyms = synonymWords.Select(x => x.WordString).ToList(); - - foreach (var word in synonymWords) - { - synonyms.AddRange(GetSynonymWords(word)); - } - return synonyms; - } - + // @CodeScene(disable-all) public bool Add(string word, List synonyms) { try