Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 95fe8267 authored by Satoshi Kataoka's avatar Satoshi Kataoka
Browse files

Move suggest logic to AOSP

Bug: 8197301

Change-Id: I2d0ffbbc9d4d57ebfc2fe43e6cb75e8b44dae466
parent 30667266
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -29,9 +29,8 @@ LATIN_IME_SRC_FULLPATH_DIR := $(LOCAL_PATH)/$(LATIN_IME_SRC_DIR)
LOCAL_C_INCLUDES += \
    $(LATIN_IME_SRC_FULLPATH_DIR) \
    $(LATIN_IME_SRC_FULLPATH_DIR)/suggest \
    $(LATIN_IME_SRC_FULLPATH_DIR)/suggest/core/dicnode \
    $(LATIN_IME_SRC_FULLPATH_DIR)/suggest/core/policy \
    $(LATIN_IME_SRC_FULLPATH_DIR)/suggest/core/session \
    $(LATIN_IME_SRC_FULLPATH_DIR)/suggest/core \
    $(addprefix $(LATIN_IME_SRC_FULLPATH_DIR)/suggest/core/, dicnode dictionary policy session) \
    $(LATIN_IME_SRC_FULLPATH_DIR)/suggest/policyimpl/typing

LOCAL_CFLAGS += -Werror -Wall -Wextra -Weffc++ -Wformat=2 -Wcast-qual -Wcast-align \
@@ -70,8 +69,10 @@ LATIN_IME_CORE_SRC_FILES := \
    suggest/core/dicnode/dic_node_utils.cpp \
    suggest/core/policy/weighting.cpp \
    suggest/core/session/dic_traverse_session.cpp \
    suggest/core/suggest.cpp \
    suggest/policyimpl/typing/scoring_params.cpp \
    suggest/policyimpl/typing/typing_scoring.cpp \
    suggest/policyimpl/typing/typing_suggest_policy.cpp \
    suggest/policyimpl/typing/typing_traversal.cpp \
    suggest/policyimpl/typing/typing_weighting.cpp \
    suggest/gesture_suggest.cpp \
+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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_SHORTCUT_UTILS
#define LATINIME_SHORTCUT_UTILS

#include "defines.h"
#include "dic_node_utils.h"
#include "terminal_attributes.h"

namespace latinime {

class ShortcutUtils {
 public:
    static int outputShortcuts(const TerminalAttributes *const terminalAttributes,
            int outputWordIndex, const int finalScore, int *const outputCodePoints,
            int *const frequencies, int *const outputTypes, const bool sameAsTyped) {
        TerminalAttributes::ShortcutIterator iterator = terminalAttributes->getShortcutIterator();
        while (iterator.hasNextShortcutTarget() && outputWordIndex < MAX_RESULTS) {
            int shortcutTarget[MAX_WORD_LENGTH];
            int shortcutProbability;
            const int shortcutTargetStringLength = iterator.getNextShortcutTarget(
                    MAX_WORD_LENGTH, shortcutTarget, &shortcutProbability);
            int shortcutScore;
            int kind;
            if (shortcutProbability == BinaryFormat::WHITELIST_SHORTCUT_PROBABILITY
                    && sameAsTyped) {
                shortcutScore = S_INT_MAX;
                kind = Dictionary::KIND_WHITELIST;
            } else {
                // shortcut entry's score == its base entry's score - 1
                shortcutScore = finalScore;
                // Protection against int underflow
                shortcutScore = max(S_INT_MIN + 1, shortcutScore) - 1;
                kind = Dictionary::KIND_CORRECTION;
            }
            outputTypes[outputWordIndex] = kind;
            frequencies[outputWordIndex] = shortcutScore;
            frequencies[outputWordIndex] = max(S_INT_MIN + 1, shortcutScore) - 1;
            const int startIndex2 = outputWordIndex * MAX_WORD_LENGTH;
            DicNodeUtils::appendTwoWords(0, 0, shortcutTarget, shortcutTargetStringLength,
                    &outputCodePoints[startIndex2]);
            ++outputWordIndex;
        }
        return outputWordIndex;
    }

 private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(ShortcutUtils);
};
} // namespace latinime
#endif // LATINIME_SHORTCUT_UTILS
+518 −0

File added.

Preview size limit exceeded, changes collapsed.

+95 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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_SUGGEST_IMPL_H
#define LATINIME_SUGGEST_IMPL_H

#include "defines.h"
#include "suggest_interface.h"
#include "suggest_policy.h"

