blob: f66e27b8abbcd6894ffa579a42128b5d643c5537 [file] [log] [blame]
satok30088252010-12-01 21:22:15 +09001/*
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 Chalard1ff8dc42012-05-02 16:00:24 +090020#include <map>
Jean Chalard9c2a96a2012-04-17 11:38:44 +090021#include <stdint.h>
22
Jean Chalardf1634c82012-05-02 19:05:27 +090023#include "defines.h"
24
satok30088252010-12-01 21:22:15 +090025namespace latinime {
26
27class Dictionary;
28class BigramDictionary {
Ken Wakasae12e9b52012-01-06 12:24:38 +090029 public:
Jean Chalardb7d7c5a2012-07-11 11:31:48 +090030 BigramDictionary(const unsigned char *dict, int maxWordLength, int maxPredictions);
satokb1ed1d42012-06-14 16:35:23 -070031 int getBigrams(const int32_t *word, int length, int *inputCodes, int codesSize,
Jean Chalard6931df92012-07-12 12:55:48 +090032 unsigned short *outWords, int *frequencies, int *outputTypes) const;
Jean Chalarde9a86e22012-06-28 21:01:29 +090033 int getBigramListPositionForWord(const int32_t *prevWord, const int prevWordLength,
34 const bool forceLowerCaseSearch) const;
Jean Chalardf1634c82012-05-02 19:05:27 +090035 void fillBigramAddressToFrequencyMapAndFilter(const int32_t *prevWord, const int prevWordLength,
satokb1ed1d42012-06-14 16:35:23 -070036 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;
satok30088252010-12-01 21:22:15 +090038 ~BigramDictionary();
Ken Wakasae12e9b52012-01-06 12:24:38 +090039 private:
satok1bc038c2012-06-14 11:25:50 -070040 DISALLOW_IMPLICIT_CONSTRUCTORS(BigramDictionary);
Jean Chalard1a69ad52012-07-11 11:37:36 +090041 bool addWordBigram(unsigned short *word, int length, int frequency,
Jean Chalardc7387a42012-07-12 15:21:11 +090042 int *bigramFreq, unsigned short *bigramChars, int *outputTypes) const;
satok18c28f42010-12-02 18:11:54 +090043 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; }
satokb1ed1d42012-06-14 16:35:23 -070048 bool checkFirstCharacter(unsigned short *word, int *inputCodes) const;
satok18c28f42010-12-02 18:11:54 +090049
50 const unsigned char *DICT;
51 const int MAX_WORD_LENGTH;
Jean Chalardb7d7c5a2012-07-11 11:31:48 +090052 const int MAX_PREDICTIONS;
satok6ba8de22012-03-28 18:21:04 +090053 // TODO: Re-implement proximity correction for bigram correction
54 static const int MAX_ALTERNATIVES = 1;
satok30088252010-12-01 21:22:15 +090055};
Ken Wakasace9e52a2011-06-18 13:09:55 +090056} // namespace latinime
satok30088252010-12-01 21:22:15 +090057#endif // LATINIME_BIGRAM_DICTIONARY_H