mict-zhaw commited on
Commit
3cf8d44
1 Parent(s): 5d4d143

Add methods `_remove_to_long_pauses` and `_filter_utterances_by_duration`

Browse files
Files changed (1) hide show
  1. chall.py +82 -7
chall.py CHANGED
@@ -20,14 +20,20 @@ logger = logging.get_logger(__name__)
20
  class ChallConfig(BuilderConfig):
21
 
22
  split_segments: bool = False
 
 
23
  max_chunk_length: Union[float, None] = None
24
- remove_trailing_pauses: bool = None
 
 
25
 
26
  def __init__(self, **kwargs):
27
- self.split_segments = kwargs.pop("split_segments", False)
28
- # settings that can only be used together with split_segments
29
- self.max_chunk_length = kwargs.pop("max_chunk_length", None)
30
- self.remove_trailing_pauses = kwargs.pop("remove_trailing_pauses", False)
 
 
31
  super(ChallConfig, self).__init__(**kwargs)
32
 
33
 
@@ -56,7 +62,10 @@ class Chall(GeneratorBasedBuilder):
56
  ChallConfig(
57
  name="asr_acl",
58
  split_segments=True,
 
59
  max_chunk_length=12,
 
 
60
  description="Settings used for the paper."
61
  )
62
  ]
@@ -97,10 +106,14 @@ class Chall(GeneratorBasedBuilder):
97
 
98
  def _info(self) -> DatasetInfo:
99
  """
100
- This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
101
  :return: The DatasetInfo object
102
  """
103
 
 
 
 
 
104
  if self.config.split_segments:
