| 1 | package morfologik.fsa; |
| 2 | |
| 3 | import static morfologik.fsa.FSAFlags.*; |
| 4 | |
| 5 | import java.io.DataInputStream; |
| 6 | import java.io.IOException; |
| 7 | import java.io.InputStream; |
| 8 | import java.util.Collections; |
| 9 | import java.util.EnumSet; |
| 10 | import java.util.Set; |
| 11 | |
| 12 | /** |
| 13 | * FSA binary format implementation for version 5. |
| 14 | * |
| 15 | * <p> |
| 16 | * Version 5 indicates the dictionary was built with these flags: |
| 17 | * {@link FSAFlags#FLEXIBLE}, {@link FSAFlags#STOPBIT} and |
| 18 | * {@link FSAFlags#NEXTBIT}. The internal representation of the FSA must |
| 19 | * therefore follow this description (please note this format describes only a |
| 20 | * single transition (arc), not the entire dictionary file). |
| 21 | * |
| 22 | * <pre> |
| 23 | * ---- this node header present only if automaton was compiled with NUMBERS option. |
| 24 | * Byte |
| 25 | * +-+-+-+-+-+-+-+-+\ |
| 26 | * 0 | | | | | | | | | \ LSB |
| 27 | * +-+-+-+-+-+-+-+-+ + |
| 28 | * 1 | | | | | | | | | | number of strings recognized |
| 29 | * +-+-+-+-+-+-+-+-+ +----- by the automaton starting |
| 30 | * : : : : : : : : : | from this node. |
| 31 | * +-+-+-+-+-+-+-+-+ + |
| 32 | * ctl-1 | | | | | | | | | / MSB |
| 33 | * +-+-+-+-+-+-+-+-+/ |
| 34 | * |
| 35 | * ---- remaining part of the node |
| 36 | * |
| 37 | * Byte |
| 38 | * +-+-+-+-+-+-+-+-+\ |
| 39 | * 0 | | | | | | | | | +------ label |
| 40 | * +-+-+-+-+-+-+-+-+/ |
| 41 | * |
| 42 | * +------------- node pointed to is next |
| 43 | * | +----------- the last arc of the node |
| 44 | * | | +--------- the arc is final |
| 45 | * | | | |
| 46 | * +-----------+ |
| 47 | * | | | | | |
| 48 | * ___+___ | | | | |
| 49 | * / \ | | | | |
| 50 | * MSB LSB | |
| 51 | * 7 6 5 4 3 2 1 0 | |
| 52 | * +-+-+-+-+-+-+-+-+ | |
| 53 | * 1 | | | | | | | | | \ \ |
| 54 | * +-+-+-+-+-+-+-+-+ \ \ LSB |
| 55 | * +-+-+-+-+-+-+-+-+ + |
| 56 | * 2 | | | | | | | | | | |
| 57 | * +-+-+-+-+-+-+-+-+ | |
| 58 | * 3 | | | | | | | | | +----- target node address (in bytes) |
| 59 | * +-+-+-+-+-+-+-+-+ | (not present except for the byte |
| 60 | * : : : : : : : : : | with flags if the node pointed to |
| 61 | * +-+-+-+-+-+-+-+-+ + is next) |
| 62 | * gtl | | | | | | | | | / MSB |
| 63 | * +-+-+-+-+-+-+-+-+ / |
| 64 | * gtl+1 (gtl = gotoLength) |
| 65 | * </pre> |
| 66 | */ |
| 67 | public final class FSA5 extends FSA { |
| 68 | /** |
| 69 | * Default filler byte. |
| 70 | */ |
| 71 | public final static byte DEFAULT_FILLER = '_'; |
| 72 | |
| 73 | /** |
| 74 | * Default annotation byte. |
| 75 | */ |
| 76 | public final static byte DEFAULT_ANNOTATION = '+'; |
| 77 | |
| 78 | /** |
| 79 | * Automaton version as in the file header. |
| 80 | */ |
| 81 | public static final byte VERSION = 5; |
| 82 | |
| 83 | /** |
| 84 | * Bit indicating that an arc corresponds to the last character of a sequence |
| 85 | * available when building the automaton. |
| 86 | */ |
| 87 | public static final int BIT_FINAL_ARC = 1 << 0; |
| 88 | |
| 89 | /** |
| 90 | * Bit indicating that an arc is the last one of the node's list and the |
| 91 | * following one belongs to another node. |
| 92 | */ |
| 93 | public static final int BIT_LAST_ARC = 1 << 1; |
| 94 | |
| 95 | /** |
| 96 | * Bit indicating that the target node of this arc follows it in the |
| 97 | * compressed automaton structure (no goto field). |
| 98 | */ |
| 99 | public static final int BIT_TARGET_NEXT = 1 << 2; |
| 100 | |
| 101 | /** |
| 102 | * An offset in the arc structure, where the address and flags field begins. |
| 103 | * In version 5 of FSA automata, this value is constant (1, skip label). |
| 104 | */ |
| 105 | public final static int ADDRESS_OFFSET = 1; |
| 106 | |
| 107 | /** |
| 108 | * An array of bytes with the internal representation of the automaton. Please |
| 109 | * see the documentation of this class for more information on how this |
| 110 | * structure is organized. |
| 111 | */ |
| 112 | public final byte[] arcs; |
| 113 | |
| 114 | /** |
| 115 | * The length of the node header structure (if the automaton was compiled with |
| 116 | * <code>NUMBERS</code> option). Otherwise zero. |
| 117 | */ |
| 118 | public final int nodeDataLength; |
| 119 | |
| 120 | /** |
| 121 | * Flags for this automaton version. |
| 122 | */ |
| 123 | private Set<FSAFlags> flags; |
| 124 | |
| 125 | /** |
| 126 | * Number of bytes each address takes in full, expanded form (goto length). |
| 127 | */ |
| 128 | public final int gtl; |
| 129 | |
| 130 | /** Filler character. */ |
| 131 | public final byte filler; |
| 132 | |
| 133 | /** Annotation character. */ |
| 134 | public final byte annotation; |
| 135 | |
| 136 | /** |
| 137 | * Read and wrap a binary automaton in FSA version 5. |
| 138 | */ |
| 139 | FSA5(InputStream stream) throws IOException { |
| 140 | DataInputStream in = new DataInputStream(stream); |
| 141 | |
| 142 | this.filler = in.readByte(); |
| 143 | this.annotation = in.readByte(); |
| 144 | final byte hgtl = in.readByte(); |
| 145 | |
| 146 | /* |
| 147 | * Determine if the automaton was compiled with NUMBERS. If so, modify |
| 148 | * ctl and goto fields accordingly. |
| 149 | */ |
| 150 | flags = EnumSet.of(FLEXIBLE, STOPBIT, NEXTBIT); |
| 151 | if ((hgtl & 0xf0) != 0) { |
| 152 | flags.add(NUMBERS); |
| 153 | } |
| 154 | |
| 155 | flags = Collections.unmodifiableSet(flags); |
| 156 | |
| 157 | this.nodeDataLength = (hgtl >>> 4) & 0x0f; |
| 158 | this.gtl = hgtl & 0x0f; |
| 159 | |
| 160 | arcs = readRemaining(in); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns the start node of this automaton. |
| 165 | */ |
| 166 | @Override |
| 167 | public int getRootNode() { |
| 168 | // Skip dummy node marking terminating state. |
| 169 | final int epsilonNode = skipArc(getFirstArc(0)); |
| 170 | |
| 171 | // And follow the epsilon node's first (and only) arc. |
| 172 | return getDestinationNodeOffset(getFirstArc(epsilonNode)); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * {@inheritDoc} |
| 177 | */ |
| 178 | @Override |
| 179 | public final int getFirstArc(int node) { |
| 180 | return nodeDataLength + node; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * {@inheritDoc} |
| 185 | */ |
| 186 | @Override |
| 187 | public final int getNextArc(int arc) { |
| 188 | if (isArcLast(arc)) |
| 189 | return 0; |
| 190 | else |
| 191 | return skipArc(arc); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * {@inheritDoc} |
| 196 | */ |
| 197 | @Override |
| 198 | public int getArc(int node, byte label) { |
| 199 | for (int arc = getFirstArc(node); arc != 0; arc = getNextArc(arc)) { |
| 200 | if (getArcLabel(arc) == label) |
| 201 | return arc; |
| 202 | } |
| 203 | |
| 204 | // An arc labeled with "label" not found. |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * {@inheritDoc} |
| 210 | */ |
| 211 | @Override |
| 212 | public int getEndNode(int arc) { |
| 213 | final int nodeOffset = getDestinationNodeOffset(arc); |
| 214 | assert nodeOffset != 0 : "No target node for terminal arcs."; |
| 215 | return nodeOffset; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * {@inheritDoc} |
| 220 | */ |
| 221 | @Override |
| 222 | public byte getArcLabel(int arc) { |
| 223 | return arcs[arc]; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * {@inheritDoc} |
| 228 | */ |
| 229 | @Override |
| 230 | public boolean isArcFinal(int arc) { |
| 231 | return (arcs[arc + ADDRESS_OFFSET] & BIT_FINAL_ARC) != 0; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * {@inheritDoc} |
| 236 | */ |
| 237 | @Override |
| 238 | public boolean isArcTerminal(int arc) { |
| 239 | return (0 == getDestinationNodeOffset(arc)); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns the number encoded at the given node. The number equals the count |
| 244 | * of the set of suffixes reachable from <code>node</code> (called its right |
| 245 | * language). |
| 246 | */ |
| 247 | @Override |
| 248 | public int getRightLanguageCount(int node) { |
| 249 | assert getFlags().contains(FSAFlags.NUMBERS) : "This FSA was not compiled with NUMBERS."; |
| 250 | return decodeFromBytes(arcs, node, nodeDataLength); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * {@inheritDoc} |
| 255 | * |
| 256 | * <p> |
| 257 | * For this automaton version, an additional {@link FSAFlags#NUMBERS} flag may |
| 258 | * be set to indicate the automaton contains extra fields for each node. |
| 259 | * </p> |
| 260 | */ |
| 261 | @Override |
| 262 | public Set<FSAFlags> getFlags() { |
| 263 | return flags; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns <code>true</code> if this arc has <code>NEXT</code> bit set. |
| 268 | * |
| 269 | * @see #BIT_LAST_ARC |
| 270 | * @param arc The node's arc identifier. |
| 271 | * @return Returns true if the argument is the last arc of a node. |
| 272 | */ |
| 273 | public boolean isArcLast(int arc) { |
| 274 | return (arcs[arc + ADDRESS_OFFSET] & BIT_LAST_ARC) != 0; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @see #BIT_TARGET_NEXT |
| 279 | * @param arc The node's arc identifier. |
| 280 | * @return Returns true if {@link #BIT_TARGET_NEXT} is set for this arc. |
| 281 | */ |
| 282 | public boolean isNextSet(int arc) { |
| 283 | return (arcs[arc + ADDRESS_OFFSET] & BIT_TARGET_NEXT) != 0; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Returns an n-byte integer encoded in byte-packed representation. |
| 288 | */ |
| 289 | static final int decodeFromBytes(final byte[] arcs, final int start, final int n) { |
| 290 | int r = 0; |
| 291 | for (int i = n; --i >= 0;) { |
| 292 | r = r << 8 | (arcs[start + i] & 0xff); |
| 293 | } |
| 294 | return r; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Returns the address of the node pointed to by this arc. |
| 299 | */ |
| 300 | final int getDestinationNodeOffset(int arc) { |
| 301 | if (isNextSet(arc)) { |
| 302 | /* The destination node follows this arc in the array. */ |
| 303 | return skipArc(arc); |
| 304 | } else { |
| 305 | /* |
| 306 | * The destination node address has to be extracted from the arc's |
| 307 | * goto field. |
| 308 | */ |
| 309 | return decodeFromBytes(arcs, arc + ADDRESS_OFFSET, gtl) >>> 3; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Read the arc's layout and skip as many bytes, as needed. |
| 315 | */ |
| 316 | private int skipArc(int offset) { |
| 317 | return offset + |
| 318 | (isNextSet(offset) |
| 319 | ? 1 + 1 /* label + flags */ |
| 320 | : 1 + gtl /* label + flags/address */); |
| 321 | } |
| 322 | } |