-
Notifications
You must be signed in to change notification settings - Fork 0
/
工具.py
64 lines (63 loc) · 2.68 KB
/
工具.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pandas as pd;import numpy as np;from collections import Counter
冓 = {"⿰":2,"⿲":3,"⿱":2,"⿳":3,"⿴":2,"⿵":2,"⿶":2,"⿷":2,"⿸":2,"⿹":2,"⿺":2,"⿻":2,"":2,"":2,"":1,"":1,"4":4,"5":5,"T":3,"Y":3,"A":1,"+":1,"W":4}
挪用歸并件 = ["戶戸","辛⾟"]# 倒〬三角頭飾
挪用反倒轉件 = ["阜阝","舟⾈","水⽔","弓⼸","矢⽮","臣⾂"]# 雋之弓、受之舟、臦之反臣、益之水、射之矢
歸并件 = ["一●乛","囗〇","冈网","尞㶫","厂丆","上丄","下丅","凵𠙴","朩汖","兕𤉡","中𠁩","且𠀃","卣㔽","乂㐅","乙𠃉","几𠘧","冉冄","田龱","眉𦭝","垂𠂹","壼𡆵","井丼","申电","卩㔾","角甪","𫢉佘","于亐","皃㫐"]+ 挪用歸并件
反倒轉件 = ["止𣥂夂㐄","爪仉𠂇又","永𠂢","人匕𠤎𠂈","身㐆","大屰","卩㔿","孑孓","了巳","欠旡","口亼","目罒","矢𢆉","刀𱍸","冉𠀎","司后","皿亚","彳亍","戶𠃛","毛氺","上下","亅𠄌","一丨"]+ 挪用反倒轉件
def 構字遞歸(構字式:str, 到=0, 級=0):
""" 僅遞歸,不生成圖像。結構符歬置 """
構成 = ''
if 到 >= len(構字式):
print('終了(歬)')
return 構成, 到
if 構字式[到] in 冓.keys():
件數 = 冓[構字式[到]];print((' '*級)+構字式[到], 件數,'件')
構成 = 構字式[到]
for 件第 in range(件數):
到 += 1
件,到 = 構字遞歸(構字式, 到, 級+1);print(' '*級,件, str(到+1),'['+str(件第+1)+']')
構成 += 件
print((' '*級)+構成+'→☯')
return '☯', 到
else:
return 構字式[到], 到
def 檢構字式(構字式:str):
構 = 構字遞歸(構字式)
if 構[1] == len(構字式)-1:
print('正確');return True
elif 構[1] > len(構字式)-1:
raise ValueError('構字式有誤!或有宂餘結構符(缺少部件),或順序有誤。')
else:
raise ValueError('構字式有誤!或有宂餘部件(缺少結構符),或順序有誤。')
def 依構件排序(構件列):
序 = []
for 構件 in 構件列:
if len(構件)==1:
序.append(1)
elif len(構件)==0:
序.append(np.inf)
else:
序.append(5)
return 序
def 依備註排序(註列):
訛宂次 = {"異體":1, "偏旁":2, "倭":3, "国字":3, "簡体":3, "附":4, "訛":5}
序 = []
for 註 in 註列:
if 註 in 訛宂次.keys():
序.append(訛宂次[註])
else:
序.append(0)
return 序
def 攏(字列):
序 = []
for 字 in 字列:
歸并組 = [x for x in 歸并件 if 字 in x]
反倒轉組 = [x for x in 反倒轉件 if 字 in x]
if len(歸并組):
代排字 = 歸并組[0][0]
elif len(反倒轉組):
代排字 = 反倒轉件[0][0]
else:
代排字 = 字
序.append(代排字)
return 序