掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 1565|回复: 3

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

[复制链接]

该用户从未签到

发表于 2017-10-17 12:34:08 | 显示全部楼层 |阅读模式
本帖最后由 henices 于 2017-10-17 13:58 编辑 ! s+ t3 V. c. ^1 u

' h' `( f( g# n6 s* }! s6 ]# q2 s+ Uhttp://www.pdawiki.com/forum/thread-21902-1-1.html  [GoldenDict] 如何查看 GoldenDict 下某部词典的 ID ?7 X# \9 d: y* `
" b3 _1 E6 x' e+ v* r5 n' f7 [& J
( |; o) Q9 L7 r  R3 y. c' a* q

  1. ( p5 I' {+ {, ?- [9 W% k
  2. <dictionary name="21**大英汉词典">139305d43503a41a44ebf590fd8f3fae</dictionary>
    2 h( ^" R! W" k0 U
  3. ; w- W- ^7 G" |) o) H
  4. >>> import hashlib
    3 }! \' E% i# U2 q; M
  5. >>> hashlib.md5('/home/henices/dictionary/21/21.mdd\0/home/henices/dictionary/21/21.mdx\0').hexdigest()3 N) a. h# L0 Q! d2 z; c* I6 |
  6. '139305d43503a41a44ebf590fd8f3fae', C! i3 g; x; [/ N8 K4 i
复制代码

7 z, u* u7 s9 ^! Q1 X1 n' s
0 c) j" v& \* M$ w; A8 V$ C使用的是 md5 哈希, 参见 dictionary.cc  -> makeDictionaryId
- R1 S3 L: e* I. s6 ]0 _9 B, c
5 Q0 x  ?) e9 k: C, z7 @
  1. 3 v3 w+ x  M0 ]
  2. string makeDictionaryId( vector< string > const & dictionaryFiles ) throw()                                                                                                                    
    " m, v# q* e# J( b8 r: k  s
  3. {                                                                                                                                                                                              $ ]# H: h) }1 g6 j3 M
  4.   std::vector< string > sortedList;                                                                                                                                                            
    ; J6 m8 u* n. ?3 w7 c* ]
  5.                                                                                                                                                                                                ' b% i( y# ~) \5 j# Y
  6.   if ( Config::isPortableVersion() )                                                                                                                                                           7 f  v3 n9 q' J  }
  7.   {                                                                                                                                                                                            $ H  l$ c/ c; |3 m  }- e
  8.     // For portable version, we use relative paths                                                                                                                                             
    2 Y  A( _) @9 h& g
  9.     sortedList.reserve( dictionaryFiles.size() );                                                                                                                                              ! e1 E) B0 I' U& Y  G
  10.                                                                                                                                                                                                ) _" m) E. M* Z8 G( c
  11.     QDir dictionariesDir( Config::getPortableVersionDictionaryDir() );                                                                                                                         / v3 ~8 N" r# p" E5 A
  12.                                                                                                                                                                                                
    4 ~6 ~, W- t1 s
  13.     for( unsigned x = 0; x < dictionaryFiles.size(); ++x )                                                                                                                                     8 a" O: K* Y- t
  14.     {                                                                                                                                                                                          
    : m9 j6 I& s% o; W
  15.       string const & full( dictionaryFiles[ x ] );                                                                                                                                             
    5 P& e6 U, l& M+ A; F
  16.                                                                                                                                                                                                
    # X% |) _( G* s5 Q3 |
  17.       QFileInfo fileInfo( FsEncoding::decode( full.c_str() ) );                                                                                                                                
    & U) v1 f9 G7 o; Y
  18.                                                                                                                                                                                                - L0 z9 P5 }( O  N3 \3 o0 Z
  19.       if ( fileInfo.isAbsolute() )                                                                                                                                                             ! l5 e; c# r2 f2 ^7 A7 R% R5 ?
  20.         sortedList.push_back( FsEncoding::encode( dictionariesDir.relativeFilePath( fileInfo.filePath() ) ) );                                                                                 
    ) \8 f* a6 I6 f  ?6 h
  21.       else                                                                                                                                                                                     ) K, q* D5 m" }, i9 K; F
  22.       {                                                                                                                                                                                        & Y3 b) s. v0 r$ b
  23.         // Well, it's relative. We don't technically support those, but                                                                                                                        
    / N+ M7 o4 ~' m" l; R- d( g. H
  24.         // what the heck                                                                                                                                                                       
    2 p3 U  i+ @( P! h" s9 u$ M; G
  25.         sortedList.push_back( full );                                                                                                                                                          / q% t9 j' H- o! G" R
  26.       }                                                                                                                                                                                        " ]; ]9 W9 Z5 ?- {
  27.     }                                                                                                                                                                                          
    ( P  |% [8 F" ]7 v2 a
  28.   }                                                                                                                                                                                            9 j( ~! D6 S; {$ G& {3 i5 [3 d' [
  29.   else                                                                                                                                                                                         
    * U' w$ l. ]4 D% ]0 s
  30.     sortedList = dictionaryFiles;                                                                                                                                                              3 u. [5 Q* A! n( o/ z
  31.                                                                                                                                                                                                
      z4 a  s& R- O) I/ d2 v4 H
  32.   std::sort( sortedList.begin(), sortedList.end() );                                                                                                                                          
    " Q6 M/ d" J9 L$ V
  33.                                                                                                                                                                                                # W, C; X# {5 ~$ p6 w5 A
  34.   QCryptographicHash hash( QCryptographicHash::Md5 );                                                                                                                                          
    4 ^" N/ r! h- L( l6 z8 G; ?
  35.                                                                                                                                                                                                2 W0 R! |' |' g% M# @2 u8 _4 H" u
  36.   for( std::vector< string >::const_iterator i = sortedList.begin();                                                                                                                           
    4 X( X: `! C4 p; O6 ~# q
  37.        i != sortedList.end(); ++i )                                                                                                                                                            
    1 ]% [" d& B" D1 z5 H& b" d& h! w
  38.     hash.addData( i->c_str(), i->size() + 1 );                                                                                                                                                 - N+ o, l( e: n# [3 X: v" R
  39.                                                                                                                                                                                                
    ( h- X" R; x& A$ s
  40.   return hash.result().toHex().data();                                                                                                                                                         % f) j9 ]3 _7 h' n8 u1 K+ T+ K
  41. } : A' U9 \, ^6 r/ }5 r
复制代码

$ ^0 `6 Y/ j$ v+ [' k$ z
# @6 G' F5 {& `. m3 S! ~9 C# w% c
各种来源的 ID 是随机生成的 (website/wiki/program/...)( ]) L; n& m" D: a* c

- f, }& w3 e, g6 B! o! R* @sources.cc
# T' W7 `9 I* b
  1. + X% ]4 X$ N& r  R! y) m
  2. void ProgramsModel::addNewProgram()                                                                                                                                                            0 Z8 B2 ?: `& w9 x0 o
  3. {                                                                                                                                                                                              5 f. Q; d4 o+ ^) O0 ^4 `
  4.   Config::Program p;                                                                                                                                                                           
    0 o  N+ |+ \# I
  5.                                                                                                                                                                                                
    . _% X0 n8 P# a/ Y. @3 P$ k! _
  6.   p.enabled = false;                                                                                                                                                                           
    # v: R/ `' |: k' m5 R& U* Z$ r
  7.   p.type = Config::Program::Audio;                                                                                                                                                             0 _( C' u. i! i" ^4 Q
  8.                                                                                                                                                                                                  B7 V  G1 ?& d* g* `+ ^- B7 q
  9.   p.id = Dictionary::generateRandomDictionaryId();                                                                                                                                             
    . l# S8 {" \5 A9 `
  10.                                                                                                                                                                                                , [1 U1 L$ X9 r7 O1 B3 r
  11.   beginInsertRows( QModelIndex(), programs.size(), programs.size() );                                                                                                                          
    9 E% a4 \& L. O" }3 Y
  12.   programs.push_back( p );                                                                                                                                                                     
    0 z( f" l8 Q- m# g: b* w
  13.   endInsertRows();                                                                                                                                                                             
    ' ~/ _+ Z1 E3 N1 g- [& {3 H
  14. } # c" n" C, U4 ?" u! M
复制代码
: C) Z4 f7 _+ x
0 R8 z5 {' S; L( H9 `
dictionary.cc
* O# l4 L( Z$ x! @7 P: R6 ?

  1. * B) e3 u4 _, X0 A1 _5 _
  2. QString generateRandomDictionaryId()                                                                                                                                                           
      G; z/ P0 Y- U3 u' u
  3. {                                                                                                                                                                                              4 ^5 K5 A2 m, f
  4.   return QString(                                                                                                                                                                              
      N8 }: a; I" h0 c, S1 g3 r2 \
  5.     QCryptographicHash::hash(                                                                                                                                                                  
    " r  l% d3 i8 h. _9 q
  6.       QDateTime::currentDateTime().toString( ""Random"dd.MM.yyyy hh:mm:ss.zzz" ).toUtf8(),                                                                                                   8 W( e( x5 r$ u! b( ^8 c8 @
  7.       QCryptographicHash::Md5 ).toHex() );                                                                                                                                                     3 w" s1 C/ k. `2 f
  8. }
    . B. s2 b! @  v: ?
复制代码

' t& d4 K) f6 d2 D( x/ ~) q5 o% O

评分

5

查看全部评分

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

    [LV.4]偶尔看看III

    发表于 2018-12-11 20:08:59 | 显示全部楼层
    本帖最后由 浔阳巷陌 于 2018-12-11 20:12 编辑 3 M1 m# L6 t# F9 d/ D' s  d: }
    8 N1 Y. `9 e  S* {
    赞!
    , T% r% e* c9 P5 H8 h' q& [我用的是便携版的 Goldendict (Win10, 64bit), 有样学样,用了论坛里的汉典测试楼主的示例代码成功(看不懂源代码...)。相对目录的排列差点使我放弃治疗。
    % i% `; m# e  A# K! l2 M
    1. >>> import hashlib) X0 C3 B- a( N# f
    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()
      $ P- W# i" `8 j3 N" m
    3. d29a3da26022910808bacbf24a3f1e9
    复制代码

    6 Z' i1 P& r# Z6 L% a  X
  • TA的每日心情

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

    [LV.7]常住居民III

    发表于 2017-10-17 13:12:32 | 显示全部楼层
    goldendict会修改词典id的名字, 于是用id来操作的js 就会出问题, 有比较好的解决方法么?
    * T" o# m: N* x6 e. T3 t比如id名称重新映射?

    该用户从未签到

    发表于 2021-10-28 16:59:21 | 显示全部楼层
    'D://dictionary//ENG//test.dsl\0'; A9 N2 P4 b" ^4 g. Z3 y- k, z
    使用网络上现成的MD5简单JavaScript代码也可以生成正确的id
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

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

    GMT+8, 2024-4-18 14:01 , Processed in 0.068928 second(s), 11 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2023, Tencent Cloud.

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