掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 2013|回复: 3

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

[复制链接]

该用户从未签到

发表于 2017-10-17 12:34:08 | 显示全部楼层 |阅读模式
本帖最后由 henices 于 2017-10-17 13:58 编辑
1 k- S6 k: ~6 `. V6 j4 Y7 Y& [/ F' _3 w( O
http://www.pdawiki.com/forum/thread-21902-1-1.html  [GoldenDict] 如何查看 GoldenDict 下某部词典的 ID ?
) M5 }3 o2 S9 R( T  y
% S2 m, i- Q, k" p6 e. O# l+ s, Q; L9 x
  1. 7 s+ e  n0 @; c6 q! N$ N
  2. <dictionary name="21**大英汉词典">139305d43503a41a44ebf590fd8f3fae</dictionary>. U/ b: E6 k6 k: f, n

  3. 9 l$ d$ A. v+ W' K% B
  4. >>> import hashlib
    # f) g2 d% \/ _, l
  5. >>> hashlib.md5('/home/henices/dictionary/21/21.mdd\0/home/henices/dictionary/21/21.mdx\0').hexdigest()% a/ A$ w9 H% p8 ^- ?8 i1 z8 S
  6. '139305d43503a41a44ebf590fd8f3fae'
    $ ]* a$ i/ p$ \
复制代码
6 U8 b  T; N1 w. z( x5 W1 j9 L

. q" q; R* G0 \使用的是 md5 哈希, 参见 dictionary.cc  -> makeDictionaryId
7 ]1 z1 F" S4 M! Y& D) t; _' _4 W6 }% {6 y
6 T% M4 M) h% ^, m  |; k$ I

  1. % v3 {$ _( }+ B1 q: Y) n* u
  2. string makeDictionaryId( vector< string > const & dictionaryFiles ) throw()                                                                                                                    2 ]: r8 {8 e3 y# O+ `& V$ g' b
  3. {                                                                                                                                                                                             
    7 E$ x( C- u) w# G3 q+ Y% H+ R
  4.   std::vector< string > sortedList;                                                                                                                                                            7 [; W/ H3 [5 V, A
  5.                                                                                                                                                                                                * T, H! d( U$ G) o# D" m0 P0 P; R) `
  6.   if ( Config::isPortableVersion() )                                                                                                                                                           5 I5 I. A7 @7 v1 T
  7.   {                                                                                                                                                                                            + q+ g3 Y" C0 q8 L, j
  8.     // For portable version, we use relative paths                                                                                                                                             
    2 P" x, ]' Q% @
  9.     sortedList.reserve( dictionaryFiles.size() );                                                                                                                                              9 j" A/ G- y' z  z* B9 t
  10.                                                                                                                                                                                                $ ]; m* F# c2 n) u$ \: i: ]' D
  11.     QDir dictionariesDir( Config::getPortableVersionDictionaryDir() );                                                                                                                         - C' a# c. D( B- S$ P- ]
  12.                                                                                                                                                                                                - _+ Z7 @/ p# W( S2 q
  13.     for( unsigned x = 0; x < dictionaryFiles.size(); ++x )                                                                                                                                    
    ( g& |2 w  O6 F0 W
  14.     {                                                                                                                                                                                          
    % u5 V& F6 D; g% S8 M* t" P
  15.       string const & full( dictionaryFiles[ x ] );                                                                                                                                             ; e/ c. T& \) P7 i- V
  16.                                                                                                                                                                                                ' E' X0 ?* x' M1 `- g
  17.       QFileInfo fileInfo( FsEncoding::decode( full.c_str() ) );                                                                                                                                + I$ o6 e) b- J  @, \# v+ Z
  18.                                                                                                                                                                                                
    6 k3 N5 A5 d9 t3 C1 P9 U* t- a
  19.       if ( fileInfo.isAbsolute() )                                                                                                                                                            
    . @! B, v% H. J
  20.         sortedList.push_back( FsEncoding::encode( dictionariesDir.relativeFilePath( fileInfo.filePath() ) ) );                                                                                 
    - Z, O# W1 x. W
  21.       else                                                                                                                                                                                     # ]. H) O9 a% x( A) y- v
  22.       {                                                                                                                                                                                       
    7 E1 ~; P+ T; J! ~
  23.         // Well, it's relative. We don't technically support those, but                                                                                                                        3 q  D( X6 b. L/ k( n" U; U
  24.         // what the heck                                                                                                                                                                       8 }! K5 L0 ]" W9 ]3 X& e4 X
  25.         sortedList.push_back( full );                                                                                                                                                          # Z2 i8 X. C7 ]8 k% F# ?9 D
  26.       }                                                                                                                                                                                          O) X1 J- _6 `9 ^4 T
  27.     }                                                                                                                                                                                          0 g7 |$ ?5 N# q7 |
  28.   }                                                                                                                                                                                            # B& e' _7 D4 F" p  `
  29.   else                                                                                                                                                                                         
    " a; \8 t/ N9 J4 \+ @& y1 I0 v
  30.     sortedList = dictionaryFiles;                                                                                                                                                              , e2 G4 g, D' w; m! x: C# k6 i
  31.                                                                                                                                                                                                
    4 ?; V) u: \% b$ i3 `
  32.   std::sort( sortedList.begin(), sortedList.end() );                                                                                                                                          
    6 E8 h( r3 E$ L! G3 s
  33.                                                                                                                                                                                                * [' |( Y5 [6 [8 Y6 A# |: {  n3 W1 h
  34.   QCryptographicHash hash( QCryptographicHash::Md5 );                                                                                                                                          
    + x4 H  N7 {' B- M! ?5 `  `$ x
  35.                                                                                                                                                                                                + Z: r# M4 K- X1 @$ ~* f0 W8 }; [
  36.   for( std::vector< string >::const_iterator i = sortedList.begin();                                                                                                                           
    ! u* b4 l" }0 `9 L
  37.        i != sortedList.end(); ++i )                                                                                                                                                              _0 G  o% a0 \% w1 t
  38.     hash.addData( i->c_str(), i->size() + 1 );                                                                                                                                                 4 K" Q- f# \$ t" k. P+ Y! X
  39.                                                                                                                                                                                                
    % e% ]1 A! T( N# Q2 l6 c
  40.   return hash.result().toHex().data();                                                                                                                                                         
    - z. z* P( r  G+ f1 t9 J
  41. } 6 E0 |' `  ~1 D, P  U+ q
复制代码
8 ?7 w( l8 C3 r1 f3 `( P! g# z& D
& f+ w( l: V% b" E: A2 o
  n! E- p/ ^  }3 S# y* J$ r" ^
各种来源的 ID 是随机生成的 (website/wiki/program/...)  U* g0 @8 k# J8 A5 i
/ i6 u' Q+ B" C2 Z. H( i
sources.cc* Y/ |& i% W* [& F* o, Q3 J

  1. / G+ Z4 j: `2 T3 ^- d* G
  2. void ProgramsModel::addNewProgram()                                                                                                                                                            
    9 E+ o( H5 a8 `$ O/ ?  \9 h# H
  3. {                                                                                                                                                                                             
    9 v+ f% ?5 m7 t3 y; z4 Y+ T3 I' c; ]
  4.   Config::Program p;                                                                                                                                                                           ) L, M6 f, C1 A
  5.                                                                                                                                                                                                ( i- n0 U# J8 e" Z4 ]
  6.   p.enabled = false;                                                                                                                                                                           
    5 a2 f0 l' S' I$ [0 W* a& |1 y
  7.   p.type = Config::Program::Audio;                                                                                                                                                               W6 O& L3 _, o5 `; w
  8.                                                                                                                                                                                                / @: q9 o0 I, a# }7 _
  9.   p.id = Dictionary::generateRandomDictionaryId();                                                                                                                                             0 c- k% A2 q* R, ]2 B6 S4 Y* M2 m' d
  10.                                                                                                                                                                                                  W! o) m( q$ c) P$ `5 L! R  r
  11.   beginInsertRows( QModelIndex(), programs.size(), programs.size() );                                                                                                                          7 N+ E1 Y% e) q+ U
  12.   programs.push_back( p );                                                                                                                                                                     
    % h! w  s9 n! F
  13.   endInsertRows();                                                                                                                                                                             6 l( `$ [, L8 H, H  E: |4 J
  14. }
    4 |; x6 c* e3 I7 x% {
复制代码

: W' I( v2 N/ i. t
  y) C, `) Z3 ~3 q/ |- sdictionary.cc % F' O1 B" x, G4 t
  1. . R. N* O5 z; q3 B6 K: X
  2. QString generateRandomDictionaryId()                                                                                                                                                           , R* J. _: U. c% _/ J: T
  3. {                                                                                                                                                                                             
    ) o; {% I; S( \, C
  4.   return QString(                                                                                                                                                                              ( m  h: B& y' u4 F3 f
  5.     QCryptographicHash::hash(                                                                                                                                                                  
    0 c* h& h% G0 G7 W
  6.       QDateTime::currentDateTime().toString( ""Random"dd.MM.yyyy hh:mm:ss.zzz" ).toUtf8(),                                                                                                   ' ~6 z) r* V9 j0 V4 M9 c
  7.       QCryptographicHash::Md5 ).toHex() );                                                                                                                                                     
    4 E' t2 n7 Y0 P6 R; _& Z1 Z
  8. }
    # U$ [( m( }' M$ j( N
复制代码
5 d# K! ^  o4 V6 X3 [* v7 c1 S

评分

5

查看全部评分

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

    [LV.4]偶尔看看III

    发表于 2018-12-11 20:08:59 | 显示全部楼层
    本帖最后由 浔阳巷陌 于 2018-12-11 20:12 编辑 ) K, V# k! }. z% `

    , T0 H* A" {. q; \6 B) Z$ t赞!$ u8 A" O- E1 v! x3 {
    我用的是便携版的 Goldendict (Win10, 64bit), 有样学样,用了论坛里的汉典测试楼主的示例代码成功(看不懂源代码...)。相对目录的排列差点使我放弃治疗。" z. B5 ?8 ~6 b) `- r0 F: b
    1. >>> import hashlib9 g1 {2 b/ k- r" U. W2 d. h1 s
    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()( X3 x; F9 N" V4 i
    3. d29a3da26022910808bacbf24a3f1e9
    复制代码

    1 V0 O- z  B' o) ?4 r# N
  • TA的每日心情

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

    [LV.7]常住居民III

    发表于 2017-10-17 13:12:32 | 显示全部楼层
    goldendict会修改词典id的名字, 于是用id来操作的js 就会出问题, 有比较好的解决方法么?
    0 h8 K8 k" P$ p6 |4 R$ C6 m+ x$ N; k9 L比如id名称重新映射?

    该用户从未签到

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

    本版积分规则

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

    GMT+8, 2025-5-15 09:14 , Processed in 0.025728 second(s), 26 queries .

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

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