掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 2739|回复: 3

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

[复制链接]

该用户从未签到

发表于 2017-10-17 12:34:08 | 显示全部楼层 |阅读模式
本帖最后由 henices 于 2017-10-17 13:58 编辑
. K* C9 |) o8 A* @" j
4 {5 s; D+ S) Y1 hhttp://www.pdawiki.com/forum/thread-21902-1-1.html  [GoldenDict] 如何查看 GoldenDict 下某部词典的 ID ?0 Z* ~" Z8 C0 p9 ?) b/ l
, s- _! M# q0 s7 Z- r3 A
! R& X6 ^5 ~' T  N3 G
  1. 6 K' f( {* j. G
  2. <dictionary name="21**大英汉词典">139305d43503a41a44ebf590fd8f3fae</dictionary>5 z- Z+ u, E. F3 u
  3. % F8 r% V. m% P
  4. >>> import hashlib
    % F- B" W- g7 z/ H% ?. [
  5. >>> hashlib.md5('/home/henices/dictionary/21/21.mdd\0/home/henices/dictionary/21/21.mdx\0').hexdigest()
    " S! L1 o/ Q% [/ E" p  i
  6. '139305d43503a41a44ebf590fd8f3fae'
    ) J" S. @5 i# W
复制代码

" i. C6 S) F! ]9 ?. W$ m7 }9 L6 C  d9 X" D& e6 d1 u
使用的是 md5 哈希, 参见 dictionary.cc  -> makeDictionaryId5 X" K  e. H6 _7 ~) n+ p' |
  ?" g2 k) ~0 p; D7 Q! |* d

  1. $ l  R; u; \5 W; w
  2. string makeDictionaryId( vector< string > const & dictionaryFiles ) throw()                                                                                                                    # s( b+ o  r* M7 Z) ~/ R
  3. {                                                                                                                                                                                             
    3 n0 h" n) O$ l
  4.   std::vector< string > sortedList;                                                                                                                                                            2 z$ D8 k' g, o: ?$ Z8 ?
  5.                                                                                                                                                                                                
    ) d( ]* _$ u9 y
  6.   if ( Config::isPortableVersion() )                                                                                                                                                           
    & m6 W) q9 M. Y, u+ E6 b- m8 U
  7.   {                                                                                                                                                                                            4 W! n- q6 W: k6 n+ f* ^0 T
  8.     // For portable version, we use relative paths                                                                                                                                             - [8 w2 U  S+ j' \2 Y+ q
  9.     sortedList.reserve( dictionaryFiles.size() );                                                                                                                                             
    % _$ |# f" g" _# o6 p, I( B9 d
  10.                                                                                                                                                                                                , ]$ x$ I" N% q  L% k7 f6 b
  11.     QDir dictionariesDir( Config::getPortableVersionDictionaryDir() );                                                                                                                        
    0 ^7 J. {! Z; |0 r* F0 h* q
  12.                                                                                                                                                                                                7 `& @  r+ {6 y3 }% [* t$ E
  13.     for( unsigned x = 0; x < dictionaryFiles.size(); ++x )                                                                                                                                     : k# J7 `% l2 i$ i7 B4 V
  14.     {                                                                                                                                                                                          . ]. G& H) R9 T
  15.       string const & full( dictionaryFiles[ x ] );                                                                                                                                             
    $ T6 |4 x( M/ E( j# ]3 K$ f- f/ j3 j
  16.                                                                                                                                                                                                7 @/ O7 `& Q+ k& F
  17.       QFileInfo fileInfo( FsEncoding::decode( full.c_str() ) );                                                                                                                                2 g* u1 k4 j5 L3 v5 a) b9 C
  18.                                                                                                                                                                                                
    & U) v* U1 g" Z% Z$ G& r6 H  P
  19.       if ( fileInfo.isAbsolute() )                                                                                                                                                            
    9 L' n& i$ i% k  q( @8 X
  20.         sortedList.push_back( FsEncoding::encode( dictionariesDir.relativeFilePath( fileInfo.filePath() ) ) );                                                                                 
    # a5 l( ]/ Y- V  z
  21.       else                                                                                                                                                                                     ; b8 k/ Y/ H( T9 k/ S) M0 g, R
  22.       {                                                                                                                                                                                        + Z# n7 C% ~2 h* H, e5 d
  23.         // Well, it's relative. We don't technically support those, but                                                                                                                        # T( ^5 N8 B& e: V0 N
  24.         // what the heck                                                                                                                                                                       
    # j( L  R2 l3 S3 _
  25.         sortedList.push_back( full );                                                                                                                                                         
    % @/ x4 h% `& g5 t
  26.       }                                                                                                                                                                                          l+ I- T: \& E
  27.     }                                                                                                                                                                                          
    : {9 m, _% l$ ?% K
  28.   }                                                                                                                                                                                            
    * z- k$ |, A- M& @4 k
  29.   else                                                                                                                                                                                         : w  S9 s1 c: n- J  r
  30.     sortedList = dictionaryFiles;                                                                                                                                                              - Y/ o: U( F' Q( B7 t
  31.                                                                                                                                                                                                
      P4 M( f- d8 x! I% {* m% D: N; T
  32.   std::sort( sortedList.begin(), sortedList.end() );                                                                                                                                          
    , S- ]  Y: J4 b0 _, V  X
  33.                                                                                                                                                                                                
    ) K$ ^' n. ~* p5 D% M3 x# f
  34.   QCryptographicHash hash( QCryptographicHash::Md5 );                                                                                                                                          / w" l; t" Q* }/ i0 M
  35.                                                                                                                                                                                                : k' c; Y* C% F% U* }
  36.   for( std::vector< string >::const_iterator i = sortedList.begin();                                                                                                                           3 R( I* a: g6 Q  e) R
  37.        i != sortedList.end(); ++i )                                                                                                                                                            
    5 r2 j, S- E& e( Z7 g# l1 i
  38.     hash.addData( i->c_str(), i->size() + 1 );                                                                                                                                                 4 g6 ]# A% k" r6 `- {6 o
  39.                                                                                                                                                                                                
    * I" R  C% U/ n
  40.   return hash.result().toHex().data();                                                                                                                                                         6 N0 @6 l3 N: P9 |; x, l4 t
  41. } & Z6 K  Z( f+ a+ ^; f) X: Y
