Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PLUGIN.Title = "EnglishToKorean"
- -- code from : https://milennium9.blog.me/20205300449
- PLUGIN.Description = "convert english chat to korean."
- _wsetlocale(LC_ALL, L"korean");
- function PLUGIN:Init()
- self:AddChatCommand( "ko", self.ToKorean )
- end
- function PLUGIN:ToKorean( netuser, cmd, args )
- local str = "";
- for i,v in pairs(args) do
- str = str .. self:englishToKorean(v) .. " "
- end
- rust.BroadcastChat( "nickname", str )
- end
- function PLUGIN:conv2utf8(unicode_list)
- local result = ''
- local w,x,y,z = 0,0,0,0
- local function modulo(a, b)
- return a - math.floor(a/b) * b
- end
- for i,v in ipairs(unicode_list) do
- if v ~= 0 and v ~= nil then
- if v <= 0x7F then -- same as ASCII
- result = result .. string.char(v)
- elseif v >= 0x80 and v <= 0x7FF then -- 2 bytes
- y = math.floor(modulo(v, 0x000800) / 64)
- z = modulo(v, 0x000040)
- result = result .. string.char(0xC0 + y, 0x80 + z)
- elseif (v >= 0x800 and v <= 0xD7FF) or (v >= 0xE000 and v <= 0xFFFF) then -- 3 bytes
- x = math.floor(modulo(v, 0x010000) / 4096)
- y = math.floor(modulo(v, 0x001000) / 64)
- z = modulo(v, 0x000040)
- result = result .. string.char(0xE0 + x, 0x80 + y, 0x80 + z)
- elseif (v >= 0x10000 and v <= 0x10FFFF) then -- 4 bytes
- w = math.floor(modulo(v, 0x200000) / 262144)
- x = math.floor(modulo(v, 0x040000) / 4096)
- y = math.floor(modulo(v, 0x001000) / 64)
- z = modulo(v, 0x000040)
- result = result .. string.char(0xF0 + w, 0x80 + x, 0x80 + y, 0x80 + z)
- end
- end
- end
- return result
- end
- function PLUGIN:englishToKorean(msg)
- if msg == nil then
- return "nil"
- end
- local text = msg;
- -- 한글의 초,중,종성의 인덱스
- local initialCode = 0;
- local medialCode = 0;
- local finalCode = 0;
- -- 루프와 결과 저장을 위한 변수
- local textLength = #text
- local ret = "";
- local cont = 0
- for idx = 1, textLength do
- -- lua는 루프 내부에서 idx 변수를 변화 시켜도 루프에 영향을 주지 않는다.
- -- 스킵한 문자를 위한 continue 처리를 위해 cont 사용
- if cont < idx then
- -- 초성 검색
- local first = text:sub( idx, idx )
- initialCode = self:getCode( 'initial', text:sub( idx, idx ) );
- idx = idx + 1; -- 다음 영타로 중성을 찾아야 한다.
- if initialCode ~= -1 then
- -- 두번째 영문으로 중성을 추론한다.
- -- ㅘ, ㅝ, ㅙ 등 2번의 입력으로 구성된 중성을 우선 검색한다.
- tempMedialCode = self:getCode( 'medial', text:sub( idx, idx + 1 ) );
- if tempMedialCode ~= -1 then
- -- 중성을 찾았다면 사용한 영타 2자를 스킵한다.
- medialCode = tempMedialCode;
- idx = idx + 2;
- else -- 찾지 못했다면 한번의 입력을 구성된 중성을 검색한다.
- medialCode = self:getCode( 'medial', text:sub( idx, idx ) );
- idx = idx + 1;
- end
- -- 마지막으로 종성을 검색한다.
- -- 않, 값 등 받침이 2번의 입력으로 구성된 종성을 우선 검색한다.
- tempFinalCode = self:getCode( 'final', text:sub( idx, idx + 1 ) );
- if tempFinalCode ~= -1 then
- finalCode = tempFinalCode;
- -- 발견했다면 이 문자가 종성인지 아니면 다음 한글의 초성인지를 판단해야 한다.
- -- 이어지는 영타가 중성에 포함되는지를 확인한다.
- tempMedialCode = self:getCode( 'medial', text:sub( idx + 2, idx + 2 ) );
- if tempMedialCode ~= -1 then
- -- 이어지는 문자가 중성이라면 2번의 입력 중 뒤에 것은 다음 글자의 초성이 된다.
- finalCode = self:getCode( 'final', text:sub( idx, idx ) );
- else
- idx = idx + 1;
- end
- else
- -- 또다시 중성이 이어진다면 종성은 존재하지 않는 것이다.
- tempMedialCode = self:getCode( 'medial', text:sub( idx + 1, idx + 1 ) );
- if tempMedialCode ~= -1 then
- -- 가ㅏ 처럼 중성이 두번 이어지면 종성은 없는 것으로 간주한다.
- finalCode = 0;
- idx = idx - 1;
- else
- -- 하나의 입력으로 이루어진 종성을 검색한다.
- finalCode = self:getCode( 'final', text:sub( idx, idx ) );
- if finalCode == -1 then finalCode = 0; end
- end
- end
- end
- -- 초성을 찾지 못했다면 특수 문자로 간주하고 그문자를 그대로 사용한다.
- if initialCode < 0 then
- ret = ret .. first
- else
- local code = 44032 + initialCode + medialCode + finalCode
- local ch = self:conv2utf8({code})
- ret = ret .. ch
- end
- cont = idx;
- end
- end
- return ret
- end
- function PLUGIN:getCode( tt, char )
- -- 초성 리스트
- local initial = { 'r','R','s','e','E','f','a','q','Q','t','T','d','w','W','c','z','x','v','g'};
- -- 중성 리스트
- local medial = { 'k', 'o', 'i', 'O', 'j', 'p', 'u', 'P', 'h', 'hk', 'ho', 'hl', 'y', 'n', 'nj', 'np', 'nl', 'b', 'm', 'ml', 'l' };
- -- 종성 리스트
- local final = { 'r', 'R', 'rt', 's', 'sw', 'sg', 'e', 'f', 'fr', 'fa', 'fq', 'ft', 'fx', 'fv', 'fg', 'a', 'q', 'qt', 't', 'T', 'd', 'w', 'c', 'z', 'x', 'v', 'g' };
- local returnCode = 0; -- 최종 인덱스
- local isFind = false; -- 발견 여부
- -- 각 리스트에서 인덱스를 검색하기 위한 로컬 함수
- local find = function(tb, ch)
- for i = 1, #tb do
- if tb[i] == ch then
- returnCode = i-1; -- Lua는 인덱스가 1부터 시작되기 때문에..
- isFind = true;
- break;
- end
- end
- end
- if char ~= nil then
- if tt == 'initial' then
- find( initial, char )
- returnCode = returnCode * 21 * 28
- elseif tt == 'medial' then
- find( medial, char )
- returnCode = returnCode * 28
- elseif tt == 'final' then
- find( final, char )
- returnCode = returnCode + 1
- end
- end
- if isFind == false then returnCode = -1; end
- return returnCode;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement