Skip to content

Commit

Permalink
fix: connect ranker with engine
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHamed3699 committed May 13, 2024
1 parent fd8e01b commit e70f7a3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

import meowdbmanager.DBManager;
import meowindexer.Tokenizer;
import meowranker.PhraseRanker;

@RestController
@RequestMapping("/")
public class QueryEngineController {
private DBManager dbManager;
private Tokenizer tokenizer;
private PhraseRanker phraseRanker;
private List<ObjectId> docs;
private String currentQuery;
private boolean isPhraseMatching, isFirstTime;
Expand All @@ -30,6 +32,7 @@ public class QueryEngineController {
public QueryEngineController() {
dbManager = new DBManager();
tokenizer = new Tokenizer();
phraseRanker = new PhraseRanker();
docs = new ArrayList<>();
currentQuery = "";
isPhraseMatching = false;
Expand Down Expand Up @@ -155,6 +158,8 @@ public String getSnippet(String doc, String phrase) {
}

private List<ObjectId> rankDocs(String[] tokens) {
if (isPhraseMatching)
return phraseRanker.rank(phrases[0]);
return dbManager.getDocIDs(tokens);
}
}
24 changes: 12 additions & 12 deletions mistermeow/app/src/main/java/meowranker/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

public class Main {
public static void main(String[] argv) {

PhraseRanker phRanker = new PhraseRanker();

String query = "The dfl;akjf;asd Free Encyclopedia"; // tests searching for unfound token
phRanker.rank(query);
query = "The Free Encyclopedia";
phRanker.rank(query);
query = "domestic cat";
phRanker.rank(query);
}
}
String query = "The dfl;akjf;asd Free Encyclopedia"; // tests searching for unfound token
phRanker.rank(query);

query = "The Free Encyclopedia";
phRanker.rank(query);

query = "I love you";
phRanker.rank(query);

}
}
59 changes: 27 additions & 32 deletions mistermeow/app/src/main/java/meowranker/PhraseRanker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,52 @@ public PhraseRanker() {
}

// TODO: change function return type
public List<Document> rank(String query) {

// applying PR algorithm
double [][] M = this.constructUrlsGraph();
double [] popularity = this.getPopularity(M , M.length);
public List<ObjectId> rank(String query) {

// Tokenizing query
// applying PR algorithm
double[][] M = this.constructUrlsGraph();
double[] popularity = this.getPopularity(M, M.length);

// Tokenizing query
List<String> searchTokens = tokenizer.tokenizeString(query);
System.out.println(searchTokens);


// getting docs common in all tokens & matches the query phrase
// getting docs common in all tokens & matches the query phrase
List<Document> matchedDocs = getMatchingDocs(searchTokens, query);

// System.out.println(matchedDocs.size());
// for (Document doc : matchedDocs) {
// System.out.println(doc.getString("URL"));
// System.out.println(doc.getString("URL"));
// }

// calculating relevance for each document
List<Double> relevance = this.calculateRelevance(matchedDocs , searchTokens , popularity);
// calculating relevance for each document
List<Double> relevance = this.calculateRelevance(matchedDocs, searchTokens, popularity);

// for (Double val: relevance){
// System.out.println(val);
// System.out.println(val);
// }

List<Map.Entry<Document , Double>> finalRank = this.combineRelWithPop(matchedDocs , relevance , popularity);
List<Map.Entry<Document, Double>> finalRank = this.combineRelWithPop(matchedDocs, relevance, popularity);

finalRank.sort(Map.Entry.comparingByValue());

System.out.println("======================================");
System.out.println("=========== Final Result =============");
System.out.println("======================================");

List<Document> SortedList = new ArrayList<>();
for(Map.Entry<Document , Double> e:finalRank){
SortedList.add(e.getKey());
List<ObjectId> SortedList = new ArrayList<>();
for (Map.Entry<Document, Double> e : finalRank) {
SortedList.add(e.getKey().getObjectId("_id"));
System.out.println(e.getKey().getString("URL") + " " + e.getValue());
}



// TODO: call function sort by higher rank
return SortedList;
}

private List<Document> getMatchingDocs(List<String> searchTokens , String query) {
private List<Document> getMatchingDocs(List<String> searchTokens, String query) {

List<Document> docs = getCommonDocs(searchTokens);

List<Document> finalDocs = new ArrayList<>();

for (Document doc : docs) {
Expand Down Expand Up @@ -92,18 +88,17 @@ private List<Document> getCommonDocs(List<String> searchTokens) {
return new ArrayList<>();

// getting the first token present in db
int ind = 0;
int ind = 0;
Document invertedInd = db.getInvertedIndex(searchTokens.get(0));
ind++;
while(invertedInd == null && ind< searchTokens.size()){

while (invertedInd == null && ind < searchTokens.size()) {
invertedInd = db.getInvertedIndex(searchTokens.get(ind));
ind++;
}

if(invertedInd == null)
return new ArrayList<>();

if (invertedInd == null)
return new ArrayList<>();

List<Document> docs = invertedInd.getList("docs", Document.class);
List<ObjectId> docsId = new ArrayList<>();
Expand All @@ -113,7 +108,7 @@ private List<Document> getCommonDocs(List<String> searchTokens) {
}

for (int i = ind; i < searchTokens.size(); i++) {

invertedInd = db.getInvertedIndex(searchTokens.get(i));
if (invertedInd != null) {

Expand All @@ -135,4 +130,4 @@ private List<Document> getCommonDocs(List<String> searchTokens) {

return commonDocs;
}
}
}

0 comments on commit e70f7a3

Please sign in to comment.