File size: 1,558 Bytes
e8cd1ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import re

def split_careers_to_json(careers, text_file_path, output_path="careers.json"):
    # Sort headings by length to avoid substring conflicts
    careers_sorted = sorted(careers, key=len, reverse=True)

    pattern = r"(" + "|".join(re.escape(c) for c in careers_sorted) + r")"

    with open(text_file_path, "r", encoding="utf-8") as f:
        text = f.read()

    parts = re.split(pattern, text)

    result = []
    current_title = None
    buffer = []

    id_counter = 1

    for part in parts:
        stripped = part.strip()
        if stripped in careers:
            if current_title:
                content = " ".join(buffer)
                result.append({
                    "id": id_counter,
                    "title": current_title,
                    "content": content
                })
                id_counter += 1

            current_title = stripped
            buffer = []
        else:
            if current_title:
                buffer.append(part)

    # Last career block
    if current_title:
        content = " ".join(buffer)
        result.append({
            "id": id_counter,
            "title": current_title,
            "content": content
        })

    # Write JSON
    with open(output_path, "w", encoding="utf-8") as f:
        json.dump(result, f, indent=2, ensure_ascii=False)

    return result


with open("career_names.json", "r", encoding="utf-8") as f:
    career_names = json.load(f)

split_careers_to_json(career_names, "cleaned_text.txt", output_path="careers_cleaned.json")