final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); spellCheck = tsm.newSpellCheckerSession(null, Local.ENGLISH, this, false);
public class SpellChecker implements SpellCheckerSessionListener { public SpellChecker(Activity a) { final TextServicesManager tsm = (TextServicesManager) a.getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); SpellCheckerSession scs = tsm.newSpellCheckerSession(null, null, this, true); TextInfo[] tia = {new TextInfo("trewgjj"), new TextInfo("great")}; scs.getSentenceSuggestions(tia, 3); } @Override public void onGetSuggestions(SuggestionsInfo[] results) { // TODO Auto-generated method stub } @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { for (int i = 0; i < results.length; ++i) { // Returned suggestions are contained in SuggestionsInfo for (int j = 0; j < results[i].getSuggestionsCount(); ++j) { SuggestionsInfo si = results[i].getSuggestionsInfoAt(j); if ((si.getSuggestionsAttributes() & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) != 0) Log.d("", "EXACT MATCH FOUND"); } } } }
private void dumpSuggestionsInfoInternal( final List<String> sb, final SuggestionsInfo si, final int length, final int offset) { // Returned suggestions are contained in SuggestionsInfo final int len = si.getSuggestionsCount(); for (int j = 0; j < len; ++j) { sb.add(si.getSuggestionAt(j)); } }
@Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { try { final List<String> sb = new ArrayList<>(); for (final SentenceSuggestionsInfo ssi : results) { for (int j = 0; j < ssi.getSuggestionsCount(); ++j) { dumpSuggestionsInfoInternal( sb, ssi.getSuggestionsInfoAt(j), ssi.getOffsetAt(j), ssi.getLengthAt(j)); } } setSuggestions(sb, true, true); } catch(Exception ignored){} } private void setCapsOn(boolean on) {
/** * Queries the input text against the SpellCheckerSession. * @param text Text to be queried. */ @CalledByNative private void requestTextCheck(String text) { // SpellCheckerSession thinks that any word ending with a period is a typo. // We trim the period off before sending the text for spellchecking in order to avoid // unnecessary red underlines when the user ends a sentence with a period. // Filed as an Android bug here: https://code.google.com/p/android/issues/detail?id=183294 if (text.endsWith(".")) { text = text.substring(0, text.length() - 1); } mSpellCheckerSession.getSentenceSuggestions(new TextInfo[] {new TextInfo(text)}, 0); }
/** * Checks for typos and sends results back to native through a JNI call. * @param results Results returned by the Android spellchecker. */ @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { ArrayList<Integer> offsets = new ArrayList<Integer>(); ArrayList<Integer> lengths = new ArrayList<Integer>(); for (SentenceSuggestionsInfo result : results) { for (int i = 0; i < result.getSuggestionsCount(); i++) { // If a word looks like a typo, record its offset and length. if ((result.getSuggestionsInfoAt(i).getSuggestionsAttributes() & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) == SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) { offsets.add(result.getOffsetAt(i)); lengths.add(result.getLengthAt(i)); } } } nativeProcessSpellCheckResults(mNativeSpellCheckerSessionBridge, convertListToArray(offsets), convertListToArray(lengths)); }
@OnClick(R.id.btnCheckSpell) public void checkSpellClicked(View view) { if (!etText.getText().toString().isEmpty()) { tvResult.setText(""); if (spellCheckerSession != null) { spellCheckerSession.getSentenceSuggestions( new TextInfo[]{new TextInfo(etText.getText().toString())}, 1); } else { Log.e("TAG_SPELL", "Couldn't obtain the spell checker service."); } } }
private void dumpSuggestionsInfoInternal( final List<String> sb, final SuggestionsInfo si, final int length, final int offset) { // Returned suggestions are contained in SuggestionsInfo final int len = si.getSuggestionsCount(); for (int j = 0; j < len; ++j) { sb.add(si.getSuggestionAt(j)); } }
@Override protected void onResume() { super.onResume(); final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); spellCheckerSession = tsm.newSpellCheckerSession(null, null, this, true); }
@Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfos) { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < sentenceSuggestionsInfos.length; ++i) { final SentenceSuggestionsInfo ssi = sentenceSuggestionsInfos[i]; for (int j = 0; j < ssi.getSuggestionsCount(); ++j) { dumpSuggestionsInfoInternal( sb, ssi.getSuggestionsInfoAt(j), ssi.getOffsetAt(j), ssi.getLengthAt(j)); } } runOnUiThread(new Runnable() { @Override public void run() { tvResult.append(sb.toString()); } }); } }
/** * Update the list of available candidates from the current composing * text. This will need to be filled in by however you are determining * candidates. */ private void updateCandidates() { if (!mCompletionOn) { if (mComposing.length() > 0) { ArrayList<String> list = new ArrayList<String>(); list.add(mComposing.toString()); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo(mComposing.toString())}, 5); setSuggestions(list, true, true); } else { setSuggestions(null, false, false); } } }
private void dumpSuggestionsInfoInternal( final StringBuilder sb, final SuggestionsInfo si, final int length, final int offset) { // Returned suggestions are contained in SuggestionsInfo final int len = si.getSuggestionsCount(); sb.append('\n'); for (int j = 0; j < len; ++j) { if (j != 0) { sb.append(", "); } sb.append(si.getSuggestionAt(j)); } sb.append(" (" + len + ")"); if (length != -1) { sb.append(" length = " + length + ", offset = " + offset); } }
/** * Constructs a SpellCheckerSessionBridge object as well as its SpellCheckerSession object. * @param nativeSpellCheckerSessionBridge Pointer to the native SpellCheckerSessionBridge. */ private SpellCheckerSessionBridge(long nativeSpellCheckerSessionBridge) { mNativeSpellCheckerSessionBridge = nativeSpellCheckerSessionBridge; Context context = ContextUtils.getApplicationContext(); final TextServicesManager textServicesManager = (TextServicesManager) context.getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); // This combination of parameters will cause the spellchecker to be based off of // the language specified at "Settings > Language & input > Spell checker > Language". // If that setting is set to "Use system language" and the system language is not on the // list of supported spellcheck languages, this call will return null. This call will also // return null if the user has turned spellchecking off at "Settings > Language & input > // Spell checker". mSpellCheckerSession = textServicesManager.newSpellCheckerSession(null, null, this, true); }
@Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { Log.d("SoftKeyboard", "onGetSentenceSuggestions"); final List<String> sb = new ArrayList<>(); for (int i = 0; i < results.length; ++i) { final SentenceSuggestionsInfo ssi = results[i]; for (int j = 0; j < ssi.getSuggestionsCount(); ++j) { dumpSuggestionsInfoInternal( sb, ssi.getSuggestionsInfoAt(j), ssi.getOffsetAt(j), ssi.getLengthAt(j)); } } Log.d("SoftKeyboard", "SUGGESTIONS: " + sb.toString()); setSuggestions(sb, true, true); } }
/** * Update the list of available candidates from the current composing * text. This will need to be filled in by however you are determining * candidates. */ private void updateCandidates() { if (!mCompletionOn) { if (mComposing.length() > 0) { ArrayList<String> list = new ArrayList<String>(); //list.add(mComposing.toString()); Log.d("SoftKeyboard", "REQUESTING: " + mComposing.toString()); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo(mComposing.toString())}, 5); setSuggestions(list, true, true); } else { setSuggestions(null, false, false); } } }
/** * http://www.tutorialspoint.com/android/android_spelling_checker.htm * Sort of copy-paste, huh. * * I need to find time to refine this code * * * @param results results */ @Override public void onGetSuggestions(SuggestionsInfo[] results) { final StringBuilder sb = new StringBuilder(); for (SuggestionsInfo result : results) { // Returned suggestions are contained in SuggestionsInfo final int len = result.getSuggestionsCount(); sb.append('\n'); for (int j = 0; j < len; ++j) { sb.append(",").append(result.getSuggestionAt(j)); } sb.append(" (").append(len).append(")"); } }
final TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); scs = tsm.newSpellCheckerSession(null, null, this, true); if (scs != null) { scs.getSuggestions(new TextInfo(text.getText().toString()), 3); } else { // Show the message to user Toast.makeText(this, "Please turn on the spell checker from setting", Toast.LENGTH_LONG).show(); // You can even open the settings page for user to turn it ON ComponentName componentToLaunch = new ComponentName("com.android.settings", "com.android.settings.Settings$SpellCheckersSettingsActivity"); Intent intent = new Intent(); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setComponent(componentToLaunch); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { this.startActivity(intent); } catch (ActivityNotFoundException e) { // Error } }
/** * http://www.tutorialspoint.com/android/android_spelling_checker.htm * @param results results */ @Override public void onGetSuggestions(SuggestionsInfo[] results) { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < results.length; ++i) { // Returned suggestions are contained in SuggestionsInfo final int len = results[i].getSuggestionsCount(); sb.append('\n'); for (int j = 0; j < len; ++j) { sb.append("," + results[i].getSuggestionAt(j)); } sb.append(" (" + len + ")"); } Log.d("SoftKeyboard", "SUGGESTIONS: " + sb.toString()); } private static final int NOT_A_LENGTH = -1;
/** * Main initialization of the input method component. Be sure to call * to super class. */ @Override public void onCreate() { super.onCreate(); mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); mWordSeparators = getResources().getString(R.string.word_separators); final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); mScs = tsm.newSpellCheckerSession(null, null, this, true); }
/** * Main initialization of the input method component. Be sure to call * to super class. */ @Override public void onCreate() { super.onCreate(); mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); mWordSeparators = getResources().getString(R.string.word_separators); final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); mScs = tsm.newSpellCheckerSession(null, null, this, true); }