2010-12-01 12:22:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LATINIME_UNIGRAM_DICTIONARY_H
|
|
|
|
#define LATINIME_UNIGRAM_DICTIONARY_H
|
|
|
|
|
2011-06-16 11:55:16 +00:00
|
|
|
#include <stdint.h>
|
2011-08-10 05:30:10 +00:00
|
|
|
#include "correction.h"
|
2011-08-10 06:44:08 +00:00
|
|
|
#include "correction_state.h"
|
2010-12-02 05:53:24 +00:00
|
|
|
#include "defines.h"
|
2011-02-22 08:28:55 +00:00
|
|
|
#include "proximity_info.h"
|
2011-12-12 11:53:22 +00:00
|
|
|
#include "words_priority_queue.h"
|
2011-12-15 07:49:12 +00:00
|
|
|
#include "words_priority_queue_pool.h"
|
2010-12-01 12:22:15 +00:00
|
|
|
|
2010-12-02 05:53:24 +00:00
|
|
|
namespace latinime {
|
2010-12-01 12:22:15 +00:00
|
|
|
|
2011-12-26 06:16:59 +00:00
|
|
|
class TerminalAttributes;
|
2010-12-01 12:22:15 +00:00
|
|
|
class UnigramDictionary {
|
2012-03-21 09:38:25 +00:00
|
|
|
typedef struct { int first; int second; int replacement; } digraph_t;
|
2012-03-06 10:54:03 +00:00
|
|
|
|
2012-01-06 03:24:38 +00:00
|
|
|
public:
|
2011-06-28 11:45:05 +00:00
|
|
|
// Mask and flags for children address type selection.
|
|
|
|
static const int MASK_GROUP_ADDRESS_TYPE = 0xC0;
|
|
|
|
static const int FLAG_GROUP_ADDRESS_TYPE_NOADDRESS = 0x00;
|
|
|
|
static const int FLAG_GROUP_ADDRESS_TYPE_ONEBYTE = 0x40;
|
|
|
|
static const int FLAG_GROUP_ADDRESS_TYPE_TWOBYTES = 0x80;
|
|
|
|
static const int FLAG_GROUP_ADDRESS_TYPE_THREEBYTES = 0xC0;
|
|
|
|
|
|
|
|
// Flag for single/multiple char group
|
|
|
|
static const int FLAG_HAS_MULTIPLE_CHARS = 0x20;
|
|
|
|
|
|
|
|
// Flag for terminal groups
|
|
|
|
static const int FLAG_IS_TERMINAL = 0x10;
|
|
|
|
|
2011-12-27 03:58:23 +00:00
|
|
|
// Flag for shortcut targets presence
|
|
|
|
static const int FLAG_HAS_SHORTCUT_TARGETS = 0x08;
|
2011-06-28 11:45:05 +00:00
|
|
|
// Flag for bigram presence
|
|
|
|
static const int FLAG_HAS_BIGRAMS = 0x04;
|
|
|
|
|
|
|
|
// Attribute (bigram/shortcut) related flags:
|
|
|
|
// Flag for presence of more attributes
|
|
|
|
static const int FLAG_ATTRIBUTE_HAS_NEXT = 0x80;
|
|
|
|
// Flag for sign of offset. If this flag is set, the offset value must be negated.
|
|
|
|
static const int FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40;
|
|
|
|
|
|
|
|
// Mask for attribute frequency, stored on 4 bits inside the flags byte.
|
|
|
|
static const int MASK_ATTRIBUTE_FREQUENCY = 0x0F;
|
|
|
|
|
|
|
|
// Mask and flags for attribute address type selection.
|
|
|
|
static const int MASK_ATTRIBUTE_ADDRESS_TYPE = 0x30;
|
|
|
|
static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE = 0x10;
|
|
|
|
static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES = 0x20;
|
|
|
|
static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES = 0x30;
|
|
|
|
|
2011-12-15 05:53:19 +00:00
|
|
|
// Error tolerances
|
|
|
|
static const int DEFAULT_MAX_ERRORS = 2;
|
|
|
|
static const int MAX_ERRORS_FOR_TWO_WORDS = 1;
|
|
|
|
|
2011-06-16 11:55:16 +00:00
|
|
|
UnigramDictionary(const uint8_t* const streamStart, int typedLetterMultipler,
|
2012-04-06 09:26:00 +00:00
|
|
|
int fullWordMultiplier, int maxWordLength, int maxWords, const unsigned int flags);
|
2012-04-23 06:37:07 +00:00
|
|
|
bool isValidWord(const int32_t* const inWord, const int length) const;
|
2011-06-17 03:45:17 +00:00
|
|
|
int getBigramPosition(int pos, unsigned short *word, int offset, int length) const;
|
2011-12-15 07:49:12 +00:00
|
|
|
int getSuggestions(ProximityInfo *proximityInfo, WordsPriorityQueuePool *queuePool,
|
2011-12-14 06:04:58 +00:00
|
|
|
Correction *correction, const int *xcoordinates,
|
2012-04-06 10:28:34 +00:00
|
|
|
const int *ycoordinates, const int *codes, const int codesSize,
|
|
|
|
const bool useFullEditDistance, unsigned short *outWords, int *frequencies);
|
2011-07-15 04:49:00 +00:00
|
|
|
virtual ~UnigramDictionary();
|
2010-12-01 12:22:15 +00:00
|
|
|
|
2012-01-06 03:24:38 +00:00
|
|
|
private:
|
2011-07-13 01:32:02 +00:00
|
|
|
void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
|
2011-12-14 06:04:58 +00:00
|
|
|
const int *ycoordinates, const int *codes, const int inputLength,
|
2012-04-06 10:28:34 +00:00
|
|
|
const bool useFullEditDistance, Correction *correction,
|
|
|
|
WordsPriorityQueuePool *queuePool);
|
2012-03-21 09:38:25 +00:00
|
|
|
int getDigraphReplacement(const int *codes, const int i, const int codesSize,
|
2012-03-06 10:54:03 +00:00
|
|
|
const digraph_t* const digraphs, const unsigned int digraphsSize) const;
|
2011-07-13 01:32:02 +00:00
|
|
|
void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
|
2011-02-25 08:56:53 +00:00
|
|
|
const int *xcoordinates, const int* ycoordinates, const int *codesBuffer,
|
2012-03-08 02:53:18 +00:00
|
|
|
int *xCoordinatesBuffer, int *yCoordinatesBuffer,
|
2012-04-06 10:28:34 +00:00
|
|
|
const int codesBufferSize, const bool useFullEditDistance, const int* codesSrc,
|
2011-12-14 06:04:58 +00:00
|
|
|
const int codesRemain, const int currentDepth, int* codesDest, Correction *correction,
|
2012-03-06 10:54:03 +00:00
|
|
|
WordsPriorityQueuePool* queuePool, const digraph_t* const digraphs,
|
|
|
|
const unsigned int digraphsSize);
|
2011-07-13 01:32:02 +00:00
|
|
|
void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
|
2012-01-16 07:21:21 +00:00
|
|
|
const int *ycoordinates, const int *codes, const int codesSize, Correction *correction);
|
2011-12-15 13:29:05 +00:00
|
|
|
void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
|
|
|
|
const int *ycoordinates, const int *codes, const bool useFullEditDistance,
|
|
|
|
const int inputLength, Correction *correction, WordsPriorityQueuePool* queuePool);
|
2011-12-14 06:04:58 +00:00
|
|
|
void getSuggestionCandidates(
|
|
|
|
const bool useFullEditDistance, const int inputLength, Correction *correction,
|
2012-01-23 07:52:37 +00:00
|
|
|
WordsPriorityQueuePool* queuePool, const bool doAutoCompletion, const int maxErrors,
|
|
|
|
const int currentWordIndex);
|
2012-01-30 09:18:30 +00:00
|
|
|
void getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo,
|
2011-12-15 13:29:05 +00:00
|
|
|
const int *xcoordinates, const int *ycoordinates, const int *codes,
|
2012-01-30 04:53:58 +00:00
|
|
|
const bool useFullEditDistance, const int inputLength,
|
2012-01-26 13:49:13 +00:00
|
|
|
Correction *correction, WordsPriorityQueuePool* queuePool,
|
2012-01-23 07:52:37 +00:00
|
|
|
const bool hasAutoCorrectionCandidate);
|
2011-12-26 06:16:59 +00:00
|
|
|
void onTerminal(const int freq, const TerminalAttributes& terminalAttributes,
|
2012-01-23 07:52:37 +00:00
|
|
|
Correction *correction, WordsPriorityQueuePool *queuePool, const bool addToMasterQueue,
|
|
|
|
const int currentWordIndex);
|
2010-12-03 07:39:16 +00:00
|
|
|
bool needsToSkipCurrentNode(const unsigned short c,
|
2010-12-03 10:38:08 +00:00
|
|
|
const int inputIndex, const int skipPos, const int depth);
|
2010-12-08 08:05:39 +00:00
|
|
|
// Process a node by considering proximity, missing and excessive character
|
2011-12-14 06:04:58 +00:00
|
|
|
bool processCurrentNode(const int initialPos, Correction *correction, int *newCount,
|
2012-01-23 07:52:37 +00:00
|
|
|
int *newChildPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool,
|
|
|
|
const int currentWordIndex);
|
2011-06-30 11:14:38 +00:00
|
|
|
int getMostFrequentWordLike(const int startInputIndex, const int inputLength,
|
2011-12-14 06:04:58 +00:00
|
|
|
ProximityInfo *proximityInfo, unsigned short *word);
|
2011-06-28 11:45:05 +00:00
|
|
|
int getMostFrequentWordLikeInner(const uint16_t* const inWord, const int length,
|
2011-12-14 06:04:58 +00:00
|
|
|
short unsigned int *outWord);
|
2012-01-26 13:49:13 +00:00
|
|
|
bool getSubStringSuggestion(
|
2012-01-26 07:13:25 +00:00
|
|
|
ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates,
|
2012-01-26 09:36:19 +00:00
|
|
|
const int *codes, const bool useFullEditDistance, Correction *correction,
|
|
|
|
WordsPriorityQueuePool* queuePool, const int inputLength,
|
|
|
|
const bool hasAutoCorrectionCandidate, const int currentWordIndex,
|
|
|
|
const int inputWordStartPos, const int inputWordLength,
|
2012-01-26 13:49:13 +00:00
|
|
|
const int outputWordStartPos, const bool isSpaceProximity, int *freqArray,
|
|
|
|
int *wordLengthArray, unsigned short* outputWord, int *outputWordLength);
|
2012-01-30 04:53:58 +00:00
|
|
|
void getMultiWordsSuggestionRec(ProximityInfo *proximityInfo,
|
|
|
|
const int *xcoordinates, const int *ycoordinates, const int *codes,
|
|
|
|
const bool useFullEditDistance, const int inputLength,
|
|
|
|
Correction *correction, WordsPriorityQueuePool* queuePool,
|
|
|
|
const bool hasAutoCorrectionCandidate, const int startPos, const int startWordIndex,
|
|
|
|
const int outputWordLength, int *freqArray, int* wordLengthArray,
|
|
|
|
unsigned short* outputWord);
|
2011-06-16 11:55:16 +00:00
|
|
|
|
|
|
|
const uint8_t* const DICT_ROOT;
|
2010-12-01 12:22:15 +00:00
|
|
|
const int MAX_WORD_LENGTH;
|
2011-01-07 06:01:51 +00:00
|
|
|
const int MAX_WORDS;
|
2010-12-02 09:11:54 +00:00
|
|
|
const int TYPED_LETTER_MULTIPLIER;
|
|
|
|
const int FULL_WORD_MULTIPLIER;
|
2011-01-07 06:01:51 +00:00
|
|
|
const int ROOT_POS;
|
2011-02-25 08:56:53 +00:00
|
|
|
const unsigned int BYTES_IN_ONE_CHAR;
|
2012-03-06 10:54:03 +00:00
|
|
|
const int MAX_DIGRAPH_SEARCH_DEPTH;
|
2012-04-06 09:26:00 +00:00
|
|
|
const int FLAGS;
|
2011-02-25 08:56:53 +00:00
|
|
|
|
2012-03-06 10:54:03 +00:00
|
|
|
static const digraph_t GERMAN_UMLAUT_DIGRAPHS[];
|
2012-03-23 07:48:49 +00:00
|
|
|
static const digraph_t FRENCH_LIGATURES_DIGRAPHS[];
|
2010-12-01 12:22:15 +00:00
|
|
|
|
2011-12-14 06:04:58 +00:00
|
|
|
// Still bundled members
|
|
|
|
unsigned short mWord[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
|
2011-08-10 06:44:08 +00:00
|
|
|
int mStackChildCount[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
|
|
|
|
int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
|
|
|
|
int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
|
2010-12-01 12:22:15 +00:00
|
|
|
};
|
2011-06-18 04:09:55 +00:00
|
|
|
} // namespace latinime
|
2010-12-01 12:22:15 +00:00
|
|
|
|
|
|
|
#endif // LATINIME_UNIGRAM_DICTIONARY_H
|