Skip to content

Commit

Permalink
add ruby library
Browse files Browse the repository at this point in the history
  • Loading branch information
paiv committed Nov 16, 2024
1 parent a5eb45d commit 5de52eb
Show file tree
Hide file tree
Showing 10 changed files with 834 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .github/workflows/build-ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Ruby

on:
push:
tags: ['*']
pull_request:
branches: [main]
paths:
- 'ruby/**/*'
- 'uklatn.gemspec'

jobs:

build-ruby:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Test
working-directory: ${{github.workspace}}/ruby
run: make test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ __pycache__/
*.egg-info/
*.so
.swiftpm/

*.gem
10 changes: 10 additions & 0 deletions ruby/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: build
.PHONY: test

all: test

build:
@gem build uklatn.gemspec

test:
@ruby test/uklatn/test_uklatn.rb
172 changes: 172 additions & 0 deletions ruby/lib/uklatn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Generated by gentables.py, do not edit.

# Ukrainian Cyrillic transliteration to and from Latin script.
#
# Tables:
# - 'DSTU_9112_A': DSTU 9112:2021 System A
# - 'DSTU_9112_B': DSTU 9112:2021 System B
# - 'KMU_55': KMU 55:2010, not reversible
class UkrainianLatin

# Transliterates a string of Ukrainian Cyrillic to Latin script.
def encode(text, table = 'DSTU_9112_A')
tr = TABLES[table][0]
return tr.transform(text) if tr
raise ArgumentError.new("invalid table #{table}")
end

# Re-transliterates a string of Ukrainian Latin to Cyrillic script.
def decode(text, table = 'DSTU_9112_A')
tr = TABLES[table][1]
return tr.transform(text) if tr
raise ArgumentError.new("invalid table #{table}")
end

class Uklatn_uk_uk_Latn_DSTU_9112_A # :nodoc:
def initialize()
@rx1 = /\b([Ьь])|([Ьь](?=[АаЕеУу])|[ЄЮЯ](?=\u0301?[а-щьюяєіїґ’])|(?<=[Б-ДЖЗК-НП-ТФ-Щб-джзк-нп-тф-щҐґ])[Йй])|([ЁЄІЇЎА-яёєіїўҐґ’])/
@maps1 = [
{"Ь"=>"Ĵ","ь"=>"ĵ"},
{"Ь"=>"J'","ь"=>"j'","Є"=>"Je","Ю"=>"Ju","Я"=>"Ja","Й"=>"'J","й"=>"'j"},
{"А"=>"A","а"=>"a","Б"=>"B","б"=>"b","В"=>"V","в"=>"v","Г"=>"Ğ","г"=>"ğ","Ґ"=>"G","ґ"=>"g","Д"=>"D","д"=>"d","Е"=>"E","е"=>"e","Є"=>"JE","є"=>"je","Ж"=>"Ž","ж"=>"ž","З"=>"Z","з"=>"z","И"=>"Y","и"=>"y","І"=>"I","і"=>"i","Ї"=>"Ï","ї"=>"ï","К"=>"K","к"=>"k","Л"=>"L","л"=>"l","М"=>"M","м"=>"m","Н"=>"N","н"=>"n","О"=>"O","о"=>"o","П"=>"P","п"=>"p","Р"=>"R","р"=>"r","С"=>"S","с"=>"s","Т"=>"T","т"=>"t","У"=>"U","у"=>"u","Ф"=>"F","ф"=>"f","Х"=>"X","х"=>"x","Ц"=>"C","ц"=>"c","Ч"=>"Č","ч"=>"č","Ш"=>"Š","ш"=>"š","Щ"=>"Ŝ","щ"=>"ŝ","Ю"=>"JU","ю"=>"ju","Я"=>"JA","я"=>"ja","Ь"=>"J","ь"=>"j","Й"=>"J","й"=>"j","’"=>"'","Ё"=>"Ö","ё"=>"ö","Ў"=>"Ŭ","ў"=>"ŭ","Ъ"=>"Ǒ","ъ"=>"ǒ","Ы"=>"Ȳ","ы"=>"ȳ","Э"=>"Ē","э"=>"ē"},
]
end

