-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_format.py
More file actions
31 lines (27 loc) · 951 Bytes
/
Copy pathconvert_format.py
File metadata and controls
31 lines (27 loc) · 951 Bytes
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
import json
import os
from tqdm import tqdm
def convert_format(input_file, output_file):
# Load first format JSON
with open(input_file, "r") as f:
data = json.load(f)
converted = []
for item in tqdm(data):
# Get file name and replace extension with .md
source_file = os.path.splitext(item["metadata"]["source"])[0] + ".md"
# Build new structure
new_item = {
"document_name": source_file,
"chunk_id": f"chunk-{item['metadata']['chunk_id']}",
"text": item["metadata"]["contextual_chunk_content"], # take contextual content
"metadata": {
"source": "markdown"
}
}
converted.append(new_item)
# Save as new JSON
with open(output_file, "w") as f:
json.dump(converted, f, indent=4)
# Example usage
if __name__ == "__main__":
convert_format("metadata_context_chunk.json", "output.json")