Merge "Reorder suggestions result according to auto correction threshold" into jb-dev

main
satok 2012-05-16 04:13:08 -07:00 committed by Android (Google) Code Review
commit f837b57bf5
3 changed files with 60 additions and 25 deletions

View File

@ -408,8 +408,6 @@ public class Suggest implements Dictionary.WordCallback {
final String typedWord, final ArrayList<SuggestedWordInfo> suggestions) { final String typedWord, final ArrayList<SuggestedWordInfo> suggestions) {
final SuggestedWordInfo typedWordInfo = suggestions.get(0); final SuggestedWordInfo typedWordInfo = suggestions.get(0);
typedWordInfo.setDebugString("+"); typedWordInfo.setDebugString("+");
double normalizedScore = BinaryDictionary.calcNormalizedScore(
typedWord, typedWordInfo.toString(), typedWordInfo.mScore);
final int suggestionsSize = suggestions.size(); final int suggestionsSize = suggestions.size();
final ArrayList<SuggestedWordInfo> suggestionsList = final ArrayList<SuggestedWordInfo> suggestionsList =
new ArrayList<SuggestedWordInfo>(suggestionsSize); new ArrayList<SuggestedWordInfo>(suggestionsSize);
@ -418,10 +416,11 @@ public class Suggest implements Dictionary.WordCallback {
// than i because we added the typed word to mSuggestions without touching mScores. // than i because we added the typed word to mSuggestions without touching mScores.
for (int i = 0; i < suggestionsSize - 1; ++i) { for (int i = 0; i < suggestionsSize - 1; ++i) {
final SuggestedWordInfo cur = suggestions.get(i + 1); final SuggestedWordInfo cur = suggestions.get(i + 1);
final double normalizedScore = BinaryDictionary.calcNormalizedScore(
typedWord, cur.toString(), cur.mScore);
final String scoreInfoString; final String scoreInfoString;
if (normalizedScore > 0) { if (normalizedScore > 0) {
scoreInfoString = String.format("%d (%4.2f)", cur.mScore, normalizedScore); scoreInfoString = String.format("%d (%4.2f)", cur.mScore, normalizedScore);
normalizedScore = 0.0;
} else { } else {
scoreInfoString = Integer.toString(cur.mScore); scoreInfoString = Integer.toString(cur.mScore);
} }

View File

@ -208,7 +208,8 @@ int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
AKLOGI("Max normalized score = %f", ns); AKLOGI("Max normalized score = %f", ns);
} }
const int suggestedWordsCount = const int suggestedWordsCount =
queuePool->getMasterQueue()->outputSuggestions(frequencies, outWords); queuePool->getMasterQueue()->outputSuggestions(
proximityInfo->getPrimaryInputWord(), codesSize, frequencies, outWords);
if (DEBUG_DICT) { if (DEBUG_DICT) {
double ns = queuePool->getMasterQueue()->getHighestNormalizedScore( double ns = queuePool->getMasterQueue()->getHighestNormalizedScore(

View File

@ -92,10 +92,12 @@ class WordsPriorityQueue {
return sw; return sw;
} }
int outputSuggestions(int *frequencies, unsigned short *outputChars) { int outputSuggestions(const unsigned short* before, const int beforeLength,
int *frequencies, unsigned short *outputChars) {
mHighestSuggestedWord = 0; mHighestSuggestedWord = 0;
const unsigned int size = min( const unsigned int size = min(
MAX_WORDS, static_cast<unsigned int>(mSuggestions.size())); MAX_WORDS, static_cast<unsigned int>(mSuggestions.size()));
SuggestedWord* swBuffer[size];
int index = size - 1; int index = size - 1;
while (!mSuggestions.empty() && index >= 0) { while (!mSuggestions.empty() && index >= 0) {
SuggestedWord* sw = mSuggestions.top(); SuggestedWord* sw = mSuggestions.top();
@ -103,17 +105,45 @@ class WordsPriorityQueue {
AKLOGI("dump word. %d", sw->mScore); AKLOGI("dump word. %d", sw->mScore);
DUMP_WORD(sw->mWord, sw->mWordLength); DUMP_WORD(sw->mWord, sw->mWordLength);
} }
swBuffer[index] = sw;
mSuggestions.pop();
--index;
}
if (size >= 2) {
SuggestedWord* nsMaxSw = 0;
unsigned int maxIndex = 0;
double maxNs = 0;
for (unsigned int i = 0; i < size; ++i) {
SuggestedWord* tempSw = swBuffer[i];
if (!tempSw) {
continue;
}
const double tempNs = getNormalizedScore(tempSw, before, beforeLength, 0, 0, 0);
if (tempNs >= maxNs) {
maxNs = tempNs;
maxIndex = i;
nsMaxSw = tempSw;
}
}
if (maxIndex > 0 && nsMaxSw) {
memmove(&swBuffer[1], &swBuffer[0], maxIndex * sizeof(SuggestedWord*));
swBuffer[0] = nsMaxSw;
}
}
for (unsigned int i = 0; i < size; ++i) {
SuggestedWord* sw = swBuffer[i];
if (!sw) {
AKLOGE("SuggestedWord is null %d", i);
continue;
}
const unsigned int wordLength = sw->mWordLength; const unsigned int wordLength = sw->mWordLength;
char* targetAdr = (char*) outputChars char* targetAdr = (char*) outputChars + i * MAX_WORD_LENGTH * sizeof(short);
+ (index) * MAX_WORD_LENGTH * sizeof(short); frequencies[i] = sw->mScore;
frequencies[index] = sw->mScore;
memcpy(targetAdr, sw->mWord, (wordLength) * sizeof(short)); memcpy(targetAdr, sw->mWord, (wordLength) * sizeof(short));
if (wordLength < MAX_WORD_LENGTH) { if (wordLength < MAX_WORD_LENGTH) {
((unsigned short*) targetAdr)[wordLength] = 0; ((unsigned short*) targetAdr)[wordLength] = 0;
} }
sw->mUsed = false; sw->mUsed = false;
mSuggestions.pop();
--index;
} }
return size; return size;
} }
@ -147,21 +177,8 @@ class WordsPriorityQueue {
if (!mHighestSuggestedWord) { if (!mHighestSuggestedWord) {
return 0.0; return 0.0;
} }
SuggestedWord* sw = mHighestSuggestedWord; return getNormalizedScore(
const int score = sw->mScore; mHighestSuggestedWord, before, beforeLength, outWord, outScore, outLength);
unsigned short* word = sw->mWord;
const int wordLength = sw->mWordLength;
if (outScore) {
*outScore = score;
}
if (outWord) {
*outWord = word;
}
if (outLength) {
*outLength = wordLength;
}
return Correction::RankingAlgorithm::calcNormalizedScore(
before, beforeLength, word, wordLength, score);
} }
private: private:
@ -182,6 +199,24 @@ class WordsPriorityQueue {
return 0; return 0;
} }
static double getNormalizedScore(SuggestedWord* sw, const unsigned short* before,
const int beforeLength, unsigned short** outWord, int *outScore, int *outLength) {
const int score = sw->mScore;
unsigned short* word = sw->mWord;
const int wordLength = sw->mWordLength;
if (outScore) {
*outScore = score;
}
if (outWord) {
*outWord = word;
}
if (outLength) {
*outLength = wordLength;
}
return Correction::RankingAlgorithm::calcNormalizedScore(
before, beforeLength, word, wordLength, score);
}
typedef std::priority_queue<SuggestedWord*, std::vector<SuggestedWord*>, typedef std::priority_queue<SuggestedWord*, std::vector<SuggestedWord*>,
wordComparator> Suggestions; wordComparator> Suggestions;
Suggestions mSuggestions; Suggestions mSuggestions;