summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2022-09-02 18:41:09 +0800
committerPeng Wu <alexepico@gmail.com>2022-09-02 18:45:04 +0800
commit054a3e03a45984eacb21777b961c66a9736d74ba (patch)
tree9425283c496ba3f2c0aec8850d2d2b0f2276404b
parent20ad271a95ff149422a55b8dbe985c114a5a27e4 (diff)
downloadlibpinyin-054a3e03a45984eacb21777b961c66a9736d74ba.tar.gz
Write ChewingTableEntry::search_suggestion method
-rw-r--r--src/storage/chewing_large_table2.h82
-rw-r--r--src/storage/pinyin_phrase3.h4
2 files changed, 82 insertions, 4 deletions
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<int phrase_length>
+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<phrase_length> &lhs,
+ const PinyinIndexItem2<phrase_length> &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<phrase_length> &lhs,
+ const PinyinIndexItem2<phrase_length> &rhs) {
+ return 0 > prefix_compare_with_tones(lhs, rhs);
+ }
+};
+
/* As this is a template class, the code will be in the header file. */
template<int phrase_length>
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<phrase_length> less_than(prefix_len);
+ std_lite::pair<const IndexItem *, const IndexItem *> 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<phrase_length> &lhs,
}
template<size_t phrase_length>
-inline int phrase_less_than_with_tones(const PinyinIndexItem2<phrase_length> &lhs,
- const PinyinIndexItem2<phrase_length> &rhs)
+inline bool phrase_less_than_with_tones(const PinyinIndexItem2<phrase_length> &lhs,
+ const PinyinIndexItem2<phrase_length> &rhs)
{
return 0 > phrase_compare_with_tones<phrase_length>(lhs, rhs);
}