aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
-rw-r--r--java/src/com/android/inputmethod/latin/Constants.java1
-rw-r--r--java/src/com/android/inputmethod/latin/RichInputConnection.java5
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java42
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestedWords.java16
-rw-r--r--java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java2
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java50
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java49
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java17
-rw-r--r--java/src/com/android/inputmethod/latin/utils/ScriptUtils.java103
9 files changed, 151 insertions, 134 deletions
diff --git a/java/src/com/android/inputmethod/latin/Constants.java b/java/src/com/android/inputmethod/latin/Constants.java
index b4e115c7d..43af66eb7 100644
--- a/java/src/com/android/inputmethod/latin/Constants.java
+++ b/java/src/com/android/inputmethod/latin/Constants.java
@@ -220,6 +220,7 @@ public final class Constants {
public static final String REGEXP_PERIOD = "\\.";
public static final String STRING_SPACE = " ";
+ public static final String STRING_PERIOD_AND_SPACE = ". ";
/**
* Special keys code. Must be negative.
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index 8f252bd84..62b55bca1 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -754,13 +754,12 @@ public final class RichInputConnection {
// Here we test whether we indeed have a period and a space before us. This should not
// be needed, but it's there just in case something went wrong.
final CharSequence textBeforeCursor = getTextBeforeCursor(2, 0);
- final String periodSpace = ". ";
- if (!TextUtils.equals(periodSpace, textBeforeCursor)) {
+ if (!TextUtils.equals(Constants.STRING_PERIOD_AND_SPACE, textBeforeCursor)) {
// Theoretically we should not be coming here if there isn't ". " before the
// cursor, but the application may be changing the text while we are typing, so
// anything goes. We should not crash.
Log.d(TAG, "Tried to revert double-space combo but we didn't find "
- + "\"" + periodSpace + "\" just before the cursor.");
+ + "\"" + Constants.STRING_PERIOD_AND_SPACE + "\" just before the cursor.");
return false;
}
// Double-space results in ". ". A backspace to cancel this should result in a single
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 9d03e8a43..e43db352d 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -123,39 +123,27 @@ public final class Suggest {
suggestionsContainer.set(i, transformedWordInfo);
}
}
- SuggestedWordInfo.removeDups(typedWord, suggestionsContainer);
+ final boolean didRemoveTypedWord =
+ SuggestedWordInfo.removeDups(typedWord, suggestionsContainer);
- // If resumed, then we don't want to upcase everything: resuming on a fully-capitalized
- // words is rarely done to switch to another fully-capitalized word, but usually to a
- // normal, non-capitalized suggestion.
- final String firstSuggestion;
+ final SuggestedWordInfo firstSuggestedWordInfo;
final String whitelistedWord;
- if (suggestionResults.isEmpty()) {
- whitelistedWord = firstSuggestion = null;
+ if (suggestionsContainer.isEmpty()) {
+ firstSuggestedWordInfo = null;
+ whitelistedWord = null;
} else {
- final SuggestedWordInfo firstSuggestedWordInfo = getTransformedSuggestedWordInfo(
- suggestionResults.first(), suggestionResults.mLocale,
- shouldMakeSuggestionsAllUpperCase, isOnlyFirstCharCapitalized,
- trailingSingleQuotesCount);
- firstSuggestion = firstSuggestedWordInfo.mWord;
+ firstSuggestedWordInfo = suggestionsContainer.get(0);
if (!firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) {
whitelistedWord = null;
} else {
- whitelistedWord = firstSuggestion;
+ whitelistedWord = firstSuggestedWordInfo.mWord;
}
}
- // We allow auto-correction if we have a whitelisted word, or if the word is not a valid
- // word of more than 1 char, except if the first suggestion is the same as the typed string
- // because in this case if it's strong enough to auto-correct that will mistakenly designate
- // the second candidate for auto-correction.
- // TODO: stop relying on indices to find where is the auto-correction in the suggested
- // words, and correct this test.
- final boolean allowsToBeAutoCorrected = (null != whitelistedWord
- && !whitelistedWord.equals(typedWord))
- || (consideredWord.length() > 1 && !mDictionaryFacilitator.isValidWord(
- consideredWord, isOnlyFirstCharCapitalized)
- && !typedWord.equals(firstSuggestion));
+ // We allow auto-correction if we have a whitelisted word, or if the word had more than
+ // one char and was not suggested.
+ final boolean allowsToBeAutoCorrected = (null != whitelistedWord)
+ || (consideredWord.length() > 1 && !didRemoveTypedWord);
final boolean hasAutoCorrection;
// TODO: using isCorrectionEnabled here is not very good. It's probably useless, because
@@ -164,10 +152,10 @@ public final class Suggest {
// the current settings. It may also be useful to know, when the setting is off, whether
// the word *would* have been auto-corrected.
if (!isCorrectionEnabled || !allowsToBeAutoCorrected || isPrediction
- || suggestionResults.isEmpty() || wordComposer.hasDigits()
+ || null == firstSuggestedWordInfo || wordComposer.hasDigits()
|| wordComposer.isMostlyCaps() || wordComposer.isResumed()
|| !mDictionaryFacilitator.hasInitializedMainDictionary()
- || suggestionResults.first().isKindOf(SuggestedWordInfo.KIND_SHORTCUT)) {
+ || firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_SHORTCUT)) {
// If we don't have a main dictionary, we never want to auto-correct. The reason for
// this is, the user may have a contact whose name happens to match a valid word in
// their language, and it will unexpectedly auto-correct. For example, if the user
@@ -179,7 +167,7 @@ public final class Suggest {
hasAutoCorrection = false;
} else {
hasAutoCorrection = AutoCorrectionUtils.suggestionExceedsAutoCorrectionThreshold(
- suggestionResults.first(), consideredWord, mAutoCorrectionThreshold);
+ firstSuggestedWordInfo, consideredWord, mAutoCorrectionThreshold);
}
if (!TextUtils.isEmpty(typedWord)) {
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index f22af7991..e587b18c9 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -330,29 +330,37 @@ public class SuggestedWords {
}
// This will always remove the higher index if a duplicate is found.
- public static void removeDups(final String typedWord,
+ public static boolean removeDups(final String typedWord,
ArrayList<SuggestedWordInfo> candidates) {
if (candidates.isEmpty()) {
- return;
+ return false;
}
+ final boolean didRemoveTypedWord;
if (!TextUtils.isEmpty(typedWord)) {
- removeSuggestedWordInfoFrom(typedWord, candidates, -1 /* startIndexExclusive */);
+ didRemoveTypedWord = removeSuggestedWordInfoFrom(typedWord, candidates,
+ -1 /* startIndexExclusive */);
+ } else {
+ didRemoveTypedWord = false;
}
for (int i = 0; i < candidates.size(); ++i) {
removeSuggestedWordInfoFrom(candidates.get(i).mWord, candidates,
i /* startIndexExclusive */);
}
+ return didRemoveTypedWord;
}
- private static void removeSuggestedWordInfoFrom(final String word,
+ private static boolean removeSuggestedWordInfoFrom(final String word,
final ArrayList<SuggestedWordInfo> candidates, final int startIndexExclusive) {
+ boolean didRemove = false;
for (int i = startIndexExclusive + 1; i < candidates.size(); ++i) {
final SuggestedWordInfo previous = candidates.get(i);
if (word.equals(previous.mWord)) {
+ didRemove = true;
candidates.remove(i);
--i;
}
}
+ return didRemove;
}
}
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index 4ebdcbd70..15ddcf9ae 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -399,7 +399,7 @@ public final class InputLogic {
}
boolean didAutoCorrect = false;
- if (Event.NOT_A_KEY_CODE != event.mKeyCode) {
+ if (event.isFunctionalKeyEvent()) {
// A special key, like delete, shift, emoji, or the settings key.
switch (event.mKeyCode) {
case Constants.CODE_DELETE:
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
index 8d495646d..d0316242b 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
@@ -37,6 +37,7 @@ import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.LocaleUtils;
+import com.android.inputmethod.latin.utils.ScriptUtils;
import com.android.inputmethod.latin.utils.StringUtils;
import java.lang.ref.WeakReference;
@@ -78,40 +79,8 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
private final HashSet<WeakReference<DictionaryCollection>> mDictionaryCollectionsList =
new HashSet<>();
- public static final int SCRIPT_LATIN = 0;
- public static final int SCRIPT_CYRILLIC = 1;
- public static final int SCRIPT_GREEK = 2;
public static final String SINGLE_QUOTE = "\u0027";
public static final String APOSTROPHE = "\u2019";
- private static final TreeMap<String, Integer> mLanguageToScript;
- static {
- // List of the supported languages and their associated script. We won't check
- // words written in another script than the selected script, because we know we
- // don't have those in our dictionary so we will underline everything and we
- // will never have any suggestions, so it makes no sense checking them, and this
- // is done in {@link #shouldFilterOut}. Also, the script is used to choose which
- // proximity to pass to the dictionary descent algorithm.
- // IMPORTANT: this only contains languages - do not write countries in there.
- // Only the language is searched from the map.
- mLanguageToScript = new TreeMap<>();
- mLanguageToScript.put("cs", SCRIPT_LATIN);
- mLanguageToScript.put("da", SCRIPT_LATIN);
- mLanguageToScript.put("de", SCRIPT_LATIN);
- mLanguageToScript.put("el", SCRIPT_GREEK);
- mLanguageToScript.put("en", SCRIPT_LATIN);
- mLanguageToScript.put("es", SCRIPT_LATIN);
- mLanguageToScript.put("fi", SCRIPT_LATIN);
- mLanguageToScript.put("fr", SCRIPT_LATIN);
- mLanguageToScript.put("hr", SCRIPT_LATIN);
- mLanguageToScript.put("it", SCRIPT_LATIN);
- mLanguageToScript.put("lt", SCRIPT_LATIN);
- mLanguageToScript.put("lv", SCRIPT_LATIN);
- mLanguageToScript.put("nb", SCRIPT_LATIN);
- mLanguageToScript.put("nl", SCRIPT_LATIN);
- mLanguageToScript.put("pt", SCRIPT_LATIN);
- mLanguageToScript.put("sl", SCRIPT_LATIN);
- mLanguageToScript.put("ru", SCRIPT_CYRILLIC);
- }
@Override public void onCreate() {
super.onCreate();
@@ -122,22 +91,13 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
onSharedPreferenceChanged(prefs, PREF_USE_CONTACTS_KEY);
}
- public static int getScriptFromLocale(final Locale locale) {
- final Integer script = mLanguageToScript.get(locale.getLanguage());
- if (null == script) {
- throw new RuntimeException("We have been called with an unsupported language: \""
- + locale.getLanguage() + "\". Framework bug?");
- }
- return script;
- }
-
private static String getKeyboardLayoutNameForScript(final int script) {
switch (script) {
- case AndroidSpellCheckerService.SCRIPT_LATIN:
+ case ScriptUtils.SCRIPT_LATIN:
return "qwerty";
- case AndroidSpellCheckerService.SCRIPT_CYRILLIC:
+ case ScriptUtils.SCRIPT_CYRILLIC:
return "east_slavic";
- case AndroidSpellCheckerService.SCRIPT_GREEK:
+ case ScriptUtils.SCRIPT_GREEK:
return "greek";
default:
throw new RuntimeException("Wrong script supplied: " + script);
@@ -413,7 +373,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
}
public DictAndKeyboard createDictAndKeyboard(final Locale locale) {
- final int script = getScriptFromLocale(locale);
+ final int script = ScriptUtils.getScriptFromLocale(locale);
final String keyboardLayoutName = getKeyboardLayoutNameForScript(script);
final InputMethodSubtype subtype = AdditionalSubtypeUtils.createAdditionalSubtype(
locale.toString(), keyboardLayoutName, null);
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
index e9d5a7a95..be33f339d 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
@@ -36,6 +36,7 @@ import com.android.inputmethod.latin.WordComposer;
import com.android.inputmethod.latin.spellcheck.AndroidSpellCheckerService.SuggestionsGatherer;
import com.android.inputmethod.latin.utils.CoordinateUtils;
import com.android.inputmethod.latin.utils.LocaleUtils;
+import com.android.inputmethod.latin.utils.ScriptUtils;
import com.android.inputmethod.latin.utils.StringUtils;
import java.util.ArrayList;
@@ -116,7 +117,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
final String localeString = getLocale();
mDictionaryPool = mService.getDictionaryPool(localeString);
mLocale = LocaleUtils.constructLocaleFromString(localeString);
- mScript = AndroidSpellCheckerService.getScriptFromLocale(mLocale);
+ mScript = ScriptUtils.getScriptFromLocale(mLocale);
}
@Override
@@ -125,44 +126,6 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
cres.unregisterContentObserver(mObserver);
}
- /*
- * Returns whether the code point is a letter that makes sense for the specified
- * locale for this spell checker.
- * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml
- * and is limited to EFIGS languages and Russian.
- * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters
- * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters.
- */
- private static boolean isLetterCheckableByLanguage(final int codePoint,
- final int script) {
- switch (script) {
- case AndroidSpellCheckerService.SCRIPT_LATIN:
- // Our supported latin script dictionaries (EFIGS) at the moment only include
- // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode
- // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF,
- // so the below is a very efficient way to test for it. As for the 0-0x3F, it's
- // excluded from isLetter anyway.
- return codePoint <= 0x2AF && Character.isLetter(codePoint);
- case AndroidSpellCheckerService.SCRIPT_CYRILLIC:
- // All Cyrillic characters are in the 400~52F block. There are some in the upper
- // Unicode range, but they are archaic characters that are not used in modern
- // Russian and are not used by our dictionary.
- return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint);
- case AndroidSpellCheckerService.SCRIPT_GREEK:
- // Greek letters are either in the 370~3FF range (Greek & Coptic), or in the
- // 1F00~1FFF range (Greek extended). Our dictionary contains both sort of characters.
- // Our dictionary also contains a few words with 0xF2; it would be best to check
- // if that's correct, but a web search does return results for these words so
- // they are probably okay.
- return (codePoint >= 0x370 && codePoint <= 0x3FF)
- || (codePoint >= 0x1F00 && codePoint <= 0x1FFF)
- || codePoint == 0xF2;
- default:
- // Should never come here
- throw new RuntimeException("Impossible value of script: " + script);
- }
- }
-
private static final int CHECKABILITY_CHECKABLE = 0;
private static final int CHECKABILITY_TOO_MANY_NON_LETTERS = 1;
private static final int CHECKABILITY_CONTAINS_PERIOD = 2;
@@ -189,7 +152,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
// Filter by first letter
final int firstCodePoint = text.codePointAt(0);
// Filter out words that don't start with a letter or an apostrophe
- if (!isLetterCheckableByLanguage(firstCodePoint, script)
+ if (!ScriptUtils.isLetterCheckableByScript(firstCodePoint, script)
&& '\'' != firstCodePoint) return CHECKABILITY_FIRST_LETTER_UNCHECKABLE;
// Filter contents
@@ -210,7 +173,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
if (Constants.CODE_PERIOD == codePoint) {
return CHECKABILITY_CONTAINS_PERIOD;
}
- if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount;
+ if (ScriptUtils.isLetterCheckableByScript(codePoint, script)) ++letterCount;
}
// Guestimate heuristic: perform spell checking if at least 3/4 of the characters
// in this word are letters
@@ -295,7 +258,9 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO
| SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS,
new String[] {
- TextUtils.join(Constants.STRING_SPACE, splitText) });
+ TextUtils.join(Constants.STRING_SPACE, splitText),
+ TextUtils.join(Constants.STRING_PERIOD_AND_SPACE,
+ splitText) });
}
}
return dictInfo.mDictionary.isValidWord(inText)
diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
index 528d500d2..47921ca0a 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
@@ -20,8 +20,6 @@ import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
-import com.android.inputmethod.accessibility.AccessibilityUtils;
-import com.android.inputmethod.accessibility.MoreSuggestionsAccessibilityDelegate;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardActionListener;
@@ -51,6 +49,7 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView {
super(context, attrs, defStyle);
}
+ // TODO: Remove redundant override method.
@Override
public void setKeyboard(final Keyboard keyboard) {
super.setKeyboard(keyboard);
@@ -58,16 +57,10 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView {
// above {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call.
// With accessibility mode on, {@link #mAccessibilityDelegate} is set to a
// {@link MoreKeysKeyboardAccessibilityDelegate} object at the above
- // {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. And the object has to be
- // overwritten by a {@link MoreSuggestionsAccessibilityDelegate} object here.
- if (AccessibilityUtils.getInstance().isAccessibilityEnabled()) {
- if (!(mAccessibilityDelegate instanceof MoreSuggestionsAccessibilityDelegate)) {
- mAccessibilityDelegate = new MoreSuggestionsAccessibilityDelegate(
- this, mKeyDetector);
- mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions);
- mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions);
- }
- mAccessibilityDelegate.setKeyboard(keyboard);
+ // {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call.
+ if (mAccessibilityDelegate != null) {
+ mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions);
+ mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions);
}
}
diff --git a/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java b/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java
new file mode 100644
index 000000000..4dfb38d80
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2012 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.utils;
+
+import java.util.Locale;
+import java.util.TreeMap;
+
+/**
+ * A class to help with handling different writing scripts.
+ */
+public class ScriptUtils {
+ public static final int SCRIPT_LATIN = 0;
+ public static final int SCRIPT_CYRILLIC = 1;
+ public static final int SCRIPT_GREEK = 2;
+ public static final TreeMap<String, Integer> mLanguageToScript;
+ static {
+ // List of the supported languages and their associated script. We won't check
+ // words written in another script than the selected script, because we know we
+ // don't have those in our dictionary so we will underline everything and we
+ // will never have any suggestions, so it makes no sense checking them, and this
+ // is done in {@link #shouldFilterOut}. Also, the script is used to choose which
+ // proximity to pass to the dictionary descent algorithm.
+ // IMPORTANT: this only contains languages - do not write countries in there.
+ // Only the language is searched from the map.
+ mLanguageToScript = new TreeMap<>();
+ mLanguageToScript.put("cs", SCRIPT_LATIN);
+ mLanguageToScript.put("da", SCRIPT_LATIN);
+ mLanguageToScript.put("de", SCRIPT_LATIN);
+ mLanguageToScript.put("el", SCRIPT_GREEK);
+ mLanguageToScript.put("en", SCRIPT_LATIN);
+ mLanguageToScript.put("es", SCRIPT_LATIN);
+ mLanguageToScript.put("fi", SCRIPT_LATIN);
+ mLanguageToScript.put("fr", SCRIPT_LATIN);
+ mLanguageToScript.put("hr", SCRIPT_LATIN);
+ mLanguageToScript.put("it", SCRIPT_LATIN);
+ mLanguageToScript.put("lt", SCRIPT_LATIN);
+ mLanguageToScript.put("lv", SCRIPT_LATIN);
+ mLanguageToScript.put("nb", SCRIPT_LATIN);
+ mLanguageToScript.put("nl", SCRIPT_LATIN);
+ mLanguageToScript.put("pt", SCRIPT_LATIN);
+ mLanguageToScript.put("sl", SCRIPT_LATIN);
+ mLanguageToScript.put("ru", SCRIPT_CYRILLIC);
+ }
+ /*
+ * Returns whether the code point is a letter that makes sense for the specified
+ * locale for this spell checker.
+ * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml
+ * and is limited to EFIGS languages and Russian.
+ * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters
+ * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters.
+ */
+ public static boolean isLetterCheckableByScript(final int codePoint, final int script) {
+ switch (script) {
+ case SCRIPT_LATIN:
+ // Our supported latin script dictionaries (EFIGS) at the moment only include
+ // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode
+ // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF,
+ // so the below is a very efficient way to test for it. As for the 0-0x3F, it's
+ // excluded from isLetter anyway.
+ return codePoint <= 0x2AF && Character.isLetter(codePoint);
+ case SCRIPT_CYRILLIC:
+ // All Cyrillic characters are in the 400~52F block. There are some in the upper
+ // Unicode range, but they are archaic characters that are not used in modern
+ // Russian and are not used by our dictionary.
+ return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint);
+ case SCRIPT_GREEK:
+ // Greek letters are either in the 370~3FF range (Greek & Coptic), or in the
+ // 1F00~1FFF range (Greek extended). Our dictionary contains both sort of characters.
+ // Our dictionary also contains a few words with 0xF2; it would be best to check
+ // if that's correct, but a web search does return results for these words so
+ // they are probably okay.
+ return (codePoint >= 0x370 && codePoint <= 0x3FF)
+ || (codePoint >= 0x1F00 && codePoint <= 0x1FFF)
+ || codePoint == 0xF2;
+ default:
+ // Should never come here
+ throw new RuntimeException("Impossible value of script: " + script);
+ }
+ }
+
+ public static int getScriptFromLocale(final Locale locale) {
+ final Integer script = mLanguageToScript.get(locale.getLanguage());
+ if (null == script) {
+ throw new RuntimeException("We have been called with an unsupported language: \""
+ + locale.getLanguage() + "\". Framework bug?");
+ }
+ return script;
+ }
+}