File size: 7,185 Bytes
0e8589b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Dataset-normalized word error rate (WER)."""

import re

import jiwer
from word2number import w2n


def convert_text(text: str) -> str:
	"""Convert the dataset's spoken English number forms to digits."""
	text = re.sub(
		r"\bcovid-nineteen\b",
		"COVID-19",
		text,
		flags=re.IGNORECASE,
	)

	decade_map = {
		"hundreds": 0,
		"tens": 10,
		"twenties": 20,
		"thirties": 30,
		"forties": 40,
		"fifties": 50,
		"sixties": 60,
		"seventies": 70,
		"eighties": 80,
		"nineties": 90,
	}

	def decade_to_number(match):
		prefix = match.group(1).lower()
		suffix = match.group(2).lower()
		base = 1900 if prefix == "nineteen" else 2000
		return f"{base + decade_map[suffix]}s"

	text = re.sub(
		r"\b(nineteen|twenty)\s+"
		r"(hundreds|tens|twenties|thirties|forties|fifties|"
		r"sixties|seventies|eighties|nineties)\b",
		decade_to_number,
		text,
		flags=re.IGNORECASE,
	)

	def spoken_year_to_number(match):
		prefix = match.group(1).lower()
		remainder = match.group(2)
		try:
			value = w2n.word_to_num(remainder)
			if 0 <= value <= 99:
				base = 1900 if prefix == "nineteen" else 2000
				return str(base + value)
		except Exception:
			pass
		return match.group(0)

	text = re.sub(
		r"\b(nineteen|twenty)\s+"
		r"(ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|"
		r"eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|"
		r"eighty|ninety)"
		r"(?:[\s-](?:zero|one|two|three|four|five|six|seven|eight|nine))?\b",
		spoken_year_to_number,
		text,
		flags=re.IGNORECASE,
	)

	text = re.sub(
		r"\b(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)"
		r"\s+(am|pm)\b",
		lambda match: (
			f"{w2n.word_to_num(match.group(1))} {match.group(2).upper()}"
		),
		text,
		flags=re.IGNORECASE,
	)

	def spoken_time_to_number(match):
		try:
			hour = w2n.word_to_num(match.group(1))
			minute = w2n.word_to_num(match.group(2))
			return f"{hour}:{minute:02d}"
		except Exception:
			return match.group(0)

	text = re.sub(
		r"\b(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)\s+"
		r"(ten|twenty|thirty|forty|fifty)"
		r"(?:[\s-](?:one|two|three|four|five|six|seven|eight|nine))?\b",
		spoken_time_to_number,
		text,
		flags=re.IGNORECASE,
	)

	def filipino_time_to_number(match):
		hour_map = {
			"una": 1,
			"dos": 2,
			"tres": 3,
			"kwatro": 4,
			"singko": 5,
			"sais": 6,
			"syete": 7,
			"otso": 8,
			"nwebe": 9,
			"dyis": 10,
			"onse": 11,
			"dose": 12,
		}
		hour = hour_map.get(match.group(2).lower())
		if hour is None:
			return match.group(0)
		return f"{hour}:30" if match.group(3) else f"{hour}:00"

	text = re.sub(
		r"\b(ala|alas)-"
		r"(una|dos|tres|kwatro|singko|sais|syete|otso|nwebe|dyis|onse|dose)"
		r"(\s+y\s+medya)?\b",
		filipino_time_to_number,
		text,
		flags=re.IGNORECASE,
	)

	def ordinal_to_number(match):
		try:
			number = w2n.word_to_num(match.group(0).replace("-", " "))
			if 10 <= number % 100 <= 20:
				suffix = "th"
			else:
				suffix = {1: "st", 2: "nd", 3: "rd"}.get(
				number % 10,
				"th",
			)
			return f"{number}{suffix}"
		except Exception:
			return match.group(0)

	ordinal_pattern = (
		r"\b(?:(?:one|two|three|four|five|six|seven|eight|nine|ten|"
		r"eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|"
		r"eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|"
		r"eighty|ninety)[\s-])*"
		r"(?:first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|"
		r"tenth|eleventh|twelfth|thirteenth|fourteenth|fifteenth|"
		r"sixteenth|seventeenth|eighteenth|nineteenth|twentieth|"
		r"thirtieth|fortieth|fiftieth|sixtieth|seventieth|eightieth|"
		r"ninetieth)\b"
	)
	text = re.sub(
		ordinal_pattern,
		ordinal_to_number,
		text,
		flags=re.IGNORECASE,
	)

	def legal_reference_to_number(match):
		digit_words = {
			"zero": "0",
			"one": "1",
			"two": "2",
			"three": "3",
			"four": "4",
			"five": "5",
			"six": "6",
			"seven": "7",
			"eight": "8",
			"nine": "9",
		}
		digits = match.group(2).lower().split()
		if not all(digit in digit_words for digit in digits):
			return match.group(0)
		return f"{match.group(1)} {''.join(digit_words[d] for d in digits)}"

	text = re.sub(
		r"\b(RA|Article|Barangay|Pavilion)\s+"
		r"((?:zero|one|two|three|four|five|six|seven|eight|nine)"
		r"(?:\s+(?:zero|one|two|three|four|five|six|seven|eight|nine))*)\b",
		legal_reference_to_number,
		text,
		flags=re.IGNORECASE,
	)

	number_words = (
		r"zero|one|two|three|four|five|six|seven|eight|nine|ten|"
		r"eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|"
		r"eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|"
		r"eighty|ninety|hundred|thousand|million|billion"
	)

	def peso_amount_to_number(match):
		try:
			amount = w2n.word_to_num(match.group(1).replace("-", " "))
			return f"{amount} pesos"
		except Exception:
			return match.group(0)

	text = re.sub(
		rf"\b(({number_words})(?:[\s-]+(?:{number_words}))*)\s+pesos\b",
		peso_amount_to_number,
		text,
		flags=re.IGNORECASE,
	)

	def regular_number_to_number(match):
		try:
			words = re.sub(
				r"\band\b",
				"",
				match.group(0),
				flags=re.IGNORECASE,
			).replace("-", " ")
			return str(w2n.word_to_num(words))
		except Exception:
			return match.group(0)

	text = re.sub(
		rf"\b(({number_words})"
		rf"(?:[\s-]+(?:and\s+)?(?:{number_words}))*)\b",
		regular_number_to_number,
		text,
		flags=re.IGNORECASE,
	)
	return text


