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
21namespace latinime {
22
23void SuggestionResults::outputSuggestions(JNIEnv *env, jintArray outSuggestionCount,
24        jintArray outputCodePointsArray, jintArray outScoresArray, jintArray outSpaceIndicesArray,
25        jintArray outTypesArray, jintArray outAutoCommitFirstWordConfidenceArray,
26        jfloatArray outWeightOfLangModelVsSpatialModel) {
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, outWeightOfLangModelVsSpatialModel, 0 /* index */,
48            mWeightOfLangModelVsSpatialModel);
49}
50
51void SuggestionResults::addPrediction(const int *const codePoints, const int codePointCount,
52        const int probability) {
53    if (probability == NOT_A_PROBABILITY) {
54        // Invalid word.
55        return;
56    }
57    addSuggestion(codePoints, codePointCount, probability, Dictionary::KIND_PREDICTION,
58            NOT_AN_INDEX, NOT_A_FIRST_WORD_CONFIDENCE);
59}
60
61void SuggestionResults::addSuggestion(const int *const codePoints, const int codePointCount,
62        const int score, const int type, const int indexToPartialCommit,
63        const int autocimmitFirstWordConfindence) {
64    if (codePointCount <= 0 || codePointCount > MAX_WORD_LENGTH) {
65        // Invalid word.
66        AKLOGE("Invalid word is added to the suggestion results. codePointCount: %d",
67                codePointCount);
68        return;
69    }
70    if (getSuggestionCount() >= mMaxSuggestionCount) {
71        const SuggestedWord &mWorstSuggestion = mSuggestedWords.top();
72        if (score > mWorstSuggestion.getScore() || (score == mWorstSuggestion.getScore()
73                && codePointCount < mWorstSuggestion.getCodePointCount())) {
74            mSuggestedWords.pop();
75        } else {
76            return;
77        }
78    }
79    mSuggestedWords.push(SuggestedWord(codePoints, codePointCount, score, type,
80            indexToPartialCommit, autocimmitFirstWordConfindence));
81}
82
83void SuggestionResults::getSortedScores(int *const outScores) const {
84    auto copyOfSuggestedWords = mSuggestedWords;
85    while (!copyOfSuggestedWords.empty()) {
86        const SuggestedWord &suggestedWord = copyOfSuggestedWords.top();
87        outScores[copyOfSuggestedWords.size() - 1] = suggestedWord.getScore();
88        copyOfSuggestedWords.pop();
89    }
90}
91
92void SuggestionResults::dumpSuggestions() const {
93    AKLOGE("weight of language model vs spatial model: %f", mWeightOfLangModelVsSpatialModel);
94    std::vector<SuggestedWord> suggestedWords;
95    auto copyOfSuggestedWords = mSuggestedWords;
96    while (!copyOfSuggestedWords.empty()) {
97        suggestedWords.push_back(copyOfSuggestedWords.top());
98        copyOfSuggestedWords.pop();
99    }
100    int index = 0;
101    for (auto it = suggestedWords.rbegin(); it != suggestedWords.rend(); ++it) {
102        DUMP_SUGGESTION(it->getCodePoint(), it->getCodePointCount(), index, it->getScore());
103        index++;
104    }
105}
106
107} // namespace latinime
108