Implement the spell checker.

Bug: 4176026
Change-Id: I297c47202e96ce0db74d703f30709777f26b93ac
This commit is contained in:
Jean Chalard 2011-08-04 20:19:12 +09:00
parent ac21e0a349
commit 3234123fba
2 changed files with 112 additions and 133 deletions

View file

@ -16,35 +16,130 @@
package com.android.inputmethod.latin.spellcheck; package com.android.inputmethod.latin.spellcheck;
import android.content.res.Resources;
import android.service.textservice.SpellCheckerService; import android.service.textservice.SpellCheckerService;
import android.util.Log; import android.util.Log;
import android.view.textservice.SuggestionsInfo; import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo; import android.view.textservice.TextInfo;
import com.android.inputmethod.compat.ArraysCompatUtils;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.ProximityInfo;
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.Collections;
import java.util.List;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
/** /**
* Service for spell checking, using LatinIME's dictionaries and mechanisms. * Service for spell checking, using LatinIME's dictionaries and mechanisms.
*/ */
public class AndroidSpellCheckerService extends SpellCheckerService { public class AndroidSpellCheckerService extends SpellCheckerService {
private static final String TAG = AndroidSpellCheckerService.class.getSimpleName(); private static final String TAG = AndroidSpellCheckerService.class.getSimpleName();
private static final boolean DBG = true; private static final boolean DBG = true;
@Override
public SuggestionsInfo getSuggestions(TextInfo textInfo, int suggestionsLimit, private final static String[] emptyArray = new String[0];
String locale) { private final ProximityInfo mProximityInfo = ProximityInfo.getDummyProximityInfo();
// TODO: implement this private final Map<String, Dictionary> mDictionaries =
final String text = textInfo.getText(); Collections.synchronizedMap(new TreeMap<String, Dictionary>());
if (DBG) {
Log.w(TAG, "getSuggestions: " + text); private static class SuggestionsGatherer implements WordCallback {
private final int DEFAULT_SUGGESTION_LENGTH = 16;
private final String[] mSuggestions;
private final int[] mScores;
private final int mMaxLength;
private int mLength = 0;
SuggestionsGatherer(final int maxLength) {
mMaxLength = maxLength;
mSuggestions = new String[mMaxLength];
mScores = new int[mMaxLength];
} }
String[] candidates0 = new String[] {text, "candidate1", "candidate2", "candidate3"};
String[] candidates1 = new String[] {text, "candidateA", "candidateB"}; @Override
final int textLength = textInfo.getText().length() % 3; synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score,
if (textLength % 3 == 0) { int dicTypeId, DataType dataType) {
return new SuggestionsInfo(2 final int positionIndex = ArraysCompatUtils.binarySearch(mScores, 0, mLength, score);
| SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, candidates0); // binarySearch returns the index if the element exists, and -<insertion index> - 1
} else if (textLength % 3 == 1) { // if it doesn't. See documentation for binarySearch.
return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, candidates1); final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
} else {
return new SuggestionsInfo(0, null); if (mLength < mMaxLength) {
final int copyLen = mLength - insertIndex;
++mLength;
System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
System.arraycopy(mSuggestions, insertIndex, mSuggestions, insertIndex + 1, copyLen);
} else {
if (insertIndex == 0) return true;
System.arraycopy(mScores, 1, mScores, 0, insertIndex);
System.arraycopy(mSuggestions, 1, mSuggestions, 0, insertIndex);
}
mScores[insertIndex] = score;
mSuggestions[insertIndex] = new String(word, wordOffset, wordLength);
return true;
}
public String[] getGatheredSuggestions() {
if (0 == mLength) return null;
final String[] results = new String[mLength];
for (int i = mLength - 1; i >= 0; --i) {
results[mLength - i - 1] = mSuggestions[i];
}
return results;
} }
} }
private Dictionary getDictionary(final String locale) {
Dictionary dictionary = mDictionaries.get(locale);
if (null == dictionary) {
final Resources resources = getResources();
final int fallbackResourceId = Utils.getMainDictionaryResourceId(resources);
final Locale localeObject = Utils.constructLocaleFromString(locale);
dictionary = DictionaryFactory.createDictionaryFromManager(this, localeObject,
fallbackResourceId);
mDictionaries.put(locale, dictionary);
}
return dictionary;
}
// 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.
*/
@Override
public SuggestionsInfo getSuggestions(final TextInfo textInfo, final int suggestionsLimit,
final String locale) {
final Dictionary dictionary = getDictionary(locale);
final String text = textInfo.getText();
final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(suggestionsLimit);
final WordComposer composer = new WordComposer();
final int length = text.length();
for (int i = 0; i < length; ++i) {
int character = text.codePointAt(i);
composer.add(character, new int[] { character },
WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
}
dictionary.getWords(composer, suggestionsGatherer, mProximityInfo);
final boolean isInDict = dictionary.isValidWord(text);
final String[] suggestions = suggestionsGatherer.getGatheredSuggestions();
final int flags = (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY : 0)
| (null != suggestions ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0);
return new SuggestionsInfo(flags, suggestions);
}
} }

View file

@ -1,116 +0,0 @@
/*
* 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.compat.ArraysCompatUtils;
import com.android.inputmethod.keyboard.ProximityInfo;
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.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;
@Override
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 = ArraysCompatUtils.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, ProximityInfo.getDummyProximityInfo());
return suggestionsGatherer.getGatheredSuggestions();
}
}