modern_lisp-machine/.config/mpv/scripts/katakana.lua

88 lines
3.7 KiB
Lua

-- half_to_full_katakana.lua
-- Convert half-width Katakana to full-width Katakana
local function half_to_full_katakana(text)
print("Debug: Converting half-width Katakana to full-width Katakana...")
local result = ""
local dakuten = false
for char in utf8.gmatch(text, ".") do
local codepoint = utf8.codepoint(char)
if codepoint == 0x3099 then -- Dakuten marker
dakuten = true
print("Debug: Found dakuten marker")
elseif codepoint >= 0xFF61 and codepoint <= 0xFF9F then -- Half-width Katakana
if dakuten then
-- Convert half-width Katakana with dakuten to full-width
if codepoint == 0xFF8A then -- ハ
result = result.. utf8.char(0x30CA) -- バ
print("Debug: Converted ハ to バ")
elseif codepoint == 0xFF77 then -- き
result = result.. utf8.char(0x30B0) -- ギ
print("Debug: Converted き to ギ")
elseif codepoint == 0xFF78 then -- く
result = result.. utf8.char(0x30B1) -- グ
print("Debug: Converted く to グ")
elseif codepoint == 0xFF7C then -- し
result = result.. utf8.char(0x30B5) -- ジ
print("Debug: Converted し to ジ")
elseif codepoint == 0xFF7D then -- す
result = result.. utf8.char(0x30B6) -- ズ
print("Debug: Converted す to ズ")
elseif codepoint == 0xFF81 then -- ち
result = result.. utf8.char(0x30C1) -- チ
print("Debug: Converted ち to チ")
elseif codepoint == 0xFF82 then -- つ
result = result.. utf8.char(0x30C4) -- ヅ
print("Debug: Converted つ to ヅ")
elseif codepoint == 0xFF8B then -- ふ
result = result.. utf8.char(0x30D5) -- フ
print("Debug: Converted ふ to フ")
elseif codepoint == 0xFF8D then -- へ
result = result.. utf8.char(0x30D8) -- ヘ
print("Debug: Converted へ to ヘ")
elseif codepoint == 0xFF8E then -- ほ
result = result.. utf8.char(0x30DB) -- ホ
print("Debug: Converted ほ to ホ")
else
result = result.. utf8.char(codepoint - 0xFF61 + 0x30A1)
print("Debug: Converted half-width Katakana to full-width Katakana")
end
dakuten = false
else
result = result.. utf8.char(codepoint - 0xFF61 + 0x30A1)
print("Debug: Converted half-width Katakana to full-width Katakana")
end
else
result = result.. char
print("Debug: Copied non-Katakana character")
end
end
print("Debug: Conversion complete")
return result
end
-- Load the subtitle file
local sub_file = mp.get_property("sub-file")
if sub_file and sub_file ~= "" then
if not io.open(sub_file, "r") then
print("Error: Unable to open subtitle file")
return
end
local subtitle_file = io.open(sub_file, "r")
local subtitle_text = subtitle_file:read("*a")
subtitle_file:close()
-- Check if the subtitle text is empty
if not subtitle_text or subtitle_text == "" then
print("Error: Subtitle file is empty")
return
end
-- Convert half-width Katakana to full-width Katakana
local modified_text, err = pcall(half_to_full_katakana, subtitle_text)
if not modified_text then
print("Error: Conversion failed: ".. err)
return
end
-- Set the modified