fix: 법령 분할 — 조문키 000 기반 장(章) 단위 분할로 변경
국가법령 XML은 <편>/<장> 태그가 아닌 <조문단위 조문키="xxxx000">에 "제X장 ..." 형태로 장 구분자가 포함됨. 이를 파싱하여 분할. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -191,29 +191,50 @@ async def _save_law_split(
|
|||||||
session, xml_text: str, law_name: str, proclamation_date: str,
|
session, xml_text: str, law_name: str, proclamation_date: str,
|
||||||
revision_type: str, prev_date: str,
|
revision_type: str, prev_date: str,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""법령 XML → 편/장 단위 Markdown 분할 저장"""
|
"""법령 XML → 장(章) 단위 Markdown 분할 저장"""
|
||||||
root = ET.fromstring(xml_text)
|
root = ET.fromstring(xml_text)
|
||||||
|
|
||||||
|
# 조문단위에서 장 구분자 찾기 (조문키가 000으로 끝나는 조문)
|
||||||
|
units = root.findall(".//조문단위")
|
||||||
|
chapters = [] # [(장제목, [조문들])]
|
||||||
|
current_chapter = None
|
||||||
|
current_articles = []
|
||||||
|
|
||||||
|
for unit in units:
|
||||||
|
key = unit.attrib.get("조문키", "")
|
||||||
|
content = (unit.findtext("조문내용", "") or "").strip()
|
||||||
|
|
||||||
|
# 장 구분자: 키가 000으로 끝나고 내용에 "제X장" 포함
|
||||||
|
if key.endswith("000") and re.search(r"제\d+장", content):
|
||||||
|
# 이전 장 저장
|
||||||
|
if current_chapter and current_articles:
|
||||||
|
chapters.append((current_chapter, current_articles))
|
||||||
|
chapter_match = re.search(r"(제\d+장\s*.+)", content)
|
||||||
|
current_chapter = chapter_match.group(1).strip() if chapter_match else content.strip()
|
||||||
|
current_articles = []
|
||||||
|
else:
|
||||||
|
current_articles.append(unit)
|
||||||
|
|
||||||
|
# 마지막 장 저장
|
||||||
|
if current_chapter and current_articles:
|
||||||
|
chapters.append((current_chapter, current_articles))
|
||||||
|
|
||||||
|
# 장 분할 성공
|
||||||
sections = []
|
sections = []
|
||||||
|
if chapters:
|
||||||
# 편(編) 단위 분할 시도
|
for chapter_title, articles in chapters:
|
||||||
for part in root.findall(".//*편"):
|
md_lines = [f"# {law_name}\n", f"## {chapter_title}\n"]
|
||||||
title = part.attrib.get("제목", part.findtext("편제목", ""))
|
for article in articles:
|
||||||
number = part.attrib.get("번호", "")
|
title = article.findtext("조문제목", "")
|
||||||
content = _xml_section_to_markdown(part)
|
content = article.findtext("조문내용", "")
|
||||||
if content.strip():
|
if title:
|
||||||
sections.append((f"제{number}편_{_safe_name(title)}", content))
|
md_lines.append(f"\n### {title}\n")
|
||||||
|
if content:
|
||||||
# 편이 없으면 장(章) 단위 시도
|
md_lines.append(content.strip())
|
||||||
if not sections:
|
section_name = _safe_name(chapter_title)
|
||||||
for chapter in root.findall(".//*장"):
|
sections.append((section_name, "\n".join(md_lines)))
|
||||||
title = chapter.attrib.get("제목", chapter.findtext("장제목", ""))
|
else:
|
||||||
number = chapter.attrib.get("번호", "")
|
# 장 분할 실패 → 전체 1파일
|
||||||
content = _xml_section_to_markdown(chapter)
|
|
||||||
if content.strip():
|
|
||||||
sections.append((f"제{number}장_{_safe_name(title)}", content))
|
|
||||||
|
|
||||||
# 편/장 둘 다 없으면 전체 1파일
|
|
||||||
if not sections:
|
|
||||||
full_md = _law_xml_to_markdown(xml_text, law_name)
|
full_md = _law_xml_to_markdown(xml_text, law_name)
|
||||||
sections.append(("전문", full_md))
|
sections.append(("전문", full_md))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user