diff options
author | Peng Wu <alexepico@gmail.com> | 2016-05-25 16:41:44 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-05-25 16:41:44 +0800 |
commit | 473d80f6681f0fe7c20bc2008c249563a0f99eee (patch) | |
tree | 5a0571e949005122027fa0a2f4b12a830660a8a5 /src | |
parent | 5af793480d676a0679d73eb9fa93053ef47aa0ea (diff) | |
download | libpinyin-473d80f6681f0fe7c20bc2008c249563a0f99eee.tar.gz libpinyin-473d80f6681f0fe7c20bc2008c249563a0f99eee.tar.xz libpinyin-473d80f6681f0fe7c20bc2008c249563a0f99eee.zip |
write compute_pronunciation_possibility function
Diffstat (limited to 'src')
-rw-r--r-- | src/storage/phonetic_key_matrix.cpp | 74 | ||||
-rw-r--r-- | src/storage/phonetic_key_matrix.h | 7 |
2 files changed, 78 insertions, 3 deletions
diff --git a/src/storage/phonetic_key_matrix.cpp b/src/storage/phonetic_key_matrix.cpp index 93a69e7..9ff35e2 100644 --- a/src/storage/phonetic_key_matrix.cpp +++ b/src/storage/phonetic_key_matrix.cpp @@ -20,9 +20,9 @@ */ #include "phonetic_key_matrix.h" -#include "pinyin_custom2.h" #include <assert.h> #include <stdio.h> +#include "pinyin_custom2.h" namespace pinyin{ @@ -222,13 +222,13 @@ int search_matrix_recur(GArray * cached_keys, const size_t size = matrix->get_column_size(start); /* assume pinyin parsers will filter invalid keys. */ - assert(0 != size); + assert(size > 0); for (size_t i = 0; i < size; ++i) { ChewingKey key; ChewingKeyRest key_rest; matrix->get_item(start, i, key, key_rest); - size_t newstart = key_rest.m_raw_end; + const size_t newstart = key_rest.m_raw_end; const ChewingKey zero_key; if (zero_key == key) { @@ -282,4 +282,72 @@ int search_matrix(FacadeChewingTable2 * table, return result; } +gfloat compute_pronunciation_possibility_recur(pinyin_option_t options, + PhoneticKeyMatrix * matrix, + size_t start, size_t end, + GArray * cached_keys, + PhraseItem & item){ + if (start > end) + return 0.; + + const size_t phrase_length = item.get_phrase_length(); + if (phrase_length < cached_keys->len) + return 0.; + + /* only do compute with 'start' and 'end'. */ + if (start == end) { + if (phrase_length != cached_keys->len) + return 0.; + + return item.get_pronunciation_possibility + (options, (ChewingKey *)cached_keys->data); + } + + gfloat result = 0.; + + 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 compute_pronunciation_possibility_recur + (options, matrix, newstart, end, cached_keys, item); + } + + /* push value */ + g_array_append_val(cached_keys, key); + + result += compute_pronunciation_possibility_recur + (options, matrix, newstart, end, cached_keys, item); + + /* pop value */ + g_array_set_size(cached_keys, cached_keys->len - 1); + } + + return result; +} + +gfloat compute_pronunciation_possibility(pinyin_option_t options, + PhoneticKeyMatrix * matrix, + size_t start, size_t end, + GArray * cached_keys, + PhraseItem & item){ + assert(end < matrix->size()); + + assert(matrix->get_column_size(start) > 0); + assert(matrix->get_column_size(end) > 0); + + return compute_pronunciation_possibility_recur + (options, matrix, start, end, cached_keys, item); +} + }; diff --git a/src/storage/phonetic_key_matrix.h b/src/storage/phonetic_key_matrix.h index 3d37829..c88b378 100644 --- a/src/storage/phonetic_key_matrix.h +++ b/src/storage/phonetic_key_matrix.h @@ -26,6 +26,7 @@ #include "novel_types.h" #include "chewing_key.h" #include "facade_chewing_table2.h" +#include "phrase_index.h" namespace pinyin { @@ -210,6 +211,12 @@ int search_matrix(FacadeChewingTable2 * table, PhoneticKeyMatrix * matrix, size_t start, size_t end, PhraseIndexRanges ranges); + +gfloat compute_pronunciation_possibility(pinyin_option_t options, + PhoneticKeyMatrix * matrix, + size_t start, size_t end, + GArray * cached_keys, + PhraseItem & item); }; #endif |