예제 #1
0
 // here codes represents codes for word1
 private int calculateSimilarity(WordComposer composer, CharSequence word2) {
   int score = 0;
   CharSequence word1 = composer.getTypedWord();
   int length = word1.length() < word2.length() ? word1.length() : word2.length();
   for (int i = 0; i < length; i++) {
     char c1 = word1.charAt(i);
     char c2 = word2.charAt(i);
     if (c1 == c2) {
       score++;
     } else {
       // check code similarity?
       int[] codes = composer.getCodesAt(i);
       for (int code : codes) {
         if ((char) code == c2) {
           score++;
         }
       }
     }
   }
   if (word1.charAt(word1.length() - 1) == word2.charAt(word2.length() - 1)) {
     score += 3;
   }
   score -= Math.abs(word2.length() - word1.length()) / 2;
   return score;
 }
예제 #2
0
  @Override
  public void getBigrams(
      final WordComposer composer,
      final CharSequence previousWord,
      final WordCallback callback,
      int[] nextLettersFrequencies) {
    CharSequence typed = composer.getTypedWord();
    if (guesses == null) {
      guesses = new ArrayList<String>(maxGuesses);
    }

    if (isValidWord(typed) && !guesses.contains(typed)) {
      guesses.add(0, typed.toString());
    }

    if (typed == null || previousWord == null) {
      return;
    }
    if (typed.length() == 0 || previousWord.length() == 0) {
      return;
    }
    int minDist = Integer.MAX_VALUE;
    // int maxScore = 0;
    String firstChar = String.valueOf(typed.charAt(0));
    String[] projection = {Bigrams.WORD1, Bigrams.WORD2, Bigrams.FREQ};
    String selection =
        "SUBSTR("
            + Bigrams.WORD2
            + ", 1, 1)=? AND "
            + Bigrams.LENGTH1
            + "<? AND "
            + Bigrams.LENGTH2
            + "<?";
    String[] selectionArgs = {
      firstChar,
      String.valueOf(previousWord.length() + maxLengthDiff),
      String.valueOf(typed.length() + maxLengthDiff)
    };
    String sortOrder = Bigrams.FREQ + " DESC";
    Cursor c =
        mContentResolver.query(
            Bigrams.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
    while (c.moveToNext()) {
      String[] guessed = {c.getString(0), c.getString(1)};
      if (!guesses.contains(guessed[1])) {
        int distance = levenshteinDistance(typed, guessed[0]);
        distance += levenshteinDistance(previousWord.toString(), guessed[1]);
        if (distance <= minDist && !guesses.contains(guessed[1])) {
          minDist = distance;
          int i = guesses.contains(typed) ? 1 : 0;
          guesses.add(i, guessed[1]);
        }

        if (guesses.size() == maxGuesses) {
          break;
        }
      }
    }
    c.close();
  }
예제 #3
0
  public void getWordsFromSwype(WordComposer composer) {
    CharSequence typed = composer.getTypedWord();
    if (typed == null || typed.length() == 0) {
      return;
    }
    int minDist = Integer.MAX_VALUE;
    if (isValidWord(typed) && !guesses.contains(typed)) {
      guesses.add(0, typed.toString());
    }
    String firstChar = String.valueOf(typed.charAt(0));
    String[] projection = {Unigrams.TOKEN, Unigrams.FREQ};
    String selection = Unigrams.LENGTH + "<? AND SUBSTR(" + Unigrams.TOKEN + ", 1, 1)=?";
    String[] selectionArgs = {String.valueOf(typed.length() + maxLengthDiff), firstChar};
    Cursor c =
        mContentResolver.query(
            Unigrams.CONTENT_URI, projection, selection, selectionArgs, Unigrams.FREQ + " DESC");
    while (c.moveToNext()) {
      String curString = c.getString(0);

      int distance = levenshteinDistance(typed, curString);
      if (distance <= minDist && !guesses.contains(curString)) {
        minDist = distance;
        int i = guesses.contains(typed) ? 1 : 0;
        guesses.add(i, curString);
      }
      if (guesses.size() == maxGuesses) {
        break;
      }
    }
    c.close();
  }
예제 #4
0
  @Override
  public void getWords(WordComposer composer, WordCallback callback, int[] nextLettersFrequencies) {
    CharSequence typed = composer.getTypedWord();
    // int maxScore = 0;
    int minDist = Integer.MAX_VALUE;
    if (typed == null || typed.length() == 0) {
      return;
    }
    String firstChar = String.valueOf(typed.charAt(0));
    String[] projection = {Unigrams.TOKEN, Unigrams.FREQ};
    String selection = Unigrams.LENGTH + "<? AND SUBSTR(" + Unigrams.TOKEN + ", 1, 1)=?";
    String[] selectionArgs = {String.valueOf(typed.length() + maxLengthDiff), firstChar};
    Cursor c =
        mContentResolver.query(
            Unigrams.CONTENT_URI, projection, selection, selectionArgs, Unigrams.FREQ + " DESC");
    while (c.moveToNext()) {
      String curString = c.getString(0);
      int distance = levenshteinDistance(typed, curString);
      if (distance <= minDist && !guesses.contains(curString)) {
        minDist = distance;
        guesses.add(0, curString);
      }
      /*int score = calculateSimilarity(composer, curString);
      			//This is mad sketchy
      //			if(score >= maxScore && !guesses.contains(curString)){
      				maxScore = score;
      				guesses.add(0, curString);
      			}*/

      if (guesses.size() == maxGuesses) {
        break;
      }
    }
    c.close();
    // TODO: callback business?
  }