From 054a3e03a45984eacb21777b961c66a9736d74ba Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Fri, 2 Sep 2022 18:41:09 +0800 Subject: Write ChewingTableEntry::search_suggestion method --- src/storage/chewing_large_table2.h | 82 +++++++++++++++++++++++++++++++++++++- src/storage/pinyin_phrase3.h | 4 +- 2 files changed, 82 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/storage/chewing_large_table2.h b/src/storage/chewing_large_table2.h index ebf6114..d37ed7c 100644 --- a/src/storage/chewing_large_table2.h +++ b/src/storage/chewing_large_table2.h @@ -38,6 +38,32 @@ namespace pinyin{ class MaskOutVisitor2; +template +class PrefixLessThanWithTones{ +protected: + int m_prefix_len; + +public: + PrefixLessThanWithTones(int prefix_len) : + m_prefix_len(prefix_len) {} + + ~PrefixLessThanWithTones() { + m_prefix_len = 0; + } + + int prefix_compare_with_tones(const PinyinIndexItem2 &lhs, + const PinyinIndexItem2 &rhs) { + ChewingKey * keys_lhs = (ChewingKey *) lhs.m_keys; + ChewingKey * keys_rhs = (ChewingKey *) rhs.m_keys; + return pinyin_compare_with_tones(keys_lhs, keys_rhs, m_prefix_len); + } + + bool operator () (const PinyinIndexItem2 &lhs, + const PinyinIndexItem2 &rhs) { + return 0 > prefix_compare_with_tones(lhs, rhs); + } +}; + /* As this is a template class, the code will be in the header file. */ template class ChewingTableEntry{ @@ -57,8 +83,8 @@ public: /* convert method. */ /* compress consecutive tokens */ int convert(const ChewingKey keys[], - const IndexItem * begin, const IndexItem * end, - PhraseIndexRanges ranges) const { + const IndexItem * begin, const IndexItem * end, + PhraseIndexRanges ranges) const { const IndexItem * iter = NULL; PhraseIndexRange cursor; GArray * head, * cursor_head = NULL; @@ -120,6 +146,58 @@ public: return convert(keys, range.first, range.second, ranges); } + int convert_suggestion(int prefix_len, + const ChewingKey prefix_keys[], + const IndexItem * begin, const IndexItem * end, + PhraseTokens tokens) const { + assert(prefix_len < phrase_length); + const IndexItem * iter = NULL; + GArray * array = NULL; + + int result = SEARCH_NONE; + for (iter = begin; iter != end; ++iter) { + if (0 != pinyin_compare_with_tones + (prefix_keys, iter->m_keys, prefix_len)) + continue; + + phrase_token_t token = iter->m_token; + array = tokens[PHRASE_INDEX_LIBRARY_INDEX(token)]; + if (NULL == array) + continue; + + result |= SEARCH_OK; + g_array_append_val(array, token); + } + + return result; + } + + /* search_suggestion method */ + int search_suggestion(int prefix_len, + /* in */ const ChewingKey prefix_keys[], + /* out */ PhraseTokens tokens) const { + /* Usually suggestion candidates will have at least two characters, + use PhraseTokens instead of PhraseIndexRanges. */ + assert(prefix_len < phrase_length); + + IndexItem item; + if (contains_incomplete_pinyin(prefix_keys, prefix_len)) { + compute_incomplete_chewing_index + (prefix_keys, item.m_keys, prefix_len); + } else { + compute_chewing_index(prefix_keys, item.m_keys, prefix_len); + } + + const IndexItem * begin = (IndexItem *) m_chunk.begin(); + const IndexItem * end = (IndexItem *) m_chunk.end(); + + PrefixLessThanWithTones less_than(prefix_len); + std_lite::pair range = + std_lite::equal_range(begin, end, item, less_than); + + return convert_suggestion(prefix_len, prefix_keys, range.first, range.second, tokens); + } + /* add/remove index method */ int add_index(/* in */ const ChewingKey keys[], /* in */ phrase_token_t token) { diff --git a/src/storage/pinyin_phrase3.h b/src/storage/pinyin_phrase3.h index d2e75ad..d23c657 100644 --- a/src/storage/pinyin_phrase3.h +++ b/src/storage/pinyin_phrase3.h @@ -223,8 +223,8 @@ inline int phrase_compare_with_tones(const PinyinIndexItem2 &lhs, } template -inline int phrase_less_than_with_tones(const PinyinIndexItem2 &lhs, - const PinyinIndexItem2 &rhs) +inline bool phrase_less_than_with_tones(const PinyinIndexItem2 &lhs, + const PinyinIndexItem2 &rhs) { return 0 > phrase_compare_with_tones(lhs, rhs); } -- cgit v1.2.1