Skip to content

Commit

Permalink
a702, a704 改用 PiauIm 類別後,程式異常除錯。
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJui committed Nov 9, 2024
1 parent acb1dce commit 51f5d9f
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 73 deletions.
Binary file modified Kong_Un.db
Binary file not shown.
22 changes: 20 additions & 2 deletions a702_查找及填入漢字標音.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,27 @@
type = get_sound_type(wb)
han_ji_khoo = get_han_ji_khoo(wb)
if han_ji_khoo == "河洛話":
ca_han_ji_thak_im(wb, sheet_name='漢字注音', cell='V3', hue_im="白話音", han_ji_khoo="河洛話", db_name='Ho_Lok_Ue.db', module_name='mod_河洛話', function_name='han_ji_ca_piau_im')
ca_han_ji_thak_im(
wb=wb,
sheet_name='漢字注音',
cell='V3',
hue_im="白話音",
han_ji_khoo="河洛話",
db_name='Ho_Lok_Ue.db',
module_name='mod_河洛話',
function_name='han_ji_ca_piau_im',
)
else:
ca_han_ji_thak_im(wb, sheet_name='漢字注音', cell='V3', hue_im="文讀音", han_ji_khoo="廣韻", db_name='Kong_Un.db', module_name='mod_廣韻', function_name='han_ji_ca_piau_im')
ca_han_ji_thak_im(
wb=wb,
sheet_name='漢字注音',
cell='V3',
hue_im="文讀音",
han_ji_khoo="廣韻",
db_name='Kong_Un.db',
module_name='mod_廣韻',
function_name='han_ji_ca_piau_im',
)

# (3) A740: 將【漢字注音】工作表的內容,轉成 HTML 網頁檔案。
# tng_sing_bang_iah(wb, '漢字注音', 'V3')
Expand Down
26 changes: 1 addition & 25 deletions a704_製作其它漢字標音.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,8 @@
copy_excel_sheet(wb, '漢字注音', piau_im_huat)
reset_han_ji_piau_im_cells(wb, piau_im_huat)

# 根據標音方法選擇對應參數
if piau_im_huat == '十五音':
hue_im = "白話音"
module_name = 'mod_標音'
function_name = 'TLPA_Tng_Zap_Goo_Im'
elif piau_im_huat == '白話字':
# 以下為待完成之虛擬程式碼
hue_im = "白話字"
module_name = 'mod_白話字'
function_name = 'bah_hoat_tng_im'
elif piau_im_huat == '台羅拼音':
# 以下為待完成之虛擬程式碼
hue_im = "台羅拼音"
module_name = 'mod_台羅拼音'
function_name = 'tai_lo_pin_im'
elif piau_im_huat == '閩拚方案':
# 以下為待完成之虛擬程式碼
hue_im = "閩拚方案"
module_name = 'mod_閩拚方案'
function_name = 'min_pian_han_im'
else:
print(f"無法識別的標音方法:{piau_im_huat}")
sys.exit(1)

# 呼叫 han_ji_piau_im 函數,並傳入動態參數
han_ji_piau_im(wb, sheet_name=piau_im_huat, cell='V3', hue_im=hue_im, module_name=module_name, function_name=function_name)
han_ji_piau_im(wb, sheet_name=piau_im_huat, cell='V3')

# (3) A740: 將【漢字注音】工作表的內容,轉成 HTML 網頁檔案。
# tng_sing_bang_iah(wb, '漢字注音', 'V3')
Expand Down
7 changes: 1 addition & 6 deletions mod_廣韻.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
用 `漢字` 查詢《廣韻》的標音
"""
def han_ji_ca_piau_im(cursor, han_ji):
def han_ji_ca_piau_im(cursor, han_ji, hue_im="文讀音"):
"""
根據漢字查詢其讀音資訊。 若資料紀錄在`常用度`欄位儲存值為空值(NULL)
,則將其視為 0,因此可排在查詢結果的最後。
Expand All @@ -30,11 +30,6 @@ def han_ji_ca_piau_im(cursor, han_ji):
results = cursor.fetchall()

