henices 发表于 2017-10-17 12:34:08

GoldenDict 词典 ID 生成算法

本帖最后由 henices 于 2017-10-17 13:58 编辑

http://www.pdawiki.com/forum/thread-21902-1-1.html 如何查看 GoldenDict 下某部词典的 ID ?



<dictionary name="21**大英汉词典">139305d43503a41a44ebf590fd8f3fae</dictionary>

>>> import hashlib
>>> hashlib.md5('/home/henices/dictionary/21/21.mdd\0/home/henices/dictionary/21/21.mdx\0').hexdigest()
'139305d43503a41a44ebf590fd8f3fae'


使用的是 md5 哈希, 参见 dictionary.cc-> makeDictionaryId


string makeDictionaryId( vector< string > const & dictionaryFiles ) throw()                                                                                                                  
{                                                                                                                                                                                             
std::vector< string > sortedList;                                                                                                                                                            
                                                                                                                                                                                             
if ( Config::isPortableVersion() )                                                                                                                                                         
{                                                                                                                                                                                          
    // For portable version, we use relative paths                                                                                                                                             
    sortedList.reserve( dictionaryFiles.size() );                                                                                                                                             
                                                                                                                                                                                             
    QDir dictionariesDir( Config::getPortableVersionDictionaryDir() );                                                                                                                        
                                                                                                                                                                                             
    for( unsigned x = 0; x < dictionaryFiles.size(); ++x )                                                                                                                                    
    {                                                                                                                                                                                          
      string const & full( dictionaryFiles[ x ] );                                                                                                                                             
                                                                                                                                                                                             
      QFileInfo fileInfo( FsEncoding::decode( full.c_str() ) );                                                                                                                              
                                                                                                                                                                                             
      if ( fileInfo.isAbsolute() )                                                                                                                                                            
      sortedList.push_back( FsEncoding::encode( dictionariesDir.relativeFilePath( fileInfo.filePath() ) ) );                                                                                 
      else                                                                                                                                                                                    
      {                                                                                                                                                                                       
      // Well, it's relative. We don't technically support those, but                                                                                                                        
      // what the heck                                                                                                                                                                     
      sortedList.push_back( full );                                                                                                                                                         
      }                                                                                                                                                                                       
    }                                                                                                                                                                                          
}                                                                                                                                                                                          
else                                                                                                                                                                                       
    sortedList = dictionaryFiles;                                                                                                                                                            
                                                                                                                                                                                             
std::sort( sortedList.begin(), sortedList.end() );                                                                                                                                          
                                                                                                                                                                                             
QCryptographicHash hash( QCryptographicHash::Md5 );                                                                                                                                          
                                                                                                                                                                                             
for( std::vector< string >::const_iterator i = sortedList.begin();                                                                                                                           
       i != sortedList.end(); ++i )                                                                                                                                                            
    hash.addData( i->c_str(), i->size() + 1 );                                                                                                                                                
                                                                                                                                                                                             
return hash.result().toHex().data();                                                                                                                                                         
}



各种来源的 ID 是随机生成的 (website/wiki/program/...)

sources.cc

void ProgramsModel::addNewProgram()                                                                                                                                                            
{                                                                                                                                                                                             
Config::Program p;                                                                                                                                                                           
                                                                                                                                                                                             
p.enabled = false;                                                                                                                                                                           
p.type = Config::Program::Audio;                                                                                                                                                            
                                                                                                                                                                                             
p.id = Dictionary::generateRandomDictionaryId();                                                                                                                                             
                                                                                                                                                                                             
beginInsertRows( QModelIndex(), programs.size(), programs.size() );                                                                                                                        
programs.push_back( p );                                                                                                                                                                     
endInsertRows();                                                                                                                                                                           
}


dictionary.cc

QString generateRandomDictionaryId()                                                                                                                                                         
{                                                                                                                                                                                             
return QString(                                                                                                                                                                              
    QCryptographicHash::hash(                                                                                                                                                                  
      QDateTime::currentDateTime().toString( "\"Random\"dd.MM.yyyy hh:mm:ss.zzz" ).toUtf8(),                                                                                                   
      QCryptographicHash::Md5 ).toHex() );                                                                                                                                                   
}

浔阳巷陌 发表于 2018-12-11 20:08:59

本帖最后由 浔阳巷陌 于 2018-12-11 20:12 编辑

赞!
我用的是便携版的 Goldendict (Win10, 64bit), 有样学样,用了论坛里的汉典测试楼主的示例代码成功(看不懂源代码...)。相对目录的排列差点使我放弃治疗。{:4_104:}
>>> import hashlib
>>> hashlib.md5('Dictionaries/CN/汉典_by_ldlcau_20120920/hd.1.mdd\x00Dictionaries/CN/汉典_by_ldlcau_20120920/hd.mdd\x00Dictionaries/CN/汉典_by_ldlcau_20120920/hd.mdx\x00'.encode()).hexdigest()
d29a3da26022910808bacbf24a3f1e9

HMPT 发表于 2017-10-17 13:12:32

goldendict会修改词典id的名字, 于是用id来操作的js 就会出问题, 有比较好的解决方法么?
比如id名称重新映射?

chigre3 发表于 2021-10-28 16:59:21

'D://dictionary//ENG//test.dsl\0'
使用网络上现成的MD5简单JavaScript代码也可以生成正确的id
页: [1]
查看完整版本: GoldenDict 词典 ID 生成算法