void analyse_link(int[] linked_product_ids) { lock (this) { for (int i = 0; i < linked_product_ids.Length; i++) { for (int j = i + 1; j < linked_product_ids.Length; j++) { int product1_id = linked_product_ids[i]; int product2_id = linked_product_ids[j]; Product product1 = Products.Get(product1_id); Product product2 = Products.Get(product2_id); CompanyPairs.MapCategories(product1, product2); //Dictionary<Field, HashSet<string>> matched_words = new Dictionary<Field, HashSet<string>>(); ////matched_words[Field.Category] = new HashSet<string>(); ////foreach (string word in product1.Words(Field.Category)) //// if (product2.Words2Count(Field.Category).ContainsKey(word)) //// matched_words[Field.Category].Add(word); //matched_words[Field.Name] = new HashSet<string>(); //foreach (string word in product1.Words(Field.Name)) // if (product2.Words2Count(Field.Name).ContainsKey(word)) // matched_words[Field.Name].Add(word); //List<ProductLink> pls = create_identical_Product_list_for_training(product1_id, product2.DbProduct.CompanyId); //foreach (ProductLink pl in pls) //{ // if (null != pl.Product2s.Where(x => x.DbProduct.Id == product2_id).FirstOrDefault()) // break; // foreach (Product p2 in pl.Product2s) // { // if (product1_id == p2.DbProduct.Id) // continue; // Dictionary<Field, List<string>> mws = pl.Get(product1_id, p2.DbProduct.Id).MatchedWords; // //List<string> week_mws = mws[Field.Category].Where(x => !matched_words[Field.Category].Contains(x)).ToList(); // //foreach (string word in week_mws) // //{ // // Configuration.Get(product1).SetWordWeight(word, 0.9 * Configuration.Get(product1).GetWordWeight(word)); // // Configuration.Get(product2).SetWordWeight(word, 0.9 * Configuration.Get(product2).GetWordWeight(word)); // //} // List<string> week_mws = mws[Field.Name].Where(x => !matched_words[Field.Name].Contains(x)).ToList(); // foreach (string word in week_mws) // { // Configuration.Company c1 = Configuration.Get(product1); // c1.SetWordWeight(word, 0.9 * c1.GetWordWeight(word)); // Configuration.Company c2 = Configuration.Get(product2); // c2.SetWordWeight(word, 0.9 * c2.GetWordWeight(word)); // } // } //} } } } }
//public void RenewConfiguration(bool auto_data_analysing) //{ // Configuration_ = new Configuration(this, auto_data_analysing); //} public List <ProductLink> CreateProductLinkList(int[] product1_ids, int company2_id /*, string[] keyword2s = null*/) { lock (this) { if (product1_ids.Length < 1) { throw new Exception("product1_ids is empty"); } Fhr.ProductOffice.Models.Product p1 = Db.Products.Where(p => product1_ids.Contains(p.Id) && p.CompanyId == company2_id).FirstOrDefault(); if (p1 != null) { throw new Exception("Product Id:" + p1.Id + " already belongs to company Id:" + p1.CompanyId + " " + p1.Company.Name + " so no more link can be found."); } //List<int> cis = (from x in Db.Products.Where(p => product1_ids.Contains(p.Id)) join y in Db.Products on x.LinkId equals y.LinkId select y.CompanyId).ToList(); //cis.Add(company2_id); //HashSet<int> cis_ = new HashSet<int>(cis); //foreach (int company_id in cis_) //{ // Configuration.Company c = Configuration.Get(company_id); // if (c.IsDataAnalysisRequired()) // { // PerformDataAnalysis(company_id); // c = new Company(this, company_id); // company_ids2Company[company_id] = c; // } //} Product[] product1s = (from x in product1_ids select Products.Get(x)).ToArray(); List <ProductLink> pls; //if (keyword2s != null && keyword2s.Length > 0) //{ // keyword2s = keyword2s.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim().ToLower()).ToArray(); // HashSet<int> product2_ids = null; // foreach (string keyword2 in keyword2s) // { // HashSet<int> p2_ids = Company.Get(company2_id).Words2ProductIds(Field.Name)[keyword2]; // if (product2_ids == null) // product2_ids = p2_ids; // else // product2_ids = (HashSet<int>)product2_ids.Intersect(p2_ids); // } // List<int> link_ids = (from p2_id in product2_ids let link_id = Product.Get(p2_id).DbProduct.LinkId where link_id > 0 group link_id by link_id into g select (int)g.Key).ToList(); // pls = (from x in link_ids select new ProductLink(product1s, Product.GetLinked(x))).ToList(); // List<int> free_product_ids = (from p2_id in product2_ids let link_id = Product.Get(p2_id).DbProduct.LinkId where link_id == null || link_id <= 0 select p2_id).ToList(); // List<ProductLink> pls2 = (from x in free_product_ids select new ProductLink(product1s, new Product[] { Product.Get(x) })).ToList(); // pls.AddRange(pls2); //} //else //{ //sw1.Start(); List <int> company2_link_ids = new List <int>(); IDataReader dr = Dbc["SELECT LinkId FROM Products WHERE CompanyId=@CompanyId AND LinkId>0 GROUP BY LinkId"].GetReader("@CompanyId", company2_id); //it's faster than LINQ while (dr.Read()) { company2_link_ids.Add((int)dr[0]); } dr.Close(); pls = (from x in company2_link_ids select new ProductLink(this, product1s, Products.GetLinked(x))).ToList(); List <int> free_product_ids = (from p in Companies.Get(company2_id).DbCompany.Products where p.LinkId == null || p.LinkId <= 0 select p.Id).ToList(); List <ProductLink> pls2 = (from x in free_product_ids select new ProductLink(this, product1s, new Product[] { Products.Get(x) })).ToList(); pls.AddRange(pls2); //} //pls = pls.OrderByDescending(x => x.Score).OrderByDescending(x => x.SecondaryScore).ToList(); pls = pls.OrderByDescending(x => x.Score).ToList(); //sw1.Stop(); //string s = "1: " + sw1.ElapsedMilliseconds + ", 2: " + sw2.ElapsedMilliseconds + ", 3: " + sw3.ElapsedMilliseconds + ", 4: " + sw4.ElapsedMilliseconds // + ", 5: " + sw5.ElapsedMilliseconds + ", 6: " + sw6.ElapsedMilliseconds + ", 7: " + sw7.ElapsedMilliseconds // + ", 8: " + sw8.ElapsedMilliseconds + ", 9: " + sw9.ElapsedMilliseconds + ", 10: " + sw10.ElapsedMilliseconds; return(pls); } }