这是一个b站使用推荐引擎推荐的案例:
0点赞1收藏2转发
<select id="getAllUserPreference" resultType="com.imooc.bilibili.domain.UserPreference">
select
userId,
videoId,
sum(case operationType
when '0' then 6
when '1' then 2
when '2' then 2
else 0 end
) as `value`
from
t_video_operation
group by userId, videoId
</select>
public class UserPreference {
private Long id;
private Long userId;
private Long videoId;
private Float value;
private Date createTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getVideoId() {
return videoId;
}
public void setVideoId(Long videoId) {
this.videoId = videoId;
}
public Float getValue() {
return value;
}
public void setValue(Float value) {
this.value = value;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
/**
* 基于用户的协同推荐
* @param userId 用户id
*/
public List<Video> recommend(Long userId) throws TasteException {
List<UserPreference> list = videoDao.getAllUserPreference();
//创建数据模型
DataModel dataModel = this.createDataModel(list);
//获取用户相似程度
UserSimilarity similarity = new UncenteredCosineSimilarity(dataModel);
System.out.println(similarity.userSimilarity(11, 12));
//获取用户邻居
UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(2, similarity, dataModel);
long[] ar = userNeighborhood.getUserNeighborhood(userId);
//构建推荐器
Recommender recommender = new GenericUserBasedRecommender(dataModel, userNeighborhood, similarity);
//推荐视频
List<RecommendedItem> recommendedItems = recommender.recommend(userId, 5);
List<Long> itemIds = recommendedItems.stream().map(RecommendedItem::getItemID).collect(Collectors.toList());
return videoDao.batchGetVideosByIds(itemIds);
}
/**
* 基于内容的协同推荐
* @param userId 用户id
* @param itemId 参考内容id(根据该内容进行相似内容推荐)
* @param howMany 需要推荐的数量
*/
public List<Video> recommendByItem(Long userId, Long itemId, int howMany) throws TasteException {
List<UserPreference> list = videoDao.getAllUserPreference();
//创建数据模型
DataModel dataModel = this.createDataModel(list);
//获取内容相似程度
ItemSimilarity similarity = new UncenteredCosineSimilarity(dataModel);
GenericItemBasedRecommender genericItemBasedRecommender = new GenericItemBasedRecommender(dataModel, similarity);
// 物品推荐相拟度,计算两个物品同时出现的次数,次数越多任务的相拟度越高
List<Long> itemIds = genericItemBasedRecommender.recommendedBecause(userId, itemId, howMany)
.stream()
.map(RecommendedItem::getItemID)
.collect(Collectors.toList());
//推荐视频
return videoDao.batchGetVideosByIds(itemIds);
}
private DataModel createDataModel(List<UserPreference> userPreferenceList) {标签:return,taste,List,推荐,userId,Long,Mahout,new,public From: https://www.cnblogs.com/15078480385zyc/p/18114129
FastByIDMap<PreferenceArray> fastByIdMap = new FastByIDMap<>();
Map<Long, List<UserPreference>> map = userPreferenceList.stream().collect(Collectors.groupingBy(UserPreference::getUserId));
Collection<List<UserPreference>> list = map.values();
for(List<UserPreference> userPreferences : list){
GenericPreference[] array = new GenericPreference[userPreferences.size()];
for(int i = 0; i < userPreferences.size(); i++){
UserPreference userPreference = userPreferences.get(i);
GenericPreference item = new GenericPreference(userPreference.getUserId(), userPreference.getVideoId(), userPreference.getValue());
array[i] = item;
}
fastByIdMap.put(array[0].getUserID(), new GenericUserPreferenceArray(Arrays.asList(array)));
}
return new GenericDataModel(fastByIdMap);
}