复制代码

7 ^+ r5 j8 ?* E/ y4 ?7 |) y' d: X# ?

4 L: T3 V: `6 t3 n各种来源的 ID 是随机生成的 (website/wiki/program/...)8 b4 n, T, B  T( h
8 |* e& U% B; O- P
sources.cc
) `  M0 A, q" y9 a$ G( k
  1. & K. C3 D% [* g- o" ^( M
  2. void ProgramsModel::addNewProgram()                                                                                                                                                            - Z( g( M( j$ e: w3 a- }2 K3 W
  3. {                                                                                                                                                                                             
    , P' \0 {3 j7 B
  4.   Config::Program p;                                                                                                                                                                           0 G! n- U# r- A: E3 ~0 \  P
  5.                                                                                                                                                                                                
    # v! u; _. q, J7 x; u
  6.   p.enabled = false;                                                                                                                                                                             P8 O$ s9 P" ^7 R" q4 J
  7.   p.type = Config::Program::Audio;                                                                                                                                                             8 w2 j5 j* Q, m/ @& i7 V; }. J
  8.                                                                                                                                                                                                
    * q5 x2 k! Q, W
  9.   p.id = Dictionary::generateRandomDictionaryId();                                                                                                                                             . S* c- A* J% w
  10.                                                                                                                                                                                                
    & H/ q" j7 Q: U2 u  J- h/ t# H
  11.   beginInsertRows( QModelIndex(), programs.size(), programs.size() );                                                                                                                          . ^1 H$ d, e! U1 S. _) J) z
  12.   programs.push_back( p );                                                                                                                                                                     ' t9 z; P4 k8 r- E' U# i
  13.   endInsertRows();                                                                                                                                                                             % g" `, M3 v& u5 G
  14. } * d; T' x3 j" E+ n# c7 k
复制代码

2 h1 Y9 l! k/ J+ u4 w0 q
$ p. P7 A7 g& R5 h& Kdictionary.cc
% [' ]# `. k' |2 p1 x: o
  1. 4 D1 H# i, W/ J! [  n
  2. QString generateRandomDictionaryId()                                                                                                                                                           
    9 l2 E0 e, H2 r0 q
  3. {                                                                                                                                                                                             
    $ T2 a/ W/ |7 G
  4.   return QString(                                                                                                                                                                              0 x+ r/ {) J7 ^; M0 x' v" s
  5.     QCryptographicHash::hash(                                                                                                                                                                  
    * Z) @" h( Q. \7 v9 E4 N
  6.       QDateTime::currentDateTime().toString( ""Random"dd.MM.yyyy hh:mm:ss.zzz" ).toUtf8(),                                                                                                   % x$ l; Q! q: n# a
  7.       QCryptographicHash::Md5 ).toHex() );                                                                                                                                                     ! g. g1 R2 t, P
  8. } & V/ O0 b. {* n
复制代码

/ _- F$ i* G, |6 ~" I1 V, S1 B  I

评分

5

查看全部评分

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

    [LV.4]偶尔看看III

    发表于 2018-12-11 20:08:59 | 显示全部楼层
    本帖最后由 浔阳巷陌 于 2018-12-11 20:12 编辑
    2 t2 l" h1 E1 Q2 R: L& w7 _9 L, d" O
    赞!
    8 g6 F/ G+ _+ ?+ e  q& A我用的是便携版的 Goldendict (Win10, 64bit), 有样学样,用了论坛里的汉典测试楼主的示例代码成功(看不懂源代码...)。相对目录的排列差点使我放弃治疗。
    , l6 |) `* z0 j- j: S& F; s  P7 X
    1. >>> import hashlib# Q6 R# C- W9 `8 O* K( S: I
    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()
      9 `$ f0 |# W/ g" y* ]$ f* L& @% }
    3. d29a3da26022910808bacbf24a3f1e9
    复制代码

    + W4 A0 L  i8 q- j5 [
  • TA的每日心情

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

    [LV.7]常住居民III

    发表于 2017-10-17 13:12:32 | 显示全部楼层
    goldendict会修改词典id的名字, 于是用id来操作的js 就会出问题, 有比较好的解决方法么?2 b! y6 K9 n% d+ T
    比如id名称重新映射?

    该用户从未签到

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

    本版积分规则

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

    GMT+8, 2026-9-7 21:09 , Processed in 0.021233 second(s), 23 queries .

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

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