Пример #1
0
        public UserNeighbourhood GetUserNeighbourhood(int userId)
        {
            UserNeighbourhood userNeighbourhood = db.UserNeighbourhoods.Find(userId);

            if (userNeighbourhood == null)
            {
                throw new MissingMemberException();
            }
            return(userNeighbourhood);
        }
Пример #2
0
        private void predictUserPreference(int userID, UserActivityList userprofile, ref List <Activities> prediction_u_i)
        {
            UserNeighbourhood neighbours = unc.GetUserNeighbourhood(userID);

            var similarity = neighbours.similiarity.Split(';').Select(double.Parse).ToList();
            var neighbour  = neighbours.neighbours.Split(';').Select(int.Parse).ToList();

            List <string> allActivities = uac.GetAllActivities();

            double averageUserProfile = userprofile.Activities.Sum(x => x.Rating) / userprofile.Activities.Count;

            userprofile.Activities.ForEach(x => allActivities.Remove(x.Activity));

            foreach (var activity in allActivities)
            {
                double top    = 0;
                double bottom = 0;
                double rating = 0.0;

                for (var i = 0; i < neighbour.Count; i++)
                {
                    if (neighbour[i] != userID)
                    {
                        var    neighbourProfile    = uac.GetUsersActivityList(neighbour[i]);
                        double avgNProfile         = neighbourProfile.Activities.Sum(x => x.Rating) / neighbourProfile.Activities.Count;
                        double ratingItemNeighbour = 0.0;
                        if (neighbourProfile.Activities.Exists(x => x.Activity == activity))
                        {
                            ratingItemNeighbour = neighbourProfile.Activities.First(x => x.Activity == activity).Rating;
                        }

                        top    = top + (similarity[i] * (ratingItemNeighbour - avgNProfile));
                        bottom = bottom + Math.Abs(similarity[i]);
                    }
                }
                if (bottom == 0)
                {
                    rating = averageUserProfile;
                }
                else
                {
                    rating = averageUserProfile + (top / bottom);
                }

                prediction_u_i.Add(new Activities(activity, rating));
            }
        }
Пример #3
0
        public void AddUserNeighbourhood(int userId, List <int> neighbours, List <double> similarity)
        {
            UserNeighbourhood row = db.UserNeighbourhoods.Find(userId);

            if (row == null)
            {
                UserNeighbourhood un = new UserNeighbourhood {
                    userId = userId, neighbours = string.Join(";", neighbours), similiarity = string.Join(";", similarity)
                };
                db.UserNeighbourhoods.Add(un);
            }
            else
            {
                row.neighbours = string.Join(";", neighbours);
            }
            row.similiarity = string.Join(";", similarity);
            db.SaveChanges();
        }