Commit bdfa06f6 authored by Ali Saeed's avatar Ali Saeed

update sortDocumentsScores1 to when put sorted TreeMap<Document,double> in...

update sortDocumentsScores1 to when put sorted TreeMap<Document,double> in TreeMap<String,double>, do not use default sorting of TreeMap<String,double> because it sort using string so will sort the results due to documents name
parent 64b85b83
...@@ -16,6 +16,7 @@ import java.nio.file.Files; ...@@ -16,6 +16,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class Leader { public class Leader {
...@@ -79,6 +80,7 @@ public class Leader { ...@@ -79,6 +80,7 @@ public class Leader {
TreeMap<Document, Double> sortedDocumentsScores = sortDocumentsScores(documentsScores); TreeMap<Document, Double> sortedDocumentsScores = sortDocumentsScores(documentsScores);
displaySortedScores(sortedDocumentsScores); displaySortedScores(sortedDocumentsScores);
TreeMap<String, Double> sortedDocumentsScores1 = sortDocumentsScores1(documentsScores); TreeMap<String, Double> sortedDocumentsScores1 = sortDocumentsScores1(documentsScores);
displaySortedScores1(sortedDocumentsScores1);
return sortedDocumentsScores1; return sortedDocumentsScores1;
} }
else { else {
...@@ -221,6 +223,7 @@ public class Leader { ...@@ -221,6 +223,7 @@ public class Leader {
return IDFs; return IDFs;
} }
private static TreeMap<Document, Double> sortDocumentsScores(Map<Document, Double> documentsScores) { private static TreeMap<Document, Double> sortDocumentsScores(Map<Document, Double> documentsScores) {
Comparator<Document> comparator = new Comparator<Document>() { Comparator<Document> comparator = new Comparator<Document>() {
@Override @Override
...@@ -240,28 +243,20 @@ public class Leader { ...@@ -240,28 +243,20 @@ public class Leader {
sortedMap.putAll(documentsScores); sortedMap.putAll(documentsScores);
return sortedMap; return sortedMap;
} }
private static TreeMap<String, Double> sortDocumentsScores1(Map<Document, Double> documentsScores) { private static TreeMap<String, Double> sortDocumentsScores1(Map<Document, Double> documentsScores) {
Comparator<Document> comparator = new Comparator<Document>() { TreeMap<String, Double> sortedMap1 = new TreeMap<>(new Comparator<String>() {
@Override @Override
public int compare(Document doc1, Document doc2) { public int compare(String documentName1, String documentName2) {
Double score1 = documentsScores.get(doc1); Double score1 = documentsScores.get(new Document(documentName1));
Double score2 = documentsScores.get(doc2); Double score2 = documentsScores.get(new Document(documentName2));
int scoreComparison = score2.compareTo(score1); return score2.compareTo(score1);
if (scoreComparison != 0) {
return scoreComparison;
} else {
return doc1.compareTo(doc2);
}
} }
}; });
TreeMap<Document, Double> sortedMap = new TreeMap<>(comparator);
sortedMap.putAll(documentsScores); sortedMap1.putAll(documentsScores.entrySet().stream()
TreeMap<String, Double> sortedMap1 = new TreeMap<>(); .collect(Collectors.toMap(entry -> entry.getKey().getName(), Map.Entry::getValue)));
for (Map.Entry<Document, Double> entry : sortedMap.entrySet()) {
String documentName = entry.getKey().getName();
Double score = entry.getValue();
sortedMap1.put(documentName,score);
}
return sortedMap1; return sortedMap1;
} }
private static void displaySortedScores(TreeMap<Document, Double> sortedScores) { private static void displaySortedScores(TreeMap<Document, Double> sortedScores) {
...@@ -272,4 +267,12 @@ public class Leader { ...@@ -272,4 +267,12 @@ public class Leader {
logger.info("Document: " + document.getName() + " , Document score: " + score); logger.info("Document: " + document.getName() + " , Document score: " + score);
} }
} }
private static void displaySortedScores1(TreeMap<String, Double> sortedScores) {
logger.info("Sorted Documents By Scores:");
for (Map.Entry<String, Double> entry : sortedScores.entrySet()) {
String document = entry.getKey();
Double score = entry.getValue();
logger.info("Document: " + document + " , Document score: " + score);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment