private static Map<String, Double> sortByComparator( Map<String, Double> map, final boolean order) { List<Entry<String, Double>> list = new LinkedList<Entry<String, Double>>(map.entrySet()); // Sorting the list based on values Collections.sort( list, new Comparator<Entry<String, Double>>() { public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { if (order) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // Maintaining insertion order with the help of LinkedList Map<String, Double> sortedMap = new LinkedHashMap<String, Double>(); for (Entry<String, Double> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; }
public static void WriteToFile() { doc_score = sortByComparator(doc_score, false); try { FileWriter wrtr = new FileWriter("D:/IR/Assignment7/Pytest/part2/output/prediction.txt"); for (String key : doc_score.keySet()) { wrtr.write(key + " : " + doc_score.get(key) + " \n"); } wrtr.close(); } catch (Exception e) { e.printStackTrace(); } }
public static void RetrieveTestMatrix() { try { BufferedReader flrdr = new BufferedReader( new FileReader("D:\\IR\\Assignment7\\Pytest\\part2\\output\\test_matrix.txt")); String line = ""; while ((line = flrdr.readLine()) != null) { SortedSet<Integer> word_ids = new TreeSet<Integer>(); int val_count = 0; String[] key_value = line.split(" :: "); if (key_value[1].trim().length() < 1) System.out.println("Error on Train Doc " + key_value[0]); key_value[1] = key_value[1].substring(1, key_value[1].length() - 2); String[] values = key_value[1].split(","); FeatureNode[] node = new FeatureNode[values.length]; for (String val : values) word_ids.add(Integer.parseInt(val.trim())); for (int val : word_ids) node[val_count++] = new FeatureNode(val, 1); double predict = Linear.predict(model, node); doc_score.put(key_value[0].trim(), predict); } flrdr.close(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } }