掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 1572|回复: 3

[经验交流] GoldenDict 词典 ID 生成算法

[复制链接]

该用户从未签到

发表于 2017-10-17 12:34:08 | 显示全部楼层 |阅读模式
本帖最后由 henices 于 2017-10-17 13:58 编辑 , c% Q1 ]! k0 u  y' E( x# {: T

5 E% c+ w# {0 c! Phttp://www.pdawiki.com/forum/thread-21902-1-1.html  [GoldenDict] 如何查看 GoldenDict 下某部词典的 ID ?
, @  E  W+ d' w' u- l- t0 s
/ B2 `" c8 z  b# D
6 y4 ]. ^/ O1 N& ^
  1. $ y7 t. |2 G) y, W$ Z) M! L/ G
  2. <dictionary name="21**大英汉词典">139305d43503a41a44ebf590fd8f3fae</dictionary>
    * i8 Z# ~7 l: C9 k' p9 s  l
  3. " P8 `5 r$ s7 g. B+ w0 _
  4. >>> import hashlib
    8 f$ Z0 T; U  u  s1 j& u
  5. >>> hashlib.md5('/home/henices/dictionary/21/21.mdd\0/home/henices/dictionary/21/21.mdx\0').hexdigest()
    * |, g- X. c9 P& ]  l0 s
  6. '139305d43503a41a44ebf590fd8f3fae'+ i4 T. a$ k3 _
复制代码

! g& R* t: z6 R1 J. e, n
  E* Y2 t5 n' q# E/ q: l使用的是 md5 哈希, 参见 dictionary.cc  -> makeDictionaryId
6 A- u& w3 W& H4 c
1 U& ^! W' N# N9 A% {) ]% f% H
  1. ) `2 W5 _1 i% z$ h0 ~8 B# c
  2. string makeDictionaryId( vector< string > const & dictionaryFiles ) throw()                                                                                                                    # O) s2 P2 x! S& m. Z2 |! `
  3. {                                                                                                                                                                                             
    , F, k; |/ i2 ^% h$ d5 `
  4.   std::vector< string > sortedList;                                                                                                                                                            
    + X, y' J: ?! p- m
  5.                                                                                                                                                                                                6 H9 }+ W: E- x
  6.   if ( Config::isPortableVersion() )                                                                                                                                                           5 d& M$ @8 P% s" k7 a4 K; Z6 S0 {
  7.   {                                                                                                                                                                                            
    + D+ l9 R, B) O/ `3 M3 P
  8.     // For portable version, we use relative paths                                                                                                                                             / H; P3 f1 m" [0 |9 u  K% b
  9.     sortedList.reserve( dictionaryFiles.size() );                                                                                                                                              3 B$ L$ n4 g4 j; b3 [7 r& P: F/ t
  10.                                                                                                                                                                                                5 P* F2 d# Q9 p  ^% q  D
  11.     QDir dictionariesDir( Config::getPortableVersionDictionaryDir() );                                                                                                                         $ C. g& X! y8 x
  12.                                                                                                                                                                                                2 {# W$ B9 S, R. @+ f# Z: O, d
  13.     for( unsigned x = 0; x < dictionaryFiles.size(); ++x )                                                                                                                                     & S% }4 t$ l; D* ?/ F: b) `
  14.     {                                                                                                                                                                                          / C, [, ^0 a- O  ^: e' [- w
  15.       string const & full( dictionaryFiles[ x ] );                                                                                                                                             1 \- H7 C, G9 F: D
  16.                                                                                                                                                                                                
    ! w# E9 C" i( B3 X9 W7 g) v, w
  17.       QFileInfo fileInfo( FsEncoding::decode( full.c_str() ) );                                                                                                                                2 u0 c7 ?- d9 A/ b
  18.                                                                                                                                                                                                $ ]4 T4 t" W3 m1 `0 H% x+ X
  19.       if ( fileInfo.isAbsolute() )                                                                                                                                                            
    , z3 [* a" B) P2 n
  20.         sortedList.push_back( FsEncoding::encode( dictionariesDir.relativeFilePath( fileInfo.filePath() ) ) );                                                                                 
    7 ~8 f, A* r; `; [
  21.       else                                                                                                                                                                                     ! V( Z) {! P/ p. \0 `; u* e
  22.       {                                                                                                                                                                                       
    $ m2 \' A6 j0 {5 `
  23.         // Well, it's relative. We don't technically support those, but                                                                                                                        
    ; P6 ]: h. K2 N+ \1 _
  24.         // what the heck                                                                                                                                                                       
    7 D0 l6 R+ s7 k+ D5 S& x5 h
  25.         sortedList.push_back( full );                                                                                                                                                          : f  }/ C* L) q' k; V
  26.       }                                                                                                                                                                                       
    0 T* h0 O1 @4 K9 N2 Y& }0 ]
  27.     }                                                                                                                                                                                          
    - R$ V6 S0 F& F
  28.   }                                                                                                                                                                                            - p# N0 T$ j) i# C9 k; `
  29.   else                                                                                                                                                                                         
    % y( E8 x# R1 o7 e1 R- g8 ~9 c8 g
  30.     sortedList = dictionaryFiles;                                                                                                                                                              
    9 X. G! j8 [# g
  31.                                                                                                                                                                                                
    : O) d+ t9 q) e% J
  32.   std::sort( sortedList.begin(), sortedList.end() );                                                                                                                                          
    " j9 N1 K) P7 ^1 Y, d  }3 G
  33.                                                                                                                                                                                                . W. \- g3 R0 E
  34.   QCryptographicHash hash( QCryptographicHash::Md5 );                                                                                                                                          % Z. S7 o2 a. X5 p
  35.                                                                                                                                                                                                + q& Z  W) q3 T& v2 j, f
  36.   for( std::vector< string >::const_iterator i = sortedList.begin();                                                                                                                           + e( y( U  f9 |
  37.        i != sortedList.end(); ++i )                                                                                                                                                            + A$ W8 n' g$ z6 Q9 q& B
  38.     hash.addData( i->c_str(), i->size() + 1 );                                                                                                                                                 $ s, s$ O6 c$ ^" _2 J, r( p7 H
  39.                                                                                                                                                                                                3 s' c: B! c' k4 L0 ?! S) w" `
  40.   return hash.result().toHex().data();                                                                                                                                                         1 S# M- v1 e* H/ @
  41. } 6 _) t  ]; V& c4 R( |+ h
复制代码

3 d1 M" ^7 e$ I' h. x5 ?* F$ H7 r& l0 W# G; f0 ~
2 @7 X" J6 Z  X) g
各种来源的 ID 是随机生成的 (website/wiki/program/...); M- r! k# E3 h

; k) c5 ]5 V0 x' q( Msources.cc$ l+ \" F8 |3 A' U8 l2 X5 K
  1. ' z7 t3 q6 a" X  y* [% q( W
  2. void ProgramsModel::addNewProgram()                                                                                                                                                            
    ! j, p, d' f& O- y/ Y
  3. {                                                                                                                                                                                             
    $ J/ }& E* x9 D# y) ~
  4.   Config::Program p;                                                                                                                                                                           
    6 |- r& C0 G3 b& a! V1 j  ]
  5.                                                                                                                                                                                                
    ' t& e! P6 L. Y# X
  6.   p.enabled = false;                                                                                                                                                                           7 H& i. y  t5 k' j( Y4 X6 u$ H" ]# s* c
  7.   p.type = Config::Program::Audio;                                                                                                                                                             0 x9 \- u8 X+ R! c% ~5 N
  8.                                                                                                                                                                                                3 f  V: q* z* d, L; @/ j
  9.   p.id = Dictionary::generateRandomDictionaryId();                                                                                                                                             
    * D! @! N9 ]/ }7 q2 N& D
  10.                                                                                                                                                                                                
    1 ~& `0 Z4 r9 H# ^  {7 y6 \
  11.   beginInsertRows( QModelIndex(), programs.size(), programs.size() );                                                                                                                          
    " f- U" g. k3 i2 E+ |6 v3 g
  12.   programs.push_back( p );                                                                                                                                                                     - ~0 K4 p5 E4 a: R3 l
  13.   endInsertRows();                                                                                                                                                                             + J6 d0 i" j$ E' A
  14. }
    3 C6 Z+ \) Q6 W3 M7 M" J9 p
复制代码
/ }+ L/ A- j' T; ~: U4 F; N) N

' Y* q" X; @' d7 _2 L. C; Y* w3 hdictionary.cc % U4 s: K) ~: J
  1. 7 z- {8 d" g7 E" I$ o
  2. QString generateRandomDictionaryId()                                                                                                                                                           
    5 v" N0 Z/ F! J/ J; X+ h, m
  3. {                                                                                                                                                                                              3 f  T, R* {- h& `/ @* J
  4.   return QString(                                                                                                                                                                              
    / O7 x9 I& d" V5 f$ n
  5.     QCryptographicHash::hash(                                                                                                                                                                  
    ) T1 S5 a, ~. W0 O7 k# O) J* N" F
  6.       QDateTime::currentDateTime().toString( ""Random"dd.MM.yyyy hh:mm:ss.zzz" ).toUtf8(),                                                                                                   
    4 m1 x8 O- c2 x' C, q$ m
  7.       QCryptographicHash::Md5 ).toHex() );                                                                                                                                                     8 z8 x1 o  X* Y* ^& k
  8. }
    - d% V" |3 n  B# y5 r
