Merge "Pass the bigram list position from the top level"

main
Jean Chalard 2012-04-24 01:13:52 -07:00 committed by Android (Google) Code Review
commit 8b01650b12
3 changed files with 55 additions and 44 deletions

View File

@ -35,9 +35,13 @@ class Dictionary {
int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates, int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
int *codes, int codesSize, bool useFullEditDistance, unsigned short *outWords, int *codes, int codesSize, bool useFullEditDistance, unsigned short *outWords,
int *frequencies) { int *frequencies) {
// bigramListPosition is, as an int, the offset of the bigram list in the file.
// If none, it's zero.
// TODO: get this from the bigram dictionary instance
const int bigramListPosition = 0;
return mUnigramDictionary->getSuggestions(proximityInfo, mWordsPriorityQueuePool, return mUnigramDictionary->getSuggestions(proximityInfo, mWordsPriorityQueuePool,
mCorrection, xcoordinates, ycoordinates, codes, mCorrection, xcoordinates, ycoordinates, codes, codesSize, bigramListPosition,
codesSize, useFullEditDistance, outWords, frequencies); useFullEditDistance, outWords, frequencies);
} }
int getBigrams(const int32_t *word, int length, int *codes, int codesSize, int getBigrams(const int32_t *word, int length, int *codes, int codesSize,

View File

@ -98,7 +98,8 @@ int UnigramDictionary::getDigraphReplacement(const int *codes, const int i, cons
void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo, void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
const int *xcoordinates, const int *ycoordinates, const int *codesBuffer, const int *xcoordinates, const int *ycoordinates, const int *codesBuffer,
int *xCoordinatesBuffer, int *yCoordinatesBuffer, int *xCoordinatesBuffer, int *yCoordinatesBuffer,
const int codesBufferSize, const bool useFullEditDistance, const int *codesSrc, const int codesBufferSize, const int bigramListPosition,
const bool useFullEditDistance, const int *codesSrc,
const int codesRemain, const int currentDepth, int *codesDest, Correction *correction, const int codesRemain, const int currentDepth, int *codesDest, Correction *correction,
WordsPriorityQueuePool *queuePool, WordsPriorityQueuePool *queuePool,
const digraph_t* const digraphs, const unsigned int digraphsSize) { const digraph_t* const digraphs, const unsigned int digraphsSize) {
@ -127,8 +128,8 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
replacementCodePoint; replacementCodePoint;
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
useFullEditDistance, codesSrc + i + 1, codesRemain - i - 1, bigramListPosition, useFullEditDistance, codesSrc + i + 1,
currentDepth + 1, codesDest + i, correction, codesRemain - i - 1, currentDepth + 1, codesDest + i, correction,
queuePool, digraphs, digraphsSize); queuePool, digraphs, digraphsSize);
// Copy the second char of the digraph in place, then continue processing on // Copy the second char of the digraph in place, then continue processing on
@ -137,9 +138,9 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
memcpy(codesDest + i, codesSrc + i, BYTES_IN_ONE_CHAR); memcpy(codesDest + i, codesSrc + i, BYTES_IN_ONE_CHAR);
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
useFullEditDistance, codesSrc + i, codesRemain - i, currentDepth + 1, bigramListPosition, useFullEditDistance, codesSrc + i, codesRemain - i,
codesDest + i, correction, queuePool, currentDepth + 1, codesDest + i, correction, queuePool, digraphs,
digraphs, digraphsSize); digraphsSize);
return; return;
} }
} }
@ -160,14 +161,16 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
} }
getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer, getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer,
startIndex + codesRemain, useFullEditDistance, correction, startIndex + codesRemain, bigramListPosition, useFullEditDistance, correction,
queuePool); queuePool);
} }
// bigramListPosition is the offset in the file to the list of bigrams for the previous word.
int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo, int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
WordsPriorityQueuePool *queuePool, Correction *correction, const int *xcoordinates, WordsPriorityQueuePool *queuePool, Correction *correction, const int *xcoordinates,
const int *ycoordinates, const int *codes, const int codesSize, const int *ycoordinates, const int *codes, const int codesSize,
const bool useFullEditDistance, unsigned short *outWords, int *frequencies) { const int bigramListPosition, const bool useFullEditDistance, unsigned short *outWords,
int *frequencies) {
queuePool->clearAll(); queuePool->clearAll();
Correction* masterCorrection = correction; Correction* masterCorrection = correction;
@ -177,8 +180,8 @@ int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
int xCoordinatesBuffer[codesSize]; int xCoordinatesBuffer[codesSize];
int yCoordinatesBuffer[codesSize]; int yCoordinatesBuffer[codesSize];
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer, getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
xCoordinatesBuffer, yCoordinatesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramListPosition,
codesSize, useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection, useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection,
queuePool, GERMAN_UMLAUT_DIGRAPHS, queuePool, GERMAN_UMLAUT_DIGRAPHS,
sizeof(GERMAN_UMLAUT_DIGRAPHS) / sizeof(GERMAN_UMLAUT_DIGRAPHS[0])); sizeof(GERMAN_UMLAUT_DIGRAPHS) / sizeof(GERMAN_UMLAUT_DIGRAPHS[0]));
} else if (BinaryFormat::REQUIRES_FRENCH_LIGATURES_PROCESSING & FLAGS) { } else if (BinaryFormat::REQUIRES_FRENCH_LIGATURES_PROCESSING & FLAGS) {
@ -186,13 +189,13 @@ int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
int xCoordinatesBuffer[codesSize]; int xCoordinatesBuffer[codesSize];
int yCoordinatesBuffer[codesSize]; int yCoordinatesBuffer[codesSize];
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer, getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
xCoordinatesBuffer, yCoordinatesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramListPosition,
codesSize, useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection, useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection,
queuePool, FRENCH_LIGATURES_DIGRAPHS, queuePool, FRENCH_LIGATURES_DIGRAPHS,
sizeof(FRENCH_LIGATURES_DIGRAPHS) / sizeof(FRENCH_LIGATURES_DIGRAPHS[0])); sizeof(FRENCH_LIGATURES_DIGRAPHS) / sizeof(FRENCH_LIGATURES_DIGRAPHS[0]));
} else { // Normal processing } else { // Normal processing
getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize, getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize,
useFullEditDistance, masterCorrection, queuePool); bigramListPosition, useFullEditDistance, masterCorrection, queuePool);
} }
PROF_START(20); PROF_START(20);
@ -225,16 +228,16 @@ int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
void UnigramDictionary::getWordSuggestions(ProximityInfo *proximityInfo, void UnigramDictionary::getWordSuggestions(ProximityInfo *proximityInfo,
const int *xcoordinates, const int *ycoordinates, const int *codes, const int *xcoordinates, const int *ycoordinates, const int *codes,
const int inputLength, const bool useFullEditDistance, Correction *correction, const int inputLength, const int bigramListPosition, const bool useFullEditDistance,
WordsPriorityQueuePool *queuePool) { Correction *correction, WordsPriorityQueuePool *queuePool) {
PROF_OPEN; PROF_OPEN;
PROF_START(0); PROF_START(0);
PROF_END(0); PROF_END(0);
PROF_START(1); PROF_START(1);
getOneWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, useFullEditDistance, getOneWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, bigramListPosition,
inputLength, correction, queuePool); useFullEditDistance, inputLength, correction, queuePool);
PROF_END(1); PROF_END(1);
PROF_START(2); PROF_START(2);
@ -305,15 +308,16 @@ static const char SPACE = ' ';
void UnigramDictionary::getOneWordSuggestions(ProximityInfo *proximityInfo, void UnigramDictionary::getOneWordSuggestions(ProximityInfo *proximityInfo,
const int *xcoordinates, const int *ycoordinates, const int *codes, const int *xcoordinates, const int *ycoordinates, const int *codes,
const bool useFullEditDistance, const int inputLength, Correction *correction, const int bigramListPosition, const bool useFullEditDistance, const int inputLength,
WordsPriorityQueuePool *queuePool) { Correction *correction, WordsPriorityQueuePool *queuePool) {
initSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, inputLength, correction); initSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, inputLength, correction);
getSuggestionCandidates(useFullEditDistance, inputLength, correction, queuePool, getSuggestionCandidates(useFullEditDistance, inputLength, bigramListPosition, correction,
true /* doAutoCompletion */, DEFAULT_MAX_ERRORS, FIRST_WORD_INDEX); queuePool, true /* doAutoCompletion */, DEFAULT_MAX_ERRORS, FIRST_WORD_INDEX);
} }
void UnigramDictionary::getSuggestionCandidates(const bool useFullEditDistance, void UnigramDictionary::getSuggestionCandidates(const bool useFullEditDistance,
const int inputLength, Correction *correction, WordsPriorityQueuePool *queuePool, const int inputLength, const int bigramListPosition,
Correction *correction, WordsPriorityQueuePool *queuePool,
const bool doAutoCompletion, const int maxErrors, const int currentWordIndex) { const bool doAutoCompletion, const int maxErrors, const int currentWordIndex) {
// TODO: Remove setCorrectionParams // TODO: Remove setCorrectionParams
correction->setCorrectionParams(0, 0, 0, correction->setCorrectionParams(0, 0, 0,
@ -333,8 +337,8 @@ void UnigramDictionary::getSuggestionCandidates(const bool useFullEditDistance,
int firstChildPos; int firstChildPos;
const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos,
correction, &childCount, &firstChildPos, &siblingPos, queuePool, bigramListPosition, correction, &childCount, &firstChildPos, &siblingPos,
currentWordIndex); queuePool, currentWordIndex);
// Update next sibling pos // Update next sibling pos
correction->setTreeSiblingPos(outputIndex, siblingPos); correction->setTreeSiblingPos(outputIndex, siblingPos);
@ -426,8 +430,10 @@ bool UnigramDictionary::getSubStringSuggestion(
initSuggestions(proximityInfo, &xcoordinates[offset], &ycoordinates[offset], initSuggestions(proximityInfo, &xcoordinates[offset], &ycoordinates[offset],
codes + offset, inputWordLength, correction); codes + offset, inputWordLength, correction);
queuePool->clearSubQueue(currentWordIndex); queuePool->clearSubQueue(currentWordIndex);
getSuggestionCandidates(useFullEditDistance, inputWordLength, correction, // TODO: pass the bigram list for substring suggestion
queuePool, false, MAX_ERRORS_FOR_TWO_WORDS, currentWordIndex); getSuggestionCandidates(useFullEditDistance, inputWordLength,
0 /* bigramListPosition */, correction, queuePool, false /* doAutoCompletion */,
MAX_ERRORS_FOR_TWO_WORDS, currentWordIndex);
if (DEBUG_DICT) { if (DEBUG_DICT) {
if (currentWordIndex < MULTIPLE_WORDS_SUGGESTION_MAX_WORDS) { if (currentWordIndex < MULTIPLE_WORDS_SUGGESTION_MAX_WORDS) {
AKLOGI("Dump word candidates(%d) %d", currentWordIndex, inputWordLength); AKLOGI("Dump word candidates(%d) %d", currentWordIndex, inputWordLength);
@ -757,15 +763,13 @@ int UnigramDictionary::getBigramPosition(int pos, unsigned short *word, int offs
// the current node in nextSiblingPosition. Thus, the caller must keep count of the nodes at any // the current node in nextSiblingPosition. Thus, the caller must keep count of the nodes at any
// given level, as output into newCount when traversing this level's parent. // given level, as output into newCount when traversing this level's parent.
inline bool UnigramDictionary::processCurrentNode(const int initialPos, inline bool UnigramDictionary::processCurrentNode(const int initialPos,
Correction *correction, int *newCount, const int bigramListPosition, Correction *correction, int *newCount,
int *newChildrenPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool, int *newChildrenPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool,
const int currentWordIndex) { const int currentWordIndex) {
if (DEBUG_DICT) { if (DEBUG_DICT) {
correction->checkState(); correction->checkState();
} }
int pos = initialPos; int pos = initialPos;
// TODO: get this as an argument
const int bigramListPosition = 0;
// Flags contain the following information: // Flags contain the following information:
// - Address type (MASK_GROUP_ADDRESS_TYPE) on two bits: // - Address type (MASK_GROUP_ADDRESS_TYPE) on two bits:
@ -842,6 +846,8 @@ inline bool UnigramDictionary::processCurrentNode(const int initialPos,
const int childrenAddressPos = BinaryFormat::skipFrequency(flags, pos); const int childrenAddressPos = BinaryFormat::skipFrequency(flags, pos);
const int attributesPos = BinaryFormat::skipChildrenPosition(flags, childrenAddressPos); const int attributesPos = BinaryFormat::skipChildrenPosition(flags, childrenAddressPos);
TerminalAttributes terminalAttributes(DICT_ROOT, flags, attributesPos); TerminalAttributes terminalAttributes(DICT_ROOT, flags, attributesPos);
// The bigramListPosition is the offset in the file of the bigrams for the previous word,
// or zero if we don't know of any bigrams for it.
const int probability = BinaryFormat::getProbability(bigramListPosition, unigramFreq); const int probability = BinaryFormat::getProbability(bigramListPosition, unigramFreq);
onTerminal(probability, terminalAttributes, correction, queuePool, needsToInvokeOnTerminal, onTerminal(probability, terminalAttributes, correction, queuePool, needsToInvokeOnTerminal,
currentWordIndex); currentWordIndex);

View File

@ -74,34 +74,35 @@ class UnigramDictionary {
bool isValidWord(const int32_t* const inWord, const int length) const; bool isValidWord(const int32_t* const inWord, const int length) const;
int getBigramPosition(int pos, unsigned short *word, int offset, int length) const; int getBigramPosition(int pos, unsigned short *word, int offset, int length) const;
int getSuggestions(ProximityInfo *proximityInfo, WordsPriorityQueuePool *queuePool, int getSuggestions(ProximityInfo *proximityInfo, WordsPriorityQueuePool *queuePool,
Correction *correction, const int *xcoordinates, Correction *correction, const int *xcoordinates, const int *ycoordinates,
const int *ycoordinates, const int *codes, const int codesSize, const int *codes, const int codesSize, const int bigramListPosition,
const bool useFullEditDistance, unsigned short *outWords, int *frequencies); const bool useFullEditDistance, unsigned short *outWords, int *frequencies);
virtual ~UnigramDictionary(); virtual ~UnigramDictionary();
private: private:
void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
const int *ycoordinates, const int *codes, const int inputLength, const int *ycoordinates, const int *codes, const int inputLength,
const bool useFullEditDistance, Correction *correction, const int bigramListPosition, const bool useFullEditDistance, Correction *correction,
WordsPriorityQueuePool *queuePool); WordsPriorityQueuePool *queuePool);
int getDigraphReplacement(const int *codes, const int i, const int codesSize, int getDigraphReplacement(const int *codes, const int i, const int codesSize,
const digraph_t* const digraphs, const unsigned int digraphsSize) const; const digraph_t* const digraphs, const unsigned int digraphsSize) const;
void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo, void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
const int *xcoordinates, const int* ycoordinates, const int *codesBuffer, const int *xcoordinates, const int* ycoordinates, const int *codesBuffer,
int *xCoordinatesBuffer, int *yCoordinatesBuffer, int *xCoordinatesBuffer, int *yCoordinatesBuffer, const int codesBufferSize,
const int codesBufferSize, const bool useFullEditDistance, const int* codesSrc, const int bigramListPosition, const bool useFullEditDistance, const int* codesSrc,
const int codesRemain, const int currentDepth, int* codesDest, Correction *correction, const int codesRemain, const int currentDepth, int* codesDest, Correction *correction,
WordsPriorityQueuePool* queuePool, const digraph_t* const digraphs, WordsPriorityQueuePool* queuePool, const digraph_t* const digraphs,
const unsigned int digraphsSize); const unsigned int digraphsSize);
void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
const int *ycoordinates, const int *codes, const int codesSize, Correction *correction); const int *ycoordinates, const int *codes, const int codesSize, Correction *correction);
void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
const int *ycoordinates, const int *codes, const bool useFullEditDistance, const int *ycoordinates, const int *codes, const int bigramListPosition,
const int inputLength, Correction *correction, WordsPriorityQueuePool* queuePool);
void getSuggestionCandidates(
const bool useFullEditDistance, const int inputLength, Correction *correction, const bool useFullEditDistance, const int inputLength, Correction *correction,
WordsPriorityQueuePool* queuePool, const bool doAutoCompletion, const int maxErrors, WordsPriorityQueuePool* queuePool);
const int currentWordIndex); void getSuggestionCandidates(
const bool useFullEditDistance, const int inputLength, const int bigramListPosition,
Correction *correction, WordsPriorityQueuePool* queuePool, const bool doAutoCompletion,
const int maxErrors, const int currentWordIndex);
void getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo, void getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo,
const int *xcoordinates, const int *ycoordinates, const int *codes, const int *xcoordinates, const int *ycoordinates, const int *codes,
const bool useFullEditDistance, const int inputLength, const bool useFullEditDistance, const int inputLength,
@ -113,9 +114,9 @@ class UnigramDictionary {
bool needsToSkipCurrentNode(const unsigned short c, bool needsToSkipCurrentNode(const unsigned short c,
const int inputIndex, const int skipPos, const int depth); const int inputIndex, const int skipPos, const int depth);
// Process a node by considering proximity, missing and excessive character // Process a node by considering proximity, missing and excessive character
bool processCurrentNode(const int initialPos, Correction *correction, int *newCount, bool processCurrentNode(const int initialPos, const int bigramListPosition,
int *newChildPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool, Correction *correction, int *newCount, int *newChildPosition, int *nextSiblingPosition,
const int currentWordIndex); WordsPriorityQueuePool *queuePool, const int currentWordIndex);
int getMostFrequentWordLike(const int startInputIndex, const int inputLength, int getMostFrequentWordLike(const int startInputIndex, const int inputLength,
ProximityInfo *proximityInfo, unsigned short *word); ProximityInfo *proximityInfo, unsigned short *word);
int getMostFrequentWordLikeInner(const uint16_t* const inWord, const int length, int getMostFrequentWordLikeInner(const uint16_t* const inWord, const int length,