|
| 1 | +import java.io.*; |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +/** |
| 8 | + * 输入:hole由正确答案填充后的groum,以及top10结果集合,且正确答案为结果集合中的第一个元素 |
| 9 | + */ |
| 10 | +public class CreateDataSet { |
| 11 | + |
| 12 | + static HashMap<String, Integer> pathCounter; |
| 13 | + static Map<String, String> API2Index; |
| 14 | + |
| 15 | + /** |
| 16 | + * 读取api2index的Map |
| 17 | + * @param file |
| 18 | + * @throws IOException |
| 19 | + */ |
| 20 | + private static void getPathCounter(File file) throws IOException { |
| 21 | + pathCounter = new HashMap<>(); |
| 22 | + FileReader fileReader = new FileReader(file); |
| 23 | + BufferedReader bufferedReader = new BufferedReader(fileReader); |
| 24 | + String row; |
| 25 | + String path; Integer num; |
| 26 | + while ((row = bufferedReader.readLine()) != null) { |
| 27 | + path = row.split(" ")[0]; |
| 28 | + num = new Integer(row.split(" ")[1]); |
| 29 | + pathCounter.put(path, num); |
| 30 | + } |
| 31 | + bufferedReader.close(); |
| 32 | + fileReader.close(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * 创建适用于SVM_Rank的数据集 |
| 37 | + * 对单个Groum进行处理 |
| 38 | + * 在做top-10结果替换的时候,做path数目的查询 |
| 39 | + * @param groum hole由正确答案填充后的groum |
| 40 | + * @param results top10结果集合, results第一个为正确答案 |
| 41 | + */ |
| 42 | + public static void create(Groum groum, List<String> results, int qId, File file, PrintWriter pW, Map<String, String> A2I) throws IOException { |
| 43 | + getPathCounter(file); |
| 44 | + API2Index = A2I; |
| 45 | + Map<String, GroumNode> nodeMap = groum.getNodeMap(); |
| 46 | + GroumNode groumNode = null; |
| 47 | + List<String> startList = null; |
| 48 | + String rows = null; |
| 49 | + for (String id : nodeMap.keySet()) { |
| 50 | + groumNode = nodeMap.get(id); |
| 51 | + if (groumNode.getOriginalApi().equals(results.get(0))) { |
| 52 | + startList = new ArrayList<>(); |
| 53 | + startList.add(id); |
| 54 | + List<List<String>> outList = GetPath.getAllPath(groum, startList, 4); // 此处所得到的path表示是使用Map中的API的index构成 |
| 55 | + rows = createRows(outList, results, groumNode.getApi(), qId); |
| 56 | + writeFile(pW, rows); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * 将String变为List,并得到填充hole的Path(aPath)以及除去hole以外的剩余API组成的Path(bPath) |
| 63 | + * @param list |
| 64 | + * @param id |
| 65 | + * @param result |
| 66 | + * @return |
| 67 | + */ |
| 68 | + private static List<String> convertListToStringAndGetAPathBPath(List<String> list, String id, String result) { |
| 69 | + List<String> ret = new ArrayList<String>(); |
| 70 | + StringBuilder aPath = new StringBuilder(); |
| 71 | + StringBuilder bPath = new StringBuilder(); |
| 72 | + boolean ifStarted = false; |
| 73 | + boolean ifMeetHole = false; |
| 74 | + for (String item : list) { |
| 75 | + if (item.equals(id)) { |
| 76 | + aPath.append(result); |
| 77 | + aPath.append(","); |
| 78 | + } |
| 79 | + else { |
| 80 | + aPath.append(item); |
| 81 | + bPath.append(item); |
| 82 | + aPath.append(","); |
| 83 | + bPath.append(","); |
| 84 | + } |
| 85 | + } |
| 86 | + if (aPath.charAt(aPath.length() - 1) == ',') aPath.deleteCharAt(aPath.length() - 1); |
| 87 | + if (bPath.charAt(bPath.length() - 1) == ',') bPath.deleteCharAt(bPath.length() - 1); |
| 88 | + ret.add(aPath.toString()); |
| 89 | + ret.add(bPath.toString()); |
| 90 | + return ret; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * 构建形式为"3 qid:1 1:1 2:1 3:0 4:0.2 5:0"每行数据元 |
| 95 | + * @param outList 使用hole抽取的Path |
| 96 | + * @param results top10结果集 |
| 97 | + * @param id 正确答案在Map中的Id |
| 98 | + * @param qId 数据元中的qid |
| 99 | + */ |
| 100 | + private static String createRows(List<List<String>> outList, List<String> results, String id, int qId) { |
| 101 | + StringBuilder rows = new StringBuilder(); |
| 102 | + List<String> midRes = null; |
| 103 | + double feature = 0; |
| 104 | + int featureId = 1; |
| 105 | + boolean ifFirst = true; |
| 106 | + String resultId; |
| 107 | + for (String result : results) { |
| 108 | + if (ifFirst) { |
| 109 | + rows.append("2"); // 正确答案的rank值为2 |
| 110 | + ifFirst = false; |
| 111 | + } |
| 112 | + else { |
| 113 | + rows.append("1"); // 非正确答案的rank值均为1 |
| 114 | + } |
| 115 | + rows.append(" ");rows.append("qid:");rows.append(qId);rows.append(" "); |
| 116 | + featureId = 1; |
| 117 | + boolean ifFirstFeature = true; |
| 118 | + for (List<String> path : outList) { |
| 119 | + resultId = API2Index.get(result); |
| 120 | + midRes = convertListToStringAndGetAPathBPath(path, id, resultId); |
| 121 | + if (midRes.get(1).length() == 1) continue; |
| 122 | + System.out.println(midRes.get(1)); |
| 123 | + System.out.println(midRes.get(0)); |
| 124 | + feature = (double)pathCounter.getOrDefault(midRes.get(1), 0) / pathCounter.getOrDefault(midRes.get(0), 1); // 此处对a值设定最小为1 |
| 125 | + if (ifFirstFeature) ifFirstFeature = false; |
| 126 | + else rows.append(" "); |
| 127 | + rows.append(featureId);rows.append(":");rows.append(feature); |
| 128 | + ++featureId; |
| 129 | + } |
| 130 | + rows.append("\n"); |
| 131 | + } |
| 132 | + return rows.toString(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * 将一个Groum的数据集写入文件 |
| 137 | + * @param pW |
| 138 | + * @param rows |
| 139 | + */ |
| 140 | + private static void writeFile(PrintWriter pW, String rows) { |
| 141 | + pW.write(rows); |
| 142 | + } |
| 143 | +} |
0 commit comments