T9 Keyboard Emulator Better

Here’s a minimal but better T9 engine:

KEY_MAP = 
    '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
    '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'

def digits_to_pattern(word): return ''.join(str(next(k for k,v in KEY_MAP.items() if ch in v)) for ch in word)

class BetterT9: def init(self, dictionary): self.trie = {} for word in dictionary: pattern = digits_to_pattern(word) node = self.trie for d in pattern: node = node.setdefault(d, {}) node.setdefault('words', []).append(word) t9 keyboard emulator better

def suggest(self, digits):
    node = self.trie
    for d in digits:
        if d not in node:
            return []
        node = node[d]
    return node.get('__words__', [])

Most beginner implementations fail in three ways:

Let’s fix that.

Here is where the "better" argument gets technical.

When an emulator offers glide-resistant single-tap prediction, it outpaces Gboard by a significant margin. Here’s a minimal but better T9 engine: KEY_MAP

If you search the app store for "T9 keyboard," you will find shovelware. Here are the three emulators that have perfected the formula and prove the "better" thesis.