def transform(text)
text = text.unicode_normalize(:nfc)
text = text.gsub(@rx1) do |m|
next @maps1[2].fetch($3, $3) unless $3.nil?
next @maps1[1].fetch($2, $2) unless $2.nil?
next @maps1[0].fetch($1, $1) unless $1.nil?
m
end
text = text.unicode_normalize(:nfc)
end
end

class Uklatn_uk_uk_Latn_DSTU_9112_B # :nodoc:
def initialize()
@rx1 = /([Ьь](?=[АаЕеІіУу])|(?<=[Б-ДЖЗК-НП-ТФ-Щб-джзк-нп-тф-щҐґ])[Йй])|([ГЄЖЇХЩШЧЮЯЁЎЪЫЭ](?=\u0301?[а-яёєіїўґ’])|\b[Ьь])|([ЁЄІЇЎА-яёєіїўҐґ’])/
@maps1 = [
{"Ь"=>"J'","ь"=>"j'","Й"=>"'J","й"=>"'j"},
{"Г"=>"Gh","Є"=>"Je","Ж"=>"Zh","Ї"=>"Ji","Х"=>"Kh","Щ"=>"Shch","Ш"=>"Sh","Ч"=>"Ch","Ю"=>"Ju","Я"=>"Ja","Ё"=>"Jow","Ў"=>"Uh","Ъ"=>"Oh","Ы"=>"Yw","Э"=>"Ehw","Ь"=>"Hj","ь"=>"hj"},
{"А"=>"A","а"=>"a","Б"=>"B","б"=>"b","В"=>"V","в"=>"v","Г"=>"GH","г"=>"gh","Ґ"=>"G","ґ"=>"g","Д"=>"D","д"=>"d","Е"=>"E","е"=>"e","Є"=>"JE","є"=>"je","Ж"=>"ZH","ж"=>"zh","З"=>"Z","з"=>"z","И"=>"Y","и"=>"y","І"=>"I","і"=>"i","Ї"=>"JI","ї"=>"ji","Х"=>"KH","х"=>"kh","К"=>"K","к"=>"k","Л"=>"L","л"=>"l","М"=>"M","м"=>"m","Н"=>"N","н"=>"n","О"=>"O","о"=>"o","П"=>"P","п"=>"p","Р"=>"R","р"=>"r","Щ"=>"SHCH","щ"=>"shch","Ш"=>"SH","ш"=>"sh","С"=>"S","с"=>"s","Т"=>"T","т"=>"t","У"=>"U","у"=>"u","Ф"=>"F","ф"=>"f","Ч"=>"CH","ч"=>"ch","Ц"=>"C","ц"=>"c","Ю"=>"JU","ю"=>"ju","Я"=>"JA","я"=>"ja","Й"=>"J","й"=>"j","Ь"=>"J","ь"=>"j","’"=>"'","Ё"=>"JOW","ё"=>"jow","Ў"=>"UH","ў"=>"uh","Ъ"=>"OH","ъ"=>"oh","Ы"=>"YW","ы"=>"yw","Э"=>"EHW","э"=>"ehw"},
]
end

def transform(text)
text = text.unicode_normalize(:nfc)
text = text.gsub(@rx1) do |m|
next @maps1[2].fetch($3, $3) unless $3.nil?
next @maps1[1].fetch($2, $2) unless $2.nil?
next @maps1[0].fetch($1, $1) unless $1.nil?
m
end
text = text.unicode_normalize(:nfc)
end
end

