Esempio n. 1
0
        //List<ProductLink> create_identical_Product_list_for_training(int product1_id, int company2_id)
        //{
        //    List<ProductLink> pls = (from x in Companies.Get(company2_id).DbCompany.Products select new ProductLink(this, Products.Get(product1_id), Products.Get(x.Id))).ToList();
        //    pls = pls.OrderByDescending(x => x.Score).OrderByDescending(x => x.SecondaryScore).ToList();
        //    return pls;
        //}
        #endregion

        #region API for analysing data
        //!!!Update Configuration in Engine being kept for reuse, after performing self-training!!!
        public void PerformDataAnalysis(int company_id)
        {
            lock (this)
            {
                Company c = Companies.Get(company_id);
                c.PrepareForDataAnalysis();
                foreach (string w in Companies.Get(company_id).Words2ProductIds(Field.Name).Keys)
                {
                    c.DefineWordWeight(w);
                }
                c.SaveAfterDataAnalysis();
            }
        }
Esempio n. 2
0
        //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);
            }
        }