105
  features = Features({
106
  "audio_id": Value("string"), # todo maybe shorten to id
@@ -234,12 +247,18 @@ class Chall(GeneratorBasedBuilder):
234
 
235
  segments = transcript.get("segments", [])
236
 
 
 
 
237
  if self.config.max_chunk_length is not None:
238
  segments = self._split_long_utterances(segments)
239
 
240
  if self.config.remove_trailing_pauses:
241
  self._remove_trailing_pauses_in_segments(segments)
242
 
 
 
 
243
  for segment_i, segment in enumerate(segments):
244
 
245
  id_ = f"{audio_id}_{str(segment_i).rjust(3, '0')}"
@@ -271,9 +290,15 @@ class Chall(GeneratorBasedBuilder):
271
 
272
  yield id_, data
273
 
274
- def _remove_trailing_pauses_in_segments(self, segments: List[dict]) -> None:
 
275
  """
276
  Removes pauses at the end/start of utterances in each segment to eliminate pauses between segments.
 
 
 
 
 
277
  :return: A list of Word objects representing the removed pause indicators from the segments.
278
  """
279
  for segment in segments:
@@ -286,9 +311,59 @@ class Chall(GeneratorBasedBuilder):
286
  if not segment["words"]:
287
  segments.remove(segment)
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  def _split_long_utterances(self, segments: List[dict]) -> List[dict]:
290
  """
291
  Splits segments into smaller chunks if their duration exceeds the maximum chunk length specified in the config.
 
 
 
 
292
  :param segments: List of original segments from the transcript.
293
  :return: List of adjusted segments, potentially split into smaller chunks.
294
  """
 
20
  class ChallConfig(BuilderConfig):
21
 
22
  split_segments: bool = False
23
+
24
+ # settings that can only be used together with split_segments
25
  max_chunk_length: Union[float, None] = None
26
+ min_chunk_length: Union[float, None] = None
27
+ max_pause_length: Union[float, None] = None
28
+ remove_trailing_pauses: bool = False
29
 
30
  def __init__(self, **kwargs):
31
+ self.split_segments = kwargs.pop("split_segments", ChallConfig.split_segments)
32
+ self.remove_trailing_pauses = kwargs.pop("remove_trailing_pauses", ChallConfig.remove_trailing_pauses)
33
+ self.max_chunk_length = kwargs.pop("max_chunk_length", ChallConfig.max_chunk_length)
34
+ self.min_chunk_length = kwargs.pop("min_chunk_length", ChallConfig.min_chunk_length)
35
+ self.max_pause_length = kwargs.pop("max_pause_length", ChallConfig.max_pause_length)
36
+
37
  super(ChallConfig, self).__init__(**kwargs)
38
 
39
 
 
62
  ChallConfig(
63
  name="asr_acl",
64
  split_segments=True,
65
+ max_pause_length=12,
66
  max_chunk_length=12,
67
+ min_chunk_length=0.5,
68
+ remove_trailing_pauses=True,
69
  description="Settings used for the paper."
70
  )
71
  ]
 
106
 
107
  def _info(self) -> DatasetInfo:
108
  """
109
+ This method specifies the datasets.DatasetInfo object which contains information and typings for the dataset
110
  :return: The DatasetInfo object
111
  """
112
 
113
+ # todo text (make word = timestamps)
114
+ # todo duration
115
+ # todo tasks
116
+
117
  if self.config.split_segments:
118
  features = Features({
119
  "audio_id": Value("string"), # todo maybe shorten to id
 
247
 
248
  segments = transcript.get("segments", [])
249
 
250
+ if self.config.max_pause_length:
251
+ segments = self._remove_to_long_pauses(segments)
252
+
253
  if self.config.max_chunk_length is not None:
254
  segments = self._split_long_utterances(segments)
255
 
256
  if self.config.remove_trailing_pauses:
257
  self._remove_trailing_pauses_in_segments(segments)
258
 
259
+ if self.config.max_pause_length is not None:
260
+ segments = self._filter_utterances_by_duration(segments, self.config.min_chunk_length, self.config.max_chunk_length)
261
+
262
  for segment_i, segment in enumerate(segments):
263
 
264
  id_ = f"{audio_id}_{str(segment_i).rjust(3, '0')}"
 
290
 
291
  yield id_, data
292
 
293
+ @staticmethod
294
+ def _remove_trailing_pauses_in_segments(segments: List[dict]) -> None:
295
  """
296
  Removes pauses at the end/start of utterances in each segment to eliminate pauses between segments.
297
+
298
+ Example:
299
+ [["Hello", "World!", "(...)"]] --> [["Hello"], ["World!"]]
300
+ [["(...)", "Hello", "World!"]] --> [["Hello"], ["World!"]]
301
+
302
  :return: A list of Word objects representing the removed pause indicators from the segments.
303
  """
304
  for segment in segments:
 
311
  if not segment["words"]:
312
  segments.remove(segment)
313
 
314
+ def _remove_to_long_pauses(self, segments: List[dict]) -> List[dict]:
315
+ """
316
+ Remove to long pauses in a segment by splitting the segment in two segments and removing the filled pause.
317
+
318
+ Example (assuming (...) is longer than max_pause_length):
319
+ [["Hello", "(...)", "World!"]] --> [["Hello"], ["World!"]]
320
+
321
+ :return: List of segments with long pauses removed
322
+ """
323
+
324
+ split_segments = []
325
+ for segment in segments:
326
+ if any(w["end"]-w["start"] >= self.config.max_pause_length and w["text"].strip() == "(...)" for w in segment["words"]):
327
+ start_i = 0
328
+ for i, word in enumerate(segment["words"]):
329
+ w_duration = word["end"] - word["start"]
330
+ if w_duration >= self.config.max_pause_length and word["text"].strip() == "(...)":
331
+ if len(segment["words"][start_i:i]) > 0:
332
+ split_segments.append({"speaker": segment["speaker"], "words": segment["words"][start_i:i]})
333
+ start_i = i+1
334
+
335
+ if len(segment["words"][start_i:]) > 0:
336
+ split_segments.append({"speaker": segment["speaker"], "words": segment["words"][start_i:]})
337
+ else:
338
+ split_segments.append(segment)
339
+ return split_segments
340
+
341
+ @staticmethod
342
+ def _filter_utterances_by_duration(segments: List[dict], min_duration: float = None, max_duration: float = None, ):
343
+ """
344
+ Removes segments with invalid duration
345
+ :param min_duration: The minimum duration allowed for a segment.
346
+ :return: A list of removed short segments.
347
+ """
348
+
349
+ filtered_segments = []
350
+ for segment in segments:
351
+ duration = segment["words"][-1]["end"] - segment["words"][0]["start"]
352
+ if min_duration is not None and duration < min_duration:
353
+ continue
354
+ if max_duration is not None and duration > max_duration:
355
+ continue
356
+ filtered_segments.append(segment)
357
+
358
+ return filtered_segments
359
+
360
  def _split_long_utterances(self, segments: List[dict]) -> List[dict]:
361
  """
362
  Splits segments into smaller chunks if their duration exceeds the maximum chunk length specified in the config.
363
+
364
+ Example (assuming each word is longer than max_duration):
365
+ [["Hello", "World!"]] --> [["Hello"], ["World!"]]
366
+
367
  :param segments: List of original segments from the transcript.
368
  :return: List of adjusted segments, potentially split into smaller chunks.
369
  """