class Uklatn_uk_uk_Latn_KMU_55 # :nodoc:
def initialize()
@rx1 = /(?<=[ЁЄІЇЎА-яёєіїўҐґ])([’\u0027])(?=[ЁЄІЇЎА-яёєіїўҐґ])/
@maps1 = [
{"’"=>"","'"=>""},
]
@rx2 = /\b([ЄЇЮЯ])(?=\u0301?[а-яёєіїўґ’])|\b([ЙйЄЇЮЯєїюя])|([Зз]Г|[ЖХЦЩШЧЄЇЮЯ])(?=\u0301?[а-яёєіїўґ’])|([Зз][Гг]|[ЄІЇА-ЩЬЮ-щьюяєіїҐґ’])/
@maps2 = [
{"Є"=>"Ye","Ї"=>"Yi","Ю"=>"Yu","Я"=>"Ya"},
{"Й"=>"Y","й"=>"y","Є"=>"YE","є"=>"ye","Ї"=>"YI","ї"=>"yi","Ю"=>"YU","ю"=>"yu","Я"=>"YA","я"=>"ya"},
{"ЗГ"=>"ZGh","зГ"=>"zGh","Ж"=>"Zh","Х"=>"Kh","Ц"=>"Ts","Щ"=>"Shch","Ш"=>"Sh","Ч"=>"Ch","Є"=>"Ie","Ї"=>"I","Ю"=>"Iu","Я"=>"Ia"},
{"ЗГ"=>"ZGH","Зг"=>"Zgh","зГ"=>"zGH","зг"=>"zgh","А"=>"A","а"=>"a","Б"=>"B","б"=>"b","В"=>"V","в"=>"v","Г"=>"H","г"=>"h","Ґ"=>"G","ґ"=>"g","Д"=>"D","д"=>"d","Е"=>"E","е"=>"e","Є"=>"IE","є"=>"ie","Ж"=>"ZH","ж"=>"zh","З"=>"Z","з"=>"z","И"=>"Y","и"=>"y","І"=>"I","і"=>"i","Ї"=>"I","ї"=>"i","Х"=>"KH","х"=>"kh","К"=>"K","к"=>"k","Л"=>"L","л"=>"l","М"=>"M","м"=>"m","Н"=>"N","н"=>"n","О"=>"O","о"=>"o","П"=>"P","п"=>"p","Р"=>"R","р"=>"r","Щ"=>"SHCH","щ"=>"shch","Ш"=>"SH","ш"=>"sh","С"=>"S","с"=>"s","Т"=>"T","т"=>"t","У"=>"U","у"=>"u","Ф"=>"F","ф"=>"f","Ч"=>"CH","ч"=>"ch","Ц"=>"TS","ц"=>"ts","Ю"=>"IU","ю"=>"iu","Я"=>"IA","я"=>"ia","Й"=>"I","й"=>"i","Ь"=>"","ь"=>"","’"=>""},
]
end

def transform(text)
text = text.unicode_normalize(:nfc)
text = text.gsub(@rx1) do |m|
next @maps1[0].fetch($1, $1) unless $1.nil?
m
end
text = text.gsub(@rx2) do |m|
next @maps2[3].fetch($4, $4) unless $4.nil?
next @maps2[2].fetch($3, $3) unless $3.nil?
next @maps2[1].fetch($2, $2) unless $2.nil?
next @maps2[0].fetch($1, $1) unless $1.nil?
m
end
text = text.unicode_normalize(:nfc)
end
end

