基于内容推荐算法java代码
时间: 2023-07-29 09:09:23 浏览: 105
基于内容的推荐算法
4星 · 用户满意度95%
以下是一个简单的基于内容推荐算法的Java代码示例:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ContentBasedRecommendation {
public static void main(String[] args) {
// 创建用户喜好矩阵
Map<String, Map<String, Integer>> userItemMatrix = new HashMap<>();
Map<String, Integer> user1 = new HashMap<>();
user1.put("item1", 5);
user1.put("item2", 3);
user1.put("item3", 2);
user1.put("item4", 4);
user1.put("item5", 1);
userItemMatrix.put("user1", user1);
Map<String, Integer> user2 = new HashMap<>();
user2.put("item1", 2);
user2.put("item2", 1);
user2.put("item3", 4);
user2.put("item4", 3);
user2.put("item5", 5);
userItemMatrix.put("user2", user2);
Map<String, Integer> user3 = new HashMap<>();
user3.put("item1", 3);
user3.put("item2", 2);
user3.put("item3", 5);
user3.put("item4", 1);
user3.put("item5", 4);
userItemMatrix.put("user3", user3);
// 创建物品特征矩阵
Map<String, Map<String, Integer>> itemFeatureMatrix = new HashMap<>();
Map<String, Integer> item1 = new HashMap<>();
item1.put("feature1", 5);
item1.put("feature2", 2);
item1.put("feature3", 1);
itemFeatureMatrix.put("item1", item1);
Map<String, Integer> item2 = new HashMap<>();
item2.put("feature1", 3);
item2.put("feature2", 5);
item2.put("feature3", 2);
itemFeatureMatrix.put("item2", item2);
Map<String, Integer> item3 = new HashMap<>();
item3.put("feature1", 4);
item3.put("feature2", 1);
item3.put("feature3", 5);
itemFeatureMatrix.put("item3", item3);
Map<String, Integer> item4 = new HashMap<>();
item4.put("feature1", 2);
item4.put("feature2", 4);
item4.put("feature3", 3);
itemFeatureMatrix.put("item4", item4);
Map<String, Integer> item5 = new HashMap<>();
item5.put("feature1", 1);
item5.put("feature2", 3);
item5.put("feature3", 4);
itemFeatureMatrix.put("item5", item5);
// 对用户进行推荐
String userId = "user1";
List<String> recommendedItems = recommend(userItemMatrix, itemFeatureMatrix, userId);
System.out.println("为用户 " + userId + " 推荐的物品列表:" + recommendedItems);
}
public static List<String> recommend(Map<String, Map<String, Integer>> userItemMatrix,
Map<String, Map<String, Integer>> itemFeatureMatrix,
String userId) {
// 获取用户评分过的物品
Map<String, Integer> userRatings = userItemMatrix.get(userId);
// 计算用户的特征向量
Map<String, Double> userVector = new HashMap<>();
for (String feature : itemFeatureMatrix.get("item1").keySet()) {
double sum = 0.0;
for (String itemId : userRatings.keySet()) {
int rating = userRatings.get(itemId);
int featureValue = itemFeatureMatrix.get(itemId).getOrDefault(feature, 0);
sum += rating * featureValue;
}
userVector.put(feature, sum);
}
// 计算物品与用户特征向量的相似度
Map<String, Double> itemSimilarity = new HashMap<>();
for (String itemId : itemFeatureMatrix.keySet()) {
double dotProduct = 0.0;
double itemNorm = 0.0;
double userNorm = 0.0;
for (String feature : itemFeatureMatrix.get(itemId).keySet()) {
int itemValue = itemFeatureMatrix.get(itemId).get(feature);
double userValue = userVector.getOrDefault(feature, 0.0);
dotProduct += itemValue * userValue;
itemNorm += itemValue * itemValue;
userNorm += userValue * userValue;
}
itemSimilarity.put(itemId, dotProduct / Math.sqrt(itemNorm * userNorm));
}
// 对物品相似度进行排序,取前N个物品作为推荐结果
List<String> recommendedItems = new ArrayList<>();
itemSimilarity.entrySet().stream()
.sorted(Map.Entry.<String, Double>comparingByValue().reversed())
.forEach(entry -> {
if (!userRatings.containsKey(entry.getKey())) {
recommendedItems.add(entry.getKey());
}
});
return recommendedItems.subList(0, Math.min(recommendedItems.size(), 3));
}
}
```
该代码实现了一个简单的基于内容推荐算法,使用了用户喜好矩阵和物品特征矩阵来计算物品之间的相似度,并根据相似度对物品进行排序,最后推荐相似度最高的前N个物品。请注意,该代码的实现仅供参考,实际应用中需要根据具体情况进行修改和优化。
阅读全文