A context-aware autocomplete engine using n-gram language modeling. Combines a character trie for prefix matching with a word-level n-gram tree for context-aware predictions, intersecting results to surface the most relevant completions based on both the current word fragment and preceding context.
Try typing words like "the", "bee", "love", or "moon" - trained on Bee Movie script and A Midsummer Night's Dream.
Uses both a character trie for prefix matching and an n-gram word tree for context-aware predictions.
The word tree captures 4-gram sequences, allowing predictions based on preceding words in a phrase.
Combines results from both trees using set intersection and probability multiplication for optimal relevance.
Tracks word frequencies during training to rank suggestions by likelihood of occurrence.
// Find completions using both trees findCompletions(input) { const words = input.split(/\s+/); const lastWord = words[words.length - 1]; // Character-based completions (prefix match) const charCompletions = this.charTree.search( lastWord.split('')); // Word-based completions (context-aware) const context = words.slice(0, -1); const wordCompletions = this.wordTree.search(context); // Intersect and combine probabilities const intersection = this.intersect( charCompletions, wordCompletions); return intersection.sort((a, b) => b.probability - a.probability); }