class Uklatn_uk_Latn_DSTU_9112_A_uk # :nodoc:
def initialize()
@rx1 = /([ÁáÉéÍíÓóÚúÝýḮḯ])/
@maps1 = [
{"Á"=>"Á","á"=>"á","É"=>"É","é"=>"é","Í"=>"Í","í"=>"í","Ó"=>"Ó","ó"=>"ó","Ú"=>"Ú","ú"=>"ú","Ý"=>"Ý","ý"=>"ý","Ḯ"=>"Ḯ","ḯ"=>"ḯ"},
]
@rx2 = /(J[Ee]|j[Ee]|J[Uu]|j[Uu]|J[Aa]|j[Aa]|[A-GIK-PR-VXYZa-gik-pr-vxyzÏÖïöČčĒēĞğĴĵŜŝŠšŬŭŽžǑǒȲȳ])|(?<=[BbCcDdFfGgKkLlMmNnPpRrSsTtVvXxZzČčĞğŜŝŠšŽž])([Jj]\u0027(?=[AaEeUu])|[Jj])|(\u0027[Jj](?![AaEeIiUu])|\u0027(?=[Jj])|[Jj])/
@maps2 = [
{"A"=>"А","a"=>"а","B"=>"Б","b"=>"б","V"=>"В","v"=>"в","Ğ"=>"Г","ğ"=>"г","G"=>"Ґ","g"=>"ґ","D"=>"Д","d"=>"д","E"=>"Е","e"=>"е","JE"=>"Є","Je"=>"Є","jE"=>"є","je"=>"є","Ž"=>"Ж","ž"=>"ж","Z"=>"З","z"=>"з","Y"=>"И","y"=>"и","I"=>"І","i"=>"і","Ï"=>"Ї","ï"=>"ї","K"=>"К","k"=>"к","L"=>"Л","l"=>"л","M"=>"М","m"=>"м","N"=>"Н","n"=>"н","O"=>"О","o"=>"о","P"=>"П","p"=>"п","R"=>"Р","r"=>"р","S"=>"С","s"=>"с","T"=>"Т","t"=>"т","U"=>"У","u"=>"у","F"=>"Ф","f"=>"ф","X"=>"Х","x"=>"х","C"=>"Ц","c"=>"ц","Č"=>"Ч","č"=>"ч","Š"=>"Ш","š"=>"ш","Ŝ"=>"Щ","ŝ"=>"щ","JU"=>"Ю","Ju"=>"Ю","jU"=>"ю","ju"=>"ю","JA"=>"Я","Ja"=>"Я","jA"=>"я","ja"=>"я","Ĵ"=>"Ь","ĵ"=>"ь","Ö"=>"Ё","ö"=>"ё","Ŭ"=>"Ў","ŭ"=>"ў","Ǒ"=>"Ъ","ǒ"=>"ъ","Ȳ"=>"Ы","ȳ"=>"ы","Ē"=>"Э","ē"=>"э"},
{"J"=>"Ь","j"=>"ь","J'"=>"Ь","j'"=>"ь"},
{"'J"=>"Й","'j"=>"й","'"=>"’","J"=>"Й","j"=>"й"},
]
end

def transform(text)
text = text.unicode_normalize(:nfc)
text = text.gsub(@rx1) do |m|
next @maps1[0].fetch($1, $1) unless $1.nil?
m
end
text = text.gsub(@rx2) do |m|
next @maps2[2].fetch($3, $3) unless $3.nil?
next @maps2[1].fetch($2, $2) unless $2.nil?
next @maps2[0].fetch($1, $1) unless $1.nil?
m
end
text = text.unicode_normalize(:nfc)
end
end

