aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/spellcheck/SpellChecker.java
blob: e3407a26968abd8551067ae68b53817c94ef49fa (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.android.inputmethod.latin.spellcheck;

import android.content.Context;
import android.content.res.Resources;

import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.Dictionary.DataType;
import com.android.inputmethod.latin.Dictionary.WordCallback;
import com.android.inputmethod.latin.DictionaryFactory;
import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.WordComposer;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * Implements spell checking methods.
 */
public class SpellChecker {

    public final Dictionary mDictionary;

    public SpellChecker(final Context context, final Locale locale) {
        final Resources resources = context.getResources();
        final int fallbackResourceId = Utils.getMainDictionaryResourceId(resources);
        mDictionary = DictionaryFactory.createDictionaryFromManager(context, locale,
                fallbackResourceId);
    }

    // Note : this must be reentrant
    /**
     * Finds out whether a word is in the dictionary or not.
     *
     * @param text the sequence containing the word to check for.
     * @param start the index of the first character of the word in text.
     * @param end the index of the next-to-last character in text.
     * @return true if the word is in the dictionary, false otherwise.
     */
    public boolean isCorrect(final CharSequence text, final int start, final int end) {
        return mDictionary.isValidWord(text.subSequence(start, end));
    }

    private static class SuggestionsGatherer implements WordCallback {
        private final int DEFAULT_SUGGESTION_LENGTH = 16;
        private final List<String> mSuggestions = new LinkedList<String>();
        private int[] mScores = new int[DEFAULT_SUGGESTION_LENGTH];
        private int mLength = 0;

        synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score,
                int dicTypeId, DataType dataType) {
            if (mLength >= mScores.length) {
                final int newLength = mScores.length * 2;
                mScores = new int[newLength];
            }
            final int positionIndex = Arrays.binarySearch(mScores, 0, mLength, score);
            // binarySearch returns the index if the element exists, and -<insertion index> - 1
            // if it doesn't. See documentation for binarySearch.
            final int insertionIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
            System.arraycopy(mScores, insertionIndex, mScores, insertionIndex + 1,
                    mLength - insertionIndex);
            mLength += 1;
            mScores[insertionIndex] = score;
            mSuggestions.add(insertionIndex, new String(word, wordOffset, wordLength));
            return true;
        }

        public List<String> getGatheredSuggestions() {
            return mSuggestions;
        }
    }

    // Note : this must be reentrant
    /**
     * Gets a list of suggestions for a specific string.
     *
     * This returns a list of possible corrections for the text passed as an
     * arguments. It may split or group words, and even perform grammatical
     * analysis.
     *
     * @param text the sequence containing the word to check for.
     * @param start the index of the first character of the word in text.
     * @param end the index of the next-to-last character in text.
     * @return a list of possible suggestions to replace the text.
     */
    public List<String> getSuggestions(final CharSequence text, final int start, final int end) {
        final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer();
        final WordComposer composer = new WordComposer();
        for (int i = start; i < end; ++i) {
            int character = text.charAt(i);
            composer.add(character, new int[] { character },
                    WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
        }
        mDictionary.getWords(composer, suggestionsGatherer);
        return suggestionsGatherer.getGatheredSuggestions();
    }
}