-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_dataset.py
More file actions
66 lines (56 loc) · 2.41 KB
/
create_dataset.py
File metadata and controls
66 lines (56 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import fitz # PyMuPDF
import re
from docx import Document
def clean_psych_text(text):
"""Specific filters for Psychology textbooks and Children's literature."""
# 1. Remove Citations like (Author, Year) or (Author et al., Year)
text = re.sub(r'\([A-Z][a-z]+(?: et al\.)?,\s\d{4}\)', '', text)
# 2. Remove textbook boilerplate (ISBN, Copyright, Publisher info)
noise_patterns = [
r'ISBN[:\s]*[\d\-xX]+',
r'Copyright\s*©.*',
r'All rights reserved',
r'Printed in.*',
r'Library of Congress.*',
r'www\.[a-z0-9\.]+'
]
for pattern in noise_patterns:
text = re.sub(pattern, '', text, flags=re.IGNORECASE)
# 3. Remove Page Headers/Footers (usually short lines with digits)
lines = text.split('\n')
cleaned_lines = []
for line in lines:
stripped = line.strip()
# Skip lines that look like page numbers or are too short to be meaningful prose
if re.match(r'^\d+$', stripped) or len(stripped) < 15:
continue
cleaned_lines.append(stripped)
return " ".join(cleaned_lines)
def process_to_chunks(input_folder, output_file, chunk_size=1500):
valid_exts = ('.pdf', '.docx')
all_text = ""
for filename in os.listdir(input_folder):
if not filename.lower().endswith(valid_exts): continue
file_path = os.path.join(input_folder, filename)
print(f"Reading: {filename}")
try:
current_file_text = ""
if filename.endswith(".pdf"):
with fitz.open(file_path) as doc:
for page in doc:
current_file_text += page.get_text("text") + " "
elif filename.endswith(".docx"):
doc = Document(file_path)
current_file_text = " ".join([p.text for p in doc.paragraphs if p.text.strip()])
all_text += clean_psych_text(current_file_text) + " <|endoftext|> "
except Exception as e:
print(f"Error {filename}: {e}")
# Sliding Window Chunking: This prevents the model from memorizing short segments
words = all_text.split()
with open(output_file, "w", encoding="utf-8") as f:
for i in range(0, len(words), chunk_size):
chunk = " ".join(words[i:i + chunk_size])
f.write(chunk + "\n")
if __name__ == "__main__":
process_to_chunks("phsyc_data", "cleaned_dataset.txt")