复制代码

  y3 C. h/ g2 H" D9 d) ?+ h! `

评分

5

查看全部评分

  • TA的每日心情
    开心
    2018-12-28 00:32
  • 签到天数: 25 天

    [LV.4]偶尔看看III

    发表于 2018-12-11 20:08:59 | 显示全部楼层
    本帖最后由 浔阳巷陌 于 2018-12-11 20:12 编辑
    % s. f. l, f+ v4 ?* z" S9 {6 c, R5 S4 c! L% S( @& S& G0 f& U
    赞!
    % J* v6 H' B; e我用的是便携版的 Goldendict (Win10, 64bit), 有样学样,用了论坛里的汉典测试楼主的示例代码成功(看不懂源代码...)。相对目录的排列差点使我放弃治疗。8 A* `: w1 ?( n& x$ {( N2 k
    1. >>> import hashlib+ J8 S% @+ p$ S3 {
    2. >>> 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()8 ]; N* M$ r; U8 ^! _  p( T
    3. d29a3da26022910808bacbf24a3f1e9
    复制代码
    3 a( i* H4 U9 Q8 j7 ^
  • TA的每日心情

    2019-9-20 08:37
  • 签到天数: 214 天

    [LV.7]常住居民III

    发表于 2017-10-17 13:12:32 | 显示全部楼层
    goldendict会修改词典id的名字, 于是用id来操作的js 就会出问题, 有比较好的解决方法么?% P1 F  D( }  N  n  G4 W
    比如id名称重新映射?

    该用户从未签到

    发表于 2021-10-28 16:59:21 | 显示全部楼层
    'D://dictionary//ENG//test.dsl\0'
    6 G0 Q0 [: m" @! p9 {9 ]0 a使用网络上现成的MD5简单JavaScript代码也可以生成正确的id
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

    小黑屋|手机版|Archiver|PDAWIKI |网站地图

    GMT+8, 2024-4-24 06:02 , Processed in 0.049251 second(s), 14 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2023, Tencent Cloud.

    快速回复 返回顶部 返回列表