3 * Library to deal with pinyin.
5 * Copyright (C) 2006-2007 Peng Wu
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "phrase_index.h"
24 bool PhraseItem::set_n_pronunciation(guint8 n_prouns){
25 m_chunk.set_content(sizeof(guint8), &n_prouns, sizeof(guint8));
29 bool PhraseItem::get_nth_pronunciation(size_t index, PinyinKey * pinyin, guint32 & freq){
30 guint8 phrase_length = get_phrase_length();
31 table_offset_t offset = phrase_item_header + phrase_length * sizeof( utf16_t) + index * ( phrase_length * sizeof (PinyinKey) + sizeof(guint32));
32 bool retval = m_chunk.get_content(offset, pinyin, phrase_length * sizeof(PinyinKey));
35 return m_chunk.get_content(offset + phrase_length * sizeof(PinyinKey), &freq , sizeof(guint32));
38 void PhraseItem::append_pronunciation(PinyinKey * pinyin, guint32 freq){
39 guint8 phrase_length = get_phrase_length();
40 set_n_pronunciation(get_n_pronunciation() + 1);
41 m_chunk.set_content(m_chunk.size(), pinyin, phrase_length * sizeof(PinyinKey));
42 m_chunk.set_content(m_chunk.size(), &freq, sizeof(guint32));
45 void PhraseItem::remove_nth_pronunciation(size_t index){
46 guint8 phrase_length = get_phrase_length();
47 set_n_pronunciation(get_n_pronunciation() - 1);
48 size_t offset = phrase_item_header + phrase_length * sizeof ( utf16_t ) + index * (phrase_length * sizeof (PinyinKey) + sizeof(guint32));
49 m_chunk.remove_content(offset, phrase_length * sizeof(PinyinKey) + sizeof(guint32));
52 bool PhraseItem::get_phrase_string(utf16_t * phrase){
53 guint8 phrase_length = get_phrase_length();
54 return m_chunk.get_content(phrase_item_header, phrase, phrase_length * sizeof(utf16_t));
57 bool PhraseItem::set_phrase_string(guint8 phrase_length, utf16_t * phrase){
58 m_chunk.set_content(0, &phrase_length, sizeof(guint8));
59 m_chunk.set_content(phrase_item_header, phrase, phrase_length * sizeof(utf16_t));
63 void PhraseItem::increase_pinyin_possibility(PinyinCustomSettings & custom,
64 PinyinKey * pinyin_keys,
66 guint8 phrase_length = get_phrase_length();
67 guint8 npron = get_n_pronunciation();
68 size_t offset = phrase_item_header + phrase_length * sizeof ( utf16_t );
69 char * buf_begin = (char *) m_chunk.begin();
70 guint32 total_freq = 0;
71 for ( int i = 0 ; i < npron ; ++i){
72 char * pinyin_begin = buf_begin + offset +
73 i * ( phrase_length * sizeof(PinyinKey) + sizeof(guint32) );
74 guint32 * freq = (guint32 *)(pinyin_begin + phrase_length * sizeof(PinyinKey));
76 if ( 0 == pinyin_compare_with_ambiguities(custom,
77 (PinyinKey *)pinyin_begin,
80 //protect against total_freq overflow.
81 if ( delta > 0 && total_freq > total_freq + delta )
90 guint32 SubPhraseIndex::get_phrase_index_total_freq(){
94 bool SubPhraseIndex::add_unigram_frequency(phrase_token_t token, guint32 delta){
95 table_offset_t offset;
97 bool result = m_phrase_index.get_content
98 ((token & PHRASE_MASK)
99 * sizeof(table_offset_t), &offset, sizeof(table_offset_t));
107 result = m_phrase_content.get_content
108 (offset + sizeof(guint8) + sizeof(guint8), &freq, sizeof(guint32));
109 //protect total_freq overflow
110 if ( delta > 0 && m_total_freq > m_total_freq + delta )
113 m_total_freq += delta;
114 return m_phrase_content.set_content(offset + sizeof(guint8) + sizeof(guint8), &freq, sizeof(guint32));
117 bool SubPhraseIndex::get_phrase_item(phrase_token_t token, PhraseItem & item){
118 table_offset_t offset;
119 guint8 phrase_length;
122 bool result = m_phrase_index.get_content
123 ((token & PHRASE_MASK)
124 * sizeof(table_offset_t), &offset, sizeof(table_offset_t));
132 result = m_phrase_content.get_content(offset, &phrase_length, sizeof(guint8));
136 result = m_phrase_content.get_content(offset+sizeof(guint8), &n_prons, sizeof(guint8));
140 size_t length = phrase_item_header + phrase_length * sizeof ( utf16_t ) + n_prons * ( phrase_length * sizeof (PinyinKey) + sizeof(guint32) );
141 item.m_chunk.set_chunk((char *)m_phrase_content.begin() + offset, length, NULL);
145 bool SubPhraseIndex::add_phrase_item(phrase_token_t token, PhraseItem * item){
146 table_offset_t offset = m_phrase_content.size();
149 m_phrase_content.set_content(offset, item->m_chunk.begin(), item->m_chunk.size());
150 m_phrase_index.set_content((token & PHRASE_MASK)
151 * sizeof(table_offset_t), &offset, sizeof(table_offset_t));
152 m_total_freq += item->get_unigram_frequency();
156 bool SubPhraseIndex::remove_phrase_item(phrase_token_t token, PhraseItem * & item){
159 int result = get_phrase_item(token, old_item);
163 item = new PhraseItem;
164 //implictly copy data from m_chunk_content.
165 item->m_chunk.set_content(0, (char *) old_item.m_chunk.begin() , old_item.m_chunk.size());
167 const table_offset_t zero_const = 0;
168 m_phrase_index.set_content((token & PHRASE_MASK)
169 * sizeof(table_offset_t), &zero_const, sizeof(table_offset_t));
170 m_total_freq -= item->get_unigram_frequency();
174 bool FacadePhraseIndex::load(guint8 phrase_index, MemoryChunk * chunk){
175 SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
177 sub_phrases = new SubPhraseIndex;
180 bool retval = sub_phrases->load(chunk, 0, chunk->size());
183 m_total_freq += sub_phrases->get_phrase_index_total_freq();
187 bool FacadePhraseIndex::store(guint8 phrase_index, MemoryChunk * new_chunk){
189 SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
193 sub_phrases->store(new_chunk, 0, end);
197 bool FacadePhraseIndex::unload(guint8 phrase_index){
198 SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
201 m_total_freq -= sub_phrases->get_phrase_index_total_freq();
207 bool SubPhraseIndex::load(MemoryChunk * chunk,
208 table_offset_t offset, table_offset_t end){
209 //save the memory chunk
216 char * buf_begin = (char *)chunk->begin();
217 chunk->get_content(offset, &m_total_freq, sizeof(guint32));
218 offset += sizeof(guint32);
219 table_offset_t index_one, index_two, index_three;
220 chunk->get_content(offset, &index_one, sizeof(table_offset_t));
221 offset += sizeof(table_offset_t);
222 chunk->get_content(offset, &index_two, sizeof(table_offset_t));
223 offset += sizeof(table_offset_t);
224 chunk->get_content(offset, &index_three, sizeof(table_offset_t));
225 offset += sizeof(table_offset_t);
226 g_return_val_if_fail(*(buf_begin + offset) == c_separate, FALSE);
227 g_return_val_if_fail(*(buf_begin + index_two - 1) == c_separate, FALSE);
228 g_return_val_if_fail(*(buf_begin + index_three - 1) == c_separate, FALSE);
229 m_phrase_index.set_chunk(buf_begin + index_one,
230 index_two - 1 - index_one, NULL);
231 m_phrase_content.set_chunk(buf_begin + index_two,
232 index_three - 1 - index_two, NULL);
233 g_return_val_if_fail( index_three <= end, FALSE);
237 bool SubPhraseIndex::store(MemoryChunk * new_chunk,
238 table_offset_t offset, table_offset_t& end){
239 new_chunk->set_content(offset, &m_total_freq, sizeof(guint32));
240 table_offset_t index = offset + sizeof(guint32);
242 offset = index + sizeof(table_offset_t) * 3 ;
243 new_chunk->set_content(offset, &c_separate, sizeof(char));
244 offset += sizeof(char);
246 new_chunk->set_content(index, &offset, sizeof(table_offset_t));
247 index += sizeof(table_offset_t);
248 new_chunk->set_content(offset, m_phrase_index.begin(), m_phrase_index.size());
249 offset += m_phrase_index.size();
250 new_chunk->set_content(offset, &c_separate, sizeof(char));
251 offset += sizeof(char);
253 new_chunk->set_content(index, &offset, sizeof(table_offset_t));
254 index += sizeof(table_offset_t);
256 new_chunk->set_content(offset, m_phrase_content.begin(), m_phrase_content.size());
257 offset += m_phrase_content.size();
258 new_chunk->set_content(offset, &c_separate, sizeof(char));
259 offset += sizeof(char);
260 new_chunk->set_content(index, &offset, sizeof(table_offset_t));
264 bool FacadePhraseIndex::load_text(guint8 phrase_index, FILE * infile){
265 SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
267 sub_phrases = new SubPhraseIndex;
272 phrase_token_t token;
274 PhraseItem * item_ptr = new PhraseItem;
275 phrase_token_t cur_token = 0;
276 while ( !feof(infile)){
277 fscanf(infile, "%s", pinyin);
278 fscanf(infile, "%s", phrase);
279 fscanf(infile, "%ld", &token);
280 fscanf(infile, "%ld", &freq);
285 utf16_t * phrase_utf16 = g_utf8_to_utf16(phrase, -1, NULL,
288 if ( 0 == cur_token ){
290 item_ptr->set_phrase_string(written, phrase_utf16);
293 if ( cur_token != token ){
294 add_phrase_item( cur_token, item_ptr);
296 item_ptr = new PhraseItem;
298 item_ptr->set_phrase_string(written, phrase_utf16);
301 PinyinDefaultParser parser;
302 NullPinyinValidator validator;
303 PinyinKeyVector keys;
304 PinyinKeyPosVector poses;
306 keys = g_array_new(FALSE, FALSE, sizeof( PinyinKey));
307 poses = g_array_new(FALSE, FALSE, sizeof( PinyinKeyPos));
308 parser.parse(validator, keys, poses, pinyin);
310 assert ( item_ptr->get_phrase_length() == keys->len );
311 item_ptr->append_pronunciation((PinyinKey *)keys->data, freq);
313 g_array_free(keys, TRUE);
314 g_array_free(poses, TRUE);
315 g_free(phrase_utf16);
318 add_phrase_item( cur_token, item_ptr);
320 m_total_freq += m_sub_phrase_indices[phrase_index]->get_phrase_index_total_freq();