namespace latinime {

class DicNode;
class DicTraverseSession;
class ProximityInfo;
class Scoring;
class Traversal;
class Weighting;

class Suggest : public SuggestInterface {
 public:
    AK_FORCE_INLINE Suggest(const SuggestPolicy *const suggestPolicy)
            : TRAVERSAL(suggestPolicy->getTraversal()),
              SCORING(suggestPolicy->getScoring()), WEIGHTING(suggestPolicy->getWeighting()) {}
    AK_FORCE_INLINE virtual ~Suggest() {}
    int getSuggestions(ProximityInfo *pInfo, void *traverseSession, int *inputXs, int *inputYs,
            int *times, int *pointerIds, int *inputCodePoints, int inputSize, int commitPoint,
            int *outWords, int *frequencies, int *outputIndices, int *outputTypes) const;

 private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(Suggest);
    void createNextWordDicNode(DicTraverseSession *traverseSession, DicNode *dicNode,
            const bool spaceSubstitution) const;
    int outputSuggestions(DicTraverseSession *traverseSession, int *frequencies,
            int *outputCodePoints, int *outputIndices, int *outputTypes) const;
    void initializeSearch(DicTraverseSession *traverseSession, int commitPoint) const;
    void expandCurrentDicNodes(DicTraverseSession *traverseSession) const;
    void processTerminalDicNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
    void processExpandedDicNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
    void weightChildNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
    float getAutocorrectScore(DicTraverseSession *traverseSession, DicNode *dicNode) const;
    void generateFeatures(
            DicTraverseSession *traverseSession, DicNode *dicNode, float *features) const;
    void processDicNodeAsOmission(DicTraverseSession *traverseSession, DicNode *dicNode) const;
    void processDicNodeAsTransposition(DicTraverseSession *traverseSession,
            DicNode *dicNode) const;
    void processDicNodeAsInsertion(DicTraverseSession *traverseSession, DicNode *dicNode) const;
    void processDicNodeAsAdditionalProximityChar(DicTraverseSession *traverseSession,
            DicNode *dicNode, DicNode *childDicNode) const;
    void processDicNodeAsSubstitution(DicTraverseSession *traverseSession, DicNode *dicNode,
            DicNode *childDicNode) const;
    void processDicNodeAsMatch(DicTraverseSession *traverseSession,
            DicNode *childDicNode) const;

    // Dic nodes cache size for lookahead (autocompletion)
    static const int LOOKAHEAD_DIC_NODES_CACHE_SIZE;
    // Max characters to lookahead
    static const int MAX_LOOKAHEAD;
    // Inputs longer than this will autocorrect if the suggestion is multi-word
    static const int MIN_LEN_FOR_MULTI_WORD_AUTOCORRECT;
    static const int MIN_CONTINUOUS_SUGGESTION_INPUT_SIZE;
    // Base value for converting costs into scores (low so will not autocorrect without classifier)
    static const float BASE_OUTPUT_SCORE;

    // Threshold for autocorrection classifier
    static const float AUTOCORRECT_CLASSIFICATION_THRESHOLD;
    // Threshold for computing the language model feature for autocorrect classification
    static const float AUTOCORRECT_LANGUAGE_FEATURE_THRESHOLD;

    // Typing error correction settings
    static const bool CORRECT_SPACE_OMISSION;
    static const bool CORRECT_TRANSPOSITION;
    static const bool CORRECT_INSERTION;

    const Traversal *const TRAVERSAL;
    const Scoring *const SCORING;
    const Weighting *const WEIGHTING;

    static const bool CORRECT_OMISSION_G;
};
} // namespace latinime
#endif // LATINIME_SUGGEST_IMPL_H
+42 −0
Original line number Diff line number Diff line
/*
 * 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.h"
#include "typing_suggest.h"
#include "typing_suggest_policy.h"

namespace latinime {

const TypingSuggestPolicy TypingSuggestPolicy::sInstance;

// A factory method for a "typing" Suggest instance
static SuggestInterface *getTypingSuggestInstance() {
    return new Suggest(TypingSuggestPolicy::getInstance());
}

// An ad-hoc internal class to register the factory method getTypingSuggestInstance() defined above
class TypingSuggestFactoryRegisterer {
 public:
    TypingSuggestFactoryRegisterer() {
        TypingSuggest::setTypingSuggestFactoryMethod(getTypingSuggestInstance);
    }
 private:
    DISALLOW_COPY_AND_ASSIGN(TypingSuggestFactoryRegisterer);
};

// To invoke the TypingSuggestFactoryRegisterer's constructor in the global constructor
static TypingSuggestFactoryRegisterer typingSuggestFactoryregisterer;
} // namespace latinime
Loading