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

org.conqat.lib.commons.datamining.TopNRecommender 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.HashSet;
import java.util.List;
import java.util.Set;

import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.CounterSet;
import org.conqat.lib.commons.collections.UnmodifiableSet;

/**
 * Trivial recommender that always returns the top n used items from the training data, independent
 * of the query. The confidence is set to a fixed value of .5 for all recommendations.
 */
public class TopNRecommender implements IRecommender {

	/** The fixed set of recommendations */
	private final Set> recommendations = new HashSet<>();

	/**
	 * Constructs a new {@link TopNRecommender} using the given rating data base. There have to be at
	 * least numRecommendations entries in the data base.
	 */
	public TopNRecommender(RecommenderRatingDatabase ratingDatabase, int numRecommendations) {
		CounterSet occurences = new CounterSet<>();
		for (IRecommenderUser user : ratingDatabase.getUsers()) {
			occurences.incAll(ratingDatabase.getLikedItems(user));
		}

		CCSMAssert.isTrue(occurences.getKeys().size() >= numRecommendations,
				"There have to be at least numRecommendation distinct items");

		List topItems = occurences.getKeysByValueDescending();

		for (int i = 0; i < numRecommendations; i++) {
			// We give each recommendation a fixed 'dummy' confidence of .5
			recommendations.add(new Recommendation(topItems.get(i), .5D));
		}
	}

	/** {@inheritDoc} */
	@Override
	public UnmodifiableSet> recommend(IRecommenderUser user) {
		return CollectionUtils.asUnmodifiable(recommendations);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy