recommend.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { CloudObject, CloudQuery } from "../../../fashion-server/lib/ncloud.js";
  2. export class RecommendationService {
  3. constructor() {
  4. this.perferQuery = new CloudQuery('Perfer'); // 偏好数据存储在 perfer 类中
  5. }
  6. /**
  7. * 获取用户的物品推荐
  8. * @param userId 用户ID
  9. * @param howMany 推荐数量
  10. * @returns 推荐的物品ID列表
  11. */
  12. async getItemRecommendations(userId, howMany) {
  13. // 获取用户的偏好数据
  14. const userPreferences = await this.getUserPreferences(userId);
  15. if (!userPreferences || userPreferences.length === 0) {
  16. return [];
  17. }
  18. // 计算物品相似度
  19. const similarItems = await this.calculateItemSimilarity(userPreferences);
  20. // 获取推荐物品
  21. return this.getTopRecommendations(similarItems, howMany);
  22. }
  23. /**
  24. * 获取用户的偏好数据
  25. * @param userId 用户ID
  26. * @returns 用户偏好物品的列表
  27. */
  28. async getUserPreferences(userId) {
  29. this.perferQuery.equalTo('user_id', userId); // 查询条件
  30. const preferences = await this.perferQuery.find(); // 获取用户的偏好数据
  31. return preferences; // 返回用户偏好物品列表
  32. }
  33. /**
  34. * 计算物品相似度
  35. * @param userPreferences 用户偏好物品
  36. * @returns 物品相似度的映射
  37. */
  38. async calculateItemSimilarity(userPreferences) {
  39. const itemSimilarity = new Map();
  40. // 遍历用户偏好物品,计算与其他物品的相似度
  41. for (const pref of userPreferences) {
  42. const itemId = pref.item_id; // 直接访问属性
  43. const preferenceValue = pref.preference; // 直接访问属性
  44. // 获取所有物品的偏好数据
  45. const allPreferences = await this.perferQuery.find();
  46. for (const otherPref of allPreferences) {
  47. const otherItemId = otherPref.item_id; // 直接访问属性
  48. const otherPreferenceValue = otherPref.preference; // 直接访问属性
  49. if (itemId !== otherItemId) { // 排除自身
  50. const similarityScore = this.computeSimilarity(preferenceValue, otherPreferenceValue);
  51. itemSimilarity.set(otherItemId, (itemSimilarity.get(otherItemId) || 0) + similarityScore);
  52. }
  53. }
  54. }
  55. return itemSimilarity;
  56. }
  57. /**
  58. * 计算两个物品之间的相似度
  59. * @param preference1 第一个物品的偏好值
  60. * @param preference2 第二个物品的偏好值
  61. * @returns 相似度分数
  62. */
  63. computeSimilarity(preference1, preference2) {
  64. // 示例:使用简单的相似度计算(如绝对值差)
  65. return Math.abs(preference1 - preference2); // 这里是一个简单的示例
  66. }
  67. /**
  68. * 获取前 N 个推荐物品
  69. * @param similarItems 物品相似度映射
  70. * @param howMany 推荐数量
  71. * @returns 推荐物品ID列表
  72. */
  73. getTopRecommendations(similarItems, howMany) {
  74. // 将相似度映射转换为数组并排序
  75. const sortedItems = Array.from(similarItems.entries()).sort((a, b) => b[1] - a[1]);
  76. // 获取前 N 个物品ID
  77. return sortedItems.slice(0, howMany).map(item => item[0]);
  78. }
  79. }
  80. // 使用示例
  81. let recommendationService = new RecommendationService();
  82. recommendationService.getItemRecommendations('266024222614683648', 2).then(result => {
  83. console.log(result);
  84. }).catch(error => {
  85. console.error("Error fetching recommendations:", error);
  86. });