T9 input method implementation

Source: Internet
Author: User

T9 input method, the name sounds strange, but it is often used by everyone. It can be said that the T9 input method is a revolution in the input method history. At least since the T9 input method, the input method has made great progress.
The mobile phone has nine numeric keys. 26 English letters are allocated to the 8 numeric keys from 2 to 9. In the past, when you wanted to enter an English word, you always had to press a key multiple times in a row to get the target letter. For example, if you want to input "hello", You need to press twice 4, twice 3, three times 5, three times 5, three times 6. it takes dozens of buttons to input a word, not to mention the frequent error. It is very troublesome to edit a text message.
The T9 input method effectively solves this problem. You can use nine numeric keys to enter English words that are complex and do not need to repeat a character. The system will find the most likely words based on the existing dictionary. For example, if the target word is "hello", you only need to enter 4, 3, 5, 6. the system automatically filters out invalid words such as "gdjjm ". To exclude invalid words, only valid words are taken into account, which greatly accelerates the input speed.
However, this is only the first step, because after each number is entered, there may be multiple candidate word prefixes matching this. This requires that the most likely (most commonly used) English words be provided based on the likelihood. For example, the system knows two words: "idea" and "hello ". Idea is the most commonly used (most likely ). When you enter, 5, and 6 in sequence, the candidate words for each key press system should be:
I (4)
ID (3)
El (5)
Hell (5)
Hello (6)
In this way, the input speed and simplicity can be greatly improved through the T9 input method.
How to Implement the T9 input method?
(1) create a dictionary
Reserve a large number of words for quick search, and the dictionary tree is undoubtedly a good choice. Each node will store the possibility of this prefix. The structure is as follows:

/** Dictionary tree */public class trie {public node root = new node (); // dictionary tree root node public class node {public int probablity; Public node [] Next; public node () {This. probablity = 0; next = new node [26] ;}} public void insert (string STR, int probablity) {node P = root; int I = 0; for (I = 0; I <Str. length (); I ++) {If (P. next [Str. charat (I)-'a']! = NULL) {P = P. next [Str. charat (I)-'a']; p. probablity + = probablity;} else {node q = new node (); q. probablity = probablity; p. next [Str. charat (I)-'a'] = Q; P = P. next [Str. charat (I)-'a'] ;}} public int search (string Str) {node P = root; int I = 0; for (I = 0; I <Str. length (); I ++) {If (P. next [Str. charat (I)-'a']! = NULL) {P = P. Next [Str. charat (I)-'a'];} else {return 0 ;}} return P. probablity ;}}

(2) Search
After creating a dictionary tree, each node can have up to 26 children representing the next character. When a search string "43556" is given, we use the breadth-first traversal to search for the possibility of all prefixes of length x. Find the largest one. The queue is used for extensive search.
The simulation process is as follows:
After 4 (G, H, I) is input, only "h" and "I" are found through dictionary tree traversal, and the maximum output (I) that may be read is found, i.
Input 3 (D, E, F) and take out the "h" in the queue to form "HD", "he", and "HF", respectively, enter a valid string to record the maximum possibility. Then, exit H. Perform the same operation on "I. Finally, find the largest possible read maximum string.
......
It can be seen that a large number of pruning and common words can be up to a dozen letters in the Process of extensive search. Therefore, the depth of the dictionary tree is also more than a dozen, with a large number of pruning, the performance is not bad.

Static int [] [] ref = {// phone keypad number-letter ing table {0}, {0}, {3, 0, 1, 2 }, // button 2, three letters a, B, c {3, 3, 4, 5}, {3, 6, 7, 8}, {3, 9, 10, 11}, {3, 12, 13, 14}, {4, 15, 16, 17, 18}, {3, 19, 20, 21}, {4, 22, 23, 24, 25 }}; public static queue <string> queue = new queue list <string> (); // this parameter is used for the public static trie in the breadth-first traversal process; // dictionary tree public static void BFS (string Str) {queue. clear (); queue. offer (""); int I = 0; for (I = 0; '1 '! = Str. charat (I); I ++) {int max_probablity = 0; // Maximum Likelihood value string probastr = ""; int pre_amount = queue. size (); // The number of previous strings in the queue Int J = 0; For (j = 0; j <pre_amount; j ++) {int K = 0; string prestr = queue. peek (); For (k = 1; k <= Ref [Str. charat (I)-'0'] [0]; k ++) {string tempstr = prestr. concat (char) (Ref [Str. charat (I)-'0'] [k] + 97) + ""); If (trie. search (tempstr)> 0) {queue. offer (tempstr);} If (trie. search (tempstr)> Max_probablity) {max_probablity = trie. search (tempstr); probastr = tempstr ;}} queue. poll ();} If ("". equals (probastr) {break;} else {system. out. println (probastr) ;}} while ('1 '! = Str. charat (I ++) {system. Out. println ("manually ");}}

Thoughts:
1. The above simulation process only gives the maximum possible degree of the string, you can also give all matching strings in order, and then the user selects.
2. It seems that the T9 input method has broken the deadlock of the previous input method, although it is only used in mobile phone input. However, I personally think that the popular smart keyboard input method also adopts similar processing. For example, enter "hao" "zi" "wei" "Zhi" in sequence using the sogou input method"
The greatest candidates are: "Good", "Mouse", "good taste", and "self-help ". Is it a bit T9 input method? If you are interested, you can discuss it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.