修复字符偏移2
This commit is contained in:
@@ -251,8 +251,8 @@ _patterns_dynasty = _build_pattern(with_dynasty=True)
|
|||||||
|
|
||||||
# === 主函数 ===
|
# === 主函数 ===
|
||||||
def extract_era_years(text):
|
def extract_era_years(text):
|
||||||
# 去除标记用的方括号/尖括号(用户标注用,非原文内容)
|
# 标注符号([]、【】)不参与匹配,但保留在文本中——位置偏移以原始文本为准
|
||||||
text = text.replace('[', '').replace(']', '').replace('【', '').replace('】', '')
|
# (含换行/空格/标注),仅在收尾时把标注符号从"原文"字段剔除
|
||||||
original_text = text
|
original_text = text
|
||||||
simplified_text = normalize_chars(cc.convert(text))
|
simplified_text = normalize_chars(cc.convert(text))
|
||||||
results = []
|
results = []
|
||||||
@@ -382,10 +382,18 @@ def extract_era_years(text):
|
|||||||
if not (1000 <= year_num <= 2100):
|
if not (1000 <= year_num <= 2100):
|
||||||
continue
|
continue
|
||||||
# 公元年后紧跟的干支(如"公元一九六〇年庚子"):无年号可锚定时,
|
# 公元年后紧跟的干支(如"公元一九六〇年庚子"):无年号可锚定时,
|
||||||
# 干支就是年份的干支,并入同一日期单位并校验
|
# 干支就是年份的干支,紧邻时并入同一日期单位——保证 位置[start:end]==原文,
|
||||||
|
# 前端可按范围整体替换;隔标注符号时(【公元一九六〇年】庚子)干支只摘入字段校验
|
||||||
ganzhi = simplified_text[end:end + 2]
|
ganzhi = simplified_text[end:end + 2]
|
||||||
if ganzhi in _ganzhi_to_offset:
|
if ganzhi in _ganzhi_to_offset:
|
||||||
end += 2
|
end += 2
|
||||||
|
else:
|
||||||
|
j = end
|
||||||
|
while j < len(simplified_text) and simplified_text[j] in '[]【】':
|
||||||
|
j += 1
|
||||||
|
ganzhi = simplified_text[j:j + 2]
|
||||||
|
if ganzhi not in _ganzhi_to_offset:
|
||||||
|
ganzhi = None
|
||||||
covered.update(range(start, end))
|
covered.update(range(start, end))
|
||||||
raw_fan = original_text[start:end]
|
raw_fan = original_text[start:end]
|
||||||
era_type = "公元纪年(中华人民共和国)" if year_num >= 1949 else "公元纪年(近代/其他)"
|
era_type = "公元纪年(中华人民共和国)" if year_num >= 1949 else "公元纪年(近代/其他)"
|
||||||
@@ -638,6 +646,12 @@ def extract_era_years(text):
|
|||||||
if result.get("公元") is not None:
|
if result.get("公元") is not None:
|
||||||
result.setdefault("干支", gregorian_to_ganzhi(result["公元"]))
|
result.setdefault("干支", gregorian_to_ganzhi(result["公元"]))
|
||||||
|
|
||||||
|
# === 收尾:原文剔除标注符号;位置保持原始文本偏移(含换行/空格/标注) ===
|
||||||
|
for result in results:
|
||||||
|
start, end = result["位置"]["起始"], result["位置"]["结束"]
|
||||||
|
result["原文"] = ''.join(ch for ch in original_text[start:end] if ch not in '[]【】')
|
||||||
|
result["原文(简体)"] = ''.join(ch for ch in simplified_text[start:end] if ch not in '[]【】')
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user