import jnr.ffi.annotations.In; import org.junit.Test; import java.util.*; public class RecommendationSystemTest { @Test public void testCalculateSimilarity() { Map user1 = new HashMap<>(); user1.put("1", 2.5); user1.put("2", 3.5); user1.put("3", 3.0); //user1.put(4, 3.5); //user1.put(5, 2.5); //user1.put(6, 3.0); Map user2 = new HashMap<>(); user2.put("1", 2.5); user2.put("2", 3.5); user2.put("3", 3.5); user2.put("4", 4.5); user2.put("5", 5.0); //user2.put(3, 3.5); //user2.put(4, 4.0); RecommenderSystem rs = new RecommenderSystem(new HashMap<>(), 3); double similarity = rs.calculateSimilarity(user1, user2); System.out.println(similarity); // Check the similarity score //assertEquals(0.39605901719066977, similarity); } @Test public void testRecommendItems() { Map user1 = new HashMap<>(); user1.put("1", 2.5); user1.put("2", 3.5); user1.put("3", 3.0); //user1.put(5, 2.5); //user1.put(6, 3.0); Map user2 = new HashMap<>(); user2.put("1", 2.5); user2.put("2", 3.5); user2.put("3", 3.0); user2.put("4", 4.0); user2.put("7", 4.0); Map user3 = new HashMap<>(); user3.put("1", 2.5); user3.put("2", 3.5); user3.put("3", 3.5); user3.put("4", 4.5); user3.put("5", 4.0); user3.put("6", 1.0); Map> userItemRatingTable = new HashMap<>(); userItemRatingTable.put("1", user1); userItemRatingTable.put("2", user2); userItemRatingTable.put("3", user3); RecommenderSystem rs = new RecommenderSystem(userItemRatingTable, 2); Map recommendations = rs.recommendItems("1", 2); System.out.println(recommendations); } @Test public void testByExcel() { LinkedList> list = ExcelUtil.readExcel("/Users/alvin/Downloads/授用信各资方家数表.xlsx", "用信家数明细表", 0, 0); HashMap olds = new HashMap<>(); for (Map map : list) { for (String s : map.keySet()) { if (map.get(s) != null && !"企业名称".equalsIgnoreCase(s)) { olds.put(map.get("企业名称") + s, (int) Double.parseDouble(map.get(s) + "")); } } } HashSet ignore = new HashSet<>(); ignore.add("企业名称"); ignore.add("总计"); ignore.add("家数"); Map> userItemRatingTable = new HashMap<>(); for (Map map : list) { HashMap tmpMap = new HashMap<>(); for (String s : map.keySet()) { if (ignore.contains(s)) { continue; } if (map.get(s) == null) { tmpMap.put(s, 0D); } else { tmpMap.put(s, Double.valueOf(map.get(s) + "")); } } userItemRatingTable.put(map.get("企业名称") + "", tmpMap); } RecommenderSystem rs = new RecommenderSystem(userItemRatingTable, userItemRatingTable.size()); ArrayList> outRes = new ArrayList<>(); for (String companyName : userItemRatingTable.keySet()) { Map recommendations = rs.recommendItems(companyName); recommendations.entrySet().removeIf(entry -> entry.getValue() == 0.0); HashMap map = new HashMap<>(); map.putAll(recommendations); for (String s : map.keySet()) { if (olds.containsKey(companyName + s)) { map.put(s, "已有业务: " + olds.get(companyName + s)); } } map.put("企业名称", companyName); outRes.add(map); } ExcelUtil.writeExcel("系统过滤推荐.xlsx", "推荐结果", 0, Arrays.asList("企业名称", "工商银行", "瀚华普惠有限公司", "平安银行", "普惠", "苏宁银行", "天能惠商贷", "邮储银行", "邮惠万家"), outRes); } }