diff --git a/Source/Engine/McBopomofoLM.cpp b/Source/Engine/McBopomofoLM.cpp index c7b51c49..9c73bcef 100644 --- a/Source/Engine/McBopomofoLM.cpp +++ b/Source/Engine/McBopomofoLM.cpp @@ -131,7 +131,7 @@ bool McBopomofoLM::externalConverterEnabled() return m_externalConverterEnabled; } -void McBopomofoLM::setExternalConvrter(std::function externalConverter) +void McBopomofoLM::setExternalConverter(std::function externalConverter) { m_externalConverter = externalConverter; } diff --git a/Source/Engine/McBopomofoLM.h b/Source/Engine/McBopomofoLM.h index 3b8a5109..de90a4a3 100644 --- a/Source/Engine/McBopomofoLM.h +++ b/Source/Engine/McBopomofoLM.h @@ -34,27 +34,74 @@ namespace McBopomofo { using namespace Formosa::Gramambular; +/// McBopomofoLM is a facade for managing a set of models including +/// the input method language model, user phrases and excluded phrases. +/// +/// It is the primary model class that the input controller and grammer builder +/// of McBopomofo talk to. When the grammer builder starts to build a sentense +/// from a series of BPMF readings, it passes the readings to the model to see +/// if there are valid unigrams, and use returned unigrams to produce the final +/// results. +/// +/// McBopomofoLM combine and transform the unigrams from the primary language +/// model and user phrases. The process is +/// +/// 1) Get the original unigrams. +/// 2) Drop the unigrams whose value is contained in the exclusion map. +/// 3) Replace the values of the unigrams using the phrase replacement map. +/// 4) Replace the values of the unigrams using an external converter lambda. +/// 5) Drop the duplicated phrases. +/// +/// The controller can ask the model to load the primary input method language +/// model while launching and to load the user phrases anytime if the custom +/// files are modified. It does not keep the reference of the data pathes but +/// you have to pass the paths when you ask it to do loading. class McBopomofoLM : public LanguageModel { public: McBopomofoLM(); ~McBopomofoLM(); + /// Asks to load the primary language model a the given path. + /// @param languageModelPath Thw path of the language model. void loadLanguageModel(const char* languageModelPath); + /// Asks to load the user phrases and excluded phrases at the given path. + /// @param userPhrasesPath The path of user phrases. + /// @param excludedPhrasesPath The path of excluded phrases. void loadUserPhrases(const char* userPhrasesPath, const char* excludedPhrasesPath); + /// Asks to load th phrase replacement table at the given path. + /// @param phraseReplacementPath The path of the phrase replacement table. void loadPhraseReplacementMap(const char* phraseReplacementPath); + /// Not implemented since we do not have data to provide bigram function. const vector bigramsForKeys(const string& preceedingKey, const string& key); + /// Returns a list of available unigram for the given key. + /// @param key A string represents the BPMF reading or a symbol key. For + /// example, it you pass "ㄇㄚ", it returns "嗎", "媽", and so on. const vector unigramsForKey(const string& key); + /// If the model has unigrams for the given key. + /// @param key The key. bool hasUnigramsForKey(const string& key); + /// Enables or disables phrase replacement. void setPhraseReplacementEnabled(bool enabled); + /// If phrease replacement is enabled or not. bool phraseReplacementEnabled(); + /// Enables or disables the external converter. void setExternalConverterEnabled(bool enabled); + /// If the external converted is enabled or not. bool externalConverterEnabled(); - void setExternalConvrter(std::function externalConverter); + /// Sets a lambda to let the values of unigrams could be converted by it. + void setExternalConverter(std::function externalConverter); protected: + /// Filters and converts the input unigrams and return a new list of unigrams. + /// + /// @param unigrams The unigrams to be processed. + /// @param excludedValues The values to excluded unigrams. + /// @param insertedValues The values for unigrams already in the results. + /// It helps to prevent duplicated unigrams. Please note that the method + /// has a side effect that it inserts values to `insertedValues`. const vector filterAndTransformUnigrams(const vector unigrams, const std::unordered_set& excludedValues, std::unordered_set& insertedValues); diff --git a/Source/LanguageModelManager.mm b/Source/LanguageModelManager.mm index 37fa2897..bdf0ac9f 100644 --- a/Source/LanguageModelManager.mm +++ b/Source/LanguageModelManager.mm @@ -68,8 +68,8 @@ static void LTLoadLanguageModelFile(NSString *filenameWithoutExtension, McBopomo return string(text.UTF8String); }; - gLanguageModelMcBopomofo.setExternalConvrter(converter); - gLanguageModelPlainBopomofo.setExternalConvrter(converter); + gLanguageModelMcBopomofo.setExternalConverter(converter); + gLanguageModelPlainBopomofo.setExternalConverter(converter); } + (BOOL)checkIfUserDataFolderExists