Prune out suggestions that have a very large edit distance.

If the number of keys picked from proximity is too large, prune out
the subtree. Otherwise you get vastly unrelated suggestions.

Fix a bug introduced with the missing_chars checkin.
diff --git a/dictionary/src/dictionary.cpp b/dictionary/src/dictionary.cpp
index 6fe7f4a..02843ab 100644
--- a/dictionary/src/dictionary.cpp
+++ b/dictionary/src/dictionary.cpp
@@ -60,8 +60,9 @@
     mMaxWords = maxWords;
     mWords = 0;
     mSkipPos = skipPos;
+    mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
 
-    getWordsRec(0, 0, mInputLength * 3, false, 1, 0);
+    getWordsRec(0, 0, mInputLength * 3, false, 1, 0, 0);
 
     if (DEBUG_DICT) LOGI("Returning %d words", mWords);
     return mWords;
@@ -108,7 +109,11 @@
 Dictionary::addWord(unsigned short *word, int length, int frequency)
 {
     word[length] = 0;
-    if (DEBUG_DICT) LOGI("Found word = %s, freq = %d : \n", word, frequency);
+    if (DEBUG_DICT) {
+        char s[length + 1];
+        for (int i = 0; i <= length; i++) s[i] = word[i];
+        LOGI("Found word = %s, freq = %d : \n", s, frequency);
+    }
 
     // Find the right insertion point
     int insertAt = 0;
@@ -176,12 +181,16 @@
 static char QUOTE = '\'';
 
 void
-Dictionary::getWordsRec(int pos, int depth, int maxDepth, bool completion, int snr, int inputIndex)
+Dictionary::getWordsRec(int pos, int depth, int maxDepth, bool completion, int snr, int inputIndex,
+                        int diffs)
 {
     // Optimization: Prune out words that are too long compared to how much was typed.
     if (depth > maxDepth) {
         return;
     }
+    if (diffs > mMaxEditDistance) {
+        return;
+    }
     int count = getCount(&pos);
     int *currentChars = NULL;
     if (mInputLength <= inputIndex) {
@@ -205,19 +214,19 @@
             }
             if (childrenAddress != 0) {
                 getWordsRec(childrenAddress, depth + 1, maxDepth,
-                            completion, snr, inputIndex);
+                            completion, snr, inputIndex, diffs);
             }
         } else if (c == QUOTE && currentChars[0] != QUOTE || mSkipPos == depth) {
             // Skip the ' or other letter and continue deeper
             mWord[depth] = c;
             if (childrenAddress != 0) {
-                getWordsRec(childrenAddress, depth + 1, maxDepth, false, snr, inputIndex);
+                getWordsRec(childrenAddress, depth + 1, maxDepth, false, snr, inputIndex, diffs);
             }
         } else {
             int j = 0;
             while (currentChars[j] > 0) {
-                int addedWeight = j == 0 ? mTypedLetterMultiplier : 1;
                 if (currentChars[j] == lowerC || currentChars[j] == c) {
+                    int addedWeight = j == 0 ? mTypedLetterMultiplier : 1;
                     mWord[depth] = c;
                     if (mInputLength == inputIndex + 1) {
                         if (terminal) {
@@ -229,11 +238,12 @@
                         }
                         if (childrenAddress != 0) {
                             getWordsRec(childrenAddress, depth + 1,
-                                    maxDepth, true, snr * addedWeight, inputIndex + 1);
+                                    maxDepth, true, snr * addedWeight, inputIndex + 1,
+                                    diffs + (j > 0));
                         }
                     } else if (childrenAddress != 0) {
                         getWordsRec(childrenAddress, depth + 1, maxDepth,
-                                false, snr * addedWeight, inputIndex + 1);
+                                false, snr * addedWeight, inputIndex + 1, diffs + (j > 0));
                     }
                 }
                 j++;