修复字符偏移

This commit is contained in:
sansen
2026-08-07 18:02:57 +08:00
parent 117eab4ad5
commit 29d14ab817
+8 -14
View File
@@ -251,8 +251,8 @@ _patterns_dynasty = _build_pattern(with_dynasty=True)
# === 主函数 ===
def extract_era_years(text):
# 去除标记用的方括号(用户标注用,非原文内容)
text = text.replace('[', '').replace(']', '')
# 去除标记用的方括号/尖括号(用户标注用,非原文内容)
text = text.replace('[', '').replace(']', '').replace('', '').replace('', '')
original_text = text
simplified_text = normalize_chars(cc.convert(text))
results = []
@@ -381,19 +381,22 @@ def extract_era_years(text):
year_num = chinese_to_num(year_text)
if not (1000 <= year_num <= 2100):
continue
# 公元年后紧跟的干支(如"公元一九六〇年庚子"):无年号可锚定时,
# 干支就是年份的干支,并入同一日期单位并校验
ganzhi = simplified_text[end:end + 2]
if ganzhi in _ganzhi_to_offset:
end += 2
covered.update(range(start, end))
raw_fan = original_text[start:end]
era_type = "公元纪年(中华人民共和国)" if year_num >= 1949 else "公元纪年(近代/其他)"
result = {
"原文": raw_fan,
"原文(简体)": m.group(0),
"原文(简体)": simplified_text[start:end],
"类型": era_type,
"公元": year_num,
"公元中文": f"{arabic_to_chinese_year(year_num)}",
"位置": {"起始": start, "结束": end},
}
# 公元年后紧跟的干支(如"公元一九六〇年庚子")用于校验年份一致性
ganzhi = ''.join(ch for ch in simplified_text[end:end + 4] if ch not in '【】').strip()[:2]
if ganzhi in _ganzhi_to_offset:
result["干支"] = ganzhi
if gregorian_to_ganzhi(year_num) != ganzhi:
@@ -635,15 +638,6 @@ def extract_era_years(text):
if result.get("公元") is not None:
result.setdefault("干支", gregorian_to_ganzhi(result["公元"]))
# === 收尾:紧邻的【】标注(用户标记的日期跨度)纳入原文 ===
for result in results:
start, end = result["位置"]["起始"], result["位置"]["结束"]
if (start > 0 and simplified_text[start - 1] == ''
and end < len(simplified_text) and simplified_text[end] == ''):
result["位置"] = {"起始": start - 1, "结束": end + 1}
result["原文"] = original_text[start - 1:end + 1]
result["原文(简体)"] = simplified_text[start - 1:end + 1]
return results