Shuwei Hou commited on
Commit
107e251
·
1 Parent(s): 9e45db3

add_pause_between_segments

Browse files
Files changed (1) hide show
  1. pause.py +44 -2
pause.py CHANGED
@@ -7,13 +7,14 @@ def annotate_pauses(session_id, threshold, base_dir="session_data"):
7
  json_file = os.path.join(session_dir, f"{session_id}_transcriptionCW.json")
8
 
9
  if not os.path.exists(json_file):
10
- print(f"Error: could not finf {json_file}")
11
  return
12
 
13
  with open(json_file, "r", encoding="utf-8") as f:
14
  data = json.load(f)
15
 
16
  segments = data.get("segments", [])
 
17
  for segment in segments:
18
  words = segment.get("words", [])
19
  if "pauses" in segment:
@@ -34,11 +35,52 @@ def annotate_pauses(session_id, threshold, base_dir="session_data"):
34
  pauses.append(pause_info)
35
  segment["pauses"] = pauses
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  with open(json_file, "w", encoding="utf-8") as f:
38
  json.dump(data, f, ensure_ascii=False, indent=4)
39
 
40
  print(f"Session {session_id} pause annotation done: {json_file}")
 
 
 
 
 
41
  return data
42
 
43
  if __name__ == "__main__":
44
- annotated_data = annotate_pauses("000030", 0.1)
 
7
  json_file = os.path.join(session_dir, f"{session_id}_transcriptionCW.json")
8
 
9
  if not os.path.exists(json_file):
10
+ print(f"Error: could not find {json_file}")
11
  return
12
 
13
  with open(json_file, "r", encoding="utf-8") as f:
14
  data = json.load(f)
15
 
16
  segments = data.get("segments", [])
17
+
18
  for segment in segments:
19
  words = segment.get("words", [])
20
  if "pauses" in segment:
 
35
  pauses.append(pause_info)
36
  segment["pauses"] = pauses
37
 
38
+ new_segments = []
39
+
40
+ for i, segment in enumerate(segments):
41
+
42
+ new_segments.append(segment)
43
+
44
+ if i < len(segments) - 1:
45
+ next_segment = segments[i + 1]
46
+
47
+ current_words = segment.get("words", [])
48
+ next_words = next_segment.get("words", [])
49
+
50
+ if current_words and next_words:
51
+ last_word_end = current_words[-1]["end"]
52
+ next_word_start = next_words[0]["start"]
53
+ gap = next_word_start - last_word_end
54
+
55
+ if gap > threshold:
56
+ pause_segment = {
57
+ "start": round(last_word_end, 3),
58
+ "end": round(next_word_start, 3),
59
+ "text": "",
60
+ "speaker": "PAUSE",
61
+ "words": [],
62
+ "pauses": [
63
+ {
64
+ "start": round(last_word_end, 3),
65
+ "end": round(next_word_start, 3),
66
+ "duration": round(next_word_start - last_word_end, 3)
67
+ }
68
+ ]
69
+ }
70
+ new_segments.append(pause_segment)
71
+
72
+ data["segments"] = new_segments
73
+
74
  with open(json_file, "w", encoding="utf-8") as f:
75
  json.dump(data, f, ensure_ascii=False, indent=4)
76
 
77
  print(f"Session {session_id} pause annotation done: {json_file}")
78
+ print(f"Total segments after processing: {len(new_segments)}")
79
+
80
+ pause_segments = [seg for seg in new_segments if seg.get("speaker") == "PAUSE"]
81
+ print(f"Added {len(pause_segments)} inter-sentence pause segments")
82
+
83
  return data
84
 
85
  if __name__ == "__main__":
86
+ annotated_data = annotate_pauses("000030", 0.1)