refactor: simplify and speedup dictionary loading via a helper function

This commit is contained in:
shenleban tongying 2022-12-25 09:05:30 -05:00
parent 2937487c6a
commit 7476b5b154
2 changed files with 42 additions and 179 deletions

View file

@ -78,8 +78,8 @@ void LoadDictionaries::run()
{ {
try try
{ {
for( Config::Paths::const_iterator i = paths.begin(); i != paths.end(); ++i ) for(const auto & path : paths)
handlePath( *i ); handlePath( path );
// Make soundDirs // Make soundDirs
{ {
@ -107,6 +107,10 @@ void LoadDictionaries::run()
} }
} }
void LoadDictionaries::addDicts( const std::vector< sptr< Dictionary::Class > >& dicts ) {
std::move(dicts.begin(), dicts.end(), std::back_inserter(dictionaries));
}
void LoadDictionaries::handlePath( Config::Path const & path ) void LoadDictionaries::handlePath( Config::Path const & path )
{ {
vector< string > allFiles; vector< string > allFiles;
@ -132,112 +136,24 @@ void LoadDictionaries::handlePath( Config::Path const & path )
allFiles.push_back( FsEncoding::encode( QDir::toNativeSeparators( fullName ) ) ); allFiles.push_back( FsEncoding::encode( QDir::toNativeSeparators( fullName ) ) );
} }
{ addDicts(Bgl::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
vector< sptr< Dictionary::Class > > bglDictionaries = addDicts(Stardict::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand ) );
Bgl::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ); addDicts(Lsa::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
addDicts(Dsl::makeDictionaries( allFiles,FsEncoding::encode( Config::getIndexDir() ),*this,maxPictureWidth,maxHeadwordSize ) );
addDicts(DictdFiles::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
addDicts(Xdxf::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
addDicts(Sdict::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
addDicts(Aard::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand ) );
addDicts(ZipSounds::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
addDicts(Mdx::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
addDicts(Gls::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
dictionaries.insert( dictionaries.end(), bglDictionaries.begin(),
bglDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > stardictDictionaries =
Stardict::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand );
dictionaries.insert( dictionaries.end(), stardictDictionaries.begin(),
stardictDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > lsaDictionaries =
Lsa::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), lsaDictionaries.begin(),
lsaDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > dslDictionaries =
Dsl::makeDictionaries(
allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxPictureWidth, maxHeadwordSize );
dictionaries.insert( dictionaries.end(), dslDictionaries.begin(),
dslDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > dictdDictionaries =
DictdFiles::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), dictdDictionaries.begin(),
dictdDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > xdxfDictionaries =
Xdxf::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), xdxfDictionaries.begin(),
xdxfDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > sdictDictionaries =
Sdict::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), sdictDictionaries.begin(),
sdictDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > aardDictionaries =
Aard::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand );
dictionaries.insert( dictionaries.end(), aardDictionaries.begin(),
aardDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > zipSoundsDictionaries =
ZipSounds::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), zipSoundsDictionaries.begin(),
zipSoundsDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > mdxDictionaries =
Mdx::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), mdxDictionaries.begin(),
mdxDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > glsDictionaries =
Gls::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), glsDictionaries.begin(),
glsDictionaries.end() );
}
#ifdef MAKE_ZIM_SUPPORT #ifdef MAKE_ZIM_SUPPORT
{ addDicts(Zim::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand ) );
vector< sptr< Dictionary::Class > > zimDictionaries = addDicts(Slob::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand ) );
Zim::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand );
dictionaries.insert( dictionaries.end(), zimDictionaries.begin(),
zimDictionaries.end() );
}
{
vector< sptr< Dictionary::Class > > slobDictionaries =
Slob::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxHeadwordToExpand );
dictionaries.insert( dictionaries.end(), slobDictionaries.begin(),
slobDictionaries.end() );
}
#endif #endif
#ifndef NO_EPWING_SUPPORT #ifndef NO_EPWING_SUPPORT
{ addDicts( Epwing::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this ) );
vector< sptr< Dictionary::Class > > epwingDictionaries =
Epwing::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
dictionaries.insert( dictionaries.end(), epwingDictionaries.begin(),
epwingDictionaries.end() );
}
#endif #endif
} }
@ -285,27 +201,20 @@ void loadDictionaries( QWidget * parent, bool showInitially,
dictionaries = loadDicts.getDictionaries(); dictionaries = loadDicts.getDictionaries();
// Helper function that will add a vector of dictionary::Class to the dictionary list
// Implemented as lambda to access method's `dictionaries` variable
auto static addDicts = [&dictionaries](const vector< sptr< Dictionary::Class >> &dicts) {
std::move(dicts.begin(), dicts.end(), std::back_inserter(dictionaries));
};
///// We create transliterations synchronously since they are very simple ///// We create transliterations synchronously since they are very simple
#ifdef MAKE_CHINESE_CONVERSION_SUPPORT #ifdef MAKE_CHINESE_CONVERSION_SUPPORT
// Make Chinese conversion addDicts(Chinese::makeDictionaries( cfg.transliteration.chinese ));
{
vector< sptr< Dictionary::Class > > chineseDictionaries =
Chinese::makeDictionaries( cfg.transliteration.chinese );
dictionaries.insert( dictionaries.end(), chineseDictionaries.begin(),
chineseDictionaries.end() );
}
#endif #endif
// Make Romaji addDicts(Romaji::makeDictionaries( cfg.transliteration.romaji ));
{
vector< sptr< Dictionary::Class > > romajiDictionaries =
Romaji::makeDictionaries( cfg.transliteration.romaji );
dictionaries.insert( dictionaries.end(), romajiDictionaries.begin(),
romajiDictionaries.end() );
}
// Make Russian transliteration // Make Russian transliteration
if ( cfg.transliteration.enableRussianTransliteration ) if ( cfg.transliteration.enableRussianTransliteration )
@ -322,67 +231,17 @@ void loadDictionaries( QWidget * parent, bool showInitially,
// Make Belarusian transliteration // Make Belarusian transliteration
if ( cfg.transliteration.enableBelarusianTransliteration ) if ( cfg.transliteration.enableBelarusianTransliteration )
{ {
vector< sptr< Dictionary::Class > > dicts = BelarusianTranslit::makeDictionaries(); addDicts(BelarusianTranslit::makeDictionaries());
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
} }
///// We create MediaWiki dicts synchronously, since they use netmgr addDicts(MediaWiki::makeDictionaries( loadDicts, cfg.mediawikis, dictNetMgr ));
addDicts(WebSite::makeDictionaries( cfg.webSites, dictNetMgr ));
addDicts(Forvo::makeDictionaries( loadDicts, cfg.forvo, dictNetMgr ));
addDicts(Lingua::makeDictionaries( loadDicts, cfg.lingua, dictNetMgr ));
addDicts(Programs::makeDictionaries( cfg.programs ));
addDicts(VoiceEngines::makeDictionaries( cfg.voiceEngines ));
addDicts(DictServer::makeDictionaries( cfg.dictServers ));
{
vector< sptr< Dictionary::Class > > dicts =
MediaWiki::makeDictionaries( loadDicts, cfg.mediawikis, dictNetMgr );
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
}
///// WebSites are very simple, no need to create them asynchronously
{
vector< sptr< Dictionary::Class > > dicts =
WebSite::makeDictionaries( cfg.webSites, dictNetMgr );
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
}
//// Forvo dictionaries
{
vector< sptr< Dictionary::Class > > dicts =
Forvo::makeDictionaries( loadDicts, cfg.forvo, dictNetMgr );
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
}
//// Lingua Libre
{
vector< sptr< Dictionary::Class > > dicts =
Lingua::makeDictionaries( loadDicts, cfg.lingua, dictNetMgr );
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
}
//// Programs
{
vector< sptr< Dictionary::Class > > dicts =
Programs::makeDictionaries( cfg.programs );
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
}
//// Text to Speech
{
vector< sptr< Dictionary::Class > > dicts =
VoiceEngines::makeDictionaries( cfg.voiceEngines );
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
}
{
vector< sptr< Dictionary::Class > > dicts =
DictServer::makeDictionaries( cfg.dictServers );
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
}
GD_DPRINTF( "Load done\n" ); GD_DPRINTF( "Load done\n" );

View file

@ -40,9 +40,7 @@ public:
std::string const & getExceptionText() const std::string const & getExceptionText() const
{ return exceptionText; } { return exceptionText; }
signals:
void indexingDictionarySignal( QString const & dictionaryName );
public: public:
@ -51,6 +49,12 @@ public:
private: private:
void handlePath( Config::Path const & ); void handlePath( Config::Path const & );
// Helper function that will add a vector of dictionary::Class to the dictionary list
void addDicts(const std::vector< sptr< Dictionary::Class > >& dicts);
signals:
void indexingDictionarySignal( QString const & dictionaryName );
}; };
/// Loads all dictionaries mentioned in the configuration passed, into the /// Loads all dictionaries mentioned in the configuration passed, into the