File size: 1,762 Bytes
3e1cbf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""HTML -> text theo dòng, giữ ranh giới khối để parser nhận diện Điều/Khoản/Điểm."""

from __future__ import annotations

import html
import re

_DROP = re.compile(r"<(style|script|head)\b[^>]*>.*?</\1>", re.I | re.S)
_BLOCK = re.compile(r"</(p|div|tr|li|h[1-6])>|<br\s*/?>", re.I)
_TAG = re.compile(r"<[^>]+>")
_MULTINL = re.compile(r"\n{2,}")


def fix_mojibake(s: str) -> str:
    """Sửa trường hợp UTF-8 bị đọc nhầm thành Latin-1 (vd 'QUá»C HỘI' -> 'QUỐC HỘI').

    Chỉ áp dụng khi phát hiện dấu hiệu mojibake rõ, để không phá văn bản vốn đúng.
    """
    markers = s.count("Ã") + s.count("á»") + s.count("Æ") + s.count("áº")
    if markers < 5:
        return s
    # Dữ liệu gốc là UTF-8 bị đọc nhầm; thường là cp1252 (có ký tự như '˜'),
    # đôi khi latin-1. Thử lần lượt, giữ bản giảm được nhiều mojibake nhất.
    for enc in ("cp1252", "latin-1"):
        try:
            fixed = s.encode(enc, errors="strict").decode("utf-8")
        except (UnicodeEncodeError, UnicodeDecodeError):
            continue
        if (fixed.count("Ã") + fixed.count("á»")) < markers:
            return fixed
    return s


def html_to_text(raw: str) -> str:
    if not raw:
        return ""
    s = fix_mojibake(raw.replace("\r\n", "\n"))
    s = _DROP.sub("", s)         # bỏ hẳn style/script/head
    s = _BLOCK.sub("\n", s)      # kết thúc khối -> xuống dòng
    s = _TAG.sub("", s)          # bỏ thẻ còn lại
    s = html.unescape(s)
    s = s.replace(" ", " ")
    lines = [re.sub(r"[ \t]+", " ", ln).strip() for ln in s.split("\n")]
    s = "\n".join(ln for ln in lines if ln)
    return _MULTINL.sub("\n", s)