File size: 12,917 Bytes
7731906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
import os, sys, json, math, re, tempfile, subprocess, shutil

def clean_text(text):
    text = re.sub(r'[\u200b\u200c\u200d\u200e\u200f\u2060\u2061\u2062\u2063\u2064\ufeff\u00ad]', '', text)
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

def slugify(name):
    name = re.sub(r'\.[^.]+$', '', name)
    name = re.sub(r'[\s_()\[\]{}]+', '-', name)
    name = re.sub(r'[^a-zA-Z0-9\u0E80-\u0EFF-]', '', name)
    name = re.sub(r'-+', '-', name)
    name = name.strip('-').lower()[:60]
    return name or 'book'

def fix_lao_text(text):
    if not text:
        return text

    # 1. Remove zero-width and invisible chars
    text = re.sub(r'[\u200b\u200c\u200d\u200e\u200f\u2060\u2061\u2062\u2063\u2064\ufeff\u00ad]', '', text)

    # 2. Remove spaces between Lao syllable components
    C = '[\u0E81-\u0EAE\u0EB0-\u0EBE]'
    V_upper = '[\u0EB4-\u0EB9\u0EBC\u0EBD]'
    V_leading = '[\u0EC0-\u0EC4]'
    TONE = '[\u0EC8-\u0ECB]'
    TAIL = '[\u0E87\u0E94\u0E99\u0EA1\u0E81\u0E8D\u0EA7\u0EA3\u0EA5]'

    # Leading vowel + space + consonant/vowel → rejoin
    text = re.sub(r'(%s) +(?=%s|%s)' % (V_leading, C, V_leading), r'\1', text)
    # Consonant + space + upper vowel → rejoin
    text = re.sub(r'(?<=%s) +(?=%s)' % (C, V_upper), '', text)
    # Consonant + space + tone mark → rejoin
    text = re.sub(r'(?<=%s) +(?=%s)' % (C, TONE), '', text)
    # Upper vowel + space + consonant/tail → rejoin
    text = re.sub(r'(?<=%s) +(?=%s|%s)' % (V_upper, C, TAIL), '', text)
    # Consonant + space + ຣ ລ ວ (common tail consonants) → rejoin
    text = re.sub(r'(?<=%s) +(?=[\u0EA3\u0EA5\u0EA7])' % C, '', text)
    # Consonant + space + ອ (vowel role) → rejoin
    text = re.sub(r'(?<=[\u0E81-\u0EBD]) +(?=\u0EAD)', '', text)
    # ອ (vowel) + space + common final consonant → rejoin
    text = re.sub(r'(?<=\u0EAD) +(?=[\u0E99\u0EA1\u0E87\u0E94\u0E9A\u0E81\u0EA7\u0E8D])', '', text)

    # 3. Fix common split Lao word patterns
    pairs = [
        ('\u0EAA \u0E95', '\u0EAA\u0EB9\u0E95'),
        ('\u0E9E \u0EA1', '\u0E9E\u0EB9\u0EA1'),
        ('\u0EAA \u0E96', '\u0EAA\u0EB0\u0E96'),
        ('\u0E9B \u0EBE', '\u0E9B\u0EBE'),
        ('\u0E88 \u0EB0', '\u0E88\u0EB0'),
        ('\u0E97 \u0EB5\u0E99', '\u0E97\u0EB5\u0EC8\u0E99'),
    ]
    for old, new in pairs:
        text = text.replace(old, new)

    # 4. Fix specific word: ຄານາ → ຄຳນຳ
    text = text.replace('\u0E84\u0EB2\u0E99\u0EB2', '\u0E84\u0EB3\u0E99\u0EB3')

    # 5. Re-attach detached vowel ື and ຸ (common PDF split)
    text = re.sub(r'(?<=%s) (?=\u0EB7)' % C, '', text)
    text = re.sub(r'(?<=%s) (?=\u0EB9)' % C, '', text)

    # 6. Flexible spacing: ensure space after sentence punctuation
    text = re.sub(r'([.!?]) +', r'\1 ', text)

    # 7. Normalize whitespace
    text = re.sub(r'\s+', ' ', text)
    return text.strip()


def validate_lao_text(text):
    if not text:
        return {"has_lao": False, "warnings": ["No text extracted"]}
    lao_chars = sum(1 for c in text if '\u0E80' <= c <= '\u0EFF')
    total_chars = sum(1 for c in text if not c.isspace())
    if total_chars == 0:
        return {"has_lao": False, "warnings": ["No extractable characters"]}
    lao_ratio = lao_chars / total_chars
    warnings = []
    if lao_ratio < 0.3 and lao_chars > 0:
        warnings.append("Low Lao character ratio ({:.0f}%)".format(lao_ratio * 100))
    if lao_chars == 0 and total_chars > 10:
        warnings.append("No Lao characters detected in text")
    return {"has_lao": lao_chars > 0, "lao_ratio": round(lao_ratio, 2), "warnings": warnings}


