@Override public List<Activity> getRecommendations(Long myUserId) { Long closestUserId = getClosestUser(createDistancesVector(myUserId)); Iterable<UserRating> myRatings = userRatingRepository.findByUserId(myUserId); Iterable<UserRating> ratings = userRatingRepository.findByUserId(closestUserId); Iterator<UserRating> ratingsIterator = ratings.iterator(); List<Activity> recommendations = new ArrayList<Activity>(); while (ratingsIterator.hasNext()) { Iterator<UserRating> myRatingsIterator = myRatings.iterator(); UserRating rating = ratingsIterator.next(); boolean activityFound = false; if (rating.getRating() >= 3) { while (myRatingsIterator.hasNext()) { UserRating myRating = myRatingsIterator.next(); if (rating.getActivity().getId() == myRating.getActivity().getId()) { activityFound = true; break; } } if (!activityFound) { recommendations.add(rating.getActivity()); } } } return recommendations; }
@Override public double[] createActivityVector(Long userId) { List<UserRating> ratings = userRatingRepository.findByUserId(userId); int count = countAllActivities().intValue(); int ratingsNo = ratings.size(); double[] activitiesRatings = new double[count]; for (int i = 0; i < ratingsNo; i++) { activitiesRatings[ratings.get(i).getActivity().getId().intValue()] = ratings.get(i).getRating(); } return activitiesRatings; }