# 將結果轉換為字典列表
# fields = [
# '漢字號', '漢字', '漢字標音', '常用度', '上字', '下字', '字義解釋',
# '七聲類', '發音部位', '聲母', '聲母標音', '清濁', '發送收',
# '韻母', '韻母標音', '攝', '調', '韻目', '目次', '等', '呼'
# ]
fields = [
'字號', '漢字', '標音', '常用度', '上字', '下字', '字義解釋',
'七聲類', '發音部位', '聲母', '聲母標音', '清濁', '發送收',
Expand Down
3 changes: 2 additions & 1 deletion mod_標音.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ def TPS_piau_im(self, siann_bu, un_bu, tiau_ho):

siann = self.Siann_Bu_Dict[siann_bu][piau_im_huat]
un = self.Un_Bu_Dict[un_bu][piau_im_huat]
tiau = self.TONE_MARKS[piau_im_huat][int(tiau_ho)]
tiau_ho = 7 if int(tiau_ho) == 6 else int(tiau_ho)
tiau = self.TONE_MARKS[piau_im_huat][tiau_ho]
piau_im = f"{siann}{un}{tiau}"

pattern = r"(ㄗㄧ|ㄘㄧ|ㄙㄧ|ㆡㄧ)"
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
108 changes: 81 additions & 27 deletions p702_Ca_Han_Ji_Thak_Im.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@
import xlwings as xw

from mod_file_access import load_module_function
from mod_標音 import TL_Tng_Zu_Im, is_valid_han_ji, split_zu_im

from mod_標音 import PiauIm, is_valid_han_ji, split_zu_im


def choose_piau_im_method(piau_im, zu_im_huat, siann_bu, un_bu, tiau_ho):
"""選擇並執行對應的注音方法"""
if zu_im_huat == "十五音":
return piau_im.SNI_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "白話字":
return piau_im.POJ_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "台羅拼音":
return piau_im.TL_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "閩拼方案":
return piau_im.BP_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "方音符號":
return piau_im.TPS_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "台語音標":
siann = piau_im.Siann_Bu_Dict[siann_bu]["台語音標"] or ""
un = piau_im.Un_Bu_Dict[un_bu]["台語音標"]
return f"{siann}{un}{tiau_ho}"
return ""

def ca_han_ji_thak_im(wb, sheet_name='漢字注音', cell='V3', hue_im="白話音", han_ji_khoo="河洛話", db_name='Ho_Lok_Ue.db', module_name='mod_河洛話', function_name='han_ji_ca_piau_im'):
# 初始化 PiauIm 類別,産生標音物件
piau_im = PiauIm(han_ji_khoo=han_ji_khoo)
# piau_im_huat = wb.names['標音方法'].refers_to_range.value
piau_im_huat = '方音符號'

# 顯示「已輸入之拼音字母及注音符號」
named_range = wb.names['顯示注音輸入']
named_range.refers_to_range.value = True
Expand Down Expand Up @@ -77,45 +100,76 @@ def ca_han_ji_thak_im(wb, sheet_name='漢字注音', cell='V3', hue_im="白話
if manual_input:
if '〔' in manual_input and '〕' in manual_input and '【' in manual_input and '】' in manual_input:
lo_ma_im_piau = manual_input.split('〔')[1].split('〕')[0]
zu_im_hu_ho = manual_input.split('【')[1].split('】')[0]
han_ji_piau_im = manual_input.split('【')[1].split('】')[0]
else:
zu_im_list = split_zu_im(manual_input)
zu_im_hu_ho = TL_Tng_Zu_Im(
siann_bu=zu_im_list[0],
un_bu=zu_im_list[1],
siann_tiau=zu_im_list[2],
cursor=cursor
)['注音符號']
# zu_im_hu_ho = TL_Tng_Zu_Im(
# siann_bu=zu_im_list[0],
# un_bu=zu_im_list[1],
# siann_tiau=zu_im_list[2],
# cursor=cursor
# )['注音符號']
lo_ma_im_piau = manual_input
piau_im_list = split_zu_im(lo_ma_im_piau)
if piau_im_list[0] == "" or piau_im_list[0] == None:
siann_bu = "Ø"
else:
siann_bu = piau_im_list[0]

han_ji_piau_im = choose_piau_im_method(
piau_im,
piau_im_huat,
siann_bu,
piau_im_list[1],
piau_im_list[2]
)

sheet.range((row - 1, col)).value = lo_ma_im_piau
sheet.range((row + 1, col)).value = zu_im_hu_ho
sheet.range((row + 1, col)).value = han_ji_piau_im
else:
result = han_ji_ca_piau_im(cursor=cursor, han_ji=han_ji, hue_im=hue_im)

if result:
if han_ji_khoo == "河洛話":
lo_ma_im_piau = result[0]['台語音標']
zu_im_hu_ho = TL_Tng_Zu_Im(
siann_bu=result[0]['聲母'],
un_bu=result[0]['韻母'],
siann_tiau=result[0]['聲調'],
cursor=cursor
# 將【台語音標】分解為【聲母】、【韻母】、【聲調】
siann_bu, un_bu, tiau_ho = split_zu_im(result[0]['台語音標'])
# lo_ma_im_piau = f'{siann_bu}{un_bu}{tiau_ho}'
# lo_ma_im_piau = siann_bu + un_bu + tiau_ho
lo_ma_im_piau = ''.join([siann_bu, un_bu, tiau_ho])

# 將【台語音標】分解為【聲母】、【韻母】、【聲調】
if siann_bu == "" or siann_bu == None:
siann_bu = "Ø"

han_ji_piau_im = choose_piau_im_method(
piau_im,
piau_im_huat,
siann_bu,
un_bu,
tiau_ho,
)
else:
lo_ma_im_piau = split_zu_im(result[0]['標音'])
zu_im_hu_ho = TL_Tng_Zu_Im(
siann_bu=lo_ma_im_piau[0],
un_bu=lo_ma_im_piau[1],
siann_tiau=lo_ma_im_piau[2],
cursor=cursor
# 將《廣韻》字庫的【標音】分解為【聲母】、【韻母】、【聲調】
siann_bu, un_bu, tiau_ho = split_zu_im(result[0]['標音'])
lo_ma_im_piau = siann_bu + un_bu + tiau_ho

# 將【台語音標】分解為【聲母】、【韻母】、【聲調】
if siann_bu == "" or siann_bu == None:
siann_bu = "Ø"

han_ji_piau_im = choose_piau_im_method(
piau_im,
piau_im_huat,
siann_bu,
un_bu,
tiau_ho,
)
sheet.range((row - 1, col)).value = ''.join(lo_ma_im_piau)
sheet.range((row + 1, col)).value = zu_im_hu_ho['注音符號']
# sheet.range((row - 1, col)).value = ''.join(lo_ma_im_piau)
sheet.range((row - 1, col)).value = lo_ma_im_piau
sheet.range((row + 1, col)).value = han_ji_piau_im
else:
msg = f"【{cell_value}】查無此字!"
if lo_ma_im_piau and zu_im_hu_ho:
print(f"({row}, {col_name}) = {han_ji} [{lo_ma_im_piau}] 【{zu_im_hu_ho}】")
if lo_ma_im_piau and han_ji_piau_im:
print(f"({row}, {col_name}) = {han_ji} [{lo_ma_im_piau}] 【{han_ji_piau_im}】")
else:
print(f"({row}, {col_name}) = {msg}")

Expand Down
55 changes: 43 additions & 12 deletions p704_漢字以十五音標注音.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@

import xlwings as xw

from mod_file_access import load_module_function
from mod_標音 import is_valid_han_ji, split_zu_im

# from mod_file_access import load_module_function
from mod_標音 import PiauIm, is_valid_han_ji, split_zu_im


def choose_piau_im_method(piau_im, zu_im_huat, siann_bu, un_bu, tiau_ho):
"""選擇並執行對應的注音方法"""
if zu_im_huat == "十五音":
return piau_im.SNI_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "白話字":
return piau_im.POJ_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "台羅拼音":
return piau_im.TL_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "閩拼方案":
return piau_im.BP_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "方音符號":
return piau_im.TPS_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "台語音標":
siann = piau_im.Siann_Bu_Dict[siann_bu]["台語音標"] or ""
un = piau_im.Un_Bu_Dict[un_bu]["台語音標"]
return f"{siann}{un}{tiau_ho}"
return ""

# 十五音標注音
def han_ji_piau_im(wb, sheet_name='十五音', cell='V3', hue_im="白話音", han_ji_khoo="河洛話", db_name='Ho_Lok_Ue.db', module_name='mod_標音', function_name='TLPA_Tng_Zap_Goo_Im'):
def han_ji_piau_im(wb, sheet_name='十五音', cell='V3', hue_im="白話音", han_ji_khoo="河洛話", db_name='Ho_Lok_Ue.db'):
# 初始化 PiauIm 類別,産生標音物件
piau_im = PiauIm(han_ji_khoo=han_ji_khoo)
piau_im_huat = wb.names['標音方法'].refers_to_range.value

# 顯示「已輸入之拼音字母及注音符號」
named_range = wb.names['顯示注音輸入']
named_range.refers_to_range.value = True

Expand All @@ -24,19 +47,18 @@ def han_ji_piau_im(wb, sheet_name='十五音', cell='V3', hue_im="白話音", ha
print("可供作業之儲存格數太少,無法進行作業!")
return

han_ji_tng_piau_im = load_module_function(module_name, function_name)
conn = sqlite3.connect(db_name)
cursor = conn.cursor()

row, index = 5, 0

def process_cell(row, col, char):
def process_cell(row, col, char, piau_im=piau_im):
"""處理單一儲存格的標音轉換和填寫"""
col_name = xw.utils.col_name(col)
cell_value = sheet.range((row, col)).value

if not is_valid_han_ji(cell_value):
# print(f"({row}, {col_name}) = 無效漢字或標音缺失")
# 若儲存格內容不是漢字,應是:標點符號或空白,故將其顯示
print(f"({row}, {col_name}) = {cell_value}")
return

Expand All @@ -47,9 +69,19 @@ def process_cell(row, col, char):

try:
siann_bu, un_bu, siann_tiau = split_zu_im(lo_ma_im_piau)
zu_im_hu_ho = han_ji_tng_piau_im(siann_bu=siann_bu, un_bu=un_bu, siann_tiau=siann_tiau, cursor=cursor)
sheet.range((row + 1, col)).value = zu_im_hu_ho['漢字標音']
print(f"({row + 1}, {col_name}) = 【{char}{zu_im_hu_ho['漢字標音']}")

if siann_bu == "" or siann_bu == None:
siann_bu = "Ø"

han_ji_piau_im = choose_piau_im_method(
piau_im,
piau_im_huat,
siann_bu,
un_bu,
siann_tiau
)
sheet.range((row + 1, col)).value = han_ji_piau_im
print(f"({row + 1}, {col_name}) = 【{char}{han_ji_piau_im}")
except ValueError as e:
print(f"【台語音標】資料格式錯誤於({row - 1}, {col_name}): {e}")

Expand All @@ -61,10 +93,9 @@ def process_cell(row, col, char):
char = v3_value[index]
if char == "\n":
index += 1
row += 4
break # 跳出內部 for 迴圈,繼續處理下一列

process_cell(row, col, char)
process_cell(row, col, char, piau_im=piau_im)
index += 1 # 處理下一個漢字

row += 4 # 移動到下一組儲存格區域
Expand Down

0 comments on commit 51f5d9f

Please sign in to comment.