1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "suggest/core/result/suggestion_results.h" 18 19 #include "utils/jni_data_utils.h" 20 21 namespace latinime { 22 23 void SuggestionResults::outputSuggestions(JNIEnv *env, jintArray outSuggestionCount, 24 jintArray outputCodePointsArray, jintArray outScoresArray, jintArray outSpaceIndicesArray, 25 jintArray outTypesArray, jintArray outAutoCommitFirstWordConfidenceArray, 26 jfloatArray outLanguageWeight) { 27 int outputIndex = 0; 28 while (!mSuggestedWords.empty()) { 29 const SuggestedWord &suggestedWord = mSuggestedWords.top(); 30 suggestedWord.getCodePointCount(); 31 const int start = outputIndex * MAX_WORD_LENGTH; 32 JniDataUtils::outputCodePoints(env, outputCodePointsArray, start, 33 MAX_WORD_LENGTH /* maxLength */, suggestedWord.getCodePoint(), 34 suggestedWord.getCodePointCount(), true /* needsNullTermination */); 35 JniDataUtils::putIntToArray(env, outScoresArray, outputIndex, suggestedWord.getScore()); 36 JniDataUtils::putIntToArray(env, outSpaceIndicesArray, outputIndex, 37 suggestedWord.getIndexToPartialCommit()); 38 JniDataUtils::putIntToArray(env, outTypesArray, outputIndex, suggestedWord.getType()); 39 if (mSuggestedWords.size() == 1) { 40 JniDataUtils::putIntToArray(env, outAutoCommitFirstWordConfidenceArray, 0 /* index */, 41 suggestedWord.getAutoCommitFirstWordConfidence()); 42 } 43 ++outputIndex; 44 mSuggestedWords.pop(); 45 } 46 JniDataUtils::putIntToArray(env, outSuggestionCount, 0 /* index */, outputIndex); 47 JniDataUtils::putFloatToArray(env, outLanguageWeight, 0 /* index */, mLanguageWeight); 48 } 49 50 void SuggestionResults::addPrediction(const int *const codePoints, const int codePointCount, 51 const int probability) { 52 if (probability == NOT_A_PROBABILITY) { 53 // Invalid word. 54 return; 55 } 56 addSuggestion(codePoints, codePointCount, probability, Dictionary::KIND_PREDICTION, 57 NOT_AN_INDEX, NOT_A_FIRST_WORD_CONFIDENCE); 58 } 59 60 void SuggestionResults::addSuggestion(const int *const codePoints, const int codePointCount, 61 const int score, const int type, const int indexToPartialCommit, 62 const int autocimmitFirstWordConfindence) { 63 if (codePointCount <= 0 || codePointCount > MAX_WORD_LENGTH) { 64 // Invalid word. 65 AKLOGE("Invalid word is added to the suggestion results. codePointCount: %d", 66 codePointCount); 67 return; 68 } 69 if (getSuggestionCount() >= mMaxSuggestionCount) { 70 const SuggestedWord &mWorstSuggestion = mSuggestedWords.top(); 71 if (score > mWorstSuggestion.getScore() || (score == mWorstSuggestion.getScore() 72 && codePointCount < mWorstSuggestion.getCodePointCount())) { 73 mSuggestedWords.pop(); 74 } else { 75 return; 76 } 77 } 78 mSuggestedWords.push(SuggestedWord(codePoints, codePointCount, score, type, 79 indexToPartialCommit, autocimmitFirstWordConfindence)); 80 } 81 82 void SuggestionResults::getSortedScores(int *const outScores) const { 83 auto copyOfSuggestedWords = mSuggestedWords; 84 while (!copyOfSuggestedWords.empty()) { 85 const SuggestedWord &suggestedWord = copyOfSuggestedWords.top(); 86 outScores[copyOfSuggestedWords.size() - 1] = suggestedWord.getScore(); 87 copyOfSuggestedWords.pop(); 88 } 89 } 90 91 void SuggestionResults::dumpSuggestions() const { 92 AKLOGE("language weight: %f", mLanguageWeight); 93 std::vector<SuggestedWord> suggestedWords; 94 auto copyOfSuggestedWords = mSuggestedWords; 95 while (!copyOfSuggestedWords.empty()) { 96 suggestedWords.push_back(copyOfSuggestedWords.top()); 97 copyOfSuggestedWords.pop(); 98 } 99 int index = 0; 100 for (auto it = suggestedWords.rbegin(); it != suggestedWords.rend(); ++it) { 101 DUMP_SUGGESTION(it->getCodePoint(), it->getCodePointCount(), index, it->getScore()); 102 index++; 103 } 104 } 105 106 } // namespace latinime 107