awtk/3rd/gpinyin/include/dictlist.h

117 lines
3.9 KiB
C
Raw Normal View History

2018-06-23 18:17:22 +08:00
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PINYINIME_INCLUDE_DICTLIST_H__
#define PINYINIME_INCLUDE_DICTLIST_H__
2018-06-24 12:23:51 +08:00
#include "file.h"
2018-06-23 18:17:22 +08:00
#include <stdlib.h>
#include "./dictdef.h"
#include "./searchutility.h"
#include "./spellingtrie.h"
#include "./utf16char.h"
namespace ime_pinyin {
class DictList {
private:
bool initialized_;
2018-06-24 18:03:28 +08:00
const SpellingTrie* spl_trie_;
2018-06-23 18:17:22 +08:00
// Number of SingCharItem. The first is blank, because id 0 is invalid.
unsigned scis_num_;
2018-06-24 18:03:28 +08:00
char16* scis_hz_;
SpellingId* scis_splid_;
2018-06-23 18:17:22 +08:00
// The large memory block to store the word list.
2018-06-24 18:03:28 +08:00
char16* buf_;
2018-06-23 18:17:22 +08:00
// Starting position of those words whose lengths are i+1, counted in
// char16
unsigned start_pos_[kMaxLemmaSize + 1];
unsigned start_id_[kMaxLemmaSize + 1];
2018-06-24 18:03:28 +08:00
int (*cmp_func_[kMaxLemmaSize])(const void*, const void*);
2018-06-23 18:17:22 +08:00
bool alloc_resource(unsigned buf_size, unsigned scim_num);
void free_resource();
#ifdef ___BUILD_MODEL___
// Calculate the requsted memory, including the start_pos[] buffer.
2018-06-24 18:03:28 +08:00
unsigned calculate_size(const LemmaEntry* lemma_arr, unsigned lemma_num);
2018-06-23 18:17:22 +08:00
2018-06-24 18:03:28 +08:00
void fill_scis(const SingleCharItem* scis, unsigned scis_num);
2018-06-23 18:17:22 +08:00
// Copy the related content to the inner buffer
// It should be called after calculate_size()
2018-06-24 18:03:28 +08:00
void fill_list(const LemmaEntry* lemma_arr, unsigned lemma_num);
2018-06-23 18:17:22 +08:00
// Find the starting position for the buffer of those 2-character Chinese word
// whose first character is the given Chinese character.
char16* find_pos2_startedbyhz(char16 hz_char);
#endif
// Find the starting position for the buffer of those words whose lengths are
// word_len. The given parameter cmp_func decides how many characters from
// beginning will be used to compare.
2018-06-24 18:03:28 +08:00
char16* find_pos_startedbyhzs(const char16 last_hzs[], unsigned word_Len,
int (*cmp_func)(const void*, const void*));
2018-06-23 18:17:22 +08:00
public:
DictList();
~DictList();
2018-06-24 18:03:28 +08:00
bool save_list(FILE* fp);
bool load_list(FILE* fp);
2018-06-23 18:17:22 +08:00
#ifdef ___BUILD_MODEL___
// Init the list from the LemmaEntry array.
// lemma_arr should have been sorted by the hanzi_str, and have been given
// ids from 1
2018-06-24 18:03:28 +08:00
bool init_list(const SingleCharItem* scis, unsigned scis_num, const LemmaEntry* lemma_arr,
unsigned lemma_num);
2018-06-23 18:17:22 +08:00
#endif
// Get the hanzi string for the given id
2018-06-24 18:03:28 +08:00
uint16 get_lemma_str(LemmaIdType id_hz, char16* str_buf, uint16 str_max);
2018-06-23 18:17:22 +08:00
2018-06-24 18:03:28 +08:00
void convert_to_hanzis(char16* str, uint16 str_len);
2018-06-23 18:17:22 +08:00
2018-06-24 18:03:28 +08:00
void convert_to_scis_ids(char16* str, uint16 str_len);
2018-06-23 18:17:22 +08:00
// last_hzs stores the last n Chinese characters history, its length should be
// less or equal than kMaxPredictSize.
// hzs_len specifies the length(<= kMaxPredictSize).
// predict_buf is used to store the result.
// buf_len specifies the buffer length.
// b4_used specifies how many items before predict_buf have been used.
// Returned value is the number of newly added items.
2018-06-24 18:03:28 +08:00
unsigned predict(const char16 last_hzs[], uint16 hzs_len, NPredictItem* npre_items,
unsigned npre_max, unsigned b4_used);
2018-06-23 18:17:22 +08:00
// If half_splid is a valid half spelling id, return those full spelling
// ids which share this half id.
2018-06-24 18:03:28 +08:00
uint16 get_splids_for_hanzi(char16 hanzi, uint16 half_splid, uint16* splids, uint16 max_splids);
2018-06-23 18:17:22 +08:00
2018-06-24 18:03:28 +08:00
LemmaIdType get_lemma_id(const char16* str, uint16 str_len);
2018-06-23 18:17:22 +08:00
};
2018-06-24 18:03:28 +08:00
} // namespace ime_pinyin
2018-06-23 18:17:22 +08:00
#endif // PINYINIME_INCLUDE_DICTLIST_H__