diff options
author | Peng Wu <alexepico@gmail.com> | 2016-05-26 13:59:13 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-05-26 13:59:13 +0800 |
commit | 5ac8a59499f7e9715b3f82747cc8962c49bc881c (patch) | |
tree | 953549704183ab0c8732a5f2206981377911a666 /src/storage | |
parent | c57ac3f8e10e8c39053ca614277ee0fa671afef4 (diff) | |
download | libpinyin-5ac8a59499f7e9715b3f82747cc8962c49bc881c.tar.gz libpinyin-5ac8a59499f7e9715b3f82747cc8962c49bc881c.tar.xz libpinyin-5ac8a59499f7e9715b3f82747cc8962c49bc881c.zip |
write increase_pronunciation_possibility function
Diffstat (limited to 'src/storage')
-rw-r--r-- | src/storage/phonetic_key_matrix.cpp | 71 | ||||
-rw-r--r-- | src/storage/phonetic_key_matrix.h | 6 |
2 files changed, 76 insertions, 1 deletions
diff --git a/src/storage/phonetic_key_matrix.cpp b/src/storage/phonetic_key_matrix.cpp index 9ff35e2..7b0876e 100644 --- a/src/storage/phonetic_key_matrix.cpp +++ b/src/storage/phonetic_key_matrix.cpp @@ -300,7 +300,7 @@ gfloat compute_pronunciation_possibility_recur(pinyin_option_t options, return 0.; return item.get_pronunciation_possibility - (options, (ChewingKey *)cached_keys->data); + (options, (ChewingKey *) cached_keys->data); } gfloat result = 0.; @@ -350,4 +350,73 @@ gfloat compute_pronunciation_possibility(pinyin_option_t options, (options, matrix, start, end, cached_keys, item); } +bool increase_pronunciation_possibility_recur(pinyin_option_t options, + PhoneticKeyMatrix * matrix, + size_t start, size_t end, + GArray * cached_keys, + PhraseItem & item, gint32 delta) { + if (start > end) + return false; + + const size_t phrase_length = item.get_phrase_length(); + if (phrase_length < cached_keys->len) + return false; + + /* only increase with 'start' and 'end'. */ + if (start == end) { + if (phrase_length != cached_keys->len) + return false; + + item.increase_pronunciation_possibility + (options, (ChewingKey *) cached_keys->data, delta); + return true; + } + + bool result = false; + + const size_t size = matrix->get_column_size(start); + /* assume pinyin parsers will filter invalid keys. */ + assert(size > 0); + + for (size_t i = 0; i < size; ++i) { + ChewingKey key; ChewingKeyRest key_rest; + matrix->get_item(start, i, key, key_rest); + + const size_t newstart = key_rest.m_raw_end; + + const ChewingKey zero_key; + if (zero_key == key) { + /* assume only one key here for "'" or the last key. */ + assert(1 == size); + return increase_pronunciation_possibility_recur + (options, matrix, newstart, end, cached_keys, item, delta); + } + + /* push value */ + g_array_append_val(cached_keys, key); + + result = increase_pronunciation_possibility_recur + (options, matrix, newstart, end, cached_keys, item, delta) || result; + + /* pop value */ + g_array_set_size(cached_keys, cached_keys->len - 1); + } + + return result; +} + +bool increase_pronunciation_possibility(pinyin_option_t options, + PhoneticKeyMatrix * matrix, + size_t start, size_t end, + GArray * cached_keys, + PhraseItem & item, gint32 delta) { + assert(end < matrix->size()); + + assert(matrix->get_column_size(start) > 0); + assert(matrix->get_column_size(end) > 0); + + return increase_pronunciation_possibility_recur + (options, matrix, start, end, cached_keys, item, delta); +} + }; diff --git a/src/storage/phonetic_key_matrix.h b/src/storage/phonetic_key_matrix.h index c88b378..5e1f510 100644 --- a/src/storage/phonetic_key_matrix.h +++ b/src/storage/phonetic_key_matrix.h @@ -217,6 +217,12 @@ gfloat compute_pronunciation_possibility(pinyin_option_t options, size_t start, size_t end, GArray * cached_keys, PhraseItem & item); + +bool increase_pronunciation_possibility(pinyin_option_t options, + PhoneticKeyMatrix * matrix, + size_t start, size_t end, + GArray * cached_keys, + PhraseItem & item, gint32 delta); }; #endif |