From 3903ac79a7aca6d913ffec7a12f566b51306e539 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 20 Feb 2022 22:18:25 +0800 Subject: [PATCH] LMInstantiator // Dealing with Namespace Pollusion. --- .../Modules/LangModelRelated/LMInstantiator.h | 34 +++++------ .../LangModelRelated/LMInstantiator.mm | 58 ++++++++++--------- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/Source/Modules/LangModelRelated/LMInstantiator.h b/Source/Modules/LangModelRelated/LMInstantiator.h index 0ee9096c..d4f49eb9 100644 --- a/Source/Modules/LangModelRelated/LMInstantiator.h +++ b/Source/Modules/LangModelRelated/LMInstantiator.h @@ -20,12 +20,12 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR TH #ifndef LMInstantiator_H #define LMInstantiator_H -#include -#include "UserPhrasesLM.h" -#include "ParselessLM.h" -#include "CNSLM.h" -#include "PhraseReplacementMap.h" #include "AssociatedPhrases.h" +#include "CNSLM.h" +#include "ParselessLM.h" +#include "PhraseReplacementMap.h" +#include "UserPhrasesLM.h" +#include #include namespace vChewing { @@ -54,7 +54,7 @@ using namespace Taiyan::Gramambular; /// 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 LMInstantiator : public LanguageModel { +class LMInstantiator : public Taiyan::Gramambular::LanguageModel { public: LMInstantiator(); ~LMInstantiator(); @@ -83,14 +83,14 @@ public: 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); + const std::vector bigramsForKeys(const std::string& preceedingKey, const std::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 + /// @param key A std::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); + const std::vector unigramsForKey(const std::string& key); /// If the model has unigrams for the given key. /// @param key The key. - bool hasUnigramsForKey(const string& key); + bool hasUnigramsForKey(const std::string& key); /// Enables or disables phrase replacement. void setPhraseReplacementEnabled(bool enabled); @@ -107,10 +107,10 @@ public: /// If the external converted is enabled or not. bool externalConverterEnabled(); /// Sets a lambda to let the values of unigrams could be converted by it. - void setExternalConverter(std::function externalConverter); + void setExternalConverter(std::function externalConverter); - const vector associatedPhrasesForKey(const string& key); - bool hasAssociatedPhrasesForKey(const string& key); + const std::vector associatedPhrasesForKey(const std::string& key); + bool hasAssociatedPhrasesForKey(const std::string& key); protected: @@ -121,9 +121,9 @@ protected: /// @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); + const std::vector filterAndTransformUnigrams(const std::vector unigrams, + const std::unordered_set& excludedValues, + std::unordered_set& insertedValues); ParselessLM m_languageModel; CNSLM m_cnsModel; @@ -134,7 +134,7 @@ protected: bool m_phraseReplacementEnabled; bool m_cnsEnabled; bool m_externalConverterEnabled; - std::function m_externalConverter; + std::function m_externalConverter; }; }; diff --git a/Source/Modules/LangModelRelated/LMInstantiator.mm b/Source/Modules/LangModelRelated/LMInstantiator.mm index 299edb64..871206d7 100644 --- a/Source/Modules/LangModelRelated/LMInstantiator.mm +++ b/Source/Modules/LangModelRelated/LMInstantiator.mm @@ -21,7 +21,7 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR TH #include #include -using namespace vChewing; +namespace vChewing { LMInstantiator::LMInstantiator() { @@ -92,49 +92,49 @@ void LMInstantiator::loadPhraseReplacementMap(const char* phraseReplacementPath) } } -const vector LMInstantiator::bigramsForKeys(const string& preceedingKey, const string& key) +const std::vector LMInstantiator::bigramsForKeys(const std::string& preceedingKey, const std::string& key) { - return vector(); + return std::vector(); } -const vector LMInstantiator::unigramsForKey(const string& key) +const std::vector LMInstantiator::unigramsForKey(const std::string& key) { if (key == " ") { - vector spaceUnigrams; - Unigram g; + std::vector spaceUnigrams; + Taiyan::Gramambular::Unigram g; g.keyValue.key = " "; - g.keyValue.value= " "; + g.keyValue.value = " "; g.score = 0; spaceUnigrams.push_back(g); return spaceUnigrams; } - vector allUnigrams; - vector userUnigrams; - vector cnsUnigrams; + std::vector allUnigrams; + std::vector userUnigrams; + std::vector cnsUnigrams; - unordered_set excludedValues; - unordered_set insertedValues; + std::unordered_set excludedValues; + std::unordered_set insertedValues; if (m_excludedPhrases.hasUnigramsForKey(key)) { - vector excludedUnigrams = m_excludedPhrases.unigramsForKey(key); + std::vector excludedUnigrams = m_excludedPhrases.unigramsForKey(key); transform(excludedUnigrams.begin(), excludedUnigrams.end(), inserter(excludedValues, excludedValues.end()), - [](const Unigram& u) { return u.keyValue.value; }); + [](const Taiyan::Gramambular::Unigram& u) { return u.keyValue.value; }); } if (m_userPhrases.hasUnigramsForKey(key)) { - vector rawUserUnigrams = m_userPhrases.unigramsForKey(key); + std::vector rawUserUnigrams = m_userPhrases.unigramsForKey(key); userUnigrams = filterAndTransformUnigrams(rawUserUnigrams, excludedValues, insertedValues); } if (m_languageModel.hasUnigramsForKey(key)) { - vector rawGlobalUnigrams = m_languageModel.unigramsForKey(key); + std::vector rawGlobalUnigrams = m_languageModel.unigramsForKey(key); allUnigrams = filterAndTransformUnigrams(rawGlobalUnigrams, excludedValues, insertedValues); } if (m_cnsModel.hasUnigramsForKey(key) && m_cnsEnabled) { - vector rawCNSUnigrams = m_cnsModel.unigramsForKey(key); + std::vector rawCNSUnigrams = m_cnsModel.unigramsForKey(key); cnsUnigrams = filterAndTransformUnigrams(rawCNSUnigrams, excludedValues, insertedValues); } @@ -143,7 +143,7 @@ const vector LMInstantiator::unigramsForKey(const string& key) return allUnigrams; } -bool LMInstantiator::hasUnigramsForKey(const string& key) +bool LMInstantiator::hasUnigramsForKey(const std::string& key) { if (key == " ") { return true; @@ -185,36 +185,36 @@ bool LMInstantiator::externalConverterEnabled() return m_externalConverterEnabled; } -void LMInstantiator::setExternalConverter(std::function externalConverter) +void LMInstantiator::setExternalConverter(std::function externalConverter) { m_externalConverter = externalConverter; } -const vector LMInstantiator::filterAndTransformUnigrams(const vector unigrams, const unordered_set& excludedValues, unordered_set& insertedValues) +const std::vector LMInstantiator::filterAndTransformUnigrams(const std::vector unigrams, const std::unordered_set& excludedValues, std::unordered_set& insertedValues) { - vector results; + std::vector results; for (auto&& unigram : unigrams) { // excludedValues filters out the unigrams with the original value. // insertedValues filters out the ones with the converted value - string originalValue = unigram.keyValue.value; + std::string originalValue = unigram.keyValue.value; if (excludedValues.find(originalValue) != excludedValues.end()) { continue; } - string value = originalValue; + std::string value = originalValue; if (m_phraseReplacementEnabled) { - string replacement = m_phraseReplacement.valueForKey(value); + std::string replacement = m_phraseReplacement.valueForKey(value); if (replacement != "") { value = replacement; } } if (m_externalConverterEnabled && m_externalConverter) { - string replacement = m_externalConverter(value); + std::string replacement = m_externalConverter(value); value = replacement; } if (insertedValues.find(value) == insertedValues.end()) { - Unigram g; + Taiyan::Gramambular::Unigram g; g.keyValue.value = value; g.keyValue.key = unigram.keyValue.key; g.score = unigram.score; @@ -225,12 +225,14 @@ const vector LMInstantiator::filterAndTransformUnigrams(const vector LMInstantiator::associatedPhrasesForKey(const string& key) +const std::vector LMInstantiator::associatedPhrasesForKey(const std::string& key) { return m_associatedPhrases.valuesForKey(key); } -bool LMInstantiator::hasAssociatedPhrasesForKey(const string& key) +bool LMInstantiator::hasAssociatedPhrasesForKey(const std::string& key) { return m_associatedPhrases.hasValuesForKey(key); } + +} // namespace vChewing