aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKeisuke Kuroynagi <ksk@google.com>2013-08-06 20:06:18 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2013-08-06 20:06:18 -0700
commit035a4bd679a57292de8e5f217aaa49c626b6fae7 (patch)
treee41551833a88a7c3e0c4aab5081af900846ad309
parentf7959a3af5de7f187326a32ebadd48110d1771f4 (diff)
parent0f75be4a804cc167dcfe3720997547b71b6791cf (diff)
downloadlatinime-035a4bd679a57292de8e5f217aaa49c626b6fae7.tar.gz
latinime-035a4bd679a57292de8e5f217aaa49c626b6fae7.tar.xz
latinime-035a4bd679a57292de8e5f217aaa49c626b6fae7.zip
am 0f75be4a: Merge "Implement getCodePointsAndProbabilityAnd... for ver3 dicts."
* commit '0f75be4a804cc167dcfe3720997547b71b6791cf': Implement getCodePointsAndProbabilityAnd... for ver3 dicts.
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp43
1 files changed, 40 insertions, 3 deletions
diff --git a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp
index 17cbdde3a..9a180e6f7 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "suggest/policyimpl/dictionary/patricia_trie_policy.h"
+#include "suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h"
#include "defines.h"
#include "suggest/core/dicnode/dic_node.h"
@@ -62,8 +62,45 @@ int DynamicPatriciaTriePolicy::getCodePointsAndProbabilityAndReturnCodePointCoun
const BinaryDictionaryInfo *const binaryDictionaryInfo,
const int nodePos, const int maxCodePointCount, int *const outCodePoints,
int *const outUnigramProbability) const {
- // TODO: Implement.
- return 0;
+ // This method traverses parent nodes from the terminal by following parent pointers; thus,
+ // node code points are stored in the buffer in the reverse order.
+ int reverseCodePoints[maxCodePointCount];
+ int mergedNodeCodePoints[maxCodePointCount];
+ int codePointCount = 0;
+
+ DynamicPatriciaTrieNodeReader nodeReader(binaryDictionaryInfo);
+ // First, read terminal node and get its probability.
+ nodeReader.fetchNodeInfoFromBufferAndGetNodeCodePoints(nodePos, maxCodePointCount,
+ mergedNodeCodePoints);
+ // Store terminal node probability.
+ *outUnigramProbability = nodeReader.getProbability();
+ // Store terminal node code points to buffer in the reverse order.
+ for (int i = nodeReader.getCodePointCount() - 1; i >= 0; --i) {
+ reverseCodePoints[codePointCount++] = mergedNodeCodePoints[i];
+ }
+ // Then, follow parent pos toward the root node.
+ while (nodeReader.getParentPos() != getRootPosition()) {
+ // codePointCount must be incremented at least once in each iteration to ensure preventing
+ // infinite loop.
+ if (nodeReader.isDeleted() || codePointCount > maxCodePointCount
+ || nodeReader.getCodePointCount() <= 0) {
+ // The nodePos is not a valid terminal node position in the dictionary.
+ *outUnigramProbability = NOT_A_PROBABILITY;
+ return 0;
+ }
+ // Read parent node.
+ nodeReader.fetchNodeInfoFromBufferAndGetNodeCodePoints(nodeReader.getParentPos(),
+ maxCodePointCount, mergedNodeCodePoints);
+ // Store node code points to buffer in the reverse order.
+ for (int i = nodeReader.getCodePointCount() - 1; i >= 0; --i) {
+ reverseCodePoints[codePointCount++] = mergedNodeCodePoints[i];
+ }
+ }
+ // Reverse the stored code points to output them.
+ for (int i = 0; i < codePointCount; ++i) {
+ outCodePoints[i] = reverseCodePoints[codePointCount - i - 1];
+ }
+ return codePointCount;
}
int DynamicPatriciaTriePolicy::getTerminalNodePositionOfWord(