public SentenceTextInfoParams getSplitWords(TextInfo originalTextInfo) { final WordIterator wordIterator = mWordIterator; final CharSequence originalText = TextInfoCompatUtils.getCharSequenceOrString(originalTextInfo); final int cookie = originalTextInfo.getCookie(); final int start = -1; final int end = originalText.length(); final ArrayList<SentenceWordItem> wordItems = new ArrayList<>(); int wordStart = wordIterator.getBeginningOfNextWord(originalText, start); int wordEnd = wordIterator.getEndOfWord(originalText, wordStart); while (wordStart <= end && wordEnd != -1 && wordStart != -1) { if (wordEnd >= start && wordEnd > wordStart) { final TextInfo ti = TextInfoCompatUtils.newInstance(originalText, wordStart, wordEnd, cookie, originalText.subSequence(wordStart, wordEnd).hashCode()); wordItems.add(new SentenceWordItem(ti, wordStart, wordEnd)); } wordStart = wordIterator.getBeginningOfNextWord(originalText, wordEnd); if (wordStart == -1) { break; } wordEnd = wordIterator.getEndOfWord(originalText, wordStart); } return new SentenceTextInfoParams(originalTextInfo, wordItems); }
@Override public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) { final SentenceSuggestionsInfo[] retval = splitAndSuggest(textInfos, suggestionsLimit); if (retval == null || retval.length != textInfos.length) { return retval; } for (int i = 0; i < retval.length; ++i) { final SentenceSuggestionsInfo tempSsi = fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]); if (tempSsi != null) { retval[i] = tempSsi; } } return retval; }
@Override public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) { final SentenceSuggestionsInfo[] retval = super.onGetSentenceSuggestionsMultiple(textInfos, suggestionsLimit); if (retval == null || retval.length != textInfos.length) { return retval; } for (int i = 0; i < retval.length; ++i) { final SentenceSuggestionsInfo tempSsi = fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]); if (tempSsi != null) { retval[i] = tempSsi; } } return retval; }
@Override public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) { long ident = Binder.clearCallingIdentity(); try { final int length = textInfos.length; final SuggestionsInfo[] retval = new SuggestionsInfo[length]; for (int i = 0; i < length; ++i) { final String prevWord; if (sequentialWords && i > 0) { final String prevWordCandidate = textInfos[i - 1].getText(); // Note that an empty string would be used to indicate the initial word // in the future. prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate; } else { prevWord = null; } retval[i] = onGetSuggestionsInternal(textInfos[i], prevWord, suggestionsLimit); retval[i].setCookieAndSequence(textInfos[i].getCookie(), textInfos[i].getSequence()); } return retval; } finally { Binder.restoreCallingIdentity(ident); } }
/** * 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); } } }
@Override public void onResume() { super.onResume(); final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); mScs = tsm.newSpellCheckerSession(null, null, this, true); if (mScs != null) { // Instantiate TextInfo for each query // TextInfo can be passed a sequence number and a cookie number to identify the result if (isSentenceSpellCheckSupported()) { // Note that getSentenceSuggestions works on JB or later. Log.d(TAG, "Sentence spellchecking supported."); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo("tgisis")}, 3); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo( "I wold like to here form you")}, 3); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo("hell othere")}, 3); } else { // Note that getSuggestions() is a deprecated API. // It is recommended for an application running on Jelly Bean or later // to call getSentenceSuggestions() only. mScs.getSuggestions(new TextInfo("tgis"), 3); mScs.getSuggestions(new TextInfo("hllo"), 3); mScs.getSuggestions(new TextInfo("helloworld"), 3); } } else { Log.e(TAG, "Couldn't obtain the spell checker service."); } }
@Override public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) { long ident = Binder.clearCallingIdentity(); try { return onGetSuggestionsInternal(textInfo, suggestionsLimit); } finally { Binder.restoreCallingIdentity(ident); } }
/** * Get sentence suggestions for specified texts in an array of TextInfo. This is taken from * SpellCheckerService#onGetSentenceSuggestionsMultiple that we can't use because it's * using private variables. * The default implementation splits the input text to words and returns * {@link SentenceSuggestionsInfo} which contains suggestions for each word. * This function will run on the incoming IPC thread. * So, this is not called on the main thread, * but will be called in series on another thread. * @param textInfos an array of the text metadata * @param suggestionsLimit the maximum number of suggestions to be returned * @return an array of {@link SentenceSuggestionsInfo} returned by * {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo, int)} */ private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos, int suggestionsLimit) { if (textInfos == null || textInfos.length == 0) { return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); } SentenceLevelAdapter sentenceLevelAdapter; synchronized(this) { sentenceLevelAdapter = mSentenceLevelAdapter; if (sentenceLevelAdapter == null) { final String localeStr = getLocale(); if (!TextUtils.isEmpty(localeStr)) { sentenceLevelAdapter = new SentenceLevelAdapter(mResources, new Locale(localeStr)); mSentenceLevelAdapter = sentenceLevelAdapter; } } } if (sentenceLevelAdapter == null) { return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); } final int infosSize = textInfos.length; final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize]; for (int i = 0; i < infosSize; ++i) { final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams = sentenceLevelAdapter.getSplitWords(textInfos[i]); final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems = textInfoParams.mItems; final int itemsSize = mItems.size(); final TextInfo[] splitTextInfos = new TextInfo[itemsSize]; for (int j = 0; j < itemsSize; ++j) { splitTextInfos[j] = mItems.get(j).mTextInfo; } retval[i] = SentenceLevelAdapter.reconstructSuggestions( textInfoParams, onGetSuggestionsMultiple( splitTextInfos, suggestionsLimit, true)); } return retval; }
@Override public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) { long ident = Binder.clearCallingIdentity(); try { final int length = textInfos.length; final SuggestionsInfo[] retval = new SuggestionsInfo[length]; for (int i = 0; i < length; ++i) { final CharSequence prevWord; if (sequentialWords && i > 0) { final TextInfo prevTextInfo = textInfos[i - 1]; final CharSequence prevWordCandidate = TextInfoCompatUtils.getCharSequenceOrString(prevTextInfo); // Note that an empty string would be used to indicate the initial word // in the future. prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate; } else { prevWord = null; } final NgramContext ngramContext = new NgramContext(new NgramContext.WordInfo(prevWord)); final TextInfo textInfo = textInfos[i]; retval[i] = onGetSuggestionsInternal(textInfo, ngramContext, suggestionsLimit); retval[i].setCookieAndSequence(textInfo.getCookie(), textInfo.getSequence()); } return retval; } finally { Binder.restoreCallingIdentity(ident); } }
@UsedForTesting public static TextInfo newInstance(CharSequence charSequence, int start, int end, int cookie, int sequenceNumber) { if (TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE != null) { return (TextInfo) CompatUtils.newInstance(TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE, charSequence, start, end, cookie, sequenceNumber); } return new TextInfo(charSequence.subSequence(start, end).toString(), cookie, sequenceNumber); }
/** * 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); }
/** * 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); } mStartMs = SystemClock.elapsedRealtime(); mSpellCheckerSession.getSentenceSuggestions(new TextInfo[] {new TextInfo(text)}, 0); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spellchecker); check = (Button) findViewById(R.id.checkerbuttoncheck); checktext = (EditText) findViewById(R.id.checkeredittext); suggestbox = (TextView) findViewById(R.id.checkersuggestview); rightbox = (TextView) findViewById(R.id.righttext); preferences = this.getSharedPreferences("team7663.project.camword",Context.MODE_PRIVATE); preferences.edit().putBoolean("team7663.project.camword.OCR.mainoff",true).apply(); checktext.setText(preferences.getString("team7663.project.camword.OCR.savespell","")); TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); scs = tsm.newSpellCheckerSession(null,null, SpellChecker.this,true); MainActivity.spell.setBackgroundResource(R.drawable.spell); check.setOnClickListener(new View.OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v){ value = checktext.getText().toString(); rightbox.setText(""); if(value.isEmpty()==false) { scs.getSuggestions(new TextInfo(value), 5); } } }); }
public SentenceWordItem(TextInfo ti, int start, int end) { mTextInfo = ti; mStart = start; mLength = end - start; }
public SentenceTextInfoParams(TextInfo ti, ArrayList<SentenceWordItem> items) { mOriginalTextInfo = ti; mItems = items; mSize = items.size(); }
/** * Returns the result of {@link TextInfo#getCharSequence()} when available. Otherwise returns * the result of {@link TextInfo#getText()} as fall back. * @param textInfo the instance for which {@link TextInfo#getCharSequence()} or * {@link TextInfo#getText()} is called. * @return the result of {@link TextInfo#getCharSequence()} when available. Otherwise returns * the result of {@link TextInfo#getText()} as fall back. If {@code textInfo} is {@code null}, * returns {@code null}. */ @UsedForTesting public static CharSequence getCharSequenceOrString(final TextInfo textInfo) { final CharSequence defaultValue = (textInfo == null ? null : textInfo.getText()); return (CharSequence) CompatUtils.invoke(textInfo, defaultValue, TEXT_INFO_GET_CHAR_SEQUENCE); }
/** * Gets a list of suggestions for a specific string. This returns a list of possible * corrections for the text passed as an argument. It may split or group words, and * even perform grammatical analysis. */ private SuggestionsInfo onGetSuggestionsInternal(final TextInfo textInfo, final int suggestionsLimit) { return onGetSuggestionsInternal(textInfo, null, suggestionsLimit); }