Debian Codegraph @

morfologik-fsa/src/main/java/morfologik/fsa/CFSA2.java

raw symbols SCIP
1 package morfologik.fsa;
2
3 import java.io.DataInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.EnumSet;
7 import java.util.Set;
8
9 /**
10 * CFSA (Compact Finite State Automaton) binary format implementation, version 2:
11 * <ul>
12 * <li>{@link #BIT_TARGET_NEXT} applicable on all arcs, not necessarily the last one.</li>
13 * <li>v-coded goto field</li>
14 * <li>v-coded perfect hashing numbers, if any</li>
15 * <li>31 most frequent labels integrated with flags byte</li>
16 * </ul>
17 *
18 * <p>The encoding of automaton body is as follows.</p>
19 *
20 * <pre>
21 * ---- CFSA header
22 * Byte Description
23 * +-+-+-+-+-+-+-+-+\
24 * 0 | | | | | | | | | +------ '\'
25 * +-+-+-+-+-+-+-+-+/
26 * +-+-+-+-+-+-+-+-+\
27 * 1 | | | | | | | | | +------ 'f'
28 * +-+-+-+-+-+-+-+-+/
29 * +-+-+-+-+-+-+-+-+\
30 * 2 | | | | | | | | | +------ 's'
31 * +-+-+-+-+-+-+-+-+/
32 * +-+-+-+-+-+-+-+-+\
33 * 3 | | | | | | | | | +------ 'a'
34 * +-+-+-+-+-+-+-+-+/
35 * +-+-+-+-+-+-+-+-+\
36 * 4 | | | | | | | | | +------ version (fixed 0xc6)
37 * +-+-+-+-+-+-+-+-+/
38 * +-+-+-+-+-+-+-+-+\
39 * 5 | | | | | | | | | +----\
40 * +-+-+-+-+-+-+-+-+/ \ flags [MSB first]
41 * +-+-+-+-+-+-+-+-+\ /
42 * 6 | | | | | | | | | +----/
43 * +-+-+-+-+-+-+-+-+/
44 * +-+-+-+-+-+-+-+-+\
45 * 7 | | | | | | | | | +------ label lookup table size
46 * +-+-+-+-+-+-+-+-+/
47 * +-+-+-+-+-+-+-+-+\
48 * 8-32 | | | | | | | | | +------ label value lookup table
49 * : : : : : : : : : |
50 * +-+-+-+-+-+-+-+-+/
51 *
52 * ---- Start of a node; only if automaton was compiled with NUMBERS option.
53 *
54 * Byte
55 * +-+-+-+-+-+-+-+-+\
56 * 0 | | | | | | | | | \
57 * +-+-+-+-+-+-+-+-+ +
58 * 1 | | | | | | | | | | number of strings recognized
59 * +-+-+-+-+-+-+-+-+ +----- by the automaton starting
60 * : : : : : : : : : | from this node. v-coding
61 * +-+-+-+-+-+-+-+-+ +
62 * | | | | | | | | | /
63 * +-+-+-+-+-+-+-+-+/
64 *
65 * ---- A vector of this node's arcs. An arc's layout depends on the combination of flags.
66 *
67 * 1) NEXT bit set, mapped arc label.
68 *
69 * +----------------------- node pointed to is next
70 * | +--------------------- the last arc of the node
71 * | | +------------------- this arc leads to a final state (acceptor)
72 * | | | _______+--------- arc's label; indexed if M &gt; 0, otherwise explicit label follows
73 * | | | / | | | |
74 * +-+-+-+-+-+-+-+-+\
75 * 0 |N|L|F|M|M|M|M|M| +------ flags + (M) index of the mapped label.
76 * +-+-+-+-+-+-+-+-+/
77 * +-+-+-+-+-+-+-+-+\
78 * 1 | | | | | | | | | +------ optional label if M == 0
79 * +-+-+-+-+-+-+-+-+/
80 * : : : : : : : : :
81 * +-+-+-+-+-+-+-+-+\
82 * |A|A|A|A|A|A|A|A| +------ v-coded goto address
83 * +-+-+-+-+-+-+-+-+/
84 * </pre>
85 */
86 public final class CFSA2 extends FSA {
87 /**
88 * Automaton header version value.
89 */
90 public static final byte VERSION = (byte) 0xc6;
91
92 /**
93 * The target node of this arc follows the last arc of the current state
94 * (no goto field).
95 */
96 public static final int BIT_TARGET_NEXT = 1 << 7;
97
98 /**
99 * The arc is the last one from the current node's arcs list.
100 */
101 public static final int BIT_LAST_ARC = 1 << 6;
102
103 /**
104 * The arc corresponds to the last character of a sequence
105 * available when building the automaton (acceptor transition).
106 */
107 public static final int BIT_FINAL_ARC = 1 << 5;
108
109 /**
110 * The count of bits assigned to storing an indexed label.
111 */
112 static final int LABEL_INDEX_BITS = 5;
113
114 /**
115 * Masks only the M bits of a flag byte.
116 */
117 static final int LABEL_INDEX_MASK = (1 << LABEL_INDEX_BITS) - 1;
118
119 /**
120 * Maximum size of the labels index.
121 */
122 public static final int LABEL_INDEX_SIZE = (1 << LABEL_INDEX_BITS) - 1;
123
124 /**
125 * An array of bytes with the internal representation of the automaton.
126 * Please see the documentation of this class for more information on how
127 * this structure is organized.
128 */
129 public byte[] arcs;
130
131 /**
132 * Flags for this automaton version.
133 */
134 private final EnumSet<FSAFlags> flags;
135
136 /**
137 * Label mapping for M-indexed labels.
138 */
139 public final byte[] labelMapping;
140
141 /**
142 * If <code>true</code> states are prepended with numbers.
143 */
144 private final boolean hasNumbers;
145
146 /**
147 * Epsilon node's offset.
148 */
149 private final int epsilon = 0;
150
151 /**
152 * Reads an automaton from a byte stream.
153 */
154 CFSA2(InputStream stream) throws IOException {
155 DataInputStream in = new DataInputStream(stream);
156
157 // Read flags.
158 short flagBits = in.readShort();
159 flags = EnumSet.noneOf(FSAFlags.class);
160 for (FSAFlags f : FSAFlags.values()) {
161 if (f.isSet(flagBits)) {
162 flags.add(f);
163 }
164 }
165
166 if (flagBits != FSAFlags.asShort(flags)) {
167 throw new IOException("Unrecognized flags: 0x" + Integer.toHexString(flagBits));
168 }
169
170 this.hasNumbers = flags.contains(FSAFlags.NUMBERS);
171
172 /*
173 * Read mapping dictionary.
174 */
175 int labelMappingSize = in.readByte() & 0xff;
176 labelMapping = new byte[labelMappingSize];
177 in.readFully(labelMapping);
178
179 /*
180 * Read arcs' data.
181 */
182 arcs = readRemaining(in);
183 }
184
185 /**
186 * {@inheritDoc}
187 */
188 @Override
189 public int getRootNode() {
190 // Skip dummy node marking terminating state.
191 return getDestinationNodeOffset(getFirstArc(epsilon));
192 }
193
194 /**
195 * {@inheritDoc}
196 */
197 @Override
198 public final int getFirstArc(int node) {
199 if (hasNumbers) {
200 return skipVInt(node);
201 } else {
202 return node;
203 }
204 }
205
206 /**
207 * {@inheritDoc}
208 */
209 @Override
210 public final int getNextArc(int arc) {
211 if (isArcLast(arc)) {
212 return 0;
213 } else {
214 return skipArc(arc);
215 }
216 }
217
218 /**
219 * {@inheritDoc}
220 */
221 @Override
222 public int getArc(int node, byte label) {
223 for (int arc = getFirstArc(node); arc != 0; arc = getNextArc(arc)) {
224 if (getArcLabel(arc) == label) {
225 return arc;
226 }
227 }
228
229 // An arc labeled with "label" not found.
230 return 0;
231 }
232
233 /**
234 * {@inheritDoc}
235 */
236 @Override
237 public int getEndNode(int arc) {
238 final int nodeOffset = getDestinationNodeOffset(arc);
239 assert nodeOffset != 0 : "Can't follow a terminal arc: " + arc;
240 assert nodeOffset < arcs.length : "Node out of bounds.";
241 return nodeOffset;
242 }
243
244 /**
245 * {@inheritDoc}
246 */
247 @Override
248 public byte getArcLabel(int arc) {
249 int index = arcs[arc] & LABEL_INDEX_MASK;
250 if (index > 0) {
251 return this.labelMapping[index];
252 } else {
253 return arcs[arc + 1];
254 }
255 }
256
257 /**
258 * {@inheritDoc}
259 */
260 @Override
261 public int getRightLanguageCount(int node) {
262 assert getFlags().contains(FSAFlags.NUMBERS) : "This FSA was not compiled with NUMBERS.";
263 return readVInt(arcs, node);
264 }
265
266 /**
267 * {@inheritDoc}
268 */
269 @Override
270 public boolean isArcFinal(int arc) {
271 return (arcs[arc] & BIT_FINAL_ARC) != 0;
272 }
273
274 /**
275 * {@inheritDoc}
276 */
277 @Override
278 public boolean isArcTerminal(int arc) {
279 return (0 == getDestinationNodeOffset(arc));
280 }
281
282 /**
283 * Returns <code>true</code> if this arc has <code>NEXT</code> bit set.
284 *
285 * @see #BIT_LAST_ARC
286 * @param arc The node's arc identifier.
287 * @return Returns true if the argument is the last arc of a node.
288 */
289 public boolean isArcLast(int arc) {
290 return (arcs[arc] & BIT_LAST_ARC) != 0;
291 }
292
293 /**
294 * @see #BIT_TARGET_NEXT
295 * @param arc The node's arc identifier.
296 * @return Returns true if {@link #BIT_TARGET_NEXT} is set for this arc.
297 */
298 public boolean isNextSet(int arc) {
299 return (arcs[arc] & BIT_TARGET_NEXT) != 0;
300 }
301
302 /**
303 * {@inheritDoc}
304 */
305 public Set<FSAFlags> getFlags() {
306 return flags;
307 }
308
309 /**
310 * Returns the address of the node pointed to by this arc.
311 */
312 final int getDestinationNodeOffset(int arc) {
313 if (isNextSet(arc)) {
314 /* Follow until the last arc of this state. */
315 while (!isArcLast(arc)) {
316 arc = getNextArc(arc);
317 }
318
319 /* And return the byte right after it. */
320 return skipArc(arc);
321 } else {
322 /*
323 * The destination node address is v-coded. v-code starts either
324 * at the next byte (label indexed) or after the next byte (label explicit).
325 */
326 return readVInt(arcs, arc + ((arcs[arc] & LABEL_INDEX_MASK) == 0 ? 2 : 1));
327 }
328 }
329
330 /**
331 * Read the arc's layout and skip as many bytes, as needed, to skip it.
332 */
333 private int skipArc(int offset) {
334 int flag = arcs[offset++];
335
336 // Explicit label?
337 if ((flag & LABEL_INDEX_MASK) == 0) {
338 offset++;
339 }
340
341 // Explicit goto?
342 if ((flag & BIT_TARGET_NEXT) == 0) {
343 offset = skipVInt(offset);
344 }
345
346 assert offset < this.arcs.length;
347 return offset;
348 }
349
350 /**
351 * Read a v-int.
352 */
353 static int readVInt(byte[] array, int offset) {
354 byte b = array[offset];
355 int value = b & 0x7F;
356
357 for (int shift = 7; b < 0; shift += 7) {
358 b = array[++offset];
359 value |= (b & 0x7F) << shift;
360 }
361
362 return value;
363 }
364
365 /**
366 * Return the byte-length of a v-coded int.
367 */
368 static int vIntLength(int value) {
369 assert value >= 0 : "Can't v-code negative ints.";
370
371 int bytes;
372 for (bytes = 1; value >= 0x80; bytes++) {
373 value >>= 7;
374 }
375
376 return bytes;
377 }
378
379 /**
380 * Skip a v-int.
381 */
382 private int skipVInt(int offset) {
383 while (arcs[offset++] < 0) {
384 // Do nothing.
385 }
386 return offset;
387 }
388 }