Refactor SpellCheckerProximityInfo as extending ProximityInfo

Change-Id: I544eec9d661796d57749dbaa2163bc1e2a891353
main
Tadashi G. Takaoka 2013-03-21 13:22:27 +09:00
parent ee91bde16c
commit 6e3f307266
3 changed files with 59 additions and 56 deletions

View File

@ -26,7 +26,7 @@ import com.android.inputmethod.latin.JniUtils;
import java.util.Arrays; import java.util.Arrays;
public final class ProximityInfo { public class ProximityInfo {
private static final String TAG = ProximityInfo.class.getSimpleName(); private static final String TAG = ProximityInfo.class.getSimpleName();
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
@ -79,22 +79,20 @@ public final class ProximityInfo {
mNativeProximityInfo = createNativeProximityInfo(touchPositionCorrection); mNativeProximityInfo = createNativeProximityInfo(touchPositionCorrection);
} }
private static ProximityInfo createDummyProximityInfo() { /**
return new ProximityInfo("", 1, 1, 1, 1, 1, 1, EMPTY_KEY_ARRAY, null); * Constructor for subclasses such as
} * {@link com.android.inputmethod.latin.spellcheck.SpellCheckerProximityInfo}.
*/
public static ProximityInfo createSpellCheckerProximityInfo(final int[] proximityCharsArray, protected ProximityInfo(final int[] proximityCharsArray, final int gridWidth,
final int rowSize, final int gridWidth, final int gridHeight) { final int gridHeight) {
final ProximityInfo spellCheckerProximityInfo = createDummyProximityInfo(); this("", 1, 1, 1, 1, 1, 1, EMPTY_KEY_ARRAY, null);
spellCheckerProximityInfo.mNativeProximityInfo = mNativeProximityInfo = setProximityInfoNative("" /* locale */,
spellCheckerProximityInfo.setProximityInfoNative("" /* locale */,
gridWidth /* displayWidth */, gridHeight /* displayHeight */, gridWidth /* displayWidth */, gridHeight /* displayHeight */,
gridWidth, gridHeight, 1 /* mostCommonKeyWidth */, proximityCharsArray, gridWidth, gridHeight, 1 /* mostCommonKeyWidth */, proximityCharsArray,
0 /* keyCount */, null /*keyXCoordinates */, null /* keyYCoordinates */, 0 /* keyCount */, null /*keyXCoordinates */, null /* keyYCoordinates */,
null /* keyWidths */, null /* keyHeights */, null /* keyCharCodes */, null /* keyWidths */, null /* keyHeights */, null /* keyCharCodes */,
null /* sweetSpotCenterXs */, null /* sweetSpotCenterYs */, null /* sweetSpotCenterXs */, null /* sweetSpotCenterYs */,
null /* sweetSpotRadii */); null /* sweetSpotRadii */);
return spellCheckerProximityInfo;
} }
private long mNativeProximityInfo; private long mNativeProximityInfo;

View File

@ -407,11 +407,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
public DictAndProximity createDictAndProximity(final Locale locale) { public DictAndProximity createDictAndProximity(final Locale locale) {
final int script = getScriptFromLocale(locale); final int script = getScriptFromLocale(locale);
final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo( final ProximityInfo proximityInfo = new SpellCheckerProximityInfo(script);
SpellCheckerProximityInfo.getProximityForScript(script),
SpellCheckerProximityInfo.ROW_SIZE,
SpellCheckerProximityInfo.PROXIMITY_GRID_WIDTH,
SpellCheckerProximityInfo.PROXIMITY_GRID_HEIGHT);
final DictionaryCollection dictionaryCollection = final DictionaryCollection dictionaryCollection =
DictionaryFactory.createMainDictionaryFromManager(this, locale, DictionaryFactory.createMainDictionaryFromManager(this, locale,
true /* useFullEditDistance */); true /* useFullEditDistance */);

View File

@ -16,38 +16,41 @@
package com.android.inputmethod.latin.spellcheck; package com.android.inputmethod.latin.spellcheck;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.CollectionUtils; import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
import java.util.TreeMap; import java.util.TreeMap;
public final class SpellCheckerProximityInfo { public final class SpellCheckerProximityInfo extends ProximityInfo {
@UsedForTesting public SpellCheckerProximityInfo(final int script) {
final public static int NUL = Constants.NOT_A_CODE; super(getProximityForScript(script), PROXIMITY_GRID_WIDTH, PROXIMITY_GRID_HEIGHT);
}
private static final int NUL = Constants.NOT_A_CODE;
// This must be the same as MAX_PROXIMITY_CHARS_SIZE else it will not work inside // This must be the same as MAX_PROXIMITY_CHARS_SIZE else it will not work inside
// native code - this value is passed at creation of the binary object and reused // native code - this value is passed at creation of the binary object and reused
// as the size of the passed array afterwards so they can't be different. // as the size of the passed array afterwards so they can't be different.
final public static int ROW_SIZE = ProximityInfo.MAX_PROXIMITY_CHARS_SIZE; private static final int ROW_SIZE = ProximityInfo.MAX_PROXIMITY_CHARS_SIZE;
// The number of keys in a row of the grid used by the spell checker. // The number of keys in a row of the grid used by the spell checker.
final public static int PROXIMITY_GRID_WIDTH = 11; private static final int PROXIMITY_GRID_WIDTH = 11;
// The number of rows in the grid used by the spell checker. // The number of rows in the grid used by the spell checker.
final public static int PROXIMITY_GRID_HEIGHT = 3; private static final int PROXIMITY_GRID_HEIGHT = 3;
final private static int NOT_AN_INDEX = -1; private static final int NOT_AN_INDEX = -1;
final public static int NOT_A_COORDINATE_PAIR = -1; public static final int NOT_A_COORDINATE_PAIR = -1;
// Helper methods // Helper methods
final protected static void buildProximityIndices(final int[] proximity, static void buildProximityIndices(final int[] proximity,
final TreeMap<Integer, Integer> indices) { final TreeMap<Integer, Integer> indices) {
for (int i = 0; i < proximity.length; i += ROW_SIZE) { for (int i = 0; i < proximity.length; i += ROW_SIZE) {
if (NUL != proximity[i]) indices.put(proximity[i], i / ROW_SIZE); if (NUL != proximity[i]) indices.put(proximity[i], i / ROW_SIZE);
} }
} }
final protected static int computeIndex(final int characterCode,
static int computeIndex(final int characterCode,
final TreeMap<Integer, Integer> indices) { final TreeMap<Integer, Integer> indices) {
final Integer result = indices.get(characterCode); final Integer result = indices.get(characterCode);
if (null == result) return NOT_AN_INDEX; if (null == result) return NOT_AN_INDEX;
@ -61,7 +64,7 @@ public final class SpellCheckerProximityInfo {
// character. // character.
// Since we need to build such an array, we want to be able to search in our big proximity // Since we need to build such an array, we want to be able to search in our big proximity
// data quickly by character, and a map is probably the best way to do this. // data quickly by character, and a map is probably the best way to do this.
final private static TreeMap<Integer, Integer> INDICES = CollectionUtils.newTreeMap(); private static final TreeMap<Integer, Integer> INDICES = CollectionUtils.newTreeMap();
// The proximity here is the union of // The proximity here is the union of
// - the proximity for a QWERTY keyboard. // - the proximity for a QWERTY keyboard.
@ -79,7 +82,7 @@ public final class SpellCheckerProximityInfo {
a s d f g h j k l a s d f g h j k l
z x c v b n m z x c v b n m
*/ */
final static int[] PROXIMITY = { static final int[] PROXIMITY = {
// Proximity for row 1. This must have exactly ROW_SIZE entries for each letter, // Proximity for row 1. This must have exactly ROW_SIZE entries for each letter,
// and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's. // and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's.
// The number of rows must be exactly PROXIMITY_GRID_HEIGHT. // The number of rows must be exactly PROXIMITY_GRID_HEIGHT.
@ -121,16 +124,18 @@ public final class SpellCheckerProximityInfo {
NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
}; };
static { static {
buildProximityIndices(PROXIMITY, INDICES); buildProximityIndices(PROXIMITY, INDICES);
} }
static int getIndexOf(int characterCode) { static int getIndexOf(int characterCode) {
return computeIndex(characterCode, INDICES); return computeIndex(characterCode, INDICES);
} }
} }
private static final class Cyrillic { private static final class Cyrillic {
final private static TreeMap<Integer, Integer> INDICES = CollectionUtils.newTreeMap(); private static final TreeMap<Integer, Integer> INDICES = CollectionUtils.newTreeMap();
// TODO: The following table is solely based on the keyboard layout. Consult with Russian // TODO: The following table is solely based on the keyboard layout. Consult with Russian
// speakers on commonly misspelled words/letters. // speakers on commonly misspelled words/letters.
/* /*
@ -207,7 +212,7 @@ public final class SpellCheckerProximityInfo {
private static final int CY_SOFT_SIGN = '\u044C'; // ь private static final int CY_SOFT_SIGN = '\u044C'; // ь
private static final int CY_BE = '\u0431'; // б private static final int CY_BE = '\u0431'; // б
private static final int CY_YU = '\u044E'; // ю private static final int CY_YU = '\u044E'; // ю
final static int[] PROXIMITY = { static final int[] PROXIMITY = {
// Proximity for row 1. This must have exactly ROW_SIZE entries for each letter, // Proximity for row 1. This must have exactly ROW_SIZE entries for each letter,
// and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's. // and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's.
// The number of rows must be exactly PROXIMITY_GRID_HEIGHT. // The number of rows must be exactly PROXIMITY_GRID_HEIGHT.
@ -280,16 +285,18 @@ public final class SpellCheckerProximityInfo {
NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
}; };
static { static {
buildProximityIndices(PROXIMITY, INDICES); buildProximityIndices(PROXIMITY, INDICES);
} }
static int getIndexOf(int characterCode) { static int getIndexOf(int characterCode) {
return computeIndex(characterCode, INDICES); return computeIndex(characterCode, INDICES);
} }
} }
private static final class Greek { private static final class Greek {
final private static TreeMap<Integer, Integer> INDICES = CollectionUtils.newTreeMap(); private static final TreeMap<Integer, Integer> INDICES = CollectionUtils.newTreeMap();
// TODO: The following table is solely based on the keyboard layout. Consult with Greek // TODO: The following table is solely based on the keyboard layout. Consult with Greek
// speakers on commonly misspelled words/letters. // speakers on commonly misspelled words/letters.
/* /*
@ -354,7 +361,7 @@ public final class SpellCheckerProximityInfo {
private static final int GR_BETA = '\u03B2'; // β private static final int GR_BETA = '\u03B2'; // β
private static final int GR_NU = '\u03BD'; // ν private static final int GR_NU = '\u03BD'; // ν
private static final int GR_MU = '\u03BC'; // μ private static final int GR_MU = '\u03BC'; // μ
final static int[] PROXIMITY = { static final int[] PROXIMITY = {
// Proximity for row 1. This must have exactly ROW_SIZE entries for each letter, // Proximity for row 1. This must have exactly ROW_SIZE entries for each letter,
// and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's. // and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's.
// The number of rows must be exactly PROXIMITY_GRID_HEIGHT. // The number of rows must be exactly PROXIMITY_GRID_HEIGHT.
@ -419,15 +426,17 @@ public final class SpellCheckerProximityInfo {
NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
}; };
static { static {
buildProximityIndices(PROXIMITY, INDICES); buildProximityIndices(PROXIMITY, INDICES);
} }
static int getIndexOf(int characterCode) { static int getIndexOf(int characterCode) {
return computeIndex(characterCode, INDICES); return computeIndex(characterCode, INDICES);
} }
} }
public static int[] getProximityForScript(final int script) { private static int[] getProximityForScript(final int script) {
switch (script) { switch (script) {
case AndroidSpellCheckerService.SCRIPT_LATIN: case AndroidSpellCheckerService.SCRIPT_LATIN:
return Latin.PROXIMITY; return Latin.PROXIMITY;