def convert_pdf(file_path, out_dir, title):
    import fitz
    os.makedirs(out_dir, exist_ok=True)
    doc = fitz.open(file_path)
    total_pages = doc.page_count
    re_lao_digit = re.compile(r'^[\u0E50-\u0E590-9.,/]+$')

    def is_multicolumn(words):
        left_words = [w for w in words if w[0] < 350]
        right_words = [w for w in words if w[0] > 450]
        if len(right_words) < 5: return False
        left_y = set(round(w[1]) for w in left_words)
        right_y = set(round(w[1]) for w in right_words)
        overlap = left_y & right_y
        if len(overlap) < 5: return False
        digit_count = sum(1 for w in right_words if re_lao_digit.match(w[4].strip()))
        return digit_count / len(right_words) >= 0.8

    def build_text_from_words(words):
        rows = {}
        for w in words:
            y = round(w[1])
            rows.setdefault(y, []).append((w[0], w[4]))
        lines = []
        for y in sorted(rows.keys()):
            row = sorted(rows[y], key=lambda t: t[0])
            lines.append(' '.join(t[1] for t in row))
        return '\n'.join(lines)

    pages_data = []
    text_index = []
    all_warnings = set()
    total_lao_chars = 0
    total_extracted = 0
    for i in range(total_pages):
        page = doc[i]
        words = page.get_text("words")
        if is_multicolumn(words):
            text = clean_text(fix_lao_text(build_text_from_words(words).strip()))
        else:
            text = clean_text(fix_lao_text(page.get_text("text").strip()))
        word_positions = [{"w": w[4], "x0": round(w[0], 1), "y0": round(w[1], 1), "x1": round(w[2], 1), "y1": round(w[3], 1)} for w in words]
        pages_data.append({"page": i + 1, "text": text, "words": word_positions})
        first_line = text[:100].replace('\n', ' ').strip()
        text_index.append({"page": i + 1, "preview": first_line})
        quality = validate_lao_text(text)
        all_warnings.update(quality.get("warnings", []))
        total_lao_chars += sum(1 for c in text if '\u0E80' <= c <= '\u0EFF')
        total_extracted += sum(1 for c in text if not c.isspace())

    with open(os.path.join(out_dir, 'pages.json'), 'w', encoding='utf-8') as f:
        json.dump(pages_data, f, ensure_ascii=False, indent=None)
    with open(os.path.join(out_dir, 'index.json'), 'w', encoding='utf-8') as f:
        json.dump(text_index, f, ensure_ascii=False, indent=None)

    # Generate cover from page 1
    mat_d = fitz.Matrix(150/72, 150/72)
    pix = doc[0].get_pixmap(matrix=mat_d)
    pix.save(os.path.join(out_dir, 'cover.png'))

    book_info = {"title": title, "year": 2026, "totalPages": total_pages, "type": "pdf"}
    with open(os.path.join(out_dir, 'book.json'), 'w', encoding='utf-8') as f:
        json.dump(book_info, f, ensure_ascii=False, indent=2)

    doc.close()
    return total_pages, list(all_warnings)

def convert_docx(file_path, out_dir, title):
    import docx
    os.makedirs(out_dir, exist_ok=True)
    doc = docx.Document(file_path)
    paragraphs = []
    for p in doc.paragraphs:
        t = clean_text(p.text)
        if t:
            paragraphs.append(t)

    total_chars = sum(len(p) for p in paragraphs)
    target_chars_per_page = 2000
    total_pages = max(1, math.ceil(total_chars / target_chars_per_page))
    chars_per_page = math.ceil(total_chars / total_pages)

    pages_data = []
    text_index = []
    current_text = []
    current_count = 0
    page_num = 1

    for text in paragraphs:
        text_len = len(text) + 1
        if current_count + text_len > chars_per_page * 1.5 and current_count > 0:
            page_text = clean_text('\n'.join(current_text))
            pages_data.append({"page": page_num, "text": page_text, "words": []})
            first_line = page_text[:100].replace('\n', ' ').strip()
            text_index.append({"page": page_num, "preview": first_line})
            page_num += 1
            current_text = [text]
            current_count = text_len
        else:
            current_text.append(text)
            current_count += text_len

    if current_text:
        page_text = clean_text('\n'.join(current_text))
        pages_data.append({"page": page_num, "text": page_text, "words": []})
        first_line = page_text[:100].replace('\n', ' ').strip()
        text_index.append({"page": page_num, "preview": first_line})

    with open(os.path.join(out_dir, 'pages.json'), 'w', encoding='utf-8') as f:
        json.dump(pages_data, f, ensure_ascii=False, indent=None)
    with open(os.path.join(out_dir, 'index.json'), 'w', encoding='utf-8') as f:
        json.dump(text_index, f, ensure_ascii=False, indent=None)

    book_info = {"title": title, "year": 2026, "totalPages": len(pages_data), "type": "docx"}
    with open(os.path.join(out_dir, 'book.json'), 'w', encoding='utf-8') as f:
        json.dump(book_info, f, ensure_ascii=False, indent=2)

    return len(pages_data)

