-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5df5ed
commit 484d22a
Showing
13 changed files
with
97 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
project( | ||
'werpy_test_v4', | ||
'c', | ||
version : '0.0.1', | ||
default_options : ['warning_level=3']) | ||
|
||
py = import('python').find_installation(pure: false) | ||
|
||
cython = find_program('cython') | ||
pyx_files = files('werpy_test_v4/metrics.pyx') | ||
c_files = custom_target('cythonize', | ||
output : 'metrics.c', | ||
input : pyx_files, | ||
command : [cython, '-3', '--fast-fail', '-o', '@OUTPUT@', '@INPUT@']) | ||
|
||
py.extension_module( | ||
'werpy_test_v4', | ||
c_files, | ||
install : true | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# metrics.pyx | ||
import numpy as np | ||
cimport numpy as np | ||
|
||
# Add cimport cython here | ||
cimport cython | ||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
cpdef np.ndarray calculations(object reference, object hypothesis): | ||
cdef list reference_word = reference.split() | ||
cdef list hypothesis_word = hypothesis.split() | ||
|
||
cdef Py_ssize_t m, n, i, j, substitution_cost, ld, insertions, deletions, substitutions | ||
cdef list inserted_words, deleted_words, substituted_words | ||
m, n = len(reference_word), len(hypothesis_word) | ||
ldm = [[0] * (n + 1) for _ in range(m + 1)] | ||
|
||
for i in range(m + 1): | ||
for j in range(n + 1): | ||
if i == 0: | ||
ldm[i][j] = j | ||
elif j == 0: | ||
ldm[i][j] = i | ||
else: | ||
substitution_cost = 0 if reference_word[i - 1] == hypothesis_word[j - 1] else 1 | ||
ldm[i][j] = min( | ||
ldm[i - 1][j] + 1, # Deletion | ||
ldm[i][j - 1] + 1, # Insertion | ||
ldm[i - 1][j - 1] + substitution_cost # Substitution | ||
) | ||
|
||
ld = ldm[m][n] | ||
wer = ld / m | ||
|
||
insertions, deletions, substitutions = 0, 0, 0 | ||
inserted_words, deleted_words, substituted_words = [], [], [] | ||
i, j = m, n | ||
while i > 0 or j > 0: | ||
if i > 0 and j > 0 and reference_word[i - 1] == hypothesis_word[j - 1]: | ||
i -= 1 | ||
j -= 1 | ||
else: | ||
if i > 0 and j > 0 and ldm[i][j] == ldm[i - 1][j - 1] + 1: | ||
substitutions += 1 | ||
substituted_words.append((reference_word[i - 1], hypothesis_word[j - 1])) | ||
i -= 1 | ||
j -= 1 | ||
elif j > 0 and ldm[i][j] == ldm[i][j - 1] + 1: | ||
insertions += 1 | ||
inserted_words.append(hypothesis_word[j - 1]) | ||
j -= 1 | ||
elif i > 0 and ldm[i][j] == ldm[i - 1][j] + 1: | ||
deletions += 1 | ||
deleted_words.append(reference_word[i - 1]) | ||
i -= 1 | ||
|
||
inserted_words.reverse(), deleted_words.reverse(), substituted_words.reverse() | ||
|
||
return np.array( | ||
[wer, ld, m, insertions, deletions, substitutions, inserted_words, deleted_words, substituted_words], | ||
dtype=object) | ||
|
||
def metrics(reference, hypothesis): | ||
vectorize_calculations = np.vectorize(calculations) | ||
result = vectorize_calculations(reference, hypothesis) | ||
return result |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.