Browse Source

将推荐的相似bug进行排序展示
将每次推荐的相似bug记录在日志里

xujiawei 5 years ago
parent
commit
ed6f539640

+ 40 - 8
src/main/java/edu/nju/controller/RecommendController.java

@@ -2,18 +2,15 @@ package edu.nju.controller;
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.CrossOrigin;
@@ -27,6 +24,8 @@ import edu.nju.service.HistoryService;
 import edu.nju.service.RecommendService;
 import edu.nju.service.UserBasedService;
 
+import java.util.Map.Entry;
+
 @Controller
 @RequestMapping(value = "/rec")
 @CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
@@ -109,6 +108,7 @@ public class RecommendController {
 	}
 	
 	/**
+	 * 用户编辑bug时推荐相似bug
 	 * 用户点击六个类别之后,都使用该接口
 	 * @param type("category", "severity", "recurrent", "page1", "page2", "page3"), content
 	 * @return List<BugMirror>
@@ -123,6 +123,8 @@ public class RecommendController {
 			List<Float> scores = new ArrayList<Float>();
 			Set<BugMirror> mirror2 = new HashSet<BugMirror>();
 			Map<BugMirror, Float> map;
+			System.out.println(type);
+			System.out.println(content);
 			if(type.equals("title") || type.equals("description")) {
 				if(type.equals("title")) {map = recservice.recommandByTitle(content, session);}
 				else {map = recservice.recommandByDes(content, session);}
@@ -147,9 +149,35 @@ public class RecommendController {
 			}
 //			mirror2.addAll(historyservice.getNew(case_take_id, (String)session.getAttribute("report")));
 //			filter(mirror2, mirror1);
-			result.put("same", new JSONArray(mirror1));
-			result.put("scores", new JSONArray(scores));
+			List<BugMirror> resultMirror = new ArrayList<BugMirror>();
+			List<Float> resultScore = new ArrayList<Float>();
+
+			Map<BugMirror,Float> hashMap=new HashMap<>();
+			for(int i=0;i<mirror1.size();i++){
+				hashMap.put(mirror1.get(i),scores.get(i));
+			}
+			//根据score排序
+			List<Entry<BugMirror, Float>> tList = new ArrayList<>(hashMap.entrySet());
+			tList.sort((a, b) -> (Float.compare(b.getValue(), a.getValue())));
+
+			//最多展示前六项分数最高的
+			for (int i = 0; i < 6 && i < tList.size(); i++) {
+				Entry<BugMirror, Float> entry = tList.get(i);
+				resultMirror.add(entry.getKey());
+				resultScore.add(entry.getValue());
+			}
+
+
+			result.put("same", new JSONArray(resultMirror));
+			result.put("scores", new JSONArray(resultScore));
 			result.put("new", new JSONArray(mirror2));
+//			System.out.println("result:"+result);
+
+			recservice.createSimilarBugLog(session,resultMirror,resultScore);
+
+
+
+
 			out.print(result);
 			out.flush();
 			out.close();
@@ -229,4 +257,8 @@ public class RecommendController {
 			if(a.contains(mirror)) {a.remove(mirror);}
 		}
 	}
+
+
+
+
 }

+ 118 - 3
src/main/java/edu/nju/service/RecommendService.java

@@ -10,6 +10,10 @@ import java.util.Map.Entry;
 
 import javax.servlet.http.HttpSession;
 
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -172,6 +176,16 @@ public class RecommendService {
 		}
 		
 		Map<String, String> map = (Map<String, String>)session.getAttribute("path");
+//		for(Entry<String, String> entry : map.entrySet()) {
+//			System.out.println(entry.getKey());
+//			System.out.println(entry.getValue());
+////			String content = ;
+//		}
+
+//		System.out.println("mirror");
+//		for(BugMirror mirror : mirrors) {
+//			System.out.println(mirror.getId());
+//		}
 		return finalScore(mirrors, map);
 	}
 	
@@ -314,7 +328,8 @@ public class RecommendService {
 		else { results = recommend(case_take_id, type, content, true, session); }
 		return results;
 	}
-	
+
+
 	@SuppressWarnings("unchecked")
 	private Map<BugMirror, Float> count(String content, List<BugMirror> lists, boolean type, HttpSession session) {
 		StringMatch match = new StringMatch();
@@ -324,17 +339,24 @@ public class RecommendService {
 		Map<String, String> titleMap = new HashMap<String, String>();
 		Map<String, String> desMap = new HashMap<String, String>();
 		Map<String, String> pmap = null;
-		
+
+		//文本相似度计算,主要是使用StringMatch
+		//获取keywords中已经分词完毕的关键词
 		for(BugMirror mirror: lists) {
 			String id = mirror.getId();
 			bmap.put(id, mirror);
 			titleMap.put(id, kwdao.findById(id).getTitle());
 			desMap.put(id, kwdao.findById(id).getDescription());
 		}
+		//对于关键词和用户输入的信息进行文本相似度计算
 		for(BugMirror mirror: lists) {
 			String id = mirror.getId();
 			float score = 0;
+			//recommendByTitle:type==true
+			//recommendByDes:type==false
 			if(type) {
+				//ansj算法进行中文分词
+				//wmd算法计算相似度?
 				score += match.score(match.Ansj(content), match.Ansj(titleMap.get(id))) * 30;
 				if(session.getAttribute("des") != null) {
 					score += match.score(match.Ansj((String)session.getAttribute("des")), match.Ansj(desMap.get(id))) * 40;
@@ -348,11 +370,13 @@ public class RecommendService {
 			tmap.put(mirror, score);
 		}
 		List<Entry<BugMirror, Float>> tlist = new ArrayList<Entry<BugMirror, Float>>(tmap.entrySet());
+		//根据score排序
 		Collections.sort(tlist, (a, b) -> (Float.compare(b.getValue(), a.getValue())));
 		if(session.getAttribute("path") != null) {
 			pmap = (Map<String, String>)session.getAttribute("path");
 		}
-		
+
+		//只推荐前六个
 		for(int i = 0; i < tlist.size() && i < 6; i ++) {
 			float score = (float) (tlist.get(i).getValue());
 			result.put(tlist.get(i).getKey(), score + categoryCount(tlist.get(i).getKey(), pmap));
@@ -436,5 +460,96 @@ public class RecommendService {
 		}
 		return 0;
 	}
+
+	public void createSimilarBugLog(HttpSession session,List<BugMirror> mirrorList,List<Float> scores){
+		Bug userBug=constructUserBug(session);
+		JSONObject similarJson=new JSONObject();
+		similarJson.put("type","similarJson");
+		similarJson.put("userJson",new JSONObject(userBug));
+		JSONArray similarBugJSONArray=new JSONArray();
+
+		//如果传过来的列表是未排过序的,就需要自己排序
+//		Map<BugMirror,Float> hashMap=new HashMap<>();
+//		for(int i=0;i<mirrorList.size();i++){
+////			System.out.println(scores.get(i));
+//			hashMap.put(mirrorList.get(i),scores.get(i));
+//		}
+//		//根据score排序
+//		List<Entry<BugMirror, Float>> tList = new ArrayList<Entry<BugMirror, Float>>(hashMap.entrySet());
+//		Collections.sort(tList,(a,b)->(Float.compare(b.getValue(),a.getValue())));
+//
+//		for(Entry<BugMirror, Float> entry:tList){
+//			Bug similarBug=bugdao.findByid(entry.getKey().getId());
+//			JSONObject similarBugJSONObject=new JSONObject();
+//			similarBugJSONObject.put("bugDetail",new JSONObject(similarBug));
+//			similarBugJSONObject.put("score",entry.getValue());
+//			System.out.println(entry.getValue());
+//			similarBugJSONArray.put(similarBugJSONObject);
+//		}
+
+		for(int i=0;i<mirrorList.size();i++){
+			Bug similarBug=bugdao.findByid(mirrorList.get(i).getId());
+			JSONObject similarBugJSONObject=new JSONObject();
+			similarBugJSONObject.put("bugDetail",new JSONObject(similarBug));
+			similarBugJSONObject.put("score",scores.get(i));
+			similarBugJSONArray.put(similarBugJSONObject);
+		}
+
+		similarJson.put("similarJson",similarBugJSONArray);
+
+		Logger logger= LoggerFactory.getLogger(RecommendService.class);
+		logger.info(String.valueOf(similarJson));
+	}
+
+	private Bug constructUserBug( HttpSession session){
+		String case_take_id="";
+		String create_time_millis=Long.toString(System.currentTimeMillis());
+		String bug_category="";
+		String description="";
+		String img_url="";
+		int severity=0;
+		int recurrent=0;
+		String title="";
+		String report_id="";
+		String bug_page="";
+		String case_id="";
+		if(session.getAttribute("case")!=null){
+			case_take_id=(String)session.getAttribute("case");
+			case_id=case_take_id.split("-")[0];
+		}
+		if(session.getAttribute("path")!=null){
+			LinkedHashMap<String,String> path=(LinkedHashMap<String,String>)session.getAttribute("path");
+			if(path.containsKey("page1")){
+				bug_page+=path.get("page1");
+				if(path.containsKey("page2")){
+					bug_page+=("-"+path.get("page2"));
+					if(path.containsKey("page3")){
+						bug_page+=("-"+path.get("page3"));
+					}
+				}
+			}
+			if(path.containsKey("bug_category")){
+				bug_category=path.get("bug_category");
+			}
+			if(path.containsKey("severity")){
+				severity=severityTranse(path.get("severity"));
+			}
+			if(path.containsKey("recurrent")){
+				recurrent=recurrentTranse(path.get("recurrent"));
+			}
+
+		}
+		if(session.getAttribute("title")!=null){
+			title=(String)session.getAttribute("title");
+		}
+		if(session.getAttribute("des")!=null){
+			description=(String)session.getAttribute("des");
+		}
+		Bug userBug=new Bug(case_take_id,create_time_millis,bug_category,description,img_url,severity,recurrent,title,report_id,bug_page,case_id);
+//		JSONObject userBugJson=new JSONObject(userBug);
+//		System.out.println(userBugJson);
+		return userBug;
+
+	}
 	
 }

+ 8 - 4
src/main/resources/application.properties

@@ -1,11 +1,15 @@
 server.port = 8090
 server.servlet.context-path = /Bug/api/
 
-spring.data.mongodb.host = ${MONGO_HOST:10.81.65.118}
-spring.data.mongodb.port = ${MONGO_PORT:29019}
-spring.data.mongodb.database = ${MONGO_DBNAME:co-report}
+#spring.data.mongodb.host = ${MONGO_HOST:10.81.65.118}
+#spring.data.mongodb.port = ${MONGO_PORT:29019}
+#spring.data.mongodb.database = ${MONGO_DBNAME:co-report}
+
+spring.data.mongodb.uri= mongodb://localhost:27017/test
 
 spring.http.encoding.force = true
 spring.http.encoding.charset = UTF-8
 spring.http.encoding.enabled = true
-server.tomcat.uri-encoding = UTF-8
+server.tomcat.uri-encoding = UTF-8
+
+logging.file=crowdsource-backend.log