Upload split_slide.py with huggingface_hub
Browse files- split_slide.py +98 -0
split_slide.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import nltk
|
| 3 |
+
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# nltk.download('punkt')
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def split_content_by_tokens(data, max_tokens=200):
|
| 10 |
+
"""
|
| 11 |
+
Splits content into chunks of approximately max_tokens, respecting sentence boundaries.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
data (list): List of dictionaries containing "page_number" and "content".
|
| 15 |
+
max_tokens (int): Maximum number of tokens per chunk.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
list: A new list of dictionaries with split content.
|
| 19 |
+
"""
|
| 20 |
+
processed_data = []
|
| 21 |
+
chunk_id = 1
|
| 22 |
+
prev_page = -1
|
| 23 |
+
current_chunk = []
|
| 24 |
+
current_token_count = 0
|
| 25 |
+
|
| 26 |
+
for record in data:
|
| 27 |
+
page_number = record.get("page_number")
|
| 28 |
+
content = record.get("content", "")
|
| 29 |
+
|
| 30 |
+
if prev_page == -1:
|
| 31 |
+
prev_page = page_number
|
| 32 |
+
elif prev_page != page_number: # not the same page
|
| 33 |
+
if current_chunk:
|
| 34 |
+
processed_data.append({
|
| 35 |
+
"id": chunk_id,
|
| 36 |
+
"page_number": prev_page,
|
| 37 |
+
"content": " ".join(current_chunk),
|
| 38 |
+
"type": "slide"
|
| 39 |
+
})
|
| 40 |
+
chunk_id += 1
|
| 41 |
+
current_chunk = []
|
| 42 |
+
current_token_count = 0
|
| 43 |
+
prev_page = page_number
|
| 44 |
+
|
| 45 |
+
# Tokenize content into sentences
|
| 46 |
+
sentences = sent_tokenize(content)
|
| 47 |
+
|
| 48 |
+
for sentence in sentences:
|
| 49 |
+
sentence_tokens = word_tokenize(sentence)
|
| 50 |
+
sentence_length = len(sentence_tokens)
|
| 51 |
+
|
| 52 |
+
# Check if adding this sentence exceeds the token limit
|
| 53 |
+
if current_token_count + sentence_length > max_tokens:
|
| 54 |
+
# Save the current chunk
|
| 55 |
+
if current_chunk:
|
| 56 |
+
processed_data.append({
|
| 57 |
+
"id": chunk_id,
|
| 58 |
+
"page_number": page_number,
|
| 59 |
+
"content": " ".join(current_chunk),
|
| 60 |
+
"type": "slide"
|
| 61 |
+
})
|
| 62 |
+
chunk_id += 1
|
| 63 |
+
# Start a new chunk
|
| 64 |
+
current_chunk = []
|
| 65 |
+
current_token_count = 0
|
| 66 |
+
|
| 67 |
+
# Add the current sentence to the chunk
|
| 68 |
+
current_chunk.append(sentence)
|
| 69 |
+
current_token_count += sentence_length
|
| 70 |
+
|
| 71 |
+
# Save the last chunk
|
| 72 |
+
if current_chunk:
|
| 73 |
+
processed_data.append({
|
| 74 |
+
"id": chunk_id+1,
|
| 75 |
+
"page_number": prev_page,
|
| 76 |
+
"content": " ".join(current_chunk),
|
| 77 |
+
"type": "slide"
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
return processed_data
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
# Load your JSON file
|
| 85 |
+
input_file = "/Users/yuchenhua/Coding/pdf/1121ppt.json"
|
| 86 |
+
output_file = "1121_ppt.json"
|
| 87 |
+
|
| 88 |
+
with open(input_file, 'r') as f:
|
| 89 |
+
data = json.load(f)
|
| 90 |
+
|
| 91 |
+
# Process the data
|
| 92 |
+
split_data = split_content_by_tokens(data)
|
| 93 |
+
|
| 94 |
+
# Save the processed data to a new JSON file
|
| 95 |
+
with open(output_file, 'w') as f:
|
| 96 |
+
json.dump(split_data, f, indent=4)
|
| 97 |
+
|
| 98 |
+
print(f"Processed content has been saved to {output_file}")
|