-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathchunker.py
76 lines (60 loc) · 2.12 KB
/
chunker.py
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
73
74
75
76
import os
from langchain.document_loaders import ReadTheDocsLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from tqdm.auto import tqdm
import hashlib
import json
import tiktoken
tokenizer = tiktoken.get_encoding('cl100k_base')
def tiktoken_len(text):
tokens = tokenizer.encode(
text,
disallowed_special=()
)
return len(tokens)
def process_html_files(folder_path):
# Get all files in the folder
all_files = os.listdir(folder_path)
# Filter out HTML files
html_files = [file for file in all_files if file.endswith('.html')]
# Initialize tokenizer and text_splitter
tokenizer = tiktoken.get_encoding('cl100k_base')
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=400,
chunk_overlap=20,
length_function=tiktoken_len,
separators=['\n\n', '\n', ' ', '']
)
documents = []
# Process each HTML file
for html_file in tqdm(html_files):
try:
file_path = os.path.join(folder_path, html_file)
# Load the HTML content
with open(file_path, 'r') as f:
content = f.read()
# Generate a unique ID based on the file path
m = hashlib.md5()
m.update(file_path.encode('utf-8'))
uid = m.hexdigest()[:12]
# Split the content into chunks
chunks = text_splitter.split_text(content)
# Create document data
for i, chunk in enumerate(chunks):
documents.append({
'id': f'{uid}-{i}',
'text': chunk,
'source': file_path
})
# Delete the HTML file after processing
os.remove(file_path)
except Exception as e:
print(f"Error processing file {html_file}: {e}")
# Save the documents to a JSONL file
with open('train.jsonl', 'w') as f:
for doc in documents:
f.write(json.dumps(doc) + '\n')
return documents
# Call the function with the folder path "websites"
folder_path = "websites"
documents = process_html_files(folder_path)