Question: Assignment: Predictive Text Assistant Introduction: Typing efficiently is essential in today's fast - paced digital world. Many devices offer predictive text assistance, suggesting letters or
Assignment: Predictive Text Assistant
Introduction:
Typing efficiently is essential in today's fastpaced digital world. Many devices offer predictive text assistance, suggesting letters or words to help users type faster. However, these predictions are not always accurate, leading to frustration and errors.
Imagine you are developing a personalized text suggestion system that predicts the most likely next letters based on your typing history. This system analyzes past usage to provide tailored suggestions, reducing errors and improving the overall typing experience. Unlike existing systems, which complete entire words, your system focuses on suggesting only the most probable next letters ensuring better flexibility and precision.
This program will simulate a text prediction engine capable of learning from user input and responding to queries with suggestions. It offers the following functionalities:
Adding words and their usage frequencies to the system's dictionary.
Respond to queries by predicting the most likely next letter based on the provided prefix. The program will appropriately inform the user if no words match a given prefix.
Example:
Suppose your dictionary contains the following data:
"hello" used times
"help" used times
"heat" used times
"hero" used times
If you query with the prefix, he the system will analyze the words starting with he and suggest the most likely next letters:
appears in "hello" and "help" a total of times.
a appears in "heat" a total of times.
r appears in "hero" a total of times.
Thus, the most likely next letter is
If you query with the prefix ho since no word starts with ho the program will respond appropriately, indicating that no matching prefix exists in the dictionary.
The Problem
You are tasked with designing a system that answers queries about the most likely next letter based on a list of words and their usage frequencies. Your program will process a series of operations, which include adding words to a dictionary and querying for prefixes.
For each query, your program must:
Return the most likely next letters for the given prefix, based on the frequencies of words in the dictionary.
If no word in the dictionary has the given prefix as a proper prefix ie the prefix is not strictly shorter than any matching word respond with "unrecognized prefix". The Input to be read from standard console input not file i Any file io in the code will get zero
The input begins with a single positive integer, boldsymboln which specifies the number of commands your program needs to process. Each of the following boldsymboln lines contains one command, in one of two formats:
Insert Command
A command that starts with the integer followed by a word and a frequency:
sf
s is a string of lowercase letters representing the word to be added to the dictionary.
f is a positive integer representing the frequency with which the word was used. This command indicates that the word s has been used an additional ftimes Note that the same word may appear multiple times in the input, as users can use the same word at different times. The total usage frequency is the cumulative sum of all occurrences of the word.
Query
Command
A command that starts with the integer followed by a prefix:
p
p is a string of lowercase letters representing the prefix for which the user wants a prediction.
This query asks the program to find the most likely next letters based on the current dictionary. A query does not modify the dictionary.
More input specification:
The total number of letters in all input words will not exceed
The sum of the frequencies of all added words will not exceed
The Output to be printed to standard console output no file io Any file io in the code will get zero
For each query command, your program must produce exactly one line of output:
If the prefix is a proper prefix for one or more words in the dictionary at the time of the query:
Print all the letters that are the most likely to come next, in alphabetical order, without space between them.
If the prefix is not a proper prefix for any word in the dictionary:
Print the string "unrecognized prefix" on a line by itself.
Implementation Restrictions
You are required to implement a trie data structure to manage all input words and handle queries. Each trie node should include the following information:
Node Frequency: The frequency of the string represented by this node ie how many times this specific string has been added to the dictionary
Sum Prefix Frequency: The total frequency of all words in the dictionary that have this string as a prefix, including the string itself.
Maximum Child Frequency: The highest frequency among all child nodes of the current node. For example, if the node represents "app" and there are currently occurrences of words starting with "appl" and with "appy," this value should be
Children Pointe
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
