Debian Codegraph @

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

raw symbols SCIP
1 package morfologik.fsa;
2
3 import static morfologik.fsa.MatchResult.*;
4
5 /**
6 * This class implements some common matching and scanning operations on a
7 * generic FSA.
8 */
9 public final class FSATraversal {
10 /**
11 * Target automaton.
12 */
13 private final FSA fsa;
14
15 /**
16 * Traversals of the given FSA.
17 *
18 * @param fsa The target automaton for traversals.
19 */
20 public FSATraversal(FSA fsa) {
21 this.fsa = fsa;
22 }
23
24 /**
25 * Calculate perfect hash for a given input sequence of bytes. The perfect hash requires
26 * that {@link FSA} is built with {@link FSAFlags#NUMBERS} and corresponds to the sequential
27 * order of input sequences used at automaton construction time.
28
29 * @param sequence The byte sequence to calculate perfect hash for.
30 * @param start Start index in the sequence array.
31 * @param length Length of the byte sequence, must be at least 1.
32 * @param node The node to start traversal from, typically the {@linkplain FSA#getRootNode() root node}.
33 *
34 * @return Returns a unique integer assigned to the input sequence in the automaton (reflecting
35 * the number of that sequence in the input used to build the automaton). Returns a negative
36 * integer if the input sequence was not part of the input from which the automaton was created.
37 * The type of mismatch is a constant defined in {@link MatchResult}.
38 */
39 public int perfectHash(byte[] sequence, int start, int length, int node) {
40 assert fsa.getFlags().contains(FSAFlags.NUMBERS) : "FSA not built with NUMBERS option.";
41 assert length > 0 : "Must be a non-empty sequence.";
42
43 int hash = 0;
44 final int end = start + length - 1;
45
46 int seqIndex = start;
47 byte label = sequence[seqIndex];
48
49 // Seek through the current node's labels, looking for 'label', update hash.
50 for (int arc = fsa.getFirstArc(node); arc != 0;) {
51 if (fsa.getArcLabel(arc) == label) {
52 if (fsa.isArcFinal(arc)) {
53 if (seqIndex == end) {
54 return hash;
55 }
56
57 hash++;
58 }
59
60 if (fsa.isArcTerminal(arc)) {
61 /* The automaton contains a prefix of the input sequence. */
62 return AUTOMATON_HAS_PREFIX;
63 }
64
65 // The sequence is a prefix of one of the sequences stored in the automaton.
66 if (seqIndex == end) {
67 return SEQUENCE_IS_A_PREFIX;
68 }
69
70 // Make a transition along the arc, go the target node's first arc.
71 arc = fsa.getFirstArc(fsa.getEndNode(arc));
72 label = sequence[++seqIndex];
73 continue;
74 } else {
75 if (fsa.isArcFinal(arc)) {
76 hash++;
77 }
78 if (!fsa.isArcTerminal(arc)) {
79 hash += fsa.getRightLanguageCount(fsa.getEndNode(arc));
80 }
81 }
82
83 arc = fsa.getNextArc(arc);
84 }
85
86 if (seqIndex > start) {
87 return AUTOMATON_HAS_PREFIX;
88 } else {
89 // Labels of this node ended without a match on the sequence.
90 // Perfect hash does not exist.
91 return NO_MATCH;
92 }
93 }
94
95 /**
96 * @param sequence The byte sequence to calculate perfect hash for.
97 * @see #perfectHash(byte[], int, int, int)
98 * @return Returns a unique integer assigned to the input sequence in the automaton (reflecting
99 * the number of that sequence in the input used to build the automaton). Returns a negative
100 * integer if the input sequence was not part of the input from which the automaton was created.
101 * The type of mismatch is a constant defined in {@link MatchResult}.
102 */
103 public int perfectHash(byte[] sequence) {
104 return perfectHash(sequence, 0, sequence.length, fsa.getRootNode());
105 }
106
107 /**
108 * Same as {@link #match(byte[], int, int, int)}, but allows passing
109 * a reusable {@link MatchResult} object so that no intermediate garbage is
110 * produced.
111 *
112 * @param reuse The {@link MatchResult} to reuse.
113 * @param sequence Input sequence to look for in the automaton.
114 * @param start Start index in the sequence array.
115 * @param length Length of the byte sequence, must be at least 1.
116 * @param node The node to start traversal from, typically the {@linkplain FSA#getRootNode() root node}.
117 *
118 * @return The same object as <code>reuse</code>, but with updated match {@link MatchResult#kind}
119 * and other relevant fields.
120 */
121 public MatchResult match(MatchResult reuse, byte[] sequence, int start, int length, int node) {
122 if (node == 0) {
123 reuse.reset(NO_MATCH, start, node);
124 return reuse;
125 }
126
127 final FSA fsa = this.fsa;
128 final int end = start + length;
129 for (int i = start; i < end; i++) {
130 final int arc = fsa.getArc(node, sequence[i]);
131 if (arc != 0) {
132 if (i + 1 == end && fsa.isArcFinal(arc)) {
133 /* The automaton has an exact match of the input sequence. */
134 reuse.reset(EXACT_MATCH, i, node);
135 return reuse;
136 }
137
138 if (fsa.isArcTerminal(arc)) {
139 /* The automaton contains a prefix of the input sequence. */
140 reuse.reset(AUTOMATON_HAS_PREFIX, i + 1, node);
141 return reuse;
142 }
143
144 // Make a transition along the arc.
145 node = fsa.getEndNode(arc);
146 } else {
147 if (i > start) {
148 reuse.reset(AUTOMATON_HAS_PREFIX, i, node);
149 } else {
150 reuse.reset(NO_MATCH, i, node);
151 }
152 return reuse;
153 }
154 }
155
156 /* The sequence is a prefix of at least one sequence in the automaton. */
157 reuse.reset(SEQUENCE_IS_A_PREFIX, 0, node);
158 return reuse;
159 }
160
161 /**
162 * Finds a matching path in the dictionary for a given sequence of labels from
163 * <code>sequence</code> and starting at node <code>node</code>.
164 *
165 * @param sequence Input sequence to look for in the automaton.
166 * @param start Start index in the sequence array.
167 * @param length Length of the byte sequence, must be at least 1.
168 * @param node The node to start traversal from, typically the {@linkplain FSA#getRootNode() root node}.
169 *
170 * @see #match(byte [], int)
171 * @return {@link MatchResult} with updated match {@link MatchResult#kind}.
172 */
173 public MatchResult match(byte[] sequence, int start, int length, int node) {
174 return match(new MatchResult(), sequence, start, length, node);
175 }
176
177 /**
178 * @param sequence Input sequence to look for in the automaton.
179 * @param node The node to start traversal from, typically the {@linkplain FSA#getRootNode() root node}.
180 *
181 * @see #match(byte [], int)
182 * @return {@link MatchResult} with updated match {@link MatchResult#kind}.
183 */
184 public MatchResult match(byte[] sequence, int node) {
185 return match(sequence, 0, sequence.length, node);
186 }
187
188 /**
189 * @param sequence Input sequence to look for in the automaton.
190 *
191 * @see #match(byte [], int)
192 * @return {@link MatchResult} with updated match {@link MatchResult#kind}.
193 */
194 public MatchResult match(byte[] sequence) {
195 return match(sequence, fsa.getRootNode());
196 }
197 }