Update: 初学记、佩文韵府 and 五车韵瑞
This commit is contained in:
56
佩文韵府/test_hybrid.py
Normal file
56
佩文韵府/test_hybrid.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import json
|
||||
import re
|
||||
|
||||
prefixes = ["韻藻", "增"]
|
||||
|
||||
def replace_pipes_hybrid(content, word):
|
||||
clean_word = word
|
||||
for p in prefixes:
|
||||
if clean_word.startswith(p) and len(clean_word) > len(p):
|
||||
clean_word = clean_word[len(p):]
|
||||
break
|
||||
|
||||
word_len = len(clean_word)
|
||||
if word_len == 0:
|
||||
return content.replace("丨", "")
|
||||
|
||||
def repl(match):
|
||||
nonlocal pipe_idx
|
||||
block = match.group(0)
|
||||
block_len = len(block)
|
||||
|
||||
if block_len % word_len == 0:
|
||||
# Full word match! Reset alignment.
|
||||
pipe_idx = 0
|
||||
return clean_word * (block_len // word_len)
|
||||
else:
|
||||
# Partial word match. Use current sequence.
|
||||
res = ""
|
||||
for _ in range(block_len):
|
||||
res += clean_word[pipe_idx % word_len]
|
||||
pipe_idx += 1
|
||||
return res
|
||||
|
||||
pipe_idx = 0
|
||||
return re.sub(r'丨+', repl, content)
|
||||
|
||||
test_cases = [
|
||||
("首陽東", "詩采葑采葑丨丨之丨"),
|
||||
("馬首東", "左傳欒黶曰吾丨丨欲丨乃歸下軍從之"),
|
||||
("澗瀍東", "書洛誥我乃卜丨水東丨水西惟洛食我又卜瀍水丨亦惟洛食"),
|
||||
("日夜東", "丨丨虞集詩絳桃風急丨丨丨王惲詩付與衡漳丨丨丨許有壬詩江水舟"),
|
||||
("東海東", "樓鑰詩萬里逺在丨丨丨張經詩崑崙之西丨"),
|
||||
]
|
||||
|
||||
for w, c in test_cases:
|
||||
print(f"Word: {w}")
|
||||
print(f"Orig: {c}")
|
||||
print(f"Hybr: {replace_pipes_hybrid(c, w)}")
|
||||
print("-" * 40)
|
||||
test_cases.append(("紫殿東", "時魚躍丨丨丨温庭筠詩一夕丨丨"))
|
||||
|
||||
for w, c in test_cases[-1:]:
|
||||
print(f"Word: {w}")
|
||||
print(f"Orig: {c}")
|
||||
print(f"Hybr: {replace_pipes_hybrid(c, w)}")
|
||||
print("-" * 40)
|
||||
Reference in New Issue
Block a user