DivRecService.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package edu.nju.service;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Map;
  8. //import java.util.HashMap;
  9. //import java.util.Map;
  10. import java.util.Set;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import edu.nju.dao.BugDao;
  14. import edu.nju.dao.BugHistoryDao;
  15. import edu.nju.dao.BugMirrorDao;
  16. import edu.nju.dao.BugPageDao;
  17. import edu.nju.dao.ThumsUpDao;
  18. import edu.nju.entities.Bug;
  19. import edu.nju.entities.BugHistory;
  20. import edu.nju.entities.BugMirror;
  21. import edu.nju.entities.ThumsUp;
  22. import edu.nju.util.PMF;
  23. @Service
  24. public class DivRecService {
  25. @Autowired
  26. BugPageDao pagedao;
  27. @Autowired
  28. BugMirrorDao mirrordao;
  29. @Autowired
  30. BugDao bugdao;
  31. @Autowired
  32. ThumsUpDao tdao;
  33. @Autowired
  34. BugHistoryDao hdao;
  35. public List<BugMirror> diverseRec(String report_id, String case_take_id) {
  36. List<BugMirror> result = new ArrayList<BugMirror>();
  37. List<Bug> all = bugdao.findByCaseid(case_take_id);
  38. Set<String> my = new HashSet<String>();
  39. List<String> allIds = bugdao.findByCase(case_take_id);
  40. int M = all.size(); //产品的数目
  41. int K = 3; //特征的数目
  42. List<String> users = new ArrayList<String>();
  43. for(Bug bug: all) {
  44. String user = bug.getReport_id();
  45. if(users.contains(user)) { continue; }
  46. users.add(user);
  47. }
  48. int N = users.size(); //用户的数目
  49. double[][] R = new double[N][M];
  50. double[][] P = new double[N][K];
  51. double[][] Q = new double[K][M];
  52. for(int i = 0; i < N; i ++) {
  53. // List<Map<String, Integer>> user_detail = personDetail(all, user);
  54. String user = users.get(i);
  55. List<List<String>> relations = getAllRelation(case_take_id, user);
  56. for(int j = 0; j < 4; j ++) {
  57. for(String id : relations.get(j)) {
  58. my.add(id);
  59. int index = allIds.indexOf(id);
  60. if(index < 0) { continue; }
  61. switch (j) {
  62. case 0: //自己提交
  63. R[i][index] = 10;
  64. break;
  65. case 1: //点过赞
  66. R[i][index] = 8;
  67. break;
  68. case 2: //点过踩
  69. R[i][index] = 1;
  70. break;
  71. case 3: //fork过
  72. R[i][index] = 5;
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. // System.out.println("R矩阵");
  79. // for(int i = 0; i < N; ++i) {
  80. // for(int j = 0; j < M; ++j){
  81. // System.out.print((int)R[i][j] + ",");
  82. // }
  83. // System.out.println();
  84. // }
  85. for(int i = 0; i < N; ++i)
  86. {
  87. for(int j = 0; j < K; ++j)
  88. {
  89. P[i][j] = Math.random() % 9;
  90. }
  91. }
  92. for(int i = 0; i < K; ++i)
  93. {
  94. for(int j = 0;j < M; ++j)
  95. {
  96. Q[i][j] = Math.random()%9;
  97. }
  98. }
  99. PMF.matrix_factorization(R, P, Q, N, M, K);
  100. double[][] fin_array = new double[N][M];
  101. for(int i = 0; i < N; ++i) {
  102. for(int j = 0; j < M; ++j) {
  103. double temp = 0;
  104. for (int k = 0; k < K; ++k){
  105. temp += P[i][k] * Q[k][j];
  106. }
  107. fin_array[i][j] = temp;
  108. // System.out.print(df.format(temp)+",");
  109. }
  110. }
  111. int this_user = users.indexOf(report_id);
  112. double[] line = fin_array[this_user];
  113. for(int i = 0; i < M; i ++) {
  114. if(line[i] >= 5 && !my.contains(allIds.get(i))) {
  115. String id = allIds.get(i);
  116. result.add(mirrordao.findById(id));
  117. }
  118. }
  119. return result;
  120. }
  121. public List<List<String>> getAllRelation(String case_take_id, String report_id) {
  122. List<List<String>> result = new ArrayList<List<String>>();
  123. result.add(new ArrayList<String>()); //获取所有自己提交
  124. result.add(new ArrayList<String>()); //获取所有点赞记录
  125. result.add(new ArrayList<String>()); //获取所有点踩记录
  126. result.add(new ArrayList<String>()); //获取所有父记录
  127. ThumsUp thum = tdao.findByReport(report_id);
  128. List<String> ids = mirrordao.findIdsByReport(report_id, case_take_id);
  129. result.get(0).addAll(ids);
  130. if(thum != null) {
  131. result.get(1).addAll(thum.getThums());
  132. result.get(2).addAll(thum.getDiss());
  133. }
  134. List<String> parents = result.get(3);
  135. for(String id : ids) {
  136. BugHistory his = hdao.findByid(id);
  137. if(his == null) { continue; }
  138. String parent = his.getParent();
  139. if(parent.equals("null") || parents.contains(parent)) { continue; }
  140. parents.add(parent);
  141. }
  142. return result;
  143. }
  144. public List<BugMirror> randomRec(String report_id, String case_take_id) {
  145. List<BugMirror> result = new ArrayList<BugMirror>();
  146. Set<String> my = new HashSet<String>();
  147. List<String> allIds = bugdao.findByCase(case_take_id);
  148. Map<String, Integer> map = new HashMap<String, Integer>();
  149. for(List<String> list: getAllRelation(case_take_id, report_id)) {
  150. for(String id: list) {
  151. my.add(id);
  152. }
  153. }
  154. for(String id: allIds) {
  155. if(my.contains(id)) { continue; }
  156. BugMirror mirror = mirrordao.findById(id);
  157. map.put(id, mirror.getGood().size() + mirror.getBad().size());
  158. }
  159. List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
  160. Collections.sort(entries, (a, b) -> (a.getValue() - b.getValue()));
  161. for(int i = 0; i < entries.size() && i < 8; i ++) {
  162. result.add(mirrordao.findById(entries.get(i).getKey()));
  163. }
  164. return result;
  165. }
  166. // private List<Map<String, Integer>> personDetail(List<Bug> all, String report_id) {
  167. // List<Map<String, Integer>> result = new ArrayList<Map<String, Integer>>();
  168. // result.add(new HashMap<String, Integer>());
  169. // result.add(new HashMap<String, Integer>());
  170. // for(Bug bug : all) {
  171. // if(bug.getReport_id().equals("report_id")) {
  172. // Map<String, Integer> pages = result.get(0);
  173. // Map<String, Integer> categories = result.get(1);
  174. // String page = bug.getBug_page();
  175. // String category = bug.getBug_category();
  176. // pages.put(page, pages.getOrDefault(page, 0) + 1);
  177. // categories.put(category, categories.getOrDefault(category, 0) + 1);
  178. // }
  179. // }
  180. // return result;
  181. // }
  182. }