class Uklatn_uk_Latn_DSTU_9112_B_uk # :nodoc:
def initialize()
@rx1 = /([ÁáÉéÍíÓóÚúÝý])/
@maps1 = [
{"Á"=>"Á","á"=>"á","É"=>"É","é"=>"é","Í"=>"Í","í"=>"í","Ó"=>"Ó","ó"=>"ó","Ú"=>"Ú","ú"=>"ú","Ý"=>"Ý","ý"=>"ý"},
]
@rx2 = /([Jj][Oo][Ww]|[Ss][Hh][Cc][Hh]|[CcGgKkSsZzUuOo][Hh]|[Yy][Ww]|[Ee][Hh][Ww]|[Jj][EeIiUuAa]|[Hh][Jj]|[A-GIK-PR-VYZa-gik-pr-vyz])|(?<=[Ss][Hh][Cc][Hh])([Jj]\u0027(?=[AaEeIiUu])|[Jj])|(?<=[CcGgKkSsZz][Hh])([Jj]\u0027(?=[AaEeIiUu])|[Jj])|(?<=[BCDFGKLMNPRSTVZbcdfgklmnprstvzv])([Jj]\u0027(?=[AaEeIiUu])|[Jj])|(\u0027[Jj](?![AaEeIiUu])|\u0027(?=[Jj])|[Jj])/
@maps2 = [
{"A"=>"А","a"=>"а","B"=>"Б","b"=>"б","V"=>"В","v"=>"в","GH"=>"Г","Gh"=>"Г","gH"=>"г","gh"=>"г","G"=>"Ґ","g"=>"ґ","D"=>"Д","d"=>"д","E"=>"Е","e"=>"е","JE"=>"Є","Je"=>"Є","jE"=>"є","je"=>"є","ZH"=>"Ж","Zh"=>"Ж","zH"=>"ж","zh"=>"ж","Z"=>"З","z"=>"з","Y"=>"И","y"=>"и","I"=>"І","i"=>"і","JI"=>"Ї","Ji"=>"Ї","jI"=>"ї","ji"=>"ї","KH"=>"Х","Kh"=>"Х","kH"=>"х","kh"=>"х","K"=>"К","k"=>"к","L"=>"Л","l"=>"л","M"=>"М","m"=>"м","N"=>"Н","n"=>"н","O"=>"О","o"=>"о","P"=>"П","p"=>"п","R"=>"Р","r"=>"р","SHCH"=>"Щ","SHCh"=>"Щ","SHcH"=>"Щ","SHch"=>"Щ","ShCH"=>"Щ","ShCh"=>"Щ","ShcH"=>"Щ","Shch"=>"Щ","sHCH"=>"щ","sHCh"=>"щ","sHcH"=>"щ","sHch"=>"щ","shCH"=>"щ","shCh"=>"щ","shcH"=>"щ","shch"=>"щ","SH"=>"Ш","Sh"=>"Ш","sH"=>"ш","sh"=>"ш","S"=>"С","s"=>"с","T"=>"Т","t"=>"т","U"=>"У","u"=>"у","F"=>"Ф","f"=>"ф","CH"=>"Ч","Ch"=>"Ч","cH"=>"ч","ch"=>"ч","C"=>"Ц","c"=>"ц","JU"=>"Ю","Ju"=>"Ю","jU"=>"ю","ju"=>"ю","JA"=>"Я","Ja"=>"Я","jA"=>"я","ja"=>"я","HJ"=>"Ь","Hj"=>"Ь","hJ"=>"ь","hj"=>"ь","JOW"=>"Ё","JOw"=>"Ё","JoW"=>"Ё","Jow"=>"Ё","jOW"=>"ё","jOw"=>"ё","joW"=>"ё","jow"=>"ё","UH"=>"Ў","Uh"=>"Ў","uH"=>"ў","uh"=>"ў","OH"=>"Ъ","Oh"=>"Ъ","oH"=>"ъ","oh"=>"ъ","YW"=>"Ы","Yw"=>"Ы","yW"=>"ы","yw"=>"ы","EHW"=>"Э","EHw"=>"Э","EhW"=>"Э","Ehw"=>"Э","eHW"=>"э","eHw"=>"э","ehW"=>"э","ehw"=>"э"},
{"J"=>"Ь","j"=>"ь","J'"=>"Ь","j'"=>"ь"},
{"J"=>"Ь","j"=>"ь","J'"=>"Ь","j'"=>"ь"},
{"J"=>"Ь","j"=>"ь","J'"=>"Ь","j'"=>"ь"},
{"'J"=>"Й","'j"=>"й","'"=>"’","J"=>"Й","j"=>"й"},
]
end

def transform(text)
text = text.unicode_normalize(:nfc)
text = text.gsub(@rx1) do |m|
next @maps1[0].fetch($1, $1) unless $1.nil?
m
end
text = text.gsub(@rx2) do |m|
next @maps2[4].fetch($5, $5) unless $5.nil?
next @maps2[3].fetch($4, $4) unless $4.nil?
next @maps2[2].fetch($3, $3) unless $3.nil?
next @maps2[1].fetch($2, $2) unless $2.nil?
next @maps2[0].fetch($1, $1) unless $1.nil?
m
end
text = text.unicode_normalize(:nfc)
end
end

TABLES = Hash.new([nil, nil]) # :nodoc:
TABLES['DSTU_9112_A'] = [Uklatn_uk_uk_Latn_DSTU_9112_A.new(), Uklatn_uk_Latn_DSTU_9112_A_uk.new()]
TABLES['DSTU-9112-A'] = TABLES['DSTU_9112_A']
TABLES['DSTU_9112_B'] = [Uklatn_uk_uk_Latn_DSTU_9112_B.new(), Uklatn_uk_Latn_DSTU_9112_B_uk.new()]
TABLES['DSTU-9112-B'] = TABLES['DSTU_9112_B']
TABLES['KMU_55'] = [Uklatn_uk_uk_Latn_KMU_55.new(), nil]
TABLES['KMU-55'] = TABLES['KMU_55']
end
Loading

0 comments on commit 5de52eb

Please sign in to comment.