diff --git a/mistermeow/app/build.gradle b/mistermeow/app/build.gradle index bc94a1a..d849410 100644 --- a/mistermeow/app/build.gradle +++ b/mistermeow/app/build.gradle @@ -8,6 +8,9 @@ plugins { // Apply the application plugin to add support for building a CLI application in Java. id 'application' + // id 'java' + id 'org.springframework.boot' version '3.2.5' + id 'io.spring.dependency-management' version '1.1.4' } apply plugin: 'eclipse' @@ -18,14 +21,19 @@ repositories { mavenCentral() } +group = 'com.mistermeow' + dependencies { // This dependency is used by the application. implementation libs.guava implementation 'javax:javaee-api:8.0' implementation 'org.jsoup:jsoup:1.17.2' implementation group: 'org.apache.opennlp', name: 'opennlp-tools', version: '2.3.2' - implementation 'org.mongodb:mongodb-driver-sync:5.0.1' + // implementation 'org.mongodb:mongodb-driver-sync:5.0.1' implementation 'commons-codec:commons-codec:1.13' + implementation 'org.glassfish.jersey.containers:jersey-container-servlet:2.25.1' + implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' + implementation 'org.springframework.boot:spring-boot-starter-web' } // Apply a specific Java toolchain to ease working on different environments. @@ -33,6 +41,7 @@ java { toolchain { languageVersion = JavaLanguageVersion.of(17) } + sourceCompatibility = '17' } @@ -118,5 +127,5 @@ tasks.register('engine') { application { // Define the main class for the application. - mainClass = 'meowindexer.Main' + mainClass = 'meowEngine.Main' } diff --git a/mistermeow/app/src/main/java/meowEngine/Main.java b/mistermeow/app/src/main/java/meowEngine/Main.java index ffd1567..25d9c07 100644 --- a/mistermeow/app/src/main/java/meowEngine/Main.java +++ b/mistermeow/app/src/main/java/meowEngine/Main.java @@ -1,21 +1,12 @@ package meowEngine; -import java.util.HashSet; -import meowdbmanager.DBManager; +import meowEngine.QueryEngineController; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +@SpringBootApplication public class Main { public static void main(String[] args) { - RestApplication api = new RestApplication(); - queryEngine qe = new queryEngine(); - DBManager dbManager = new DBManager(); - - String doc = dbManager.getUrlDocument("https://en.wikipedia.org/wiki/Cat"); - HashSet query = new HashSet(); - query.add("cat"); - query.add("kitten"); - - String phrase = "cats can"; - System.out.println(qe.getSnippet(doc, query)); - System.out.println(qe.getSnippet(doc, phrase)); + SpringApplication.run(Main.class, args); } } diff --git a/mistermeow/app/src/main/java/meowEngine/QueryEngineController.example.java b/mistermeow/app/src/main/java/meowEngine/QueryEngineController.example.java new file mode 100644 index 0000000..41cf1e0 --- /dev/null +++ b/mistermeow/app/src/main/java/meowEngine/QueryEngineController.example.java @@ -0,0 +1,119 @@ +// +// package meowEngine; +// +// import java.util.ArrayList; +// import java.util.List; +// import javax.ws.rs.PathParam; +// import meowdbmanager.DBManager; +// import org.bson.Document; +// import org.bson.types.ObjectId; +// import org.jsoup.Jsoup; +// import org.springframework.web.bind.annotation.GetMapping; +// import org.springframework.web.bind.annotation.PostMapping; +// import org.springframework.web.bind.annotation.RequestMapping; +// import org.springframework.web.bind.annotation.RequestParam; +// import org.springframework.web.bind.annotation.RestController; +// +// @RestController +// @RequestMapping("/api/search") +// public class QueryEngineController { +// +// private final DBManager dbManager; +// private final int numOfDocsInPage = 20; +// private final int windowCharSize = 100; +// +// public QueryEngineController(DBManager dbManager) { +// this.dbManager = dbManager; +// } +// +// @GetMapping("/suggestions") +// public List getSuggestions(@RequestParam("query") String query) { +// return dbManager.getSuggestions(query, 10); +// } +// +// @PostMapping("/documents/{page}") +// public SearchResponse searchQuery(@PathParam("q") String query, +// @PathParam("page") int page) { +// +// List results = processQuery(query, page); +// return new SearchResponse(results); +// } +// +// private List processQuery(String query, int page) { +// // Implement parsing logic (similar to original parse method) +// List tokens = parseQuery(query); +// List docIds = dbManager.getDocs(tokens.toArray(new String[0])); +// +// // Pagination logic +// int startIndex = page * numOfDocsInPage; +// int endIndex = Math.min(startIndex + numOfDocsInPage, docIds.size()); +// List documents = +// dbManager.getDocuments(docIds.subList(startIndex, endIndex)); +// +// // Add snippets and other info (implement getSnippet methods) +// for (Document doc : documents) { +// String snippet = getSnippet(doc.getString("content"), tokens); +// doc.append("snippet", snippet); +// // Add other relevant information to the document as needed +// } +// return documents; +// } +// +// private String getSnippet(String content, List tokens) { +// // Implement snippet generation logic (similar to original getSnippet) +// String textContent = Jsoup.parse(content).text(); +// for (String token : tokens) { +// if (textContent.contains(token)) { +// int index = textContent.indexOf(token); +// return textContent.substring(index - windowCharSize, +// index + windowCharSize); +// } +// } +// return "No Snippet Found"; +// } +// +// // Implement other methods like getSnippet(String doc, String phrase) if +// // needed +// +// private List parseQuery(String query) { +// // Implement parsing logic to extract tokens from the query +// // You can use similar logic to the original parse method +// List tokens = new ArrayList<>(); +// // ... (parsing logic) +// return tokens; +// } +// +// // This class is for structuring the search response (optional) +// public static class SearchResponse { +// +// private final List results; +// +// public SearchResponse(List results) { this.results = results; } +// +// public List getResults() { return results; } +// } +// } +// +// // Replace with your actual implementation of DBManager +// class DBManager { +// +// public List getSuggestions(String query, int limit) { +// // Implement logic to retrieve suggestions from your database +// return new ArrayList<>(); // Replace with actual implementation +// } +// +// public List getDocuments(String[] tokens) { +// // Implement logic to retrieve documents based on tokens from your database +// return new ArrayList<>(); // Replace with actual implementation +// } +// +// public List getDocuments(List docIds) { +// // Implement logic to retrieve documents based on ObjectIds from your +// // database +// return new ArrayList<>(); // Replace with actual implementation +// } +// +// public void insertSuggestion(String query) { +// // Implement logic to insert suggestion into your database +// } +// } diff --git a/mistermeow/app/src/main/java/meowEngine/README.md b/mistermeow/app/src/main/java/meowEngine/README.md new file mode 100644 index 0000000..0b16fec --- /dev/null +++ b/mistermeow/app/src/main/java/meowEngine/README.md @@ -0,0 +1,17 @@ +# How to run the server guide + +- This is the preferred way + +``` +grade bootRun +``` + +- but you could also run this (not recommended) + +``` +gradle engine +``` + +- Watch this to know how to convert the code [Convert](https://youtu.be/xY5TgA005cw?si=FjnEnZnGSR7uRNmB) + +- you will find your api at `localhost:8080` diff --git a/mistermeow/app/src/main/java/meowEngine/RestApplication.java b/mistermeow/app/src/main/java/meowEngine/RestApplication.java index 2045eae..c994bfa 100644 --- a/mistermeow/app/src/main/java/meowEngine/RestApplication.java +++ b/mistermeow/app/src/main/java/meowEngine/RestApplication.java @@ -4,5 +4,4 @@ import javax.ws.rs.core.Application; @ApplicationPath("/") -public class RestApplication extends Application { -} +public class RestApplication extends Application {}