123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import jnr.ffi.annotations.In;
- import org.junit.Test;
- import java.util.*;
- public class RecommendationSystemTest {
- @Test
- public void testCalculateSimilarity() {
- Map<String, Double> 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<String, Double> 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<String, Double> 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<String, Double> 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<String, Double> 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<String, Map<String, Double>> userItemRatingTable = new HashMap<>();
- userItemRatingTable.put("1", user1);
- userItemRatingTable.put("2", user2);
- userItemRatingTable.put("3", user3);
- RecommenderSystem rs = new RecommenderSystem(userItemRatingTable, 2);
- Map<String, Double> recommendations = rs.recommendItems("1", 2);
- System.out.println(recommendations);
- }
- @Test
- public void testByExcel() {
- LinkedList<Map<String, Object>> list = ExcelUtil.readExcel("/Users/alvin/Downloads/授用信各资方家数表.xlsx", "用信家数明细表", 0, 0);
- HashMap<String, Object> olds = new HashMap<>();
- for (Map<String, Object> 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<String> ignore = new HashSet<>();
- ignore.add("企业名称");
- ignore.add("总计");
- ignore.add("家数");
- Map<String, Map<String, Double>> userItemRatingTable = new HashMap<>();
- for (Map<String, Object> map : list) {
- HashMap<String, Double> 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<Map<String, Object>> outRes = new ArrayList<>();
- for (String companyName : userItemRatingTable.keySet()) {
- Map<String, Double> recommendations = rs.recommendItems(companyName);
- recommendations.entrySet().removeIf(entry -> entry.getValue() == 0.0);
- HashMap<String, Object> 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);
- }
- }
|