RecommendationSystemTest.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import jnr.ffi.annotations.In;
  2. import org.junit.Test;
  3. import java.util.*;
  4. public class RecommendationSystemTest {
  5. @Test
  6. public void testCalculateSimilarity() {
  7. Map<String, Double> user1 = new HashMap<>();
  8. user1.put("1", 2.5);
  9. user1.put("2", 3.5);
  10. user1.put("3", 3.0);
  11. //user1.put(4, 3.5);
  12. //user1.put(5, 2.5);
  13. //user1.put(6, 3.0);
  14. Map<String, Double> user2 = new HashMap<>();
  15. user2.put("1", 2.5);
  16. user2.put("2", 3.5);
  17. user2.put("3", 3.5);
  18. user2.put("4", 4.5);
  19. user2.put("5", 5.0);
  20. //user2.put(3, 3.5);
  21. //user2.put(4, 4.0);
  22. RecommenderSystem rs = new RecommenderSystem(new HashMap<>(), 3);
  23. double similarity = rs.calculateSimilarity(user1, user2);
  24. System.out.println(similarity);
  25. // Check the similarity score
  26. //assertEquals(0.39605901719066977, similarity);
  27. }
  28. @Test
  29. public void testRecommendItems() {
  30. Map<String, Double> user1 = new HashMap<>();
  31. user1.put("1", 2.5);
  32. user1.put("2", 3.5);
  33. user1.put("3", 3.0);
  34. //user1.put(5, 2.5);
  35. //user1.put(6, 3.0);
  36. Map<String, Double> user2 = new HashMap<>();
  37. user2.put("1", 2.5);
  38. user2.put("2", 3.5);
  39. user2.put("3", 3.0);
  40. user2.put("4", 4.0);
  41. user2.put("7", 4.0);
  42. Map<String, Double> user3 = new HashMap<>();
  43. user3.put("1", 2.5);
  44. user3.put("2", 3.5);
  45. user3.put("3", 3.5);
  46. user3.put("4", 4.5);
  47. user3.put("5", 4.0);
  48. user3.put("6", 1.0);
  49. Map<String, Map<String, Double>> userItemRatingTable = new HashMap<>();
  50. userItemRatingTable.put("1", user1);
  51. userItemRatingTable.put("2", user2);
  52. userItemRatingTable.put("3", user3);
  53. RecommenderSystem rs = new RecommenderSystem(userItemRatingTable, 2);
  54. Map<String, Double> recommendations = rs.recommendItems("1", 2);
  55. System.out.println(recommendations);
  56. }
  57. @Test
  58. public void testByExcel() {
  59. LinkedList<Map<String, Object>> list = ExcelUtil.readExcel("/Users/alvin/Downloads/授用信各资方家数表.xlsx", "用信家数明细表", 0, 0);
  60. HashMap<String, Object> olds = new HashMap<>();
  61. for (Map<String, Object> map : list) {
  62. for (String s : map.keySet()) {
  63. if (map.get(s) != null && !"企业名称".equalsIgnoreCase(s)) {
  64. olds.put(map.get("企业名称") + s, (int) Double.parseDouble(map.get(s) + ""));
  65. }
  66. }
  67. }
  68. HashSet<String> ignore = new HashSet<>();
  69. ignore.add("企业名称");
  70. ignore.add("总计");
  71. ignore.add("家数");
  72. Map<String, Map<String, Double>> userItemRatingTable = new HashMap<>();
  73. for (Map<String, Object> map : list) {
  74. HashMap<String, Double> tmpMap = new HashMap<>();
  75. for (String s : map.keySet()) {
  76. if (ignore.contains(s)) {
  77. continue;
  78. }
  79. if (map.get(s) == null) {
  80. tmpMap.put(s, 0D);
  81. } else {
  82. tmpMap.put(s, Double.valueOf(map.get(s) + ""));
  83. }
  84. }
  85. userItemRatingTable.put(map.get("企业名称") + "", tmpMap);
  86. }
  87. RecommenderSystem rs = new RecommenderSystem(userItemRatingTable, userItemRatingTable.size());
  88. ArrayList<Map<String, Object>> outRes = new ArrayList<>();
  89. for (String companyName : userItemRatingTable.keySet()) {
  90. Map<String, Double> recommendations = rs.recommendItems(companyName);
  91. recommendations.entrySet().removeIf(entry -> entry.getValue() == 0.0);
  92. HashMap<String, Object> map = new HashMap<>();
  93. map.putAll(recommendations);
  94. for (String s : map.keySet()) {
  95. if (olds.containsKey(companyName + s)) {
  96. map.put(s, "已有业务: " + olds.get(companyName + s));
  97. }
  98. }
  99. map.put("企业名称", companyName);
  100. outRes.add(map);
  101. }
  102. ExcelUtil.writeExcel("系统过滤推荐.xlsx", "推荐结果", 0,
  103. Arrays.asList("企业名称", "工商银行", "瀚华普惠有限公司", "平安银行", "普惠", "苏宁银行", "天能惠商贷", "邮储银行", "邮惠万家"),
  104. outRes);
  105. }
  106. }