Example #1
0
        /// <summary>
        /// 计算从源用户到目标用户的相似度
        /// </summary>
        /// <param name="user">目标用户</param>
        /// <param name="srcClothesList">源用户衣物列表用户</param>
        /// <returns>相似度值 范围为0-1 值越大相似度越高</returns>
        private static double calculateUserSimilarity(User user, List <Clothes.Clothes> srcClothesList)
        {
            //获取匹配目标用户最近常穿的衣橱
            IEnumerable <int> wardrobeIDs = WardrobeManager.getByUser(user.id).wardrobes.ToList().Select(x => x.id);

            IEnumerable <IEnumerable <int> > clothesIDLists = wardrobeIDs.Select(x => ClothesManager.getByWardrobe(x).clothes.Select(y => y.id));

            List <Clothes.Clothes> clothesList = new List <Clothes.Clothes>();

            foreach (var clothesIDs in clothesIDLists)
            {
                foreach (var clothesID in clothesIDs)
                {
                    clothesList.Add(ClothesManager.get(clothesID));
                }
            }

            IEnumerable <Clothes.Clothes> mostDressedClothes = clothesList.OrderByDescending(x => x.wearingFrequency).OrderByDescending(x => x.lastWearingTime).Take(Config.matchClothesNum);

            double ans = 0;

            foreach (var clothes in mostDressedClothes)
            {
                double maxSimilarity = double.MinValue;

                foreach (var targetClothes in srcClothesList)
                {
                    double temp = ClothesManager.getSimilarity(new KeyValuePair <Clothes.Clothes, Clothes.Clothes>(targetClothes, clothes));

                    if (temp > maxSimilarity)
                    {
                        maxSimilarity = temp;
                    }
                }

                if (maxSimilarity > double.MinValue)
                {
                    ans += maxSimilarity * Config.clothesTypeCoeffiencient[clothes.type];
                }
            }

            if (mostDressedClothes.Any())
            {
                ans /= mostDressedClothes.Sum(x => Config.clothesTypeCoeffiencient[x.type]);
            }

            return(ans);
        }
Example #2
0
 public ClothesResponseList getByWardrobe(int id)
 {
     return(ClothesManager.getByWardrobe(id));
 }
Example #3
0
        /// <summary>
        /// 查找与指定用户衣物喜好相似的用户
        /// </summary>
        /// <param name="id">源用户ID</param>
        /// <returns>找到的所有相似用户ID列表</returns>
        public static IEnumerable <User> similarUsers(string id)
        {
            User user = userDb.GetById(id);

            if (user == null || WardrobeManager.getByUser(id).wardrobes.Count() == 0)
#if DEBUG
            { throw new Exception(); }
#else
            { return(new List <User>()); }
#endif

            //获取源用户衣物列表
            IEnumerable <int> wardrobeIDs = WardrobeManager.getByUser(user.id).wardrobes.ToList().Select(x => x.id);

            IEnumerable <IEnumerable <int> > clothesIDLists = wardrobeIDs.Select(x => ClothesManager.getByWardrobe(x).clothes.Select(y => y.id));

            List <Clothes.Clothes> clothes = new List <Clothes.Clothes>();

            foreach (var clothesIDs in clothesIDLists)
            {
                foreach (var clothesID in clothesIDs)
                {
                    clothes.Add(ClothesManager.get(clothesID));
                }
            }

            Dictionary <double, User> similarUsers = new Dictionary <double, User>();

            foreach (User anotherUser in userDb.GetList())
            {
                if (anotherUser.id == user.id)
                {
                    continue;
                }

                double similarity = calculateUserSimilarity(anotherUser, clothes);

                if (similarUsers.Count == 0 || similarity > similarUsers.Keys.Min())
                {
                    if (similarUsers.Count == 0)
                    {
                        similarUsers.Add(similarity, anotherUser);
                    }

                    if (similarUsers.Count >= Config.matchUserNum)
                    {
                        similarUsers.Remove(similarUsers.Min().Key);
                    }

                    if (similarUsers.ContainsKey(similarity))
                    {
                        similarity += 0.000001;
                    }

                    similarUsers.Add(similarity, anotherUser);
                }
            }

            return(similarUsers.Values);
        }