android.view.textservice.SentenceSuggestionsInfo Java Examples

The following examples show how to use android.view.textservice.SentenceSuggestionsInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: AndroidSpellCheckerSession.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: AndroidSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #4
Source File: AndroidSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #5
Source File: SpellCheckerSessionBridge.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #6
Source File: AndroidSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: SentenceLevelAdapter.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static SentenceSuggestionsInfo reconstructSuggestions(
        SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
    if (results == null || results.length == 0) {
        return null;
    }
    if (originalTextInfoParams == null) {
        return null;
    }
    final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie();
    final int originalSequence =
            originalTextInfoParams.mOriginalTextInfo.getSequence();

    final int querySize = originalTextInfoParams.mSize;
    final int[] offsets = new int[querySize];
    final int[] lengths = new int[querySize];
    final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize];
    for (int i = 0; i < querySize; ++i) {
        final SentenceWordItem item = originalTextInfoParams.mItems.get(i);
        SuggestionsInfo result = null;
        for (int j = 0; j < results.length; ++j) {
            final SuggestionsInfo cur = results[j];
            if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) {
                result = cur;
                result.setCookieAndSequence(originalCookie, originalSequence);
                break;
            }
        }
        offsets[i] = item.mStart;
        lengths[i] = item.mLength;
        reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO;
    }
    return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}
 
Example #8
Source File: AndroidSpellCheckerSession.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: SentenceLevelAdapter.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static SentenceSuggestionsInfo reconstructSuggestions(
        SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
    if (results == null || results.length == 0) {
        return null;
    }
    if (originalTextInfoParams == null) {
        return null;
    }
    final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie();
    final int originalSequence =
            originalTextInfoParams.mOriginalTextInfo.getSequence();

    final int querySize = originalTextInfoParams.mSize;
    final int[] offsets = new int[querySize];
    final int[] lengths = new int[querySize];
    final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize];
    for (int i = 0; i < querySize; ++i) {
        final SentenceWordItem item = originalTextInfoParams.mItems.get(i);
        SuggestionsInfo result = null;
        for (int j = 0; j < results.length; ++j) {
            final SuggestionsInfo cur = results[j];
            if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) {
                result = cur;
                result.setCookieAndSequence(originalCookie, originalSequence);
                break;
            }
        }
        offsets[i] = item.mStart;
        lengths[i] = item.mLength;
        reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO;
    }
    return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}
 
Example #10
Source File: SentenceLevelAdapter.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static SentenceSuggestionsInfo reconstructSuggestions(
        SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
    if (results == null || results.length == 0) {
        return null;
    }
    if (originalTextInfoParams == null) {
        return null;
    }
    final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie();
    final int originalSequence =
            originalTextInfoParams.mOriginalTextInfo.getSequence();

    final int querySize = originalTextInfoParams.mSize;
    final int[] offsets = new int[querySize];
    final int[] lengths = new int[querySize];
    final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize];
    for (int i = 0; i < querySize; ++i) {
        final SentenceWordItem item = originalTextInfoParams.mItems.get(i);
        SuggestionsInfo result = null;
        for (int j = 0; j < results.length; ++j) {
            final SuggestionsInfo cur = results[j];
            if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) {
                result = cur;
                result.setCookieAndSequence(originalCookie, originalSequence);
                break;
            }
        }
        offsets[i] = item.mStart;
        lengths[i] = item.mLength;
        reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO;
    }
    return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}
 
Example #11
Source File: AndroidSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #12
Source File: SentenceLevelAdapter.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static SentenceSuggestionsInfo reconstructSuggestions(
        SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
    if (results == null || results.length == 0) {
        return null;
    }
    if (originalTextInfoParams == null) {
        return null;
    }
    final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie();
    final int originalSequence =
            originalTextInfoParams.mOriginalTextInfo.getSequence();

    final int querySize = originalTextInfoParams.mSize;
    final int[] offsets = new int[querySize];
    final int[] lengths = new int[querySize];
    final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize];
    for (int i = 0; i < querySize; ++i) {
        final SentenceWordItem item = originalTextInfoParams.mItems.get(i);
        SuggestionsInfo result = null;
        for (int j = 0; j < results.length; ++j) {
            final SuggestionsInfo cur = results[j];
            if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) {
                result = cur;
                result.setCookieAndSequence(originalCookie, originalSequence);
                break;
            }
        }
        offsets[i] = item.mStart;
        lengths[i] = item.mLength;
        reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO;
    }
    return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}
 
