Description of the project
Purpose: To implement a simple automatic word-completion system.
Task 1: Implement algorithms for DLB insertion and traversal.
Task 2: Implement an algorithm for retrieving one word prediction.
Background
Autocomplete is a commonly used feature in mobile phones, text editors, and search engines. As
a user types in letters, the system shows a list of word predictions to help the user complete the
word they are typing. The core of an efficient autocompletion system is a fast algorithm for
retrieving word predictions based on the user input. The word predictions are all the words (in a
given dictionary) that start with what the user has typed so far (i.e., the list of words for which the
user's input is a prefix).
In this assignment we will build a simple autocompletion system using a DLB trie which allows the
user to add a new word to its dictionary when none of the predictions are selected by the user.
The AutoCompleteInterface defines a Java interface for a dictionary that provides word predictions
for such an autocompletion system. Besides storing a set of words, the dictionary keeps track of a
prefix String, which starts with the empty String. You will implement methods to add one character
to and delete the last character of that prefix String. You will also implement methods that use that
prefix String for various operations, such as checking if the prefix String is one of the words in the
dictionary, retrieving the number of words that have the prefix String as a prefix, and adding the
prefix String to the dictionary.
Details
In the first part of this assignment, you will implement algorithms for:
inserting a StringBuilder object into a DLB trie by filling in the add(String word) method below. You
may want to add and call a private recursive helper method.
The DLBNode class is already defined for you in AutoComplete.java. You will use a doubly-linked
list of siblings, a child reference, and an upwards reference to the node's parent. The isWord field
of a node is true if and only if the node is at the end of a word in the dictionary. The size field
keeps track of the number of nodes with isWord==true in the subtree rooted at the node, including
the node itself. The add method should update the size field for some of the nodes that it goes
over. You will need to think about for which nodes size should be incremented.
The next set of the methods modify the prefix String maintained by the dictionary. These methods
are: advance(char c) for appending a character to the prefix String, retreat() for deleting the last
character, and reset() for resetting it back to the empty String. More details about these methods
can be found below. Your code should maintain a DLBNode currentNode reference to always
point to the DLB node at the end of the prefix String. currentNode is null when the prefix String is
"", moves over the sibling list of the root node upon the first call to advance, moves possibly
sideways then down the tr.
The byproduct of sericulture in different industries.pptx
Description of the project Purpose To implement a simple au.pdf
1. Description of the project
Purpose: To implement a simple automatic word-completion system.
Task 1: Implement algorithms for DLB insertion and traversal.
Task 2: Implement an algorithm for retrieving one word prediction.
Background
Autocomplete is a commonly used feature in mobile phones, text editors, and search engines. As
a user types in letters, the system shows a list of word predictions to help the user complete the
word they are typing. The core of an efficient autocompletion system is a fast algorithm for
retrieving word predictions based on the user input. The word predictions are all the words (in a
given dictionary) that start with what the user has typed so far (i.e., the list of words for which the
user's input is a prefix).
In this assignment we will build a simple autocompletion system using a DLB trie which allows the
user to add a new word to its dictionary when none of the predictions are selected by the user.
The AutoCompleteInterface defines a Java interface for a dictionary that provides word predictions
for such an autocompletion system. Besides storing a set of words, the dictionary keeps track of a
prefix String, which starts with the empty String. You will implement methods to add one character
to and delete the last character of that prefix String. You will also implement methods that use that
prefix String for various operations, such as checking if the prefix String is one of the words in the
dictionary, retrieving the number of words that have the prefix String as a prefix, and adding the
prefix String to the dictionary.
Details
In the first part of this assignment, you will implement algorithms for:
inserting a StringBuilder object into a DLB trie by filling in the add(String word) method below. You
may want to add and call a private recursive helper method.
The DLBNode class is already defined for you in AutoComplete.java. You will use a doubly-linked
list of siblings, a child reference, and an upwards reference to the node's parent. The isWord field
of a node is true if and only if the node is at the end of a word in the dictionary. The size field
keeps track of the number of nodes with isWord==true in the subtree rooted at the node, including
the node itself. The add method should update the size field for some of the nodes that it goes
over. You will need to think about for which nodes size should be incremented.
The next set of the methods modify the prefix String maintained by the dictionary. These methods
are: advance(char c) for appending a character to the prefix String, retreat() for deleting the last
character, and reset() for resetting it back to the empty String. More details about these methods
can be found below. Your code should maintain a DLBNode currentNode reference to always
point to the DLB node at the end of the prefix String. currentNode is null when the prefix String is
"", moves over the sibling list of the root node upon the first call to advance, moves possibly
sideways then down the trie with each further call to advance, moves possibly sideways then up
the trie with retreat until resetting to null when the prefix String becomes empty, and resets to null
with reset.
The last set of methods operate on the prefix String by checking whether it is a word in the
dictionary (isWord()), adding it if not (add()), retrieving the number predictions for the prefix String
(getNumberOfPredictions), and retrieving one of the predictions (if any) (retrievePrediction).
2. Retrieving the number predictions should be as simple as returning the size field of the
currentNode. The add() method should increment the size field for some of the nodes (which
ones?). (Hint: you may need to climb up the trie.)
/**
* An implementation of the AutoCompleteInterface using a DLB Trie.
*/
import java.util.ArrayList;
import javax.xml.crypto.Data;
public class AutoComplete implements AutoCompleteInterface {
private DLBNode root;
private StringBuilder currentPrefix;
private DLBNode currentNode;
private static final char SENTINEL = '^';
//TODO: Add more instance variables as needed
public AutoComplete(){
root = null;
currentPrefix = new StringBuilder();
currentNode = null;
}
/**
* Adds a word to the dictionary in O(alphabet size*word.length()) time
* @param word the String to be added to the dictionary
* @return true if add is successful, false if word already exists
* @throws IllegalArgumentException if word is the empty string
*/
public boolean add(String word){
//TODO: implement this method
recursively with a helper method
}
/**
* appends the character c to the current prefix in O(alphabet size) time.
* This method doesn't modify the dictionary.
* @param c: the character to append
* @return true if the current prefix after appending c is a prefix to a word
* in the dictionary and false otherwise
*/
public boolean advance(char c){
//TODO: implement this method
return false;
}
/**
* removes the last character from the current prefix in O(1) time. This
3. * method doesn't modify the dictionary.
* @throws IllegalStateException if the current prefix is the empty string
*/
public void retreat(){
//TODO: implement this method
}
/**
* resets the current prefix to the empty string in O(1) time
*/
public void reset(){
//TODO: implement this method
}
/**
* @return true if the current prefix is a word in the dictionary and false
* otherwise. The running time is O(1).
*/
public boolean isWord(){
//TODO: implement this method
return false;
}
/**
* adds the current prefix as a word to the dictionary (if not already a word)
* The running time is O(alphabet size*length of the current prefix).
*/
public void add(){
//TODO: implement this method
}
/**
* @return the number of words in the dictionary that start with the current
* prefix (including the current prefix if it is a word). The running time is
* O(1).
*/
public int getNumberOfPredictions(){
//TODO: implement this method
return 0;
}
/**
* retrieves one word prediction for the current prefix. The running time is
* O(prediction.length())
* @return a String or null if no predictions exist for the current prefix
*/
public String retrievePrediction(){
4. //TODO: implement this method
return null;
}
/* ==============================
* Helper methods for debugging.
* ==============================
*/
//print the subtrie rooted at the node at the end of the start String
public void printTrie(String start){
System.out.println("==================== START: DLB Trie Starting from ""+ start + ""
====================");
if(start.equals("")){
printTrie(root, 0);
} else {
DLBNode startNode = getNode(root, start, 0);
if(startNode != null){
printTrie(startNode.child, 0);
}
}
System.out.println("==================== END: DLB Trie Starting from ""+ start + ""
====================");
}
//a helper method for printTrie
private void printTrie(DLBNode node, int depth){
if(node != null){
for(int i=0; i<depth; i++){
System.out.print(" ");
}
System.out.print(node.data);
if(node.isWord){
System.out.print(" *");
}
System.out.println(" (" + node.size + ")");
printTrie(node.child, depth+1);
printTrie(node.nextSibling, depth);
}
}
//return a pointer to the node at the end of the start String
//in O(start.length() - index)
private DLBNode getNode(DLBNode node, String start, int index){
if(start.length() == 0){
5. return node;
}
DLBNode result = node;
if(node != null){
if((index < start.length()-1) && (node.data == start.charAt(index))) {
result = getNode(node.child, start, index+1);
} else if((index == start.length()-1) && (node.data == start.charAt(index))) {
result = node;
} else {
result = getNode(node.nextSibling, start, index);
}
}
return result;
}
//The DLB node class
private class DLBNode{
private char data;
private int size;
private boolean isWord;
private DLBNode nextSibling;
private DLBNode previousSibling;
private DLBNode child;
private DLBNode parent;
private DLBNode(char data){
this.data = data;
size = 0;
isWord = false;
nextSibling = previousSibling = child = parent = null;
}
}
}
the below is the interface
/**
* An interface for a dictionary that provides word suggestions for an
* auto-complete system and maintains a current prefix
*/
public interface AutoCompleteInterface {
/**
* Adds a word to the dictionary in O(alphabet size*word.length()) time
* @param word the String to be added to the dictionary
* @return true if add is successful, false if word already exists
* @throws IllegalArgumentException if word is the empty string
6. */
public boolean add(String word);
/**
* appends the character c to the current prefix in O(alphabet size) time.
* This method doesn't modify the dictionary.
* @param c: the character to append
* @return true if the current prefix after appending c is a prefix to a word
* in the dictionary and false otherwise
*/
public boolean advance(char c);
/**
* removes the last character from the current prefix in O(alphabet size)
* time. This method doesn't modify the dictionary.
* @throws IllegalStateException if the current prefix is the empty string
*/
public void retreat();
/**
* resets the current prefix to the empty string in O(1) time
*/
public void reset();
/**
* @return true if the current prefix is a word in the dictionary and false
* otherwise. The running time is O(1).
*/
public boolean isWord();
/**
* adds the current prefix as a word to the dictionary (if not already a word)
* The running time is O(alphabet size*length of the current prefix).
*/
public void add();
/**
* @return the number of words in the dictionary that start with the current
* prefix (including the current prefix if it is a word). The running time is
* O(1).
*/
public int getNumberOfPredictions();
/**
* retrieves one word prediction for the current prefix. The running time is
* O(prediction.length())
* @return a String or null if no predictions exist for the current prefix
*/
public String retrievePrediction();
7. }
complete the todo according the comment given on the interface and the add (string word) method
should be in the similar structure like put method
public void put(String key, Value val) {
if (key == null) throw new IllegalArgumentException("calls put() with a null key");
key = key + SENTINEL;
root = put(root, key, val, 0);
}
private Node put(Node x, String key, Value val, int pos) {
Node result = x;
if (x == null){
result = new Node();
result.letter = key.charAt(pos);
result.level = pos;
if(pos < key.length()-1){
result.child = put(result.child,key ,val ,pos +1);/*TODO: Recurse on the child node*/
} else {
result.val = val;
}
} else if(x.letter == key.charAt(pos)) {
if(pos < key.length()-1){
result.child = put(result.child,key ,val ,pos +1); /*TODO: Recurse on the child node*/
} else {
result.val = val; //update
}
} else {
result.sibling = put(result.sibling, key , val, pos); /*TODO: Recurse on the sibling node*/
}
return result;
}