satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | #ifndef LATINIME_BIGRAM_DICTIONARY_H |
| 18 | #define LATINIME_BIGRAM_DICTIONARY_H |
| 19 | |
Jean Chalard | 1ff8dc4 | 2012-05-02 16:00:24 +0900 | [diff] [blame] | 20 | #include <map> |
Jean Chalard | 9c2a96a | 2012-04-17 11:38:44 +0900 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
Jean Chalard | f1634c8 | 2012-05-02 19:05:27 +0900 | [diff] [blame] | 23 | #include "defines.h" |
| 24 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 25 | namespace latinime { |
| 26 | |
| 27 | class Dictionary; |
| 28 | class BigramDictionary { |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 29 | public: |
Jean Chalard | b7d7c5a | 2012-07-11 11:31:48 +0900 | [diff] [blame] | 30 | BigramDictionary(const unsigned char *dict, int maxWordLength, int maxPredictions); |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 31 | int getBigrams(const int32_t *word, int length, int *inputCodes, int codesSize, |
Jean Chalard | 6931df9 | 2012-07-12 12:55:48 +0900 | [diff] [blame] | 32 | unsigned short *outWords, int *frequencies, int *outputTypes) const; |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 33 | int getBigramListPositionForWord(const int32_t *prevWord, const int prevWordLength, |
| 34 | const bool forceLowerCaseSearch) const; |
Jean Chalard | f1634c8 | 2012-05-02 19:05:27 +0900 | [diff] [blame] | 35 | void fillBigramAddressToFrequencyMapAndFilter(const int32_t *prevWord, const int prevWordLength, |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 36 | std::map<int, int> *map, uint8_t *filter) const; |
| 37 | bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2) const; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 38 | ~BigramDictionary(); |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 39 | private: |
satok | 1bc038c | 2012-06-14 11:25:50 -0700 | [diff] [blame] | 40 | DISALLOW_IMPLICIT_CONSTRUCTORS(BigramDictionary); |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 41 | bool addWordBigram(unsigned short *word, int length, int frequency, |
Jean Chalard | c7387a4 | 2012-07-12 15:21:11 +0900 | [diff] [blame] | 42 | int *bigramFreq, unsigned short *bigramChars, int *outputTypes) const; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 43 | int getBigramAddress(int *pos, bool advance); |
| 44 | int getBigramFreq(int *pos); |
| 45 | void searchForTerminalNode(int addressLookingFor, int frequency); |
| 46 | bool getFirstBitOfByte(int *pos) { return (DICT[*pos] & 0x80) > 0; } |
| 47 | bool getSecondBitOfByte(int *pos) { return (DICT[*pos] & 0x40) > 0; } |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 48 | bool checkFirstCharacter(unsigned short *word, int *inputCodes) const; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 49 | |
| 50 | const unsigned char *DICT; |
| 51 | const int MAX_WORD_LENGTH; |
Jean Chalard | b7d7c5a | 2012-07-11 11:31:48 +0900 | [diff] [blame] | 52 | const int MAX_PREDICTIONS; |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 53 | // TODO: Re-implement proximity correction for bigram correction |
| 54 | static const int MAX_ALTERNATIVES = 1; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 55 | }; |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 56 | } // namespace latinime |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 57 | #endif // LATINIME_BIGRAM_DICTIONARY_H |