Ejemplo n.º 1
0
  /**
   * Adds a word to the dictionary and makes it persistent.
   *
   * @param word the word to add. If the word is capitalized, then the dictionary will recognize it
   *     as a capitalized word when searched.
   * @param frequency the frequency of occurrence of the word. A frequency of 255 is considered the
   *     highest. @TODO use a higher or float range for frequency
   */
  @Override
  public synchronized void addWord(String word, int frequency) {
    // Force load the dictionary here synchronously
    if (getRequiresReload()) loadDictionaryAsync();
    // Safeguard against adding long words. Can cause stack overflow.
    if (word.length() >= getMaxWordLength()) return;

    super.addWord(word, frequency);

    // Update the user dictionary provider
    final ContentValues values = new ContentValues(5);
    values.put(Words.WORD, word);
    values.put(Words.FREQUENCY, frequency);
    values.put(Words.LOCALE, mLocale);
    values.put(Words.APP_ID, 0);

    final ContentResolver contentResolver = getContext().getContentResolver();
    new Thread("addWord") {
      public void run() {
        contentResolver.insert(Words.CONTENT_URI, values);
      }
    }.start();

    // In case the above does a synchronous callback of the change observer
    setRequiresReload(false);
  }
Ejemplo n.º 2
0
  private void addWords(Cursor cursor) {
    clearDictionary();

    final int maxWordLength = getMaxWordLength();
    if (cursor.moveToFirst()) {
      while (!cursor.isAfterLast()) {
        String word = cursor.getString(INDEX_WORD);
        int frequency = cursor.getInt(INDEX_FREQUENCY);
        // Safeguard against adding really long words. Stack may overflow due
        // to recursion
        if (word.length() < maxWordLength) {
          super.addWord(word, frequency);
        }
        cursor.moveToNext();
      }
    }
    cursor.close();
  }
Ejemplo n.º 3
0
  private void addWords(Cursor cursor) {
    clearDictionary();

    final int maxWordLength = getMaxWordLength();
    if (cursor.moveToFirst()) {
      while (!cursor.isAfterLast()) {
        String name = cursor.getString(INDEX_NAME);

        if (name != null) {
          int len = name.length();

          // TODO: Better tokenization for non-Latin writing systems
          for (int i = 0; i < len; i++) {
            if (Character.isLetter(name.charAt(i))) {
              int j;
              for (j = i + 1; j < len; j++) {
                char c = name.charAt(j);

                if (!(c == '-' || c == '\'' || Character.isLetter(c))) {
                  break;
                }
              }

              String word = name.substring(i, j);
              i = j - 1;

              // Safeguard against adding really long words. Stack
              // may overflow due to recursion
              // Also don't add single letter words, possibly confuses
              // capitalization of i.
              final int wordLen = word.length();
              if (wordLen < maxWordLength && wordLen > 1) {
                super.addWord(word, 128);
              }
            }
          }
        }

        cursor.moveToNext();
      }
    }
    cursor.close();
  }