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++;
diff --git a/dictionary/src/dictionary.h b/dictionary/src/dictionary.h
index 70dfa73..9173e39 100644
--- a/dictionary/src/dictionary.h
+++ b/dictionary/src/dictionary.h
@@ -51,7 +51,7 @@
     bool addWord(unsigned short *word, int length, int frequency);
     unsigned short toLowerCase(unsigned short c, int depth);
     void getWordsRec(int pos, int depth, int maxDepth, bool completion, int frequency,
-            int inputIndex);
+            int inputIndex, int diffs);
     bool isValidWordRec(int pos, unsigned short *word, int offset, int length);
 
     unsigned char *mDict;
@@ -67,6 +67,7 @@
     int mMaxAlternatives;
     unsigned short mWord[128];
     int mSkipPos;
+    int mMaxEditDistance;
 
     int mFullWordMultiplier;
     int mTypedLetterMultiplier;
diff --git a/src/com/android/inputmethod/latin/BinaryDictionary.java b/src/com/android/inputmethod/latin/BinaryDictionary.java
index ab66c5b..836c803 100644
--- a/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -101,10 +101,11 @@
         // completions.
         if (ENABLE_MISSED_CHARACTERS && count < 5) {
             for (int skip = 0; skip < codesSize; skip++) {
-                count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize,
+                int tempCount = getSuggestionsNative(mNativeDict, mInputCodes, codesSize,
                         mOutputChars, mFrequencies,
                         MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES, skip);
-                if (count > 0) break;
+                count = Math.max(count, tempCount);
+                if (tempCount > 0) break;
             }
         }