Most-Likely Next-Word Continuation Predictor
Problem statement
Train a word-transition predictor from trainingData. Every adjacent pair (word, nextWord) in a training sequence contributes one occurrence to that transition.
For each word, its predicted successor is the outgoing word with the greatest transition count. Break equal-count ties by lexicographically smaller successor.
For each word in queries, construct a continuation by repeatedly appending its predicted successor and using that successor as the next current word. Stop after maxWords appended words or as soon as the current word has no learned successor. Return one continuation row per query.
Function
predictContinuations(trainingData: String[][], queries: String[], maxWords: int) → String[][]Examples
Example 1
trainingData = [["I","am","sam"],["am","sam"]]queries = ["I","am"]maxWords = 3return = [["am","sam"],["sam"]]I transitions to am, then am transitions to sam. No successor was learned for sam.
Example 2
trainingData = [["a","b"],["a","c"],["a","b"],["b","d"]]queries = ["a","missing"]maxWords = 2return = [["b","d"],[]]The transition a → b appears twice while a → c appears once. The unseen query has no continuation.
Example 3
trainingData = [["x","z"],["x","y"],["y","x"]]queries = ["x"]maxWords = 4return = [["y","x","y","x"]]x → y wins the equal-frequency tie over x → z. The learned cycle is bounded by maxWords.
Constraints
- The total number of words across
trainingDatais at most100000. 1 <= queries.length <= 10000.1 <= maxWords <= 100.- Every token and query is a nonempty case-sensitive string of at most
40characters. - A training sequence may contain one word and therefore contribute no transition.