3 * Library to deal with pinyin.
5 * Copyright (C) 2012 Peng Wu <alexepico@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef FACADE_PHRASE_TABLE2_H
23 #define FACADE_PHRASE_TABLE2_H
25 #include "phrase_large_table2.h"
32 * The facade class of phrase large table2.
36 class FacadePhraseTable2{
38 PhraseLargeTable2 * m_system_phrase_table;
39 PhraseLargeTable2 * m_user_phrase_table;
42 if (m_system_phrase_table) {
43 delete m_system_phrase_table;
44 m_system_phrase_table = NULL;
47 if (m_user_phrase_table) {
48 delete m_user_phrase_table;
49 m_user_phrase_table = NULL;
55 * FacadePhraseTable2::FacadePhraseTable2:
57 * The constructor of the FacadePhraseTable2.
60 FacadePhraseTable2() {
61 m_system_phrase_table = NULL;
62 m_user_phrase_table = NULL;
66 * FacadePhraseTable2::~FacadePhraseTable2:
68 * The destructor of the FacadePhraseTable2.
71 ~FacadePhraseTable2() {
76 * FacadePhraseTable2::load:
77 * @system: the memory chunk of the system phrase table.
78 * @user: the memory chunk of the user phrase table.
79 * @returns: whether the load operation is successful.
81 * Load the system or user phrase table from the memory chunks.
84 bool load(MemoryChunk * system, MemoryChunk * user) {
89 m_system_phrase_table = new PhraseLargeTable2;
90 result = m_system_phrase_table->load(system) || result;
93 m_user_phrase_table = new PhraseLargeTable2;
94 result = m_user_phrase_table->load(user) || result;
100 * FacadePhraseTable2::store:
101 * @new_user: the memory chunk to store the user phrase table.
102 * @returns: whether the store operation is successful.
104 * Store the user phrase table to the memory chunk.
107 bool store(MemoryChunk * new_user) {
108 if (NULL == m_user_phrase_table)
110 return m_user_phrase_table->store(new_user);
114 * FacadePhraseTable2::search:
115 * @phrase_length: the length of the phrase to be searched.
116 * @phrase: the ucs4 characters of the phrase to be searched.
117 * @tokens: the GArray of tokens to store the matched phrases.
118 * @returns: the search result of enum SearchResult.
120 * Search the phrase tokens according to the ucs4 characters.
123 int search(int phrase_length, /* in */ ucs4_t phrase[],
124 /* out */ PhraseTokens tokens) const {
126 for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
128 g_array_set_size(tokens[i], 0);
131 int result = SEARCH_NONE;
133 if (NULL != m_system_phrase_table)
134 result |= m_system_phrase_table->search
135 (phrase_length, phrase, tokens);
137 if (NULL != m_user_phrase_table)
138 result |= m_user_phrase_table->search
139 (phrase_length, phrase, tokens);
145 * FacadePhraseTable2::add_index:
146 * @phrase_length: the length of the phrase to be added.
147 * @phrase: the ucs4 characters of the phrase to be added.
148 * @token: the token of the phrase to be added.
149 * @returns: the add result of enum ErrorResult.
151 * Add the phrase token to the user phrase table.
154 int add_index(int phrase_length, /* in */ ucs4_t phrase[],
155 /* in */ phrase_token_t token) {
156 if (NULL == m_user_phrase_table)
157 return ERROR_NO_USER_TABLE;
159 return m_user_phrase_table->add_index
160 (phrase_length, phrase, token);
164 * FacadePhraseTable2::remove_index:
165 * @phrase_length: the length of the phrase to be removed.
166 * @phrase: the ucs4 characters of the phrase to be removed.
167 * @token: the token of the phrase to be removed.
168 * @returns: the remove result of enum ErrorResult.
170 * Remove the phrase token from the user phrase table.
173 int remove_index(int phrase_length, /* in */ ucs4_t phrase[],
174 /* in */ phrase_token_t token) {
175 if (NULL == m_user_phrase_table)
176 return ERROR_NO_USER_TABLE;
178 return m_user_phrase_table->remove_index
179 (phrase_length, phrase, token);
183 * FacadePhraseTable2::mask_out:
186 * @returns: whether the mask out operation is successful.
188 * Mask out the matched phrase index.
191 bool mask_out(phrase_token_t mask, phrase_token_t value) {
192 if (NULL == m_user_phrase_table)
195 return m_user_phrase_table->mask_out