aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/inputmethod/latin/SuggestHelper.java
blob: 5930ea36eba108c4a549e2f3139526991005bebe (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2010 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;

import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.KeyDetector;
import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.keyboard.LatinKeyboard;
import com.android.inputmethod.keyboard.ProximityKeyDetector;

import android.content.Context;
import android.text.TextUtils;

import java.io.File;
import java.util.List;

public class SuggestHelper {
    protected final Suggest mSuggest;
    private final LatinKeyboard mKeyboard;
    private final KeyDetector mKeyDetector;

    public SuggestHelper(Context context, int dictionaryId, KeyboardId keyboardId) {
        mSuggest = new Suggest(context, dictionaryId);
        mKeyboard = new LatinKeyboard(context, keyboardId);
        mKeyDetector = new ProximityKeyDetector();
        init();
    }

    protected SuggestHelper(Context context, File dictionaryPath, long startOffset, long length,
            KeyboardId keyboardId) {
        mSuggest = new Suggest(dictionaryPath, startOffset, length);
        mKeyboard = new LatinKeyboard(context, keyboardId);
        mKeyDetector = new ProximityKeyDetector();
        init();
    }

    private void init() {
        mSuggest.setQuickFixesEnabled(false);
        mSuggest.setCorrectionMode(Suggest.CORRECTION_FULL);
        mKeyDetector.setKeyboard(mKeyboard, 0, 0);
        mKeyDetector.setProximityCorrectionEnabled(true);
        mKeyDetector.setProximityThreshold(KeyDetector.getMostCommonKeyWidth(mKeyboard));
    }

    public void setCorrectionMode(int correctionMode) {
        mSuggest.setCorrectionMode(correctionMode);
    }

    public boolean hasMainDictionary() {
        return mSuggest.hasMainDictionary();
    }

    private void addKeyInfo(WordComposer word, char c) {
        final List<Key> keys = mKeyboard.getKeys();
        for (final Key key : keys) {
            if (key.mCode == c) {
                final int x = key.mX + key.mWidth / 2;
                final int y = key.mY + key.mHeight / 2;
                final int[] codes = mKeyDetector.newCodeArray();
                mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes);
                word.add(c, codes, x, y);
                return;
            }
        }
        word.add(c, new int[] { c }, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
    }

    protected WordComposer createWordComposer(CharSequence s) {
        WordComposer word = new WordComposer();
        for (int i = 0; i < s.length(); i++) {
            final char c = s.charAt(i);
            addKeyInfo(word, c);
        }
        return word;
    }

    public boolean isValidWord(CharSequence typed) {
        return AutoCorrection.isValidWordForAutoCorrection(mSuggest.getUnigramDictionaries(),
                typed, false);
    }

    // TODO: This may be slow, but is OK for test so far.
    public SuggestedWords getSuggestions(CharSequence typed) {
        return mSuggest.getSuggestions(null, createWordComposer(typed), null);
    }

    public CharSequence getFirstSuggestion(CharSequence typed) {
        WordComposer word = createWordComposer(typed);
        SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null);
        // Note that suggestions.getWord(0) is the word user typed.
        return suggestions.size() > 1 ? suggestions.getWord(1) : null;
    }

    public CharSequence getAutoCorrection(CharSequence typed) {
        WordComposer word = createWordComposer(typed);
        SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null);
        // Note that suggestions.getWord(0) is the word user typed.
        return (suggestions.size() > 1 && mSuggest.hasAutoCorrection())
                ? suggestions.getWord(1) : null;
    }

    public int getSuggestIndex(CharSequence typed, CharSequence expected) {
        WordComposer word = createWordComposer(typed);
        SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null);
        // Note that suggestions.getWord(0) is the word user typed.
        for (int i = 1; i < suggestions.size(); i++) {
            if (TextUtils.equals(suggestions.getWord(i), expected))
                return i;
        }
        return -1;
    }

    private void getBigramSuggestions(CharSequence previous, CharSequence typed) {
        if (!TextUtils.isEmpty(previous) && (typed.length() > 1)) {
            WordComposer firstChar = createWordComposer(Character.toString(typed.charAt(0)));
            mSuggest.getSuggestions(null, firstChar, previous);
        }
    }

    public CharSequence getBigramFirstSuggestion(CharSequence previous, CharSequence typed) {
        WordComposer word = createWordComposer(typed);
        getBigramSuggestions(previous, typed);
        SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous);
        return suggestions.size() > 1 ? suggestions.getWord(1) : null;
    }

    public CharSequence getBigramAutoCorrection(CharSequence previous, CharSequence typed) {
        WordComposer word = createWordComposer(typed);
        getBigramSuggestions(previous, typed);
        SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous);
        return (suggestions.size() > 1 && mSuggest.hasAutoCorrection())
                ? suggestions.getWord(1) : null;
    }

    public int searchBigramSuggestion(CharSequence previous, CharSequence typed,
            CharSequence expected) {
        WordComposer word = createWordComposer(typed);
        getBigramSuggestions(previous, typed);
        SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous);
        for (int i = 1; i < suggestions.size(); i++) {
            if (TextUtils.equals(suggestions.getWord(i), expected))
                return i;
        }
        return -1;
    }
}