Debian Codegraph @

morfologik-speller/src/main/java/morfologik/speller/HMatrix.java

raw symbols SCIP
1 package morfologik.speller;
2
3 /**
4 * Keeps track of already computed values of edit distance.
5 * Remarks: To save space, the matrix is kept in a vector.
6 */
7 public class HMatrix {
8 private int[] p; /* the vector */
9 private int rowLength; /* row length of matrix */
10 int columnHeight; /* column height of matrix */
11 int editDistance; /* edit distance */
12
13 /**
14 * Allocates memory and initializes matrix (constructor).
15 *
16 * @param distance (int) max edit distance allowed for
17 * candidates;
18 * @param maxLength (int) max length of words.
19 *
20 * Remarks: See Oflazer. To save space, the matrix is
21 * stored as a vector. To save time, additional rows and
22 * columns are added. They are initialized to their distance in
23 * the matrix, so that no bound checking is necessary during
24 * access.
25 */
26 public HMatrix(final int distance, final int maxLength) {
27 rowLength = maxLength + 2;
28 columnHeight = 2 * distance + 3;
29 editDistance = distance;
30 final int size = rowLength * columnHeight;
31 p = new int[size];
32 // Initialize edges of the diagonal band to distance + 1 (i.e.
33 // distance too big)
34 for (int i = 0; i < rowLength - distance - 1; i++) {
35 p[i] = distance + 1; // H(distance + j, j) = distance + 1
36 p[size - i - 1] = distance + 1; // H(i, distance + i) = distance
37 // + 1
38 }
39 // Initialize items H(i,j) with at least one index equal to zero to
40 // |i - j|
41 for (int j = 0; j < 2 * distance + 1; j++) {
42 p[j * rowLength] = distance + 1 - j; // H(i=0..distance+1,0)=i
43 // FIXME: fordistance == 2 we exceed the array size here.
44 // there's a bug in spell.cc, Jan Daciuk has been notified about it.
45 p[Math.min(p.length - 1, (j + distance + 1) * rowLength + j)] = j; // H(0,j=0..distance+1)=j
46 }
47 }
48
49 /**
50 * Provide an item of hMatrix indexed by indices.
51 *
52 * @param i
53 * - (int) row number;
54 * @param j
55 * - (int) column number.
56 * @return Item <code>H[i][j]</code>. Remarks: H matrix is really simulated. What is needed is only
57 * <code>2 * edit_distance + 1</code> wide band around the diagonal. In fact
58 * this diagonal has been pushed up to the upper border of the
59 * matrix.
60 *
61 * The matrix in the vector looks likes this:
62
63 * <pre>
64 * +---------------------+
65 * 0 |#####################| j=i-e-1
66 * 1 | | j=i-e
67 * : :
68 * e+1 | | j=i-1
69 * +---------------------+
70 * e+2 | | j=i
71 * +---------------------+
72 * e+3 | | j=i+1
73 * : :
74 * 2e+2| | j=i+e
75 * 2e+3|#####################| j=i+e+1
76 * +---------------------+
77 * </pre>
78 */
79 public int get(final int i, final int j) {
80 return p[(j - i + editDistance + 1) * rowLength + j];
81 }
82
83 /**
84 * Set an item in hMatrix.
85 * No checking for i &amp; j is done. They must be correct.
86 *
87 * @param i
88 * - (int) row number;
89 * @param j
90 * - (int) column number;
91 * @param val
92 * - (int) value to put there.
93 */
94 public void set(final int i, final int j, final int val) {
95 p[(j - i + editDistance + 1) * rowLength + j] = val;
96 }
97
98 }