Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| import re | |
| import html | |
| from typing import Any | |
| # HTML ํ๊ทธ ์ ๊ฑฐ ์ ๊ท์ (์คํฌ๋ฆฝํธ ๋ฐ ์คํ์ผ ํ๊ทธ ์์ญ ํฌํจ ์ ๊ฑฐ) | |
| HTML_TAG_PATTERN = re.compile(r'<[^>]+>', re.IGNORECASE) | |
| HTML_STYLE_PATTERN = re.compile(r'<style[^>]*>.*?</style>', re.DOTALL | re.IGNORECASE) | |
| HTML_SCRIPT_PATTERN = re.compile(r'<script[^>]*>.*?</script>', re.DOTALL | re.IGNORECASE) | |
| # ์ ์ ์๋ ๋ ธ์ด์ฆ ํน์ ๊ธฐํธ ํจํด | |
| NOISE_CHARS_PATTERN = re.compile(r'[\u2591-\u2593โ\u0000\ufffd]') | |
| def purify_text_noise(raw_text: Any) -> str: | |
| """ | |
| ํ ์คํธ ๋ด์ HTML ๋ ธ์ด์ฆ, ํน์ ๋ถํธ ๋ฐ ์ํฐํฐ ์ฝ๋๋ฅผ ์๋ฒฝํ ์ ์ ํ์ฌ | |
| ์๋ฒ ๋ฉ์ ์ ํฉํ ํด๋ฆฐํ ํ๋ ์ธ ํ ์คํธ(Plain Text)๋ก ๋ฐํํฉ๋๋ค. | |
| """ | |
| if not raw_text: | |
| return "" | |
| text = str(raw_text) | |
| # 1. HTML ์ํฐํฐ ๋ณต์ (e.g. -> ๊ณต๋ฐฑ, & -> &) | |
| text = html.unescape(text) | |
| # 2. HTML ์คํฌ๋ฆฝํธ ๋ฐ ์คํ์ผ ๋ณธ๋ฌธ ์์ญ ์ ์ฒด ์ ๊ฑฐ | |
| text = HTML_STYLE_PATTERN.sub(" ", text) | |
| text = HTML_SCRIPT_PATTERN.sub(" ", text) | |
| # 3. ๋ชจ๋ HTML ํ๊ทธ ์คํธ๋ฆฌํ | |
| text = HTML_TAG_PATTERN.sub(" ", text) | |
| # 4. ๊นจ์ง ์ ๋์ฝ๋ ๋ฐ ์ ์ ์๋ ๊ธฐํธ(โ ๋ฑ) ์ ๊ฑฐ | |
| text = NOISE_CHARS_PATTERN.sub("", text) | |
| # 5. ๊ณผ๋ํ ๊ณต๋ฐฑ ๋ฐ ๊ฐํ ์ ๊ทํ | |
| text = re.sub(r'\n\s*\n', '\n\n', text) | |
| text = re.sub(r'[ \t]+', ' ', text) | |
| return text.strip() | |