Example #13
Source File: AndroidSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #14
Source File: SpellCheckerSessionBridge.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {
    mStopMs = SystemClock.elapsedRealtime();

    if (mNativeSpellCheckerSessionBridge == 0) {
        return;
    }

    ArrayList<Integer> offsets = new ArrayList<Integer>();
    ArrayList<Integer> lengths = new ArrayList<Integer>();

    for (SentenceSuggestionsInfo result : results) {
        if (result == null) {
            // In some cases null can be returned by the selected spellchecking service,
            // see crbug.com/651458. In this case skip to next result to avoid a
            // NullPointerException later on.
            continue;
        }
        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));

    RecordHistogram.recordTimesHistogram("SpellCheck.Android.Latency",
            mStopMs - mStartMs, TimeUnit.MILLISECONDS);
}
 
Example #15
Source File: SentenceLevelAdapter.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 4 votes vote down vote up
public static SentenceSuggestionsInfo[] getEmptySentenceSuggestionsInfo() {
    return EmptySentenceSuggestionsInfosInitializationHolder.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
}
 
Example #16
Source File: SentenceLevelAdapter.java    From Indic-Keyboard with Apache License 2.0 4 votes vote down vote up
public static SentenceSuggestionsInfo[] getEmptySentenceSuggestionsInfo() {
    return EmptySentenceSuggestionsInfosInitializationHolder.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
}
 
Example #17
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Get sentence suggestions for specified texts in an array of TextInfo.
 * 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.
 * When you override this method, make sure that suggestionsLimit is applied to suggestions
 * that share the same start position and length.
 * @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 SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit) {
    if (textInfos == null || textInfos.length == 0) {
        return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
    }
    if (DBG) {
        Log.d(TAG, "onGetSentenceSuggestionsMultiple: + " + textInfos.length + ", "
                + suggestionsLimit);
    }
    if (mSentenceLevelAdapter == null) {
        synchronized(this) {
            if (mSentenceLevelAdapter == null) {
                final String localeStr = getLocale();
                if (!TextUtils.isEmpty(localeStr)) {
                    mSentenceLevelAdapter = new SentenceLevelAdapter(new Locale(localeStr));
                }
            }
        }
    }
    if (mSentenceLevelAdapter == null) {
        return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
    }
    final int infosSize = textInfos.length;
    final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize];
    for (int i = 0; i < infosSize; ++i) {
        final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams =
                mSentenceLevelAdapter.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;
}
 
Example #18
Source File: SentenceLevelAdapter.java    From Android-Keyboard with Apache License 2.0 4 votes vote down vote up
public static SentenceSuggestionsInfo[] getEmptySentenceSuggestionsInfo() {
    return EmptySentenceSuggestionsInfosInitializationHolder.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
}
 
Example #19
Source File: SentenceLevelAdapter.java    From openboard with GNU General Public License v3.0 4 votes vote down vote up
public static SentenceSuggestionsInfo[] getEmptySentenceSuggestionsInfo() {
    return EmptySentenceSuggestionsInfosInitializationHolder.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
}
 
Example #20
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public static SentenceSuggestionsInfo reconstructSuggestions(
        SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
    if (results == null || results.length == 0) {
        return null;
    }
    if (DBG) {
        Log.w(TAG, "Adapter: onGetSuggestions: got " + results.length);
    }
    if (originalTextInfoParams == null) {
        if (DBG) {
            Log.w(TAG, "Adapter: originalTextInfoParams is null.");
        }
        return null;
    }
    final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie();
    final int originalSequence =
            originalTextInfoParams.mOriginalTextInfo.getSequence();

    final int querySize = originalTextInfoParams.mSize;
    final int[] offsets = new int[querySize];
    final int[] lengths = new int[querySize];
    final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize];
    for (int i = 0; i < querySize; ++i) {
        final SentenceWordItem item = originalTextInfoParams.mItems.get(i);
        SuggestionsInfo result = null;
        for (int j = 0; j < results.length; ++j) {
            final SuggestionsInfo cur = results[j];
            if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) {
                result = cur;
                result.setCookieAndSequence(originalCookie, originalSequence);
                break;
            }
        }
        offsets[i] = item.mStart;
        lengths[i] = item.mLength;
        reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO;
        if (DBG) {
            final int size = reconstructedSuggestions[i].getSuggestionsCount();
            Log.w(TAG, "reconstructedSuggestions(" + i + ")" + size + ", first = "
                    + (size > 0 ? reconstructedSuggestions[i].getSuggestionAt(0)
                            : "<none>") + ", offset = " + offsets[i] + ", length = "
                    + lengths[i]);
        }
    }
    return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}