Loading native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.cpp +19 −18 Original line number Diff line number Diff line Loading @@ -310,9 +310,13 @@ bool Ver4PatriciaTriePolicy::addNgramEntry(const PrevWordsInfo *const prevWordsI if (prevWordIds.empty()) { return false; } // TODO: Support N-gram. if (prevWordIds[0] == NOT_A_WORD_ID) { if (prevWordsInfo->isNthPrevWordBeginningOfSentence(1 /* n */)) { for (size_t i = 0; i < prevWordIds.size(); ++i) { if (prevWordIds[i] != NOT_A_WORD_ID) { continue; } if (!prevWordsInfo->isNthPrevWordBeginningOfSentence(i + 1 /* n */)) { return false; } const std::vector<UnigramProperty::ShortcutProperty> shortcuts; const UnigramProperty beginningOfSentenceUnigramProperty( true /* representsBeginningOfSentence */, true /* isNotAWord */, Loading @@ -325,15 +329,13 @@ bool Ver4PatriciaTriePolicy::addNgramEntry(const PrevWordsInfo *const prevWordsI } // Refresh word ids. prevWordsInfo->getPrevWordIds(this, &prevWordIdArray, false /* tryLowerCaseSearch */); } else { return false; } } const int wordId = getWordId(CodePointArrayView(*bigramProperty->getTargetCodePoints()), false /* forceLowerCaseSearch */); if (wordId == NOT_A_WORD_ID) { return false; } // TODO: Support N-gram. bool addedNewEntry = false; WordIdArray<MAX_PREV_WORD_COUNT_FOR_N_GRAM> prevWordsPtNodePos; for (size_t i = 0; i < prevWordsPtNodePos.size(); ++i) { Loading Loading @@ -375,8 +377,7 @@ bool Ver4PatriciaTriePolicy::removeNgramEntry(const PrevWordsInfo *const prevWor WordIdArray<MAX_PREV_WORD_COUNT_FOR_N_GRAM> prevWordIdArray; const WordIdArrayView prevWordIds = prevWordsInfo->getPrevWordIds(this, &prevWordIdArray, false /* tryLowerCaseSerch */); // TODO: Support N-gram. if (prevWordIds.empty() || prevWordIds[0] == NOT_A_WORD_ID) { if (prevWordIds.empty() || prevWordIds.contains(NOT_A_WORD_ID)) { return false; } const int wordId = getWordId(wordCodePoints, false /* forceLowerCaseSearch */); Loading native/jni/src/utils/int_array_view.h +7 −2 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ #ifndef LATINIME_INT_ARRAY_VIEW_H #define LATINIME_INT_ARRAY_VIEW_H #include <algorithm> #include <array> #include <cstdint> #include <cstring> Loading Loading @@ -92,12 +93,16 @@ class IntArrayView { return mPtr + mSize; } AK_FORCE_INLINE bool contains(const int value) const { return std::find(begin(), end(), value) != end(); } // Returns the view whose size is smaller than or equal to the given count. const IntArrayView limit(const size_t maxSize) const { AK_FORCE_INLINE const IntArrayView limit(const size_t maxSize) const { return IntArrayView(mPtr, std::min(maxSize, mSize)); } const IntArrayView skip(const size_t n) const { AK_FORCE_INLINE const IntArrayView skip(const size_t n) const { if (mSize <= n) { return IntArrayView(); } Loading native/jni/tests/utils/int_array_view_test.cpp +13 −0 Original line number Diff line number Diff line Loading @@ -58,6 +58,19 @@ TEST(IntArrayViewTest, TestConstructFromObject) { EXPECT_EQ(object, intArrayView[0]); } TEST(IntArrayViewTest, TestContains) { EXPECT_FALSE(IntArrayView().contains(0)); EXPECT_FALSE(IntArrayView().contains(1)); const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; IntArrayView intArrayView(intVector); EXPECT_TRUE(intArrayView.contains(0)); EXPECT_TRUE(intArrayView.contains(3)); EXPECT_TRUE(intArrayView.contains(-2)); EXPECT_FALSE(intArrayView.contains(-3)); EXPECT_FALSE(intArrayView.limit(0).contains(3)); } TEST(IntArrayViewTest, TestLimit) { const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; IntArrayView intArrayView(intVector); Loading Loading
native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.cpp +19 −18 Original line number Diff line number Diff line Loading @@ -310,9 +310,13 @@ bool Ver4PatriciaTriePolicy::addNgramEntry(const PrevWordsInfo *const prevWordsI if (prevWordIds.empty()) { return false; } // TODO: Support N-gram. if (prevWordIds[0] == NOT_A_WORD_ID) { if (prevWordsInfo->isNthPrevWordBeginningOfSentence(1 /* n */)) { for (size_t i = 0; i < prevWordIds.size(); ++i) { if (prevWordIds[i] != NOT_A_WORD_ID) { continue; } if (!prevWordsInfo->isNthPrevWordBeginningOfSentence(i + 1 /* n */)) { return false; } const std::vector<UnigramProperty::ShortcutProperty> shortcuts; const UnigramProperty beginningOfSentenceUnigramProperty( true /* representsBeginningOfSentence */, true /* isNotAWord */, Loading @@ -325,15 +329,13 @@ bool Ver4PatriciaTriePolicy::addNgramEntry(const PrevWordsInfo *const prevWordsI } // Refresh word ids. prevWordsInfo->getPrevWordIds(this, &prevWordIdArray, false /* tryLowerCaseSearch */); } else { return false; } } const int wordId = getWordId(CodePointArrayView(*bigramProperty->getTargetCodePoints()), false /* forceLowerCaseSearch */); if (wordId == NOT_A_WORD_ID) { return false; } // TODO: Support N-gram. bool addedNewEntry = false; WordIdArray<MAX_PREV_WORD_COUNT_FOR_N_GRAM> prevWordsPtNodePos; for (size_t i = 0; i < prevWordsPtNodePos.size(); ++i) { Loading Loading @@ -375,8 +377,7 @@ bool Ver4PatriciaTriePolicy::removeNgramEntry(const PrevWordsInfo *const prevWor WordIdArray<MAX_PREV_WORD_COUNT_FOR_N_GRAM> prevWordIdArray; const WordIdArrayView prevWordIds = prevWordsInfo->getPrevWordIds(this, &prevWordIdArray, false /* tryLowerCaseSerch */); // TODO: Support N-gram. if (prevWordIds.empty() || prevWordIds[0] == NOT_A_WORD_ID) { if (prevWordIds.empty() || prevWordIds.contains(NOT_A_WORD_ID)) { return false; } const int wordId = getWordId(wordCodePoints, false /* forceLowerCaseSearch */); Loading
native/jni/src/utils/int_array_view.h +7 −2 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ #ifndef LATINIME_INT_ARRAY_VIEW_H #define LATINIME_INT_ARRAY_VIEW_H #include <algorithm> #include <array> #include <cstdint> #include <cstring> Loading Loading @@ -92,12 +93,16 @@ class IntArrayView { return mPtr + mSize; } AK_FORCE_INLINE bool contains(const int value) const { return std::find(begin(), end(), value) != end(); } // Returns the view whose size is smaller than or equal to the given count. const IntArrayView limit(const size_t maxSize) const { AK_FORCE_INLINE const IntArrayView limit(const size_t maxSize) const { return IntArrayView(mPtr, std::min(maxSize, mSize)); } const IntArrayView skip(const size_t n) const { AK_FORCE_INLINE const IntArrayView skip(const size_t n) const { if (mSize <= n) { return IntArrayView(); } Loading
native/jni/tests/utils/int_array_view_test.cpp +13 −0 Original line number Diff line number Diff line Loading @@ -58,6 +58,19 @@ TEST(IntArrayViewTest, TestConstructFromObject) { EXPECT_EQ(object, intArrayView[0]); } TEST(IntArrayViewTest, TestContains) { EXPECT_FALSE(IntArrayView().contains(0)); EXPECT_FALSE(IntArrayView().contains(1)); const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; IntArrayView intArrayView(intVector); EXPECT_TRUE(intArrayView.contains(0)); EXPECT_TRUE(intArrayView.contains(3)); EXPECT_TRUE(intArrayView.contains(-2)); EXPECT_FALSE(intArrayView.contains(-3)); EXPECT_FALSE(intArrayView.limit(0).contains(3)); } TEST(IntArrayViewTest, TestLimit) { const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; IntArrayView intArrayView(intVector); Loading