import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* holegroumtop10
*/
public class CreateDataSet {
static HashMap pathCounter;
static Map API2Index;
/**
* api2indexMap
* @param file
* @throws IOException
*/
private static void getPathCounter(File file) throws IOException {
pathCounter = new HashMap();
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String row;
String path; Integer num;
while ((row = bufferedReader.readLine()) != null) {
path = row.split(" ")[0];
num = new Integer(row.split(" ")[1]);
pathCounter.put(path, num);
}
bufferedReader.close();
fileReader.close();
}
/**
* SVM_Rank
* Groum
* top-10path
* @param groum holegroum
* @param results top10, results
*/
public static void create(Groum groum, List results, int qId, File file, PrintWriter pW, Map A2I) throws IOException {
getPathCounter(file);
API2Index = A2I;
Map nodeMap = groum.getNodeMap();
GroumNode groumNode = null;
List startList = null;
String rows = null;
for (String id : nodeMap.keySet()) {
groumNode = nodeMap.get(id);
if (groumNode.getOriginalApi().equals(results.get(0))) {
startList = new ArrayList();
startList.add(id);
List outList = GetPath.getAllPath(groum, startList, 4); // pathMapAPIindex
rows = createRows(outList, results, groumNode.getApi(), qId);
writeFile(pW, rows);
}
}
}
/**
* StringListholePathaPathholeAPIPathbPath
* @param list
* @param id
* @param result
* @return
*/
private static List convertListToStringAndGetAPathBPath(List list, String id, String result) {
List ret = new ArrayList();
StringBuilder aPath = new StringBuilder();
StringBuilder bPath = new StringBuilder();
boolean ifStarted = false;
boolean ifMeetHole = false;
for (String item : list) {
if (item.equals(id)) {
aPath.append(result);
aPath.append(",");
}
else {
aPath.append(item);
bPath.append(item);
aPath.append(",");
bPath.append(",");
}
}
if (aPath.charAt(aPath.length() - 1) == ',') aPath.deleteCharAt(aPath.length() - 1);
if (bPath.charAt(bPath.length() - 1) == ',') bPath.deleteCharAt(bPath.length() - 1);
ret.add(aPath.toString());
ret.add(bPath.toString());
return ret;
}
/**
* "3 qid:1 1:1 2:1 3:0 4:0.2 5:0"
* @param outList holePath
* @param results top10
* @param id MapId
* @param qId qid
*/
private static String createRows(List outList, List results, String id, int qId) {
StringBuilder rows = new StringBuilder();
List midRes = null;
double feature = 0;
int featureId = 1;
boolean ifFirst = true;
String resultId;
for (String result : results) {
if (ifFirst) {
rows.append("2"); // rank2
ifFirst = false;
}
else {
rows.append("1"); // rank1
}
rows.append(" ");rows.append("qid:");rows.append(qId);rows.append(" ");
featureId = 1;
boolean ifFirstFeature = true;
for (List path : outList) {
resultId = API2Index.get(result);
midRes = convertListToStringAndGetAPathBPath(path, id, resultId);
if (midRes.get(1).length() == 1) continue;
System.out.println(midRes.get(1));
System.out.println(midRes.get(0));
feature = (double)pathCounter.getOrDefault(midRes.get(1), 0) / pathCounter.getOrDefault(midRes.get(0), 1); // a1
if (ifFirstFeature) ifFirstFeature = false;
else rows.append(" ");
rows.append(featureId);rows.append(":");rows.append(feature);
++featureId;
}
rows.append("\n");
}
return rows.toString();
}
/**
* Groum
* @param pW
* @param rows
*/
private static void writeFile(PrintWriter pW, String rows) {
pW.write(rows);
}
}