]> gitweb.fperrin.net Git - Dictionary.git/blob - dictionary-format.txt
Allow to escape only unicode chars
[Dictionary.git] / dictionary-format.txt
1 This is a quick write-up of the dictionary file format, v7.
2 v6 is troublesome as it relies on Java serialization but is now
3 also documented in dictionary-format-v6.txt.
4 This hasn't been checked much for correctness and likely has some bugs.
5 Also, I really should have used some standard format for writing this...
6
7 ===========================================
8
9 Some basic types:
10
11 [Short]
12   2 bytes: big-endian, signed value (note: negative values generally not used here)
13
14 [Int]
15   4 bytes: big-endian, signed value (note: negative values generally not used here)
16
17 [Long]
18   8 bytes: big-endian, signed value (note: negative values generally not used here)
19
20
21 [String]
22   [Short]: string length
23   n bytes: string, modified UTF-8, n is value from previous element
24            note: no zero termination
25
26 ======================================================
27
28 [Dictionary]
29
30 [Int]: version, fixed value 7
31 [Long]: file creation time (in milliseconds since Jan. 1st 1970)
32 [String]: dictionary information (human-readable)
33
34 list_of([source])
35 list_of([pair_entry], block size 64, compressed)
36 list_of([text_entry])
37 list_of([html_entry], block size 64, compressed)
38 list_of([html_entry_data], block size 128, compressed)
39 list_of([index])
40
41 [String]: string "END OF DICTIONARY" (length value 17)
42
43
44 Note: block size and compression flags is only what
45 the dictionary generation currently uses and seems
46 a good compromise between performance and size for
47 wiktionary content. It is not a requirement
48 of the file format.
49
50 ==============================
51
52 [varInt]
53
54 Much of the data inside the dictionary uses a varInt format
55 (variable length integer), always stored in big-endian,
56 which can take on of these forms:
57 1 byte: value in 0x0 - 0x7F range.
58 2 bytes: value in 0x80 - 0x3FFF range, offset by 0x8000
59 3 bytes: value in 0x4000 - 0x1FFFFF range, offset by 0xC00000
60 4 bytes: value in 0x200000 - 0xFFFFFFF range, offset by 0xE0000000
61 5 bytes: all other values, including negative, written as is with the value
62 0xF0 prepended in the first byte.
63
64 For decoding, the number of leading 1s in the first byte is the overall
65 length - 1.
66 Note that this scheme would allow storing an even larger range of values
67 in the 5-byte variant and can be extended to arbitrary length, however
68 that is not currently implemented.
69
70 ===========================
71
72 All list_of entries describe a list of elements.
73 These elements can have variable size, thus an index (table-of-contents, TOC)
74 is needed.
75 To reduce the cost of this table and enable more efficient compression,
76 multiple entries can be stored in a block that gets one single index entry.
77 I.e. it is only possible to do random-access to the start of a block,
78 seeking to elements further inside the block must be done via reading.
79 Caching should be used to reduce the performance impact of this (so
80 that when entries 5, 4, 3 etc. of a block are read sequentially,
81 parsing and decompression is done only once).
82
83 These lists have the following base format:
84
85 [varInt]: number of entries in the list (must be >= 0) (<size>)
86 [varInt]: compression block size (in entries) (must be >= 1) (<blockSize>)
87 [varInt]: flags. Currently only bit 0 used, indicating compression is used
88
89 <toc size>=(<size>/<blockSize>)*4 + 4 bytes:
90   (note division with rounding up if not divisible)
91   table-of-contents.
92   [Int] offset value for each block of entries.
93   Followed by a final [Int] offset value to the end of the list data (<end offset>).
94   Each offset is relative to the start of this block.
95   Note that currently for simplicity Java int type is used
96   to process these values, even though negative values make no sense.
97   This limits the maximum amount of data to around 2GB.
98
99 <end offset>-<toc size> bytes:
100   entry data
101
102 If compression is enabled, the data for each block is
103 deflate compressed.
104 Note that this is really raw deflate, not e.g. gzip,
105 so neither header nor footer.
106 There is also no decompressed size, so to decompress
107 the start of the next block must be used to find the end
108 of the compressed data and signal the end of input to the
109 inflater, and whatever data the inflater produces is the
110 decompressed input (note: this might be an implementation
111 detail, you might not need the decompressed size to decompress
112 and parse the contained data, but it prevents things like
113 the inflater overreading and producing spurious errors).
114
115 ==========================================================
116
117 [source]
118
119 [String]: name of source, e.g. "enwiktionary"
120 [Int]: number of entries from that source (I kind of wouldn't rely on that one
121 being useful/correct...)
122
123 ========================================================
124
125 [pair entry]
126
127 [varInt]: source index (see list_of([source]))
128 [varInt]: number of pairs in this entry (<num_pairs>)
129 <num_pairs> times:
130   [String]: in first language
131   [String]: in second language (possibly empty)
132
133 =================================================
134
135 [text_entry]
136
137 [varInt]: source index (see list_of([source]))
138 [String]: text
139
140 ===========================================
141
142 [html_entry]
143
144 [varInt]: source index (see list_of([source]))
145 [String]: title for HTML entry
146
147 ====================================
148
149 [html_entry_data]
150
151 [varInt]: length of data in bytes (<len>)
152 <len> bytes: HTML page data, UTF-8 encoded
153
154 =====================================
155
156 [index]
157
158 Note: this structure is used for binary search.
159 It is thus critical that all entries are correctly
160 sorted.
161 The sorting is according to libicu, however as Java
162 and Android versions do not match special hacks
163 have been added, like ignoring "-" for the comparison
164 (unless that makes them equal, then they are
165 compared including the dash).
166
167 [String]: index short name
168 [String]: index long name
169 [String]: language ISO code (sort order depends on this)
170 [String]: ICU normalizer rules to apply for sorting/searching
171 1 byte: swap pair entries (if != 0, this index is for the second language entries in [pair_entry])
172 [Int]: number of main tokens (?)
173 list_of([index_entry], block size 32, compressed)
174 [varInt]: stop list size <stoplist_size>
175 <stoplist_size> times:
176     [String]: stop list words
177 uniform_list_of([row])
178
179
180 with uniform_list_of:
181 [Int]: number of entries in list <num_entries>
182 [Int]: size of entry <entry_size>
183 <num_entries>*<entry_size> bytes: data
184
185
186 ================================================
187
188 [index_entry]
189
190 [String]: token
191 [varInt]: start index into uniform_list_of([row])
192 [varInt]: number of rows covered
193 1 byte: <has_normalized>
194 if <has_normalized> != 0:
195   [String]: normalized token
196 [varInt]: <num_html_entries>
197 <num_html_entries> times:
198   [varInt]: index into list_of([html_entries])/list_of([html_entries_data])
199
200 =======================================
201
202 [row]
203
204 1 byte: highest 3 bits <type>, lowest 5 bits additional high bits for next value (bits 16 - 20)
205 [Short]: lowest 16 bit of index
206
207 <type> means:
208 1: index into list_of([pair_entry])
209 2: index into list_of([index_entry]) (mark as "main word header" entry)
210 3: index into list_of([text_entry])
211 4: index into list_of([index_entry]) (mark as "extra info/translation" entry)
212 5: index into list_of([html_entry])/list_of([html_entry_data])