Skip to content

Commit c19ca61

Browse files
committed
fix(cython): use cnp.int32_t typed buffers for Levenshtein DP matrices
1 parent 616e076 commit c19ca61

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

werpy/metrics.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ cpdef cnp.ndarray calculations(object reference, object hypothesis):
5555
# SAFETY: All cells are explicitly initialized below (row 0, col 0, then DP loop).
5656
# Allocate the (m+1) x (n+1) DP matrix without zero-initialization to avoid
5757
# redundant memory writes. Boundary conditions are initialized explicitly.
58-
cdef int[:, :] ldm = np.empty((m + 1, n + 1), dtype=np.int32)
58+
cdef cnp.int32_t[:, :] ldm = np.empty((m + 1, n + 1), dtype=np.int32)
5959

6060
# Initialize first column and first row (boundary conditions)
6161
for i in range(m + 1):
62-
ldm[i, 0] = <int>i
62+
ldm[i, 0] = <cnp.int32_t>i
6363
for j in range(n + 1):
64-
ldm[0, j] = <int>j
64+
ldm[0, j] = <cnp.int32_t>j
6565

6666
# Fill the Levenshtein distance matrix
6767
# Compute edit distances using a branch-free inner loop and manual minimum
@@ -181,13 +181,13 @@ cpdef cnp.ndarray calculations_fast(object reference, object hypothesis):
181181
cdef int cost, del_cost, ins_cost, sub_cost, best
182182

183183
# Allocate the (m+1) x (n+1) DP matrix without zero-initialization
184-
cdef int[:, :] ldm = np.empty((m + 1, n + 1), dtype=np.int32)
184+
cdef cnp.int32_t[:, :] ldm = np.empty((m + 1, n + 1), dtype=np.int32)
185185

186186
# Initialize first column and first row (boundary conditions)
187187
for i in range(m + 1):
188-
ldm[i, 0] = <int>i
188+
ldm[i, 0] = <cnp.int32_t>i
189189
for j in range(n + 1):
190-
ldm[0, j] = <int>j
190+
ldm[0, j] = <cnp.int32_t>j
191191

192192
# Fill the Levenshtein distance matrix
193193
for i in range(1, m + 1):
@@ -295,14 +295,14 @@ cpdef cnp.ndarray calculations_wer_only(object reference, object hypothesis):
295295
cdef cnp.ndarray prev_arr = np.empty(n + 1, dtype=np.int32)
296296
cdef cnp.ndarray curr_arr = np.empty(n + 1, dtype=np.int32)
297297

298-
cdef int[:] prev = prev_arr
299-
cdef int[:] curr = curr_arr
298+
cdef cnp.int32_t[:] prev = prev_arr
299+
cdef cnp.int32_t[:] curr = curr_arr
300300

301301
for j in range(n + 1):
302-
prev[j] = <int>j
302+
prev[j] = <cnp.int32_t>j
303303

304304
for i in range(1, m + 1):
305-
curr[0] = <int>i
305+
curr[0] = <cnp.int32_t>i
306306
for j in range(1, n + 1):
307307
cost = 0 if reference_word[i - 1] == hypothesis_word[j - 1] else 1
308308

0 commit comments

Comments
 (0)