Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
ZwerOxotnik committed May 18, 2024
1 parent 8ea5a7e commit 80d1b1e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 45 deletions.
7 changes: 2 additions & 5 deletions characters_to_sitelen_pona.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ return {
[""] = "",
["."] = ".",
[","] = ",",
[""] = "",
[""] = "",
[""] = ",",
[""] = ".",
["+"] = "󱤊",
["?"] = "󱥙",
["1"] = "󱥳",
["2"] = "󱥮",
["5"] = "󱤭",
}
3 changes: 3 additions & 0 deletions latin_to_sitelen_pona.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
-- Created for "sitelen seli kiwen" font (https://www.kreativekorp.com/software/fonts/sitelenselikiwen/)

return {
["1"] = "󱥳",
["2"] = "󱥮",
["5"] = "󱤭",
a = "󱤀",
akesi = "󱤁",
ala = "󱤂",
Expand Down
139 changes: 99 additions & 40 deletions sitelen_pona.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

---@class SitelenPonaModule : module
local M = {
_VERSION = "0.0.2",
_VERSION = "0.0.3",
_LICENSE = "MIT",
_SOURCE = "https://github.com/ZwerOxotnik/sitelen_pona_lua"
}
Expand Down Expand Up @@ -68,7 +68,7 @@ __special_char_expr = __special_char_expr .. "])"


---@class SitelenPonaPart : table
---@field sitelep_pona SitelenPona?
---@field sitelen_pona SitelenPona?
---@field original string?
---@field is_add_space true?
---@field is_new_line boolean?
Expand All @@ -89,18 +89,65 @@ function M.toki_pona_mute_to_sitelen_pona(_text, new_line_pattern)
local is_sitelen_pona = false
local result = {}


---@param word string
---@return string?
local function split_numbers(word)
local last_part = word
local last_result_i = 0
while true do
local first_i, last_i, number = last_part:find("(%d+)", last_result_i + 1)
if first_i == nil then
if last_result_i == 1 then
return word
else
return last_part:sub(last_result_i+1, #last_part)
end
end

if last_result_i < last_i and last_result_i > 0 then
local prev_part = last_part:sub(last_result_i+1, first_i-1)
local sitelen_pona_char = __syntax[prev_part]
if sitelen_pona_char then
result[#result+1] = {
sitelen_pona = sitelen_pona_char,
original = prev_part
}
else
result[#result+1] = {original = prev_part}
end
end

local sitelen_pona_char = __syntax[number]
if sitelen_pona_char then
result[#result+1] = {
sitelen_pona = sitelen_pona_char,
original = number
}
else
result[#result+1] = {original = number}
end

last_result_i = last_i

if last_i == #number then
return
end
end
end

---@param word string
---@return string
---@return string?
local function find_special_characters(word)
local last_part = word
local last_result_i = 1
while true do
local first_i, last_i, char = last_part:find(__special_char_expr, last_result_i)
if first_i == nil then
if last_result_i == 1 then
return word
return split_numbers(word)
else
return last_part:sub(last_result_i, #last_part)
return split_numbers(last_part:sub(last_result_i, #last_part))
end
end

Expand All @@ -109,11 +156,14 @@ function M.toki_pona_mute_to_sitelen_pona(_text, new_line_pattern)
local sitelen_pona_char = __syntax[prev_part]
if sitelen_pona_char then
result[#result+1] = {
sitelep_pona = sitelen_pona_char,
sitelen_pona = sitelen_pona_char,
original = prev_part
}
else
result[#result+1] = {original = prev_part}
local _word = split_numbers(prev_part)
if _word then
result[#result+1] = {original = _word}
end
end
end

Expand All @@ -122,14 +172,33 @@ function M.toki_pona_mute_to_sitelen_pona(_text, new_line_pattern)
local sitelen_pona_char = __characters_syntax[original_char]
if sitelen_pona_char then
result[#result+1] = {
sitelep_pona = sitelen_pona_char,
sitelen_pona = sitelen_pona_char,
original = original_char
}
else
result[#result+1] = {original = original_char}
end
if last_i == #char then
return ""
return nil
end
end
end

---@param text string
local function add_punctuations(text)
for char in text:gmatch(".") do
sitelen_pona_char = __characters_syntax[char]
if sitelen_pona_char then
result[#result+1] = {
sitelen_pona = sitelen_pona_char,
original = char,
is_add_space = (is_end and word == nil)
}
else
result[#result+1] = {
original = char,
is_add_space = (is_end and word == nil)
}
end
end
end
Expand All @@ -143,6 +212,9 @@ function M.toki_pona_mute_to_sitelen_pona(_text, new_line_pattern)
word = nil
else
word = find_special_characters(word)
if word == "" then
word = nil
end
end
if punc2 == "" then
punc2 = nil
Expand All @@ -151,43 +223,26 @@ function M.toki_pona_mute_to_sitelen_pona(_text, new_line_pattern)
local sitelen_pona_char
local is_end = #text == end_i
if punc then
sitelen_pona_char = __characters_syntax[punc]
if sitelen_pona_char then
result[#result+1] = {
sitelep_pona = sitelen_pona_char,
original = punc,
is_add_space = (is_end and word == nil)
}
else
result[#result+1] = {
original = punc,
is_add_space = (is_end and word == nil)
}
end
add_punctuations(punc)
end
if word then
local sitelen_pona = __syntax[word]
result[#result+1] = {
sitelep_pona = sitelen_pona,
original = word,
is_add_space = (is_end and punc2 == nil)
}
end
if punc2 then
sitelen_pona_char = __characters_syntax[punc2]
if sitelen_pona_char then
if sitelen_pona then
result[#result+1] = {
sitelep_pona = sitelen_pona_char,
original = punc2,
is_add_space = is_end
sitelen_pona = sitelen_pona,
original = word,
is_add_space = (is_end and punc2 == nil)
}
else
result[#result+1] = {
original = punc2,
is_add_space = is_end
original = word,
is_add_space = (is_end and punc2 == nil)
}
end
end
if punc2 then
add_punctuations(punc2)
end

if not is_end then
parse(text:sub(end_i+1, #text))
Expand All @@ -203,6 +258,10 @@ function M.toki_pona_mute_to_sitelen_pona(_text, new_line_pattern)
}
end

if #result > 0 and result[#result].is_new_line then
result[#result] = nil
end

return result, is_sitelen_pona
end

Expand All @@ -228,7 +287,7 @@ function M.compound_sitelen_pona(parts)

i = i + 1
local part = parts_copy[i]
if not part.sitelep_pona then
if not part.sitelen_pona then
compound_length = 0
compound_syntax = __compound_syntax
original_text = ""
Expand All @@ -242,12 +301,12 @@ function M.compound_sitelen_pona(parts)
goto skip
end

compound_syntax = compound_syntax[part.sitelep_pona] or __compound_syntax
compound_syntax = compound_syntax[part.sitelen_pona] or __compound_syntax
if compound_syntax == __compound_syntax then
original_text = ""
compound_length = 0

compound_syntax = compound_syntax[part.sitelep_pona]
compound_syntax = compound_syntax[part.sitelen_pona]
if compound_syntax == nil then
compound_syntax = __compound_syntax
goto skip
Expand All @@ -267,7 +326,7 @@ function M.compound_sitelen_pona(parts)
end
i = prev_i
parts_copy[i] = {
sitelep_pona = compound_syntax,
sitelen_pona = compound_syntax,
original = original_text,
is_add_space = part.is_add_space
}
Expand Down

0 comments on commit 80d1b1e

Please sign in to comment.