-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocess.py
More file actions
72 lines (57 loc) · 2.02 KB
/
preprocess.py
File metadata and controls
72 lines (57 loc) · 2.02 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
67
68
69
70
71
72
import os
import json
from transformers import AutoTokenizer
import nltk
nltk.download("punkt")
from nltk.tokenize import sent_tokenize
with open("transcript.txt", "r", encoding="utf-8") as f:
INPUT_TEXT = f.read()
OUTPUT_DIR = "chunks_output"
CHUNK_SIZE = 50
OUTPUT_JSON_FILE = "text_chunks.json"
def chunk_text(
text,
tokenizer_name="facebook/bart-large-cnn",
max_tokens=900, # leave buffer for BART
overlap_tokens=100
):
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
sentences = sent_tokenize(text)
chunks = []
current_chunk = []
current_tokens = 0
for sentence in sentences:
sentence_tokens = tokenizer.encode(
sentence,
add_special_tokens=False
)
n_tokens = len(sentence_tokens)
# Handle very long sentences
if n_tokens > max_tokens:
sentence_tokens = sentence_tokens[:max_tokens]
sentence = tokenizer.decode(sentence_tokens)
if current_tokens + n_tokens > max_tokens:
chunk_text = " ".join(current_chunk)
chunks.append(chunk_text)
# overlap
overlap = tokenizer.encode(
chunk_text,
add_special_tokens=False
)[-overlap_tokens:]
current_chunk = [tokenizer.decode(overlap)]
current_tokens = len(overlap)
current_chunk.append(sentence)
current_tokens += n_tokens
if current_chunk:
chunks.append(" ".join(current_chunk))
return chunks
def save_chunks_to_json(text, output_dir=OUTPUT_DIR, output_file=OUTPUT_JSON_FILE, chunk_size=CHUNK_SIZE):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
chunks = chunk_text(text=text)
output_path = os.path.join(output_dir, output_file)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(chunks, f, indent=4, ensure_ascii=False)
print(f"Saved {len(chunks)} chunks to {output_path}")
if __name__ == "__main__":
save_chunks_to_json(INPUT_TEXT)