Commit f0d41f49 authored by Ali Saeed's avatar Ali Saeed

Update Leader class to make it's functions static

parent 23f07acd
package Registration_Discovery;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
......@@ -16,31 +19,24 @@ import java.util.*;
public class Leader {
private int PORT = 12345;
private ServiceRegistry serviceRegistry;
List<DocumentTermsInfo> documentTermsInfo;
static List<DocumentTermsInfo> documentTermsInfo;
private static final String FOLDER_PATH = "D:\\DS_Project\\resources";
private static final Logger logger = LoggerFactory.getLogger(Leader.class);
public Leader(int port, ServiceRegistry serviceRegistry) {
this.PORT = port;
this.serviceRegistry = serviceRegistry;
public Leader() {
}
public void start() throws IOException, InterruptedException {
String searchQuery = "";
while (!searchQuery.equals("exit")) {
documentTermsInfo = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
logger.info("Enter your Search Query: ");
searchQuery = scanner.nextLine();
public static TreeMap<String,Double> start(String searchQuery1) throws IOException, InterruptedException {
String searchQuery = searchQuery1;
documentTermsInfo = new ArrayList<>();
logger.info("Search Query: "+searchQuery);
if(!searchQuery.isEmpty()){
// Get the list of documents from the resources folder
List<Document> documents = getDocumentsFromResources();
Map<Document,Double> documentsScores = new HashMap<>();
List<String> allServiceAddresses = serviceRegistry.getAllServiceAddresses();
List<String> allServiceAddresses = ServiceRegistry.getAllServiceAddresses();
int numAddresses = allServiceAddresses.size();
int numDocuments = documents.size();
if(numAddresses != 0){
......@@ -82,21 +78,27 @@ public class Leader {
documentsScores = calculateDocumentsScore(IDFs ,documentTermsInfo);
TreeMap<Document, Double> sortedDocumentsScores = sortDocumentsScores(documentsScores);
displaySortedScores(sortedDocumentsScores);
TreeMap<String, Double> sortedDocumentsScores1 = sortDocumentsScores1(documentsScores);
return sortedDocumentsScores1;
}
else {
logger.info("No results !!!!");
return null;
}
}
else {
logger.info("Try again later !!!!");
return null;
}
}else {
logger.info("Please enter search query !!!");
return null;
}
}
}
private Map<Document, Double> calculateDocumentsScore(Map<String, Double> idFs, List<DocumentTermsInfo> documentTermsInfo) {
private static Map<Document, Double> calculateDocumentsScore(Map<String, Double> idFs, List<DocumentTermsInfo> documentTermsInfo) {
Map<Document, Double> documentsScores = new TreeMap<>();
......@@ -114,7 +116,7 @@ public class Leader {
return documentsScores;
}
private List<Document> getDocumentsFromResources() {
private static List<Document> getDocumentsFromResources() {
List<Document> documents = new ArrayList<>();
try {
Path resourcesPath = Paths.get(FOLDER_PATH);
......@@ -133,7 +135,7 @@ public class Leader {
return documents;
}
private void displayDocumentTermsInfos(List<DocumentTermsInfo> documentTermsInfoList) {
private static void displayDocumentTermsInfos(List<DocumentTermsInfo> documentTermsInfoList) {
System.out.println();
System.out.println();
for (DocumentTermsInfo documentTermsInfo : documentTermsInfoList) {
......@@ -150,7 +152,7 @@ public class Leader {
logger.info("................................................");
}
}
private void startSearchOnWorker(String workerAddress,DataForSearch dataForSearch) throws IOException, InterruptedException {
private static void startSearchOnWorker(String workerAddress,DataForSearch dataForSearch) throws IOException, InterruptedException {
String[] addressParts = workerAddress.split(":");
int port = Integer.parseInt(addressParts[1]);
String ipAddress = addressParts[0];
......@@ -179,7 +181,7 @@ public class Leader {
}
}
private Map<String,Double> calculateIDF(List<DocumentTermsInfo> documentTermsInfo,Double totalDocuments ,String searchQuery){
private static Map<String,Double> calculateIDF(List<DocumentTermsInfo> documentTermsInfo,Double totalDocuments ,String searchQuery){
Map<String,Double> IDFs = new HashMap<>();
Map<String, Double> wordDocumentCount = new HashMap<>();
String[] queryWords = searchQuery.split("\\s+");
......@@ -219,7 +221,7 @@ public class Leader {
return IDFs;
}
private TreeMap<Document, Double> sortDocumentsScores(Map<Document, Double> documentsScores) {
private static TreeMap<Document, Double> sortDocumentsScores(Map<Document, Double> documentsScores) {
Comparator<Document> comparator = new Comparator<Document>() {
@Override
public int compare(Document doc1, Document doc2) {
......@@ -238,7 +240,31 @@ public class Leader {
sortedMap.putAll(documentsScores);
return sortedMap;
}
private void displaySortedScores(TreeMap<Document, Double> sortedScores) {
private static TreeMap<String, Double> sortDocumentsScores1(Map<Document, Double> documentsScores) {
Comparator<Document> comparator = new Comparator<Document>() {
@Override
public int compare(Document doc1, Document doc2) {
Double score1 = documentsScores.get(doc1);
Double score2 = documentsScores.get(doc2);
int scoreComparison = score2.compareTo(score1);
if (scoreComparison != 0) {
return scoreComparison;
} else {
return doc1.compareTo(doc2);
}
}
};
TreeMap<Document, Double> sortedMap = new TreeMap<>(comparator);
sortedMap.putAll(documentsScores);
TreeMap<String, Double> sortedMap1 = new TreeMap<>();
for (Map.Entry<Document, Double> entry : sortedMap.entrySet()) {
String documentName = entry.getKey().getName();
Double score = entry.getValue();
sortedMap1.put(documentName,score);
}
return sortedMap1;
}
private static void displaySortedScores(TreeMap<Document, Double> sortedScores) {
logger.info("Sorted Documents By Scores:");
for (Map.Entry<Document, Double> entry : sortedScores.entrySet()) {
Document document = entry.getKey();
......
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