1/*
2 * Copyright (C) 2009 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 PINYINIME_INCLUDE_SPELLINGTABLE_H__
18#define PINYINIME_INCLUDE_SPELLINGTABLE_H__
19
20#include <stdlib.h>
21#include "./dictdef.h"
22
23namespace ime_pinyin {
24
25#ifdef ___BUILD_MODEL___
26
27const size_t kMaxSpellingSize = kMaxPinyinSize;
28
29typedef struct {
30  char str[kMaxSpellingSize + 1];
31  double freq;
32} RawSpelling, *PRawSpelling;
33
34// This class is used to store the spelling strings
35// The length of the input spelling string should be less or equal to the
36// spelling_size_ (set by init_table). If the input string is too long,
37// we only keep its first spelling_size_ chars.
38class SpellingTable {
39 private:
40  static const size_t kNotSupportNum = 3;
41  static const char kNotSupportList[kNotSupportNum][kMaxSpellingSize + 1];
42
43  bool need_score_;
44
45  size_t spelling_max_num_;
46
47  RawSpelling *raw_spellings_;
48
49  // Used to store spelling strings. If the spelling table needs to calculate
50  // score, an extra char after each spelling string is the score.
51  // An item with a lower score has a higher probability.
52  char *spelling_buf_;
53  size_t spelling_size_;
54
55  double total_freq_;
56
57  size_t spelling_num_;
58
59  double score_amplifier_;
60
61  unsigned char average_score_;
62
63  // If frozen is true, put_spelling() and contain() are not allowed to call.
64  bool frozen_;
65
66  size_t get_hash_pos(const char* spelling_str);
67  size_t hash_pos_next(size_t hash_pos);
68  void free_resource();
69 public:
70  SpellingTable();
71  ~SpellingTable();
72
73  // pure_spl_size is the pure maximum spelling string size. For example,
74  // "zhuang" is the longgest item in Pinyin, so pure_spl_size should be 6.
75  // spl_max_num is the maximum number of spelling strings to store.
76  // need_score is used to indicate whether the caller needs to calculate a
77  // score for each spelling.
78  bool init_table(size_t pure_spl_size, size_t spl_max_num, bool need_score);
79
80  // Put a spelling string to the table.
81  // It always returns false if called after arrange() withtout a new
82  // init_table() operation.
83  // freq is the spelling's occuring count.
84  // If the spelling has been in the table, occuring count will accumulated.
85  bool put_spelling(const char* spelling_str, double spl_count);
86
87  // Test whether a spelling string is in the table.
88  // It always returns false, when being called after arrange() withtout a new
89  // init_table() operation.
90  bool contain(const char* spelling_str);
91
92  // Sort the spelling strings and put them from the begin of the buffer.
93  // Return the pointer of the sorted spelling strings.
94  // item_size and spl_num return the item size and number of spelling.
95  // Because each spelling uses a '\0' as terminator, the returned item_size is
96  // at least one char longer than the spl_size parameter specified by
97  // init_table(). If the table is initialized to calculate score, item_size
98  // will be increased by 1, and current_spl_str[item_size - 1] stores an
99  // unsinged char score.
100  // An item with a lower score has a higher probability.
101  // Do not call put_spelling() and contains() after arrange().
102  const char* arrange(size_t *item_size, size_t *spl_num);
103
104  float get_score_amplifier();
105
106  unsigned char get_average_score();
107};
108#endif  // ___BUILD_MODEL___
109}
110
111#endif  // PINYINIME_INCLUDE_SPELLINGTABLE_H__
112