Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1178)

Unified Diff: third_party/android_prediction/suggest/core/dictionary/dictionary.cpp

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: format README and CHROMIUM.diff Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/android_prediction/suggest/core/dictionary/dictionary.cpp
diff --git a/third_party/android_prediction/suggest/core/dictionary/dictionary.cpp b/third_party/android_prediction/suggest/core/dictionary/dictionary.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..92b2909c05c3df437951ba88e91d3e1b9a7cc313
--- /dev/null
+++ b/third_party/android_prediction/suggest/core/dictionary/dictionary.cpp
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2009, 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.
+ */
+
+#define LOG_TAG "LatinIME: dictionary.cpp"
+
+#include "third_party/android_prediction/suggest/core/dictionary/dictionary.h"
+
+#include "third_party/android_prediction/defines.h"
+#include "third_party/android_prediction/suggest/core/dictionary/dictionary_utils.h"
+#include "third_party/android_prediction/suggest/core/policy/dictionary_header_structure_policy.h"
+#include "third_party/android_prediction/suggest/core/result/suggestion_results.h"
+#include "third_party/android_prediction/suggest/core/session/dic_traverse_session.h"
+#include "third_party/android_prediction/suggest/core/session/prev_words_info.h"
+#include "third_party/android_prediction/suggest/core/suggest.h"
+#include "third_party/android_prediction/suggest/core/suggest_options.h"
+#include "third_party/android_prediction/suggest/policyimpl/gesture/gesture_suggest_policy_factory.h"
+#include "third_party/android_prediction/suggest/policyimpl/typing/typing_suggest_policy_factory.h"
+#include "third_party/android_prediction/utils/time_keeper.h"
+
+namespace latinime {
+
+const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32;
+
+Dictionary::Dictionary(DictionaryStructureWithBufferPolicy::StructurePolicyPtr
+ dictionaryStructureWithBufferPolicy)
+ : mDictionaryStructureWithBufferPolicy(std::move(dictionaryStructureWithBufferPolicy)),
+ mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
+ mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
+}
+
+void Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
+ int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
+ int inputSize, const PrevWordsInfo *const prevWordsInfo,
+ const SuggestOptions *const suggestOptions, const float languageWeight,
+ SuggestionResults *const outSuggestionResults) const {
+ TimeKeeper::setCurrentTime();
+ traverseSession->init(this, prevWordsInfo, suggestOptions);
+ const auto &suggest = suggestOptions->isGesture() ? mGestureSuggest : mTypingSuggest;
+ suggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
+ ycoordinates, times, pointerIds, inputCodePoints, inputSize,
+ languageWeight, outSuggestionResults);
+ if (DEBUG_DICT) {
+ outSuggestionResults->dumpSuggestions();
+ }
+}
+
+Dictionary::NgramListenerForPrediction::NgramListenerForPrediction(
+ const PrevWordsInfo *const prevWordsInfo, SuggestionResults *const suggestionResults,
+ const DictionaryStructureWithBufferPolicy *const dictStructurePolicy)
+ : mPrevWordsInfo(prevWordsInfo), mSuggestionResults(suggestionResults),
+ mDictStructurePolicy(dictStructurePolicy) {}
+
+void Dictionary::NgramListenerForPrediction::onVisitEntry(const int ngramProbability,
+ const int targetPtNodePos) {
+ if (targetPtNodePos == NOT_A_DICT_POS) {
+ return;
+ }
+ if (mPrevWordsInfo->isNthPrevWordBeginningOfSentence(1 /* n */)
+ && ngramProbability == NOT_A_PROBABILITY) {
+ return;
+ }
+ int targetWordCodePoints[MAX_WORD_LENGTH];
+ int unigramProbability = 0;
+ const int codePointCount = mDictStructurePolicy->
+ getCodePointsAndProbabilityAndReturnCodePointCount(targetPtNodePos,
+ MAX_WORD_LENGTH, targetWordCodePoints, &unigramProbability);
+ if (codePointCount <= 0) {
+ return;
+ }
+ const int probability = mDictStructurePolicy->getProbability(
+ unigramProbability, ngramProbability);
+ mSuggestionResults->addPrediction(targetWordCodePoints, codePointCount, probability);
+}
+
+void Dictionary::getPredictions(const PrevWordsInfo *const prevWordsInfo,
+ SuggestionResults *const outSuggestionResults) const {
+ TimeKeeper::setCurrentTime();
+ NgramListenerForPrediction listener(prevWordsInfo, outSuggestionResults,
+ mDictionaryStructureWithBufferPolicy.get());
+ int prevWordsPtNodePos[MAX_PREV_WORD_COUNT_FOR_N_GRAM];
+ prevWordsInfo->getPrevWordsTerminalPtNodePos(
+ mDictionaryStructureWithBufferPolicy.get(), prevWordsPtNodePos,
+ true /* tryLowerCaseSearch */);
+ mDictionaryStructureWithBufferPolicy->iterateNgramEntries(prevWordsPtNodePos, &listener);
+}
+
+int Dictionary::getProbability(const int *word, int length) const {
+ return getNgramProbability(nullptr /* prevWordsInfo */, word, length);
+}
+
+int Dictionary::getMaxProbabilityOfExactMatches(const int *word, int length) const {
+ TimeKeeper::setCurrentTime();
+ return DictionaryUtils::getMaxProbabilityOfExactMatches(
+ mDictionaryStructureWithBufferPolicy.get(), word, length);
+}
+
+int Dictionary::getNgramProbability(const PrevWordsInfo *const prevWordsInfo, const int *word,
+ int length) const {
+ TimeKeeper::setCurrentTime();
+ int nextWordPos = mDictionaryStructureWithBufferPolicy->getTerminalPtNodePositionOfWord(word,
+ length, false /* forceLowerCaseSearch */);
+ if (NOT_A_DICT_POS == nextWordPos) return NOT_A_PROBABILITY;
+ if (!prevWordsInfo) {
+ return getDictionaryStructurePolicy()->getProbabilityOfPtNode(
+ nullptr /* prevWordsPtNodePos */, nextWordPos);
+ }
+ int prevWordsPtNodePos[MAX_PREV_WORD_COUNT_FOR_N_GRAM];
+ prevWordsInfo->getPrevWordsTerminalPtNodePos(
+ mDictionaryStructureWithBufferPolicy.get(), prevWordsPtNodePos,
+ true /* tryLowerCaseSearch */);
+ return getDictionaryStructurePolicy()->getProbabilityOfPtNode(prevWordsPtNodePos, nextWordPos);
+}
+
+bool Dictionary::addUnigramEntry(const int *const word, const int length,
+ const UnigramProperty *const unigramProperty) {
+ if (unigramProperty->representsBeginningOfSentence()
+ && !mDictionaryStructureWithBufferPolicy->getHeaderStructurePolicy()
+ ->supportsBeginningOfSentence()) {
+ AKLOGE("The dictionary doesn't support Beginning-of-Sentence.");
+ return false;
+ }
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->addUnigramEntry(word, length, unigramProperty);
+}
+
+bool Dictionary::removeUnigramEntry(const int *const codePoints, const int codePointCount) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->removeUnigramEntry(codePoints, codePointCount);
+}
+
+bool Dictionary::addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
+ const BigramProperty *const bigramProperty) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->addNgramEntry(prevWordsInfo, bigramProperty);
+}
+
+bool Dictionary::removeNgramEntry(const PrevWordsInfo *const prevWordsInfo,
+ const int *const word, const int length) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->removeNgramEntry(prevWordsInfo, word, length);
+}
+
+bool Dictionary::flush(const char *const filePath) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->flush(filePath);
+}
+
+bool Dictionary::flushWithGC(const char *const filePath) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->flushWithGC(filePath);
+}
+
+bool Dictionary::needsToRunGC(const bool mindsBlockByGC) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->needsToRunGC(mindsBlockByGC);
+}
+
+void Dictionary::getProperty(const char *const query, const int queryLength, char *const outResult,
+ const int maxResultLength) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->getProperty(query, queryLength, outResult,
+ maxResultLength);
+}
+
+const WordProperty Dictionary::getWordProperty(const int *const codePoints,
+ const int codePointCount) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->getWordProperty(
+ codePoints, codePointCount);
+}
+
+int Dictionary::getNextWordAndNextToken(const int token, int *const outCodePoints,
+ int *const outCodePointCount) {
+ TimeKeeper::setCurrentTime();
+ return mDictionaryStructureWithBufferPolicy->getNextWordAndNextToken(
+ token, outCodePoints, outCodePointCount);
+}
+
+} // namespace latinime

Powered by Google App Engine
This is Rietveld 408576698