Debian Codegraph @

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

raw symbols SCIP
1 package morfologik.fsa;
2
3 import static morfologik.fsa.FSAFlags.*;
4
5 import java.io.*;
6 import java.util.*;
7
8 /**
9 * CFSA (Compact Finite State Automaton) binary format implementation. This is a
10 * slightly reorganized version of {@link FSA5} offering smaller automata size
11 * at some (minor) performance penalty.
12 *
13 * <p><b>Note:</b> Serialize to {@link CFSA2} for new code.</p>
14 *
15 * <p>The encoding of automaton body is as follows.</p>
16 *
17 * <pre>
18 * ---- FSA header (standard)
19 * Byte Description
20 * +-+-+-+-+-+-+-+-+\
21 * 0 | | | | | | | | | +------ '\'
22 * +-+-+-+-+-+-+-+-+/
23 * +-+-+-+-+-+-+-+-+\
24 * 1 | | | | | | | | | +------ 'f'
25 * +-+-+-+-+-+-+-+-+/
26 * +-+-+-+-+-+-+-+-+\
27 * 2 | | | | | | | | | +------ 's'
28 * +-+-+-+-+-+-+-+-+/
29 * +-+-+-+-+-+-+-+-+\
30 * 3 | | | | | | | | | +------ 'a'
31 * +-+-+-+-+-+-+-+-+/
32 * +-+-+-+-+-+-+-+-+\
33 * 4 | | | | | | | | | +------ version (fixed 0xc5)
34 * +-+-+-+-+-+-+-+-+/
35 * +-+-+-+-+-+-+-+-+\
36 * 5 | | | | | | | | | +------ filler character
37 * +-+-+-+-+-+-+-+-+/
38 * +-+-+-+-+-+-+-+-+\
39 * 6 | | | | | | | | | +------ annot character
40 * +-+-+-+-+-+-+-+-+/
41 * +-+-+-+-+-+-+-+-+\
42 * 7 |C|C|C|C|G|G|G|G| +------ C - node data size (ctl), G - address size (gotoLength)
43 * +-+-+-+-+-+-+-+-+/
44 * +-+-+-+-+-+-+-+-+\
45 * 8-32 | | | | | | | | | +------ labels mapped for type (1) of arc encoding.
46 * : : : : : : : : : |
47 * +-+-+-+-+-+-+-+-+/
48 *
49 * ---- Start of a node; only if automaton was compiled with NUMBERS option.
50 *
51 * Byte
52 * +-+-+-+-+-+-+-+-+\
53 * 0 | | | | | | | | | \ LSB
54 * +-+-+-+-+-+-+-+-+ +
55 * 1 | | | | | | | | | | number of strings recognized
56 * +-+-+-+-+-+-+-+-+ +----- by the automaton starting
57 * : : : : : : : : : | from this node.
58 * +-+-+-+-+-+-+-+-+ +
59 * ctl-1 | | | | | | | | | / MSB
60 * +-+-+-+-+-+-+-+-+/
61 *
62 * ---- A vector of node's arcs. Conditional format, depending on flags.
63 *
64 * 1) NEXT bit set, mapped arc label.
65 *
66 * +--------------- arc's label mapped in M bits if M's field value &gt; 0
67 * | +------------- node pointed to is next
68 * | | +----------- the last arc of the node
69 * _______| | | +--------- the arc is final
70 * / | | | |
71 * +-+-+-+-+-+-+-+-+\
72 * 0 |M|M|M|M|M|1|L|F| +------ flags + (M) index of the mapped label.
73 * +-+-+-+-+-+-+-+-+/
74 *
75 * 2) NEXT bit set, label separate.
76 *
77 * +--------------- arc's label stored separately (M's field is zero).
78 * | +------------- node pointed to is next
79 * | | +----------- the last arc of the node
80 * | | | +--------- the arc is final
81 * | | | |
82 * +-+-+-+-+-+-+-+-+\
83 * 0 |0|0|0|0|0|1|L|F| +------ flags
84 * +-+-+-+-+-+-+-+-+/
85 * +-+-+-+-+-+-+-+-+\
86 * 1 | | | | | | | | | +------ label
87 * +-+-+-+-+-+-+-+-+/
88 *
89 * 3) NEXT bit not set. Full arc.
90 *
91 * +------------- node pointed to is next
92 * | +----------- the last arc of the node
93 * | | +--------- the arc is final
94 * | | |
95 * +-+-+-+-+-+-+-+-+\
96 * 0 |A|A|A|A|A|0|L|F| +------ flags + (A) address field, lower bits
97 * +-+-+-+-+-+-+-+-+/
98 * +-+-+-+-+-+-+-+-+\
99 * 1 | | | | | | | | | +------ label
100 * +-+-+-+-+-+-+-+-+/
101 * : : : : : : : : :
102 * +-+-+-+-+-+-+-+-+\
103 * gtl-1 |A|A|A|A|A|A|A|A| +------ address, continuation (MSB)
104 * +-+-+-+-+-+-+-+-+/
105 * </pre>
106 */
107 public final class CFSA extends FSA {
108 /**
109 * Automaton header version value.
110 */
111 public static final byte VERSION = (byte) 0xC5;
112
113 /**
114 * Bitmask indicating that an arc corresponds to the last character of a
115 * sequence available when building the automaton.
116 */
117 public static final int BIT_FINAL_ARC = 1 << 0;
118
119 /**
120 * Bitmask indicating that an arc is the last one of the node's list and the
121 * following one belongs to another node.
122 */
123 public static final int BIT_LAST_ARC = 1 << 1;
124
125 /**
126 * Bitmask indicating that the target node of this arc follows it in the
127 * compressed automaton structure (no goto field).
128 */
129 public static final int BIT_TARGET_NEXT = 1 << 2;
130
131 /**
132 * An array of bytes with the internal representation of the automaton.
133 * Please see the documentation of this class for more information on how
134 * this structure is organized.
135 */
136 public byte[] arcs;
137
138 /**
139 * The length of the node header structure (if the automaton was compiled with
140 * <code>NUMBERS</code> option). Otherwise zero.
141 */
142 public final int nodeDataLength;
143
144 /**
145 * Flags for this automaton version.
146 */
147 private final Set<FSAFlags> flags;
148
149 /**
150 * Number of bytes each address takes in full, expanded form (goto length).
151 */
152 public final int gtl;
153
154 /**
155 * Label mapping for arcs of type (1) (see class documentation). The array
156 * is indexed by mapped label's value and contains the original label.
157 */
158 public final byte[] labelMapping;
159
160 /**
161 * Creates a new automaton, reading it from a file in FSA format, version 5.
162 */
163 CFSA(InputStream stream) throws IOException {
164 DataInputStream in = new DataInputStream(stream);
165
166 // Skip legacy header fields.
167 in.readByte(); // filler
168 in.readByte(); // annotation
169 final byte hgtl = in.readByte();
170
171 /*
172 * Determine if the automaton was compiled with NUMBERS. If so, modify
173 * ctl and goto fields accordingly.
174 */
175 flags = EnumSet.of(FLEXIBLE, STOPBIT, NEXTBIT);
176 if ((hgtl & 0xf0) != 0) {
177 this.nodeDataLength = (hgtl >>> 4) & 0x0f;
178 this.gtl = hgtl & 0x0f;
179 flags.add(NUMBERS);
180 } else {
181 this.nodeDataLength = 0;
182 this.gtl = hgtl & 0x0f;
183 }
184
185 /*
186 * Read mapping dictionary.
187 */
188 labelMapping = new byte[1 << 5];
189 in.readFully(labelMapping);
190
191 /*
192 * Read arcs' data.
193 */
194 arcs = readRemaining(in);
195 }
196
197 /**
198 * Returns the start node of this automaton. May return <code>0</code> if
199 * the start node is also an end node.
200 */
201 @Override
202 public int getRootNode() {
203 // Skip dummy node marking terminating state.
204 final int epsilonNode = skipArc(getFirstArc(0));
205
206 // And follow the epsilon node's first (and only) arc.
207 return getDestinationNodeOffset(getFirstArc(epsilonNode));
208 }
209
210 /**
211 * {@inheritDoc}
212 */
213 @Override
214 public final int getFirstArc(int node) {
215 return nodeDataLength + node;
216 }
217
218 /**
219 * {@inheritDoc}
220 */
221 @Override
222 public final int getNextArc(int arc) {
223 if (isArcLast(arc))
224 return 0;
225 else
226 return skipArc(arc);
227 }
228
229 /**
230 * {@inheritDoc}
231 */
232 @Override
233 public int getArc(int node, byte label) {
234 for (int arc = getFirstArc(node); arc != 0; arc = getNextArc(arc)) {
235 if (getArcLabel(arc) == label)
236 return arc;
237 }
238
239 // An arc labeled with "label" not found.
240 return 0;
241 }
242
243 /**
244 * {@inheritDoc}
245 */
246 @Override
247 public int getEndNode(int arc) {
248 final int nodeOffset = getDestinationNodeOffset(arc);
249 if (0 == nodeOffset) {
250 throw new RuntimeException("This is a terminal arc [" + arc + "]");
251 }
252 return nodeOffset;
253 }
254
255 /**
256 * {@inheritDoc}
257 */
258 @Override
259 public byte getArcLabel(int arc) {
260 if (isNextSet(arc) && isLabelCompressed(arc)) {
261 return this.labelMapping[(arcs[arc] >>> 3) & 0x1f];
262 } else {
263 return arcs[arc + 1];
264 }
265 }
266
267 /**
268 * {@inheritDoc}
269 */
270 @Override
271 public int getRightLanguageCount(int node) {
272 assert getFlags().contains(FSAFlags.NUMBERS): "This FSA was not compiled with NUMBERS.";
273 return FSA5.decodeFromBytes(arcs, node, nodeDataLength);
274 }
275
276 /**
277 * {@inheritDoc}
278 */
279 @Override
280 public boolean isArcFinal(int arc) {
281 return (arcs[arc] & BIT_FINAL_ARC) != 0;
282 }
283
284 /**
285 * {@inheritDoc}
286 */
287 @Override
288 public boolean isArcTerminal(int arc) {
289 return (0 == getDestinationNodeOffset(arc));
290 }
291
292 /**
293 * Returns <code>true</code> if this arc has <code>NEXT</code> bit set.
294 *
295 * @see #BIT_LAST_ARC
296 * @param arc The node's arc identifier.
297 * @return Returns true if the argument is the last arc of a node.
298 */
299 public boolean isArcLast(int arc) {
300 return (arcs[arc] & BIT_LAST_ARC) != 0;
301 }
302
303 /**
304 * @see #BIT_TARGET_NEXT
305 * @param arc The node's arc identifier.
306 * @return Returns true if {@link #BIT_TARGET_NEXT} is set for this arc.
307 */
308 public boolean isNextSet(int arc) {
309 return (arcs[arc] & BIT_TARGET_NEXT) != 0;
310 }
311
312 /**
313 * @param arc The node's arc identifier.
314 * @return Returns <code>true</code> if the label is compressed inside flags byte.
315 */
316 public boolean isLabelCompressed(int arc) {
317 assert isNextSet(arc) : "Only applicable to arcs with NEXT bit.";
318 return (arcs[arc] & (-1 << 3)) != 0;
319 }
320
321 /**
322 * {@inheritDoc}
323 *
324 * <p>For this automaton version, an additional {@link FSAFlags#NUMBERS} flag
325 * may be set to indicate the automaton contains extra fields for each node.</p>
326 */
327 public Set<FSAFlags> getFlags() {
328 return Collections.unmodifiableSet(flags);
329 }
330
331 /**
332 * Returns the address of the node pointed to by this arc.
333 */
334 final int getDestinationNodeOffset(int arc) {
335 if (isNextSet(arc)) {
336 /* The destination node follows this arc in the array. */
337 return skipArc(arc);
338 } else {
339 /*
340 * The destination node address has to be extracted from the arc's
341 * goto field.
342 */
343 int r = 0;
344 for (int i = gtl; --i >= 1;) {
345 r = r << 8 | (arcs[arc + 1 + i] & 0xff);
346 }
347 r = r << 8 | (arcs[arc] & 0xff);
348 return r >>> 3;
349 }
350 }
351
352 /**
353 * Read the arc's layout and skip as many bytes, as needed, to skip it.
354 */
355 private int skipArc(int offset) {
356 if (isNextSet(offset)) {
357 if (isLabelCompressed(offset)) {
358 offset++;
359 } else {
360 offset += 1 + 1;
361 }
362 } else {
363 offset += 1 + gtl;
364 }
365 return offset;
366 }
367 }