public static void BuildProductIdentitiesDict(string connProductString) { var cacheKey = "ProductIdentitiesDict"; var cacheServer = CacheManager.GetCacheServer("ProductIdentity"); ProductIdentitiesDict = cacheServer.Get <Dictionary <int, List <WebProductIdentity> > >(cacheKey, true); if (ProductIdentitiesDict == null) { var startTime = DateTime.Now; ProductIdentitiesDict = new Dictionary <int, List <WebProductIdentity> >(); var listProductIdentities = ProductIdentityBAL.GetListCompletedProductIdentity(connProductString); var getDBDuration = (DateTime.Now - startTime).TotalSeconds; startTime = DateTime.Now; foreach (var productIdentity in listProductIdentities) { var webProductIdentity = new WebProductIdentity() { SynonymsKeywordSet = new List <SynonymsKeywordHash>(), KeywordBlackList = new List <int>(), ProductID = productIdentity.ProductID, MinPrice = productIdentity.MinPrice, MaxPrice = productIdentity.MaxPrice }; foreach (var synonymsKeywords in productIdentity.KeywordSets) { var listHash = new List <int>(); foreach (var keyword in synonymsKeywords.SynonymKeywords) { listHash.Add(Tools.getCRC32(AutoCorrector.Correct(keyword.Replace('-', ' ')).ToLower())); } webProductIdentity.SynonymsKeywordSet.Add(new SynonymsKeywordHash(listHash)); } foreach (var keyword in productIdentity.ExcludeKeywords) { webProductIdentity.KeywordBlackList.Add(Tools.getCRC32(AutoCorrector.Correct(keyword.Replace('-', ' ')).ToLower())); } foreach (var synonymsKeywords in webProductIdentity.SynonymsKeywordSet) { foreach (var hash in synonymsKeywords.KeywordHashList) { if (ProductIdentitiesDict.ContainsKey(hash)) { ProductIdentitiesDict[hash].Add(webProductIdentity); } else { ProductIdentitiesDict.Add(hash, new List <WebProductIdentity> { webProductIdentity }); } } } } var buidDuration = (DateTime.Now - startTime).TotalSeconds; Log.InfoFormat("Build ProductIdentities Dict. GetDB Time: {0}, Build Time: {1} s", getDBDuration, buidDuration); cacheServer.Set(cacheKey, ProductIdentitiesDict, true, new TimeSpan(20, 0, 0, 0)); } }
public static long IdentifyProduct(string name, long price) { long result = 0; if (string.IsNullOrEmpty(name)) { result = 0; } else { name = AutoCorrector.Correct(name.Replace('-', ' ').Replace('/', ' ').ToLower()); var length = name.Split(' ').Length; var ngrams = new List <string>(); for (int i = 1; i <= length; i++) { ngrams.AddRange(Tools.NGram(i, name)); } var hashSet = new HashSet <int>(); for (int i = 0; i < ngrams.Count; i++) { var stringHash = Tools.getCRC32(ngrams[i]); if (!hashSet.Contains(stringHash)) { hashSet.Add(stringHash); } } var productIdentitySet = new Dictionary <long, WebProductIdentity>(); foreach (var hash in hashSet) { if (ProductIdentitiesDict.ContainsKey(hash)) { foreach (var webProductIdentity in ProductIdentitiesDict[hash]) { if (!productIdentitySet.ContainsKey(webProductIdentity.ProductID)) { productIdentitySet.Add(webProductIdentity.ProductID, webProductIdentity); } } } } foreach (var webProductIdentity in productIdentitySet) { if (webProductIdentity.Value.Identify(hashSet, price)) { result = webProductIdentity.Value.ProductID; break; } } } return(result); }