salmamohammedhamed22 commited on
Commit
bf7f7cb
·
verified ·
1 Parent(s): 7bcf07e

Upload generate_dataset.py

Browse files
Files changed (1) hide show
  1. generate_dataset.py +104 -0
generate_dataset.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import json
3
+ import random
4
+ import string
5
+ import re
6
+
7
+ # Configuration
8
+ TARGET_TOTAL = 15000 # target samples per label
9
+ MAX_PREFIXES = 5 # max negative prefixes per utterance
10
+ CONTEXT_TURNS = 4 # max number of previous turns to include (0..CONTEXT_TURNS)
11
+ random.seed(42)
12
+
13
+ arabic_punctuations = '''`÷×؛<>_()*&^%][ـ،/:"؟.,'{}~¦+|!”…“–ـ'''
14
+ english_punctuations = string.punctuation
15
+ all_punctuations = arabic_punctuations + english_punctuations
16
+
17
+ def normalize_text(text):
18
+ """Normalize excessive spacing but keep punctuation."""
19
+ if not isinstance(text, str):
20
+ return ""
21
+ text = re.sub(r"\s+", " ", text)
22
+ return text.strip()
23
+
24
+ def generate_prefixes(sentence):
25
+ """Generate multiple incomplete prefixes (label=0)."""
26
+ words = sentence.split()
27
+ prefixes = []
28
+ total_prefixes = min(len(words) - 1, MAX_PREFIXES)
29
+ for k in range(1, total_prefixes + 1):
30
+ prefix = " ".join(words[:k]).rstrip(all_punctuations).strip()
31
+ if prefix:
32
+ prefixes.append(prefix)
33
+ return prefixes
34
+
35
+ def build_eou_dataset_with_sliding_context(input_csv, output_json):
36
+ print(f"Loading dataset: {input_csv}")
37
+ df = pd.read_csv(input_csv)
38
+ text_col = "GroundTruthText"
39
+ time_col = "SegmentStart" if "SegmentStart" in df.columns else df.columns[2]
40
+ df = df.sort_values(by=["FileName", time_col])
41
+
42
+ print("Generating EOU samples with sliding window context…")
43
+
44
+ all_pos = []
45
+ all_neg = []
46
+
47
+ for filename, group in df.groupby("FileName"):
48
+ utterances = group[text_col].dropna().tolist()
49
+ utterances = [normalize_text(u) for u in utterances if u.strip()]
50
+
51
+ for idx, utt in enumerate(utterances):
52
+ # --- Create multiple context versions ---
53
+ for c_len in range(0, CONTEXT_TURNS + 1):
54
+ context_start = max(0, idx - c_len)
55
+ context_turns = utterances[context_start:idx]
56
+
57
+ if context_turns:
58
+ context_text = " [SEP] ".join(context_turns)
59
+ full_text = f"{context_text} [SEP] {utt}"
60
+ else:
61
+ full_text = utt
62
+
63
+ # Positive sample
64
+ all_pos.append({
65
+ "text": full_text,
66
+ "label": 1
67
+ })
68
+
69
+ # Negative samples (prefixes)
70
+ prefixes = generate_prefixes(utt)
71
+ for p in prefixes:
72
+ if context_turns:
73
+ neg_text = f"{context_text} [SEP] {p}"
74
+ else:
75
+ neg_text = p
76
+ all_neg.append({
77
+ "text": neg_text,
78
+ "label": 0
79
+ })
80
+
81
+ # Shuffle
82
+ random.shuffle(all_pos)
83
+ random.shuffle(all_neg)
84
+
85
+ # Balance dataset to TARGET_TOTAL per label if needed
86
+ pos_final = all_pos[:TARGET_TOTAL]
87
+ neg_final = all_neg[:TARGET_TOTAL]
88
+
89
+ final = pos_final + neg_final
90
+ random.shuffle(final)
91
+
92
+ print(f"Total positive (label=1): {len(pos_final)}")
93
+ print(f"Total negative (label=0): {len(neg_final)}")
94
+ print(f"Final dataset size: {len(final)}")
95
+
96
+ # Save
97
+ with open(output_json, "w", encoding="utf-8") as f:
98
+ json.dump(final, f, ensure_ascii=False, indent=2)
99
+
100
+ print(f"Saved to {output_json}")
101
+ print("Done!")
102
+
103
+ # Run the script
104
+ build_eou_dataset_with_sliding_context("train.csv", "eou_sada_augmented.json")