Removes unused code and fixes a typo.

This commit is contained in:
zonble 2022-01-12 13:16:10 +08:00
parent abdf97f652
commit 84fc2f068b
3 changed files with 16 additions and 28 deletions

View File

@ -58,8 +58,6 @@ namespace Formosa {
vector<string> readingsAtRange(size_t begin, size_t end) const; vector<string> readingsAtRange(size_t begin, size_t end) const;
Grid& grid(); Grid& grid();
bool checkIfUnigramExistInVector(Unigram& unigram, vector<Unigram>vector);
protected: protected:
void build(); void build();
@ -198,16 +196,6 @@ namespace Formosa {
return m_grid; return m_grid;
} }
inline bool BlockReadingBuilder::checkIfUnigramExistInVector(Unigram& unigram, vector<Unigram>vector)
{
for (std::vector<Unigram>::iterator it=vector.begin(); it!=vector.end(); ++it) {
if (it->keyValue.value == unigram.keyValue.value) {
return true;
}
}
return false;
}
inline void BlockReadingBuilder::build() inline void BlockReadingBuilder::build()
{ {
if (!m_LM) { if (!m_LM) {

View File

@ -13,7 +13,7 @@ McBopomofoLM::~McBopomofoLM()
{ {
m_languageModel.close(); m_languageModel.close();
m_userPhrases.close(); m_userPhrases.close();
m_excluddePhrases.close(); m_excludedPhrases.close();
} }
void McBopomofoLM::loadLanguageModel(const char* languageModelDataPath) void McBopomofoLM::loadLanguageModel(const char* languageModelDataPath)
@ -27,8 +27,8 @@ void McBopomofoLM::loadUserPhrases(const char* userPhrasesDataPath,
{ {
m_userPhrases.close(); m_userPhrases.close();
m_userPhrases.open(userPhrasesDataPath); m_userPhrases.open(userPhrasesDataPath);
m_excluddePhrases.close(); m_excludedPhrases.close();
m_excluddePhrases.open(excludedPhrasesDataPath); m_excludedPhrases.open(excludedPhrasesDataPath);
} }
const vector<Bigram> McBopomofoLM::bigramsForKeys(const string& preceedingKey, const string& key) const vector<Bigram> McBopomofoLM::bigramsForKeys(const string& preceedingKey, const string& key)
@ -40,35 +40,35 @@ const vector<Unigram> McBopomofoLM::unigramsForKey(const string& key)
{ {
vector<Unigram> unigrams; vector<Unigram> unigrams;
vector<Unigram> userUnigrams; vector<Unigram> userUnigrams;
// Use unordered_set so that you don't have to do O(n*m) // Use unordered_set so that you don't have to do O(n*m)
unordered_set<string> excludedValues; unordered_set<string> excludedValues;
unordered_set<string> userValues; unordered_set<string> userValues;
if (m_excluddePhrases.hasUnigramsForKey(key)) { if (m_excludedPhrases.hasUnigramsForKey(key)) {
vector<Unigram> excludedUnigrams = m_excluddePhrases.unigramsForKey(key); vector<Unigram> excludedUnigrams = m_excludedPhrases.unigramsForKey(key);
transform(excludedUnigrams.begin(), excludedUnigrams.end(), transform(excludedUnigrams.begin(), excludedUnigrams.end(),
inserter(excludedValues, excludedValues.end()), inserter(excludedValues, excludedValues.end()),
[](const Unigram &u) { return u.keyValue.value; }); [](const Unigram &u) { return u.keyValue.value; });
} }
if (m_userPhrases.hasUnigramsForKey(key)) { if (m_userPhrases.hasUnigramsForKey(key)) {
vector<Unigram> rawUserUnigrams = m_userPhrases.unigramsForKey(key); vector<Unigram> rawUserUnigrams = m_userPhrases.unigramsForKey(key);
for (auto&& unigram : rawUserUnigrams) { for (auto&& unigram : rawUserUnigrams) {
if (excludedValues.find(unigram.keyValue.value) == excludedValues.end()) { if (excludedValues.find(unigram.keyValue.value) == excludedValues.end()) {
userUnigrams.push_back(unigram); userUnigrams.push_back(unigram);
} }
} }
transform(userUnigrams.begin(), userUnigrams.end(), transform(userUnigrams.begin(), userUnigrams.end(),
inserter(userValues, userValues.end()), inserter(userValues, userValues.end()),
[](const Unigram &u) { return u.keyValue.value; }); [](const Unigram &u) { return u.keyValue.value; });
} }
if (m_languageModel.hasUnigramsForKey(key)) { if (m_languageModel.hasUnigramsForKey(key)) {
vector<Unigram> globalUnigrams = m_languageModel.unigramsForKey(key); vector<Unigram> globalUnigrams = m_languageModel.unigramsForKey(key);
for (auto&& unigram : globalUnigrams) { for (auto&& unigram : globalUnigrams) {
if (excludedValues.find(unigram.keyValue.value) == excludedValues.end() && if (excludedValues.find(unigram.keyValue.value) == excludedValues.end() &&
userValues.find(unigram.keyValue.value) == userValues.end()) { userValues.find(unigram.keyValue.value) == userValues.end()) {
@ -76,17 +76,17 @@ const vector<Unigram> McBopomofoLM::unigramsForKey(const string& key)
} }
} }
} }
unigrams.insert(unigrams.begin(), userUnigrams.begin(), userUnigrams.end()); unigrams.insert(unigrams.begin(), userUnigrams.begin(), userUnigrams.end());
return unigrams; return unigrams;
} }
bool McBopomofoLM::hasUnigramsForKey(const string& key) bool McBopomofoLM::hasUnigramsForKey(const string& key)
{ {
if (!m_excluddePhrases.hasUnigramsForKey(key)) { if (!m_excludedPhrases.hasUnigramsForKey(key)) {
return m_userPhrases.hasUnigramsForKey(key) || return m_userPhrases.hasUnigramsForKey(key) ||
m_languageModel.hasUnigramsForKey(key); m_languageModel.hasUnigramsForKey(key);
} }
return unigramsForKey(key).size() > 0; return unigramsForKey(key).size() > 0;
} }

View File

@ -24,7 +24,7 @@ public:
protected: protected:
FastLM m_languageModel; FastLM m_languageModel;
FastLM m_userPhrases; FastLM m_userPhrases;
FastLM m_excluddePhrases; FastLM m_excludedPhrases;
}; };
}; };