Chamaka8 commited on
Commit
84bd2db
·
verified ·
1 Parent(s): 37a7107

Upload clean_kawmuthu.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. clean_kawmuthu.py +94 -0
clean_kawmuthu.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Cleanup script for kawmuthu_training.jsonl
3
+ Fixes:
4
+ 1. Strips dates (-2002, --1998, --April 2010, etc.)
5
+ 2. Removes event/institutional notes (Freshers Nite, Faculty of Engineering, SLBC, etc.)
6
+ 3. Collapses excessive whitespace/padding
7
+ 4. Fixes mismatched theme instructions
8
+
9
+ Usage:
10
+ python clean_kawmuthu.py
11
+ Input: kawmuthu_training.jsonl
12
+ Output: kawmuthu_training_clean.jsonl
13
+ """
14
+
15
+ import json
16
+ import re
17
+
18
+ def clean_poem_content(text):
19
+ # Remove date patterns like -2002, --1998, --April 2010, -2021.09.17
20
+ text = re.sub(r'\n[-–]+\s*\d{4}[\.\d]*.*', '', text)
21
+ text = re.sub(r'\n[-–]+\s*[A-Za-z]+\s*\d{4}.*', '', text)
22
+
23
+ # Remove event/institutional notes
24
+ # Patterns like "2005 Freshers Nite souvenir(held by Batch 04,..."
25
+ text = re.sub(r'\n?\d{4}\s+Freshers.*?(?=\n\n|\Z)', '', text, flags=re.DOTALL)
26
+ # "a music teacher at Sujatha Vidyalaya..." type lines
27
+ text = re.sub(r'\n?,?\s*a music teacher at.*?(?=\n\n|\Z)', '', text, flags=re.DOTALL)
28
+ # Any remaining SLBC/UOM/Faculty references
29
+ text = re.sub(r'\n.*?(Faculty of Engineering|University of Moratuwa|UOM|SLBC|Ruhunu Sevaya|Sujatha Vidyalaya).*', '', text)
30
+ # Remove ©Author and @Author lines
31
+ text = re.sub(r'\n?[@©][A-Za-z\s\.]+', '', text)
32
+
33
+ # Collapse excessive whitespace (4+ spaces in a row = alignment padding in blog)
34
+ text = re.sub(r'[ \t]{4,}', ' ', text)
35
+
36
+ # Collapse 3+ blank lines into max 2
37
+ text = re.sub(r'\n{3,}', '\n\n', text)
38
+
39
+ return text.strip()
40
+
41
+ def fix_theme_instruction(instruction, content):
42
+ """Fix cases where theme instruction doesn't match poem content."""
43
+ # 'සිංහල ගැන සිංහල කවියක් ලියන්න' means 'write a poem about Sinhala language'
44
+ # but was used for all poems regardless of theme - replace with generic
45
+ if instruction == 'සිංහල ගැන සිංහල කවියක් ලියන්න':
46
+ return 'සිංහල කවියක් ලියන්න'
47
+ return instruction
48
+
49
+ def clean_example(example):
50
+ content = clean_poem_content(example['output'])
51
+ instruction = fix_theme_instruction(example['instruction'], content)
52
+
53
+ # Rebuild the text field from scratch
54
+ text = (
55
+ 'Below is an instruction that describes a task. '
56
+ 'Write a response that appropriately completes the request.\n\n'
57
+ f'### Instruction:\n{instruction}\n\n'
58
+ f'### Response:\n{content}'
59
+ )
60
+
61
+ example['output'] = content
62
+ example['instruction'] = instruction
63
+ example['text'] = text
64
+ return example
65
+
66
+ # ── Main ────────────────────────────────────────────────────
67
+
68
+ input_file = 'kawmuthu_poems_training.jsonl'
69
+ output_file = 'kawmuthu_poems_training_clean.jsonl'
70
+
71
+ examples = []
72
+ with open(input_file, encoding='utf-8') as f:
73
+ for line in f:
74
+ line = line.strip()
75
+ if line:
76
+ examples.append(json.loads(line))
77
+
78
+ print(f'Loaded {len(examples)} examples')
79
+
80
+ cleaned = [clean_example(ex) for ex in examples]
81
+
82
+ # Show a before/after sample
83
+ print('\n--- BEFORE ---')
84
+ print(examples[0]['output'][:300])
85
+ print('\n--- AFTER ---')
86
+ print(cleaned[0]['output'][:300])
87
+
88
+ with open(output_file, 'w', encoding='utf-8') as f:
89
+ for ex in cleaned:
90
+ f.write(json.dumps(ex, ensure_ascii=False) + '\n')
91
+
92
+ print(f'\nSaved {len(cleaned)} cleaned examples to {output_file}')
93
+ print('\nNext step:')
94
+ print(' cat combined_training.jsonl kawmuthu_training_clean.jsonl > final_training.jsonl')