heonian-language-exercises/strings.lua
2022-05-19 19:19:00 +02:00

75 lines
2.3 KiB
Lua

function string.split(str, find, tbl)
split = tbl or {}
local p = 0
local continue = true
while continue do
word = string.sub(str,p,string.find(str,find,p+1,-1))
table.insert(split,word)
if string.find(str,find,p) then
str = string.sub(str,p)
p = string.find(str,find,p)
continue = true
p = p + 1
else
continue = false
end
end
return split
end
function strip_symbols(str)
local symbol_table = "'~()!?:></.,\t"
for i=1, #symbol_table do
while string.find(str, "%"..string.sub(symbol_table,i,i)) do
str = string.gsub(str, "%"..string.sub(symbol_table,i,i),"")
end
end
return str
end
function find_n_replace(str, find,repl, caution)
if string.find(str, find) then
if caution then
if not string.find(str, caution, string.find(str, find)+string.len(find)-string.len(caution)) then -- sometimes you want to avoid certain false positives
str = string.gsub(str,find, repl)
end
else
str = string.gsub(str,find, repl)
end
end
return str
end
function minimum_apostrophes(str)
local restr = ""
for i = 1, #str do
if string.sub(str,i,i) == "'" then -- only makes sense to separate vowels from vowel, or specified final separation-
if string.find(string.sub(str,i-1,i-1),"[aeiou]") then
if string.find(string.sub(str,i+1,i+1),"[aeiou]") then
restr = restr .. string.sub(str,i,i)
else
if string.find(string.sub(str,i+2,i+2),"[aeiou]") then -- for patterns like V'CV where it isnt clear wether VC'V or VC'V only works for single consontants so...
restr = restr .. string.sub(str,i,i)
elseif string.find(string.sub(str,i+3,i+3),"[aeiou]") -- we check for those specifically
and (string.sub(str,i+1,i+2) == "sh"
or string.sub(str,i+1,i+2) == "ch"
or string.sub(str,i+1,i+2) == "ny")
then
restr = restr .. string.sub(str,i,i)
end
end
else -- only consontant meaningful of checking is n'y opposed to ny
if string.find(string.sub(str,i-1,i-1),"[n]")
and string.find(string.sub(str,i+1,i+1),"[y]") then
restr = restr .. string.sub(str,i,i)
elseif string.find(string.sub(str,i+1,i+1),"[y]") then -- or consonant from vowels
restr = restr .. string.sub(str,i,i)
end
end
else
restr = restr .. string.sub(str,i,i)
end
end
return restr
end