def convert_doc(file_path, out_dir, title):
    os.makedirs(out_dir, exist_ok=True)
    tmp_txt = os.path.join(tempfile.mkdtemp(), 'output.txt')
    full_text = ""
    try:
        subprocess.run(['textutil', '-convert', 'txt', '-output', tmp_txt, file_path], check=True, capture_output=True, timeout=120)
        with open(tmp_txt, 'r', encoding='utf-8', errors='replace') as f:
            full_text = f.read()
    except:
        pass
    finally:
        if os.path.exists(tmp_txt):
            os.unlink(tmp_txt)
            os.rmdir(os.path.dirname(tmp_txt))

    if not full_text.strip():
        book_info = {"title": title, "year": 2026, "totalPages": 0, "type": "docx"}
        with open(os.path.join(out_dir, 'book.json'), 'w', encoding='utf-8') as f:
            json.dump(book_info, f, ensure_ascii=False, indent=2)
        with open(os.path.join(out_dir, 'pages.json'), 'w', encoding='utf-8') as f:
            json.dump([], f)
        with open(os.path.join(out_dir, 'index.json'), 'w', encoding='utf-8') as f:
            json.dump([], f)
        return 0

    paragraphs = [clean_text(p) for p in full_text.split('\n') if clean_text(p)]
    total_chars = sum(len(p) for p in paragraphs)
    total_pages = max(1, math.ceil(total_chars / 2000))
    chars_per_page = math.ceil(total_chars / total_pages)

    pages_data = []
    text_index = []
    current_text = []
    current_count = 0
    page_num = 1
    for text in paragraphs:
        text_len = len(text) + 1
        if current_count + text_len > chars_per_page * 1.5 and current_count > 0:
            page_text = clean_text('\n'.join(current_text))
            pages_data.append({"page": page_num, "text": page_text, "words": []})
            first_line = page_text[:100].replace('\n', ' ').strip()
            text_index.append({"page": page_num, "preview": first_line})
            page_num += 1
            current_text = [text]
            current_count = text_len
        else:
            current_text.append(text)
            current_count += text_len
    if current_text:
        page_text = clean_text('\n'.join(current_text))
        pages_data.append({"page": page_num, "text": page_text, "words": []})
        first_line = page_text[:100].replace('\n', ' ').strip()
        text_index.append({"page": page_num, "preview": first_line})

    with open(os.path.join(out_dir, 'pages.json'), 'w', encoding='utf-8') as f:
        json.dump(pages_data, f, ensure_ascii=False, indent=None)
    with open(os.path.join(out_dir, 'index.json'), 'w', encoding='utf-8') as f:
        json.dump(text_index, f, ensure_ascii=False, indent=None)

    book_info = {"title": title, "year": 2026, "totalPages": len(pages_data), "type": "docx"}
    with open(os.path.join(out_dir, 'book.json'), 'w', encoding='utf-8') as f:
        json.dump(book_info, f, ensure_ascii=False, indent=2)
    return len(pages_data)

if __name__ == '__main__':
    if len(sys.argv) < 4:
        print(json.dumps({"success": False, "error": "Usage: convert-book.py <file_path> <slug> <out_dir> [title]"}))
        sys.exit(1)

    file_path = sys.argv[1]
    slug = sys.argv[2]
    out_dir = sys.argv[3]
    title = sys.argv[4] if len(sys.argv) > 4 else slug

    if not os.path.exists(file_path):
        print(json.dumps({"success": False, "error": f"File not found: {file_path}"}))
        sys.exit(1)

    ext = os.path.splitext(file_path)[1].lower()

    try:
        quality_warnings = []
        if ext == '.pdf':
            total_pages, quality_warnings = convert_pdf(file_path, out_dir, title)
            book_type = 'pdf'
        elif ext == '.docx':
            total_pages = convert_docx(file_path, out_dir, title)
            book_type = 'docx'
        elif ext == '.doc':
            total_pages = convert_doc(file_path, out_dir, title)
            book_type = 'docx'
        else:
            print(json.dumps({"success": False, "error": f"Unsupported format: {ext}"}))
            sys.exit(1)

        quality = {"warnings": quality_warnings} if quality_warnings else {}
        print(json.dumps({
            "success": True,
            "slug": slug,
            "title": title,
            "type": book_type,
            "totalPages": total_pages,
            "quality": quality
        }, ensure_ascii=False))
    except Exception as e:
        print(json.dumps({"success": False, "error": str(e)}, ensure_ascii=False))
        sys.exit(1)