def normalize_for_wer(text: object) -> str:
	"""Apply the dataset's casing, punctuation, and whitespace cleanup."""
	text = str(text).casefold()
	text = re.sub(r"[^\w\s']", " ", text, flags=re.UNICODE)
	text = text.replace("_", " ")
	return " ".join(text.split())


def wer(reference, hypothesis):
	"""Compute the same digit-aware corpus WER with a JiWER-like API.

	For each pair, the normalized reference and its spoken-number-to-digit
	variant are compared with the hypothesis. The variant with fewer word edits
	is used in the final corpus score, matching the dataset evaluator.
	"""
	references = [reference] if isinstance(reference, str) else list(reference)
	hypotheses = (
		[hypothesis] if isinstance(hypothesis, str) else list(hypothesis)
	)
	if len(references) != len(hypotheses):
		raise ValueError(
			"reference and hypothesis must contain the same number of sentences"
		)

	normalized_references = []
	normalized_hypotheses = []
	for reference_text, hypothesis_text in zip(references, hypotheses):
		reference_raw = normalize_for_wer(reference_text)
		reference_with_digits = normalize_for_wer(
			convert_text(str(reference_text))
		)
		hypothesis_normalized = normalize_for_wer(hypothesis_text)

		raw_result = jiwer.process_words(
			reference_raw,
			hypothesis_normalized,
		)
		digit_result = jiwer.process_words(
			reference_with_digits,
			hypothesis_normalized,
		)
		raw_errors = (
			raw_result.substitutions
			+ raw_result.deletions
			+ raw_result.insertions
		)
		digit_errors = (
			digit_result.substitutions
			+ digit_result.deletions
			+ digit_result.insertions
		)
		normalized_references.append(
			reference_raw
			if raw_errors < digit_errors
			else reference_with_digits
		)
		normalized_hypotheses.append(hypothesis_normalized)

	return jiwer.wer(normalized_references, normalized_hypotheses)