Spaces:
Running
Running
Fixed bug in phone error rate
Browse files- phone_distance.py +28 -1
phone_distance.py
CHANGED
|
@@ -82,6 +82,33 @@ Examples:
|
|
| 82 |
|
| 83 |
"""
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
| 87 |
class PhoneDistance(evaluate.Metric):
|
|
@@ -128,7 +155,7 @@ class PhoneDistance(evaluate.Metric):
|
|
| 128 |
else:
|
| 129 |
hd = distance_computer.hamming_feature_edit_distance(p, r)
|
| 130 |
hamming_distances.append(hd)
|
| 131 |
-
per =
|
| 132 |
phone_error_rates.append(per)
|
| 133 |
fer = distance_computer.feature_error_rate(p, r)
|
| 134 |
feature_error_rates.append(fer)
|
|
|
|
| 82 |
|
| 83 |
"""
|
| 84 |
|
| 85 |
+
def phone_error_rate(prediction:str, reference: str, distance_computer:panphon.distance.Distance):
|
| 86 |
+
"""Computes phone error rates. This is similar to the Distance.phoneme_error_rate function, but
|
| 87 |
+
is more efficient and fixes some bugs related to normalization in the original function.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
distance_computer (panphon.distance.Distance): computes edit distance and resolves characters to phones
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
float: the phone error rate
|
| 94 |
+
"""
|
| 95 |
+
if reference: # Can only be computed when the length of the reference greater than 0
|
| 96 |
+
pred_phones = distance_computer.fm.ipa_segs(prediction)
|
| 97 |
+
ref_phones = distance_computer.fm.ipa_segs(reference)
|
| 98 |
+
|
| 99 |
+
phone_edits = distance_computer.min_edit_distance(
|
| 100 |
+
lambda x: 1, # deletion cost
|
| 101 |
+
lambda x: 1, # insertion cost
|
| 102 |
+
lambda x, y: 0 if x == y else 1, # substitution cost,
|
| 103 |
+
[[]],
|
| 104 |
+
pred_phones,
|
| 105 |
+
ref_phones
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
return phone_edits / len(ref_phones)
|
| 109 |
+
else:
|
| 110 |
+
return 0.0
|
| 111 |
+
|
| 112 |
|
| 113 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
| 114 |
class PhoneDistance(evaluate.Metric):
|
|
|
|
| 155 |
else:
|
| 156 |
hd = distance_computer.hamming_feature_edit_distance(p, r)
|
| 157 |
hamming_distances.append(hd)
|
| 158 |
+
per = phone_error_rate(p, r, distance_computer)
|
| 159 |
phone_error_rates.append(per)
|
| 160 |
fer = distance_computer.feature_error_rate(p, r)
|
| 161 |
feature_error_rates.append(fer)
|