org.conqat.lib.commons.datamining.RecommenderRatingDatabase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
/*
* 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.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.ListMap;
import org.conqat.lib.commons.collections.UnmodifiableSet;
/**
* Binary rating database. A rating is either 'like' or 'unrated'.
*/
public class RecommenderRatingDatabase implements Serializable {
/** Serial ID */
private static final long serialVersionUID = 1L;
/** The data */
private final ListMap data = new ListMap<>();
/** Returns all users contained in this database. */
public UnmodifiableSet getUsers() {
return CollectionUtils.asUnmodifiable(data.getKeys());
}
/** Adds the given users to this rating database. */
public void add(IRecommenderUser user, Set likedItems) {
data.addAll(user, likedItems);
}
/** Removes the user completely */
public void remove(IRecommenderUser user) {
data.removeCollection(user);
}
/**
* Returns the items liked by the given user. If no entry for this user is found an
* IllegalArgumentException is thrown.
*/
public Set getLikedItems(IRecommenderUser user) {
if (!data.containsCollection(user)) {
throw new IllegalArgumentException("No such user: " + user);
}
return new HashSet<>(data.getCollection(user));
}
/**
* Constructs a new {@link RecommenderRatingDatabase} from the given shopping baskets
*/
public static RecommenderRatingDatabase fromShoppingBaskets(Set> shoppingBaskets) {
RecommenderRatingDatabase ratingDatabase = new RecommenderRatingDatabase<>();
for (Set basket : shoppingBaskets) {
ratingDatabase.add(new ShoppingBasketUser(basket), basket);
}
return ratingDatabase;
}
/** {@inheritDoc} */
@Override
public String toString() {
return data.toString();
}
}