ylmmhf commited on
Commit
0dbe13b
·
verified ·
1 Parent(s): 1306c6f

Debug the last interval issue

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -12,7 +12,7 @@ def create_interval_data_dict(xmin, xmax, sentence):
12
  return {'xmin': float(xmin), 'xmax': float(xmax), 'text': sentence}
13
 
14
  def write_textgrid_file(intervals, output_file_path, total_xmax, tier_name):
15
- with open(output_file_path, 'w', encoding='utf-8') as f:
16
  f.write('File type = "ooTextFile"\n')
17
  f.write('Object class = "TextGrid"\n\n')
18
  f.write('xmin = 0\n')
@@ -25,7 +25,7 @@ def write_textgrid_file(intervals, output_file_path, total_xmax, tier_name):
25
  f.write(f' name = "{tier_name}"\n')
26
  f.write(' xmin = 0\n')
27
  f.write(f' xmax = {str(float(total_xmax))}\n')
28
- f.write(f' intervals: size = {len(intervals) + 1}\n')
29
 
30
  for idx, interval in enumerate(intervals):
31
  f.write(f' intervals [{idx + 1}]:\n')
@@ -33,11 +33,6 @@ def write_textgrid_file(intervals, output_file_path, total_xmax, tier_name):
33
  f.write(f' xmax = {interval["xmax"]}\n')
34
  f.write(f' text = "{interval["text"]}"\n')
35
 
36
- if len(intervals) > 0:
37
- f.write(f' intervals [{len(intervals) + 1}]:\n')
38
- f.write(f' xmin = {intervals[-1]["xmax"]}\n')
39
- f.write(f' xmax = {intervals[-1]["xmax"]}\n')
40
- f.write(f' text = ""\n')
41
 
42
  def validate_csv_format(header):
43
  valid_headers = [
@@ -168,10 +163,15 @@ def csv_to_textgrid(file, tier_name=""):
168
  print(f"Error processing row {row}: {e}")
169
  continue
170
 
 
171
  if prev_filename is not None and prev_filename != filename:
172
  if words:
173
  intervals.append(create_interval_data_dict(iu_xmin, iu_xmax, ' '.join(words)))
174
 
 
 
 
 
175
  if intervals:
176
  textgrid_path = os.path.join(output_directory, f"{prev_filename}.TextGrid")
177
  write_textgrid_file(intervals, textgrid_path, intervals[-1]['xmax'], tier_name)
@@ -199,10 +199,21 @@ def csv_to_textgrid(file, tier_name=""):
199
  words.append(text)
200
  iu_xmax = xmax
201
 
 
202
  if not current_file_processed and prev_filename:
203
  if words:
204
  intervals.append(create_interval_data_dict(iu_xmin, iu_xmax, ' '.join(words)))
205
-
 
 
 
 
 
 
 
 
 
 
206
  if intervals:
207
  textgrid_path = os.path.join(output_directory, f"{prev_filename}.TextGrid")
208
  write_textgrid_file(intervals, textgrid_path, intervals[-1]['xmax'], tier_name)
 
12
  return {'xmin': float(xmin), 'xmax': float(xmax), 'text': sentence}
13
 
14
  def write_textgrid_file(intervals, output_file_path, total_xmax, tier_name):
15
+ with open(output_file_path, 'w') as f:
16
  f.write('File type = "ooTextFile"\n')
17
  f.write('Object class = "TextGrid"\n\n')
18
  f.write('xmin = 0\n')
 
25
  f.write(f' name = "{tier_name}"\n')
26
  f.write(' xmin = 0\n')
27
  f.write(f' xmax = {str(float(total_xmax))}\n')
28
+ f.write(f' intervals: size = {len(intervals)}\n')
29
 
30
  for idx, interval in enumerate(intervals):
31
  f.write(f' intervals [{idx + 1}]:\n')
 
33
  f.write(f' xmax = {interval["xmax"]}\n')
34
  f.write(f' text = "{interval["text"]}"\n')
35
 
 
 
 
 
 
36
 
37
  def validate_csv_format(header):
38
  valid_headers = [
 
163
  print(f"Error processing row {row}: {e}")
164
  continue
165
 
166
+ # Handle file transition
167
  if prev_filename is not None and prev_filename != filename:
168
  if words:
169
  intervals.append(create_interval_data_dict(iu_xmin, iu_xmax, ' '.join(words)))
170
 
171
+ if intervals:
172
+ last_xmax = intervals[-1]['xmax']
173
+ intervals.append(create_interval_data_dict(last_xmax, last_xmax + 0.001, '')) # New interval
174
+
175
  if intervals:
176
  textgrid_path = os.path.join(output_directory, f"{prev_filename}.TextGrid")
177
  write_textgrid_file(intervals, textgrid_path, intervals[-1]['xmax'], tier_name)
 
199
  words.append(text)
200
  iu_xmax = xmax
201
 
202
+ # Process the last file
203
  if not current_file_processed and prev_filename:
204
  if words:
205
  intervals.append(create_interval_data_dict(iu_xmin, iu_xmax, ' '.join(words)))
206
+
207
+ # Add the new interval with xmin as last xmax and xmax as last xmax + 0.001
208
+ if intervals:
209
+ last_xmax = intervals[-1]['xmax']
210
+ new_xmin = last_xmax
211
+ new_xmax = last_xmax + 0.001
212
+
213
+ # Only add the new interval if it's not a duplicate
214
+ if new_xmin < new_xmax: # Ensure they are not the same
215
+ intervals.append(create_interval_data_dict(new_xmin, new_xmax, '')) # New interval
216
+
217
  if intervals:
218
  textgrid_path = os.path.join(output_directory, f"{prev_filename}.TextGrid")
219
  write_textgrid_file(intervals, textgrid_path, intervals[-1]['xmax'], tier_name)