feat: Add Java Search Suggestions project#37
Conversation
WalkthroughAdds a new Java Trie-based prefix search suggestions implementation with CLI, sample dictionary, README, and a project .gitignore. Changes
Sequence DiagramsequenceDiagram
participant User
participant Main as main()
participant Trie as SearchSuggestions
participant File as dictionary.txt
Main->>File: loadDictionary(filename)
File-->>Main: lines (words)
loop for each word
Main->>Trie: insert(word)
end
Note over Trie: Trie built from dictionary
Main->>User: prompt for prefix
User->>Main: prefix input
Main->>Trie: getSuggestions(prefix)
Note over Trie: traverse to prefix node\ncollect descendant words
Trie-->>Main: suggestions list
Main->>User: display suggestions
alt user continues
loop until 'q' or empty
User->>Main: next prefix
Main->>Trie: getSuggestions(prefix)
Trie-->>Main: suggestions list
Main->>User: display suggestions
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (3)
🧰 Additional context used🪛 LanguageToolSearch suggestion system/dictionary.txt[uncategorized] ~1-~1: Did you mean the proper noun “Apple” (= technology company)? (APPLE_PRODUCTS) 🔇 Additional comments (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (5)
.gitignore (1)
1-14: Consider adding additional common patterns.The .gitignore covers essential Java artifacts and IDE files. Consider optionally adding these common patterns for a more comprehensive setup:
# Java *.class *.log *.jar *.war *.ear + +# Build directories +target/ +build/ +out/ +bin/ + +# Editor temporary files +*.swp +*~ + # IDE files .idea/ .vscode/ *.iml *.iws *.ipr + # Other .DS_Store +Thumbs.dbSearch suggestion system/README.md (1)
59-64: Consider adjusting list indentation.The markdown linter suggests using 0-space indentation instead of 2 spaces for the unordered list items. While this is a stylistic preference, standardizing on 0-space indentation improves consistency with common markdown conventions.
Apply this diff:
- * `TrieNode`: A private inner class holds a `Map<Character, TrieNode>` for its children and a `boolean` to mark the end of a word. - * `insert(String word)`: Iterates through each character of the word, creating new nodes in the Trie as needed. - * `getSuggestions(String prefix)`: - 1. Traverses the Trie to the node corresponding to the last character of the prefix. - 2. If the prefix path doesn't exist, it returns an empty list. - 3. If it does, it calls a recursive helper `findAllWords()` to explore all child paths from that node and collect all complete words. +* `TrieNode`: A private inner class holds a `Map<Character, TrieNode>` for its children and a `boolean` to mark the end of a word. +* `insert(String word)`: Iterates through each character of the word, creating new nodes in the Trie as needed. +* `getSuggestions(String prefix)`: + 1. Traverses the Trie to the node corresponding to the last character of the prefix. + 2. If the prefix path doesn't exist, it returns an empty list. + 3. If it does, it calls a recursive helper `findAllWords()` to explore all child paths from that node and collect all complete words.Search suggestion system/SearchSuggestions.java (3)
25-31: Add input validation for robustness.The insert method doesn't validate the input parameter. If a null or empty word is passed, it could lead to unexpected behavior or exceptions.
Apply this diff to add input validation:
public void insert(String word) { + if (word == null || word.isEmpty()) { + return; // or throw IllegalArgumentException + } TrieNode node = root; for (char c : word.toLowerCase().toCharArray()) { node = node.children.computeIfAbsent(c, k -> new TrieNode()); } node.isEndOfWord = true; }
33-47: Add input validation for robustness.Similar to the insert method, getSuggestions doesn't validate the input parameter. If null is passed, it will throw a NullPointerException at line 34.
Apply this diff to add input validation:
public List<String> getSuggestions(String prefix) { + if (prefix == null) { + return Collections.emptyList(); + } String lowerPrefix = prefix.toLowerCase(); TrieNode node = root;
100-123: Consider using try-with-resources for Scanner.The Scanner is properly closed at line 122, but using try-with-resources would be more robust and ensure cleanup even if an exception occurs.
Apply this diff:
- Scanner scanner = new Scanner(System.in); - - while (true) { + try (Scanner scanner = new Scanner(System.in)) { + while (true) { + System.out.print("\nEnter prefix to search (or 'q' to quit): "); + String prefix = scanner.nextLine(); + + if (prefix.equalsIgnoreCase("q") || prefix.isEmpty()) { + break; + } + + List<String> suggestions = trie.getSuggestions(prefix); + + if (suggestions.isEmpty()) { + System.out.println("No suggestions found for '" + prefix + "'."); + } else { + System.out.println("Suggestions for '" + prefix + "':"); + for (String suggestion : suggestions) { + System.out.println(" -> " + suggestion); + } + } + } - System.out.print("\nEnter prefix to search (or 'q' to quit): "); - String prefix = scanner.nextLine(); - - if (prefix.equalsIgnoreCase("q") || prefix.isEmpty()) { - break; - } - - List<String> suggestions = trie.getSuggestions(prefix); - - if (suggestions.isEmpty()) { - System.out.println("No suggestions found for '" + prefix + "'."); - } else { - System.out.println("Suggestions for '" + prefix + "':"); - for (String suggestion : suggestions) { - System.out.println(" -> " + suggestion); - } - } + System.out.println("Goodbye!"); } - - scanner.close(); - System.out.println("Goodbye!");
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
.gitignore(1 hunks)Search suggestion system/README.md(1 hunks)Search suggestion system/SearchSuggestions.java(1 hunks)Search suggestion system/dictionary.txt(1 hunks)
🧰 Additional context used
🪛 LanguageTool
Search suggestion system/dictionary.txt
[uncategorized] ~1-~1: Did you mean the proper noun “Apple” (= technology company)?
Context: apple app application apply apricot banana ba...
(APPLE_PRODUCTS)
🪛 markdownlint-cli2 (0.18.1)
Search suggestion system/README.md
35-35: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
59-59: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
60-60: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
61-61: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
🔇 Additional comments (6)
Search suggestion system/dictionary.txt (1)
1-23: LGTM!The dictionary file contains a good set of sample words for testing the search suggestion system. The words are properly formatted (one per line, lowercase) and cover various prefixes for meaningful testing.
Search suggestion system/SearchSuggestions.java (5)
14-17: LGTM!The TrieNode inner class is well-designed with appropriate use of HashMap for children and a boolean flag for word endings. The private visibility provides good encapsulation.
21-23: LGTM!The constructor properly initializes the root node. The root field being final is a good practice.
49-59: LGTM!The recursive DFS traversal with backtracking is correctly implemented. The use of StringBuilder for efficient string building and proper backtracking with
setLengthdemonstrates good understanding of the algorithm.Note: Since HashMap doesn't guarantee iteration order, suggestions will not be returned in any particular order (e.g., not alphabetically sorted). This is acceptable for the current use case, but could be enhanced by sorting if needed.
64-81: LGTM!The loadDictionary method demonstrates good practices:
- Try-with-resources for automatic resource management
- Proper exception handling with specific catches
- Trimming and validation of input lines
- Appropriate error messages to stderr
86-91: Document the working directory requirement clearly.The comments explain that the program must be run from within the project directory, which is a reasonable constraint for this demonstration project. The README provides clear instructions for this.
Search suggestion system/README.md
Outdated
| ``` | ||
| Loading dictionary from 'dictionary.txt'... | ||
| Successfully loaded 10 words. | ||
|
|
||
| Enter prefix to search (or 'q' to quit): pro | ||
| Suggestions for 'pro': | ||
| -> profile | ||
| -> program | ||
| -> project | ||
| -> python | ||
|
|
||
| Enter prefix to search (or 'q' to quit): app | ||
| Suggestions for 'app': | ||
| -> app | ||
| -> apple | ||
| -> apply | ||
| -> application | ||
|
|
||
| Enter prefix to search (or 'q' to quit): q | ||
| Goodbye! | ||
| ``` |
There was a problem hiding this comment.
Incorrect example output.
Line 44 shows "python" as a suggestion for the prefix "pro", but "python" doesn't start with "pro". Based on the dictionary.txt file, the correct suggestions should only include: profile, program, and project.
Apply this diff to fix the example:
Enter prefix to search (or 'q' to quit): pro
Suggestions for 'pro':
-> profile
-> program
-> project
- -> pythonAdditionally, consider specifying the language for the output block to address the markdown linter warning:
-```
+```text
Loading dictionary from 'dictionary.txt'...🧰 Tools
🪛 markdownlint-cli2 (0.18.1)
35-35: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents
In Search suggestion system/README.md around lines 35 to 55, the example output
is wrong and the code block lacks a language: remove " -> python" from the
suggestions for prefix "pro" so the list shows only "profile", "program", and
"project", and change the opening fenced code block from ``` to ```text to
satisfy the markdown linter; keep the rest of the example output identical.
|
Hi @gitsofaryan, kindly review it!! |
This PR adds a "Search Suggestions System" project, written in Java.
Project Details:
Search suggestion systemSearchSuggestions.java(Implements a Trie data structure)dictionary.txt(A sample dictionary file)README.md(Includes compile/run instructions)This project is a clean implementation that demonstrates the practical application of a Trie (Prefix tree). Words in
dictionary.txtcan be extended as per user's requirements.Summary by CodeRabbit
New Features
Documentation
Chores
Assets