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

186 lines
7.4 KiB
Lua
Raw Permalink Normal View History

2024-09-08 20:06:38 +00:00
-- 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 text:gmatch(".") do
local byte = string.byte(char)
if byte == 0xFF9E then -- Dakuten marker
dakuten = true
print("Debug: Found dakuten marker")
elseif byte >= 0xFF61 and byte <= 0xFF9F then -- Half-width Katakana
if dakuten then
-- Convert half-width Katakana with dakuten to full-width
if byte == 0xFF8A then -- ハ
result = result .. "\xEF\xBD\xA0" -- バ
print("Debug: Converted ハ to バ")
elseif byte == 0xFF77 then -- き
result = result .. "\xEF\xBD\xAE" -- ギ
print("Debug: Converted き to ギ")
elseif byte == 0xFF78 then -- く
result = result .. "\xEF\xBD\xB0" -- グ
print("Debug: Converted く to グ")
elseif byte == 0xFF7C then -- し
result = result .. "\xEF\xBD\xB8" -- ジ
print("Debug: Converted し to ジ")
elseif byte == 0xFF7D then -- す
result = result .. "\xEF\xBD\xBA" -- ズ
print("Debug: Converted す to ズ")
elseif byte == 0xFF81 then -- ち
result = result .. "\xEF\xBD\xC6" -- チ
print("Debug: Converted ち to チ")
elseif byte == 0xFF82 then -- つ
result = result .. "\xEF\xBD\xC5" -- ヅ
print("Debug: Converted つ to ヅ")
elseif byte == 0xFF8B then -- ふ
result = result .. "\xEF\xBD\xD5" -- フ
print("Debug: Converted ふ to フ")
elseif byte == 0xFF8D then -- へ
result = result .. "\xEF\xBD\xD8" -- ヘ
print("Debug: Converted へ to ヘ")
elseif byte == 0xFF8E then -- ほ
result = result .. "\xEF\xBD\xDB" -- ホ
print("Debug: Converted ほ to ホ")
else
result = result .. string.char(byte - 0xFF61 + 0x30A1)
print("Debug: Converted half-width Katakana to full-width Katakana")
end
dakuten = false
else
result = result .. string.char(byte - 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.exists(sub_file) then
print("Error: Subtitle file does not exist")
return
end
local subtitle_file = io.open(sub_file, "r")
if not subtitle_file then
print("Error: Unable to open subtitle file")
return
end
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 subtitle text
mp.set_property("sub-text", modified_text)
print("Debug: Set modified subtitle text")
else
print("Error: Subtitle file not set")
return
end-- 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 text:gmatch(".") do
local byte = string.byte(char)
if byte == 0xFF9E then -- Dakuten marker
dakuten = true
print("Debug: Found dakuten marker")
elseif byte >= 0xFF61 and byte <= 0xFF9F then -- Half-width Katakana
if dakuten then
-- Convert half-width Katakana with dakuten to full-width
if byte == 0xFF8A then -- ハ
result = result .. "\xEF\xBD\xA0" -- バ
print("Debug: Converted ハ to バ")
elseif byte == 0xFF77 then -- き
result = result .. "\xEF\xBD\xAE" -- ギ
print("Debug: Converted き to ギ")
elseif byte == 0xFF78 then -- く
result = result .. "\xEF\xBD\xB0" -- グ
print("Debug: Converted く to グ")
elseif byte == 0xFF7C then -- し
result = result .. "\xEF\xBD\xB8" -- ジ
print("Debug: Converted し to ジ")
elseif byte == 0xFF7D then -- す
result = result .. "\xEF\xBD\xBA" -- ズ
print("Debug: Converted す to ズ")
elseif byte == 0xFF81 then -- ち
result = result .. "\xEF\xBD\xC6" -- チ
print("Debug: Converted ち to チ")
elseif byte == 0xFF82 then -- つ
result = result .. "\xEF\xBD\xC5" -- ヅ
print("Debug: Converted つ to ヅ")
elseif byte == 0xFF8B then -- ふ
result = result .. "\xEF\xBD\xD5" -- フ
print("Debug: Converted ふ to フ")
elseif byte == 0xFF8D then -- へ
result = result .. "\xEF\xBD\xD8" -- ヘ
print("Debug: Converted へ to ヘ")
elseif byte == 0xFF8E then -- ほ
result = result .. "\xEF\xBD\xDB" -- ホ
print("Debug: Converted ほ to ホ")
else
result = result .. string.char(byte - 0xFF61 + 0x30A1)
print("Debug: Converted half-width Katakana to full-width Katakana")
end
dakuten = false
else
result = result .. string.char(byte - 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 subtitle_file = io.open(mp.get_property("sub-file"), "r")
if not subtitle_file then
print("Error: Unable to open subtitle file")
return
end
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 subtitle text
mp.set_property("sub-text", modified_text)
print("Debug: Set modified subtitle text")