All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.conqat.lib.commons.datamining.CFRatingRecommender Maven / Gradle / Ivy

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.conqat.lib.commons.datamining;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.conqat.lib.commons.collections.Pair;

/**
 * A user rating-based recommender using collaborative filtering.
 */
public class CFRatingRecommender implements IRecommender {

	/** The database containing the ratings. */
	private RecommenderRatingDatabase ratingDatabase;

	/** Number of neighbors to consider for computing recommendations */
	private int numNeighbors;

	/** Maximum number of recommendations */
	private int maxRecommendations;

	/** Constructor */
	public CFRatingRecommender(RecommenderRatingDatabase ratingDatabase, int numNeighbors, int maxRecommendations) {
		this.ratingDatabase = ratingDatabase;
		this.numNeighbors = numNeighbors;
		this.maxRecommendations = maxRecommendations;
	}

	/** {@inheritDoc} */
	@Override
	public Set> recommend(IRecommenderUser queryUser) {
		Set users = ratingDatabase.getUsers();
		List> neighbors = new ArrayList<>();
		for (IRecommenderUser user : users) {
			if (user.equals(queryUser)) {
				continue;
			}
			neighbors.add(new Pair<>(user.similarity(queryUser), user));
		}

		Collections.sort(neighbors);
		Collections.reverse(neighbors);

		neighbors = neighbors.subList(0, numNeighbors);

		Set> result = new HashSet<>();
		final Map recommendedItems = new HashMap<>();
		double sumSimilarity = 0;

		Set userItems = ratingDatabase.getLikedItems(queryUser);
		for (Pair neighbor : neighbors) {
			sumSimilarity += neighbor.getFirst();
			Set neighborItems = ratingDatabase.getLikedItems(neighbor.getSecond());
			for (T item : neighborItems) {
				if (!userItems.contains(item)) {
					if (!recommendedItems.containsKey(item)) {
						recommendedItems.put(item, 0D);
					}
					recommendedItems.put(item, recommendedItems.get(item) + neighbor.getFirst());
				}
			}
		}

		List sortedItems = new ArrayList<>(recommendedItems.keySet());
		Collections.sort(sortedItems, new Comparator() {

			@Override
			public int compare(T item1, T item2) {
				return recommendedItems.get(item2).compareTo(recommendedItems.get(item1));
			}
		});

		while (result.size() < maxRecommendations && !sortedItems.isEmpty()) {
			T item = sortedItems.get(0);
			double confidence = 0;
			if (sumSimilarity > 0) {
				confidence = recommendedItems.get(item) / sumSimilarity;
			}
			result.add(new Recommendation(item, confidence));
			sortedItems.remove(0);
		}

		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy