Merge "Implement ProbabilityDictContent."

This commit is contained in:
Keisuke Kuroyanagi 2013-10-29 04:47:02 +00:00 committed by Android (Google) Code Review
commit 7ee5e9364a
8 changed files with 146 additions and 5 deletions

View file

@ -87,7 +87,8 @@ LATIN_IME_CORE_SRC_FILES := \
dynamic_patricia_trie_writing_utils.cpp) \ dynamic_patricia_trie_writing_utils.cpp) \
$(addprefix suggest/policyimpl/dictionary/structure/v4/, \ $(addprefix suggest/policyimpl/dictionary/structure/v4/, \
ver4_dict_constants.cpp \ ver4_dict_constants.cpp \
ver4_patricia_trie_policy.cpp) \ ver4_patricia_trie_policy.cpp \
ver4_patricia_trie_reading_utils.cpp ) \
$(addprefix suggest/policyimpl/dictionary/utils/, \ $(addprefix suggest/policyimpl/dictionary/utils/, \
buffer_with_extendable_buffer.cpp \ buffer_with_extendable_buffer.cpp \
byte_array_utils.cpp \ byte_array_utils.cpp \

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 2013, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LATINIME_PROBABILITY_DICT_CONTENT_H
#define LATINIME_PROBABILITY_DICT_CONTENT_H
#include "defines.h"
#include "suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_reading_utils.h"
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
namespace latinime {
class ProbabilityDictContent : public SingleDictContent {
public:
ProbabilityDictContent(const char *const dictDirPath, const bool isUpdatable)
: SingleDictContent(dictDirPath, Ver4DictConstants::FREQ_FILE_EXTENSION,
isUpdatable) {}
int getProbability(const int terminalId) const {
if (terminalId < 0 || terminalId >= getSize()) {
return NOT_A_PROBABILITY;
}
return Ver4PatriciaTrieReadingUtils::getProbability(getBuffer(), terminalId);
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ProbabilityDictContent);
int getSize() const {
return getBuffer()->getTailPosition() / (Ver4DictConstants::PROBABILITY_SIZE
+ Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE);
}
};
} // namespace latinime
#endif /* LATINIME_PROBABILITY_DICT_CONTENT_H */

View file

@ -39,6 +39,15 @@ class SingleDictContent : public DictContent {
return mMmappedBuffer.get() != 0; return mMmappedBuffer.get() != 0;
} }
protected:
BufferWithExtendableBuffer *getWritableBuffer() {
return &mExpandableContentBuffer;
}
const BufferWithExtendableBuffer *getBuffer() const {
return &mExpandableContentBuffer;
}
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(SingleDictContent); DISALLOW_IMPLICIT_CONSTRUCTORS(SingleDictContent);

View file

@ -18,6 +18,7 @@
#define LATINIME_VER4_DICT_BUFFER_H #define LATINIME_VER4_DICT_BUFFER_H
#include "defines.h" #include "defines.h"
#include "suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.h"
#include "suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h" #include "suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h"
#include "suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.h" #include "suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
@ -42,10 +43,18 @@ class Ver4DictBuffers {
&& mShortcutDictContent.isValid(); && mShortcutDictContent.isValid();
} }
AK_FORCE_INLINE const uint8_t *getRawDictBuffer() const { AK_FORCE_INLINE uint8_t *getRawDictBuffer() const {
return mDictBuffer.get()->getBuffer(); return mDictBuffer.get()->getBuffer();
} }
AK_FORCE_INLINE int getRawDictBufferSize() const {
return mDictBuffer.get()->getBufferSize();
}
AK_FORCE_INLINE const ProbabilityDictContent *getProbabilityDictContent() const {
return &mProbabilityDictContent;
}
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4DictBuffers); DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4DictBuffers);
@ -54,8 +63,7 @@ class Ver4DictBuffers {
: mDictBuffer(dictBuffer), : mDictBuffer(dictBuffer),
mTerminalAddressTable(dictDirPath, mTerminalAddressTable(dictDirPath,
Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION, isUpdatable), Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION, isUpdatable),
mProbabilityDictContent(dictDirPath, Ver4DictConstants::FREQ_FILE_EXTENSION, mProbabilityDictContent(dictDirPath, isUpdatable),
isUpdatable),
mBigramDictContent(dictDirPath, mBigramDictContent(dictDirPath,
Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION,
Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION, Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION,
@ -67,7 +75,7 @@ class Ver4DictBuffers {
const MmappedBuffer::MmappedBufferPtr mDictBuffer; const MmappedBuffer::MmappedBufferPtr mDictBuffer;
SingleDictContent mTerminalAddressTable; SingleDictContent mTerminalAddressTable;
SingleDictContent mProbabilityDictContent; ProbabilityDictContent mProbabilityDictContent;
SparseTableDictContent mBigramDictContent; SparseTableDictContent mBigramDictContent;
SparseTableDictContent mShortcutDictContent; SparseTableDictContent mShortcutDictContent;
}; };

View file

@ -31,5 +31,7 @@ const char *const Ver4DictConstants::SHORTCUT_CONTENT_TABLE_FILE_EXTENSION =
".shortcut_index_shortcut"; ".shortcut_index_shortcut";
const int Ver4DictConstants::NOT_A_TERMINAL = -1; const int Ver4DictConstants::NOT_A_TERMINAL = -1;
const int Ver4DictConstants::PROBABILITY_SIZE = 1;
const int Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE = 1;
} // namespace latinime } // namespace latinime

View file

@ -35,6 +35,8 @@ class Ver4DictConstants {
static const char *const SHORTCUT_CONTENT_TABLE_FILE_EXTENSION; static const char *const SHORTCUT_CONTENT_TABLE_FILE_EXTENSION;
static const int NOT_A_TERMINAL; static const int NOT_A_TERMINAL;
static const int PROBABILITY_SIZE;
static const int FLAGS_IN_PROBABILITY_FILE_SIZE;
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4DictConstants); DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4DictConstants);

View file

@ -0,0 +1,32 @@
/*
* Copyright (C) 2013, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_reading_utils.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
namespace latinime {
/* static */ int Ver4PatriciaTrieReadingUtils::getProbability(
const BufferWithExtendableBuffer *const probabilityBuffer, const int terminalId) {
int pos = terminalId * (Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE
+ Ver4DictConstants::PROBABILITY_SIZE)
+ Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE;
return probabilityBuffer->readUintAndAdvancePosition(Ver4DictConstants::PROBABILITY_SIZE, &pos);
}
} // namespace latinime

View file

@ -0,0 +1,37 @@
/*
* Copyright (C) 2013, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LATINIME_VER4_PATRICIA_TRIE_READING_UTILS_H
#define LATINIME_VER4_PATRICIA_TRIE_READING_UTILS_H
#include <stdint.h>
#include "defines.h"
namespace latinime {
class BufferWithExtendableBuffer;
class Ver4PatriciaTrieReadingUtils {
public:
static int getProbability(const BufferWithExtendableBuffer *const probabilityBuffer,
const int terminalId);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTrieReadingUtils);
};
} // namespace latinime
#endif /* LATINIME_VER4_PATRICIA_TRIE_READING_UTILS_H */