Documentation for this module may be created at Module:grk-stems/trials/τεκμηρίωση

--[=[
STEM PRODUCTION for Greek monotonic or polytonic scripts (with character conversions at [[Module:grk-stems/trials/data]])
TRIALS for [[Module:grk-stems]]
STRUCTURE: 
	a) ALL that is in [[Module:grk-stems]]
	b) +plus trials here
PROBLEMS
main function:
	For function accent_penultima, I get 'time allocated expired'
]=]--

local export = {}

local module_path = 'Module:grk-stems/trials'
local m_data = require(module_path .."/data") --all character conversions
 
--------------------------------------------------------------------------
--                             MAIN CODES                               --
--------------------------------------------------------------------------


-- ===================== true or false: does it have an accent?
-- previously Lf['έχει τόνο'] in  Module:κλίση/el/ουσιαστικό 
function has_accent(anygreekstring)
    --για κάθε χαρακτήρα
    for codepoint in mw.ustring.gcodepoint( anygreekstring ) do
        if m_data.accented_to_unaccented[mw.ustring.char(codepoint)] ~= nil then
            return true
        end
    end
    return false
end

-- export it
function export.hasaccent(frame)
return has_accent(term)
end

-- ===================== removeaccent or accent_0 - previously in Module:grc-nouns-decl
-- Η συνάρτηση αφαιρεί τον τόνο από οποιαδήποτε λέξη
-- DO NOT add export.
function removeaccent(word)
    -- for every character / για κάθε χαρακτήρα
    local wordproduced = ''
    for codepoint in mw.ustring.gcodepoint( word ) do
        -- replace it according to instructions / αντικατάστησέ τον με βάση τον πίνακα
        mychar = mw.ustring.char(codepoint)
        convertedchar = m_data.accented_to_unaccented[mychar]            
        -- if an accented character is found / αν βρήκε τονισμένο γράμμα προς για αντικατάσταση
        if convertedchar ~= nil then
            -- add it / πρόσθεσέ το στη δημιουργούμενη κλείδα
            wordproduced = wordproduced .. convertedchar
        else
            -- let it be / αλλιώς άσε το ίδιο
            wordproduced = wordproduced .. mychar        
        end
    end
    return wordproduced
end

-- ===================== accent_ultima, place accent on the last vowel
--[=[
-- previously names neostonos in [[Module:grc-nouns-decl]] by Flyax
The function begins at the last letter of stem going backwards.
	When it reaches the first unaccented vowel, it adds accent
	The stem paramters should not have any accented vowels.
Η συνάρτηση αρχίζει από το τελευταίο γράμμα του θέματος και πηγαίνει προς το πρώτο. 
	Όταν συναντήσει το πρώτο (άτονο) φωνήεν, το τονίζει και τέλος
	Εννοείται ότι η μεταβλητή stem δεν πρέπει να περιέχει τονισμένα φωνήεντα.
]=]--
function accent_1(word)
    stempoint = mw.ustring.len(word)
    wordproduced = ""
    while stempoint > 0 do
       mychar = mw.ustring.sub(word, stempoint, stempoint)
       newchar = m_data.unaccented_to_accented[mychar]
       if newchar ~= nil then
          if stempoint > 1 then
              return mw.ustring.sub(word, 1, stempoint-1 ) .. newchar .. wordproduced
          else
              return newchar .. wordproduced
          end
       else
           wordproduced = mychar .. wordproduced
           stempoint = stempoint -1
       end
    end
    return wordproduced
end

-- ===================== accent_2  to penulitma, no synizesis
-- previously as tonospro in [[Module:grc-nouns-decl]] by Flyax
--[=[
It find the vowel of the penultima and adds oxia.
	It supposes that the second vowel from the end, of the stem
	belongs to the penultima.
Η συνάρτηση βρίσκει το φωνήεν της προπαραλήγουσας και του βάζει οξεία.
	Σε αυτή τη φάση υποθέτει ότι το δεύτερο φωνήεν του θέματος από το τέλος
	ανήκει στην προπαραλήγουσα
]=]--
function accent_2(stem)
    stempoint = mw.ustring.len(stem)
    wordproduced = ""
    profound = false
    while stempoint > 0 do
       mychar = mw.ustring.sub(stem, stempoint, stempoint)
       newchar = m_data.unaccented_to_accented[mychar]
       if newchar ~= nil and profound then
            if stempoint > 1 then
                return mw.ustring.sub(stem, 1, stempoint-1 ) .. newchar .. wordproduced
            else
                return newchar .. wordproduced
            end
       else
       	   if newchar ~= nil then profound = true end
           wordproduced = mychar .. wordproduced
           stempoint = stempoint -1
       end
    end
    return wordproduced
end

-- !!!!!!!!NOT CHECKED
-- ===================== accent to penulitma + synizesis
-- previously as neostonos in [[Module:el-verb-conj]] by Flyax, 2013
-- move accent to penultima or accent_2 (check synizesis)
-- μετάθεση του τόνου στην παραλήγουσα ελέγχοντας αν υπάρχει συνίζηση
function accent_2syn(word, syniz)
    local wordproduced = ""
    -- for every combination with accent+dialytics / για κάθε συνδυασμό γραμμάτων με τόνο και διαλυτικά
    -- for doubleaccented, newvalue in pairs(m_data.digraphs2) do
    for diplotonismeno, neatimh in pairs(m_data.digraphs2) do
    	-- beginning, ending = mw.ustring.find(word, doubleaccented)
        arxi, telos = mw.ustring.find(word, diplotonismeno)
        if arxi ~= nil then
            return mw.ustring.gsub(word, diplotonismeno, neatimh, 1) --replace once / μια φορά αντικατάσταση
            --accent was found and moved / βρέθηκε ο τόνος και μετατέθηκε
        end        
    end
    -- if dialytics are not found, we start from the end looking for an accent
    -- αν δεν βρέθηκαν διαλυτικά αρχίζουμε από το τέλος για να βρούμε πού είναι ο τόνος
    stempoint = mw.ustring.len(word)
    accentNotFound = true
    while accentNotFound do
       mychar = mw.ustring.sub(word, stempoint, stempoint)
       newchar = m_data.accented_to_unaccented[mychar]
       if newchar ~= nil then
           wordproduced = newchar .. wordproduced
           accentNotFound = false
       else
           wordproduced = mychar .. wordproduced
       end
       stempoint = stempoint -1
    end
    -- check if there is synizesis: defined by the editor
    -- να εξετάσουμε αν υπάρχει συνίζηση: πρέπει να μας το πει ο χρήστης, 
    -- EXAMPLES: αιφνιδιάζω (συνίζ=nil), μεριάζω (συνίζ=1), αδειάζω (συνίζ=2) (number of vowels counting as one)
    if syniz ~= nil then
        syniz = tonumber(syniz)
        wordproduced = mw.ustring.sub(word, stempoint+1-syniz, stempoint) .. wordproduced
        stempoint = stempoint - syniz
    end
    -- check if accent is on a digraph / να εξετάσουμε αν ο τόνος ήταν σε δίψηφο φωνήεν
    twoletters = mw.ustring.sub(word, stempoint, stempoint) .. newchar
    for _,v in pairs(m_data.digraphs) do
        if v == twoletters then
            wordproduced = mw.ustring.sub(word, stempoint, stempoint) .. wordproduced
            stempoint = stempoint -1
        end
    end
    return accent_1(mw.ustring.sub(word, 1, stempoint)) .. wordproduced
end

-- !!!!!!!!NOT CHECKED
-- ===================== Convert perispomene (circumflex) to oxia (= acute = tonos)
-- previously [[Module:grc-nouns-decl]] by Flyax
-- Η συνάρτηση αλλάζει την περισπωμένη του θέματος σε οξεία. Αν δεν βρει περισπωμένη δεν κάνει τίποτα.
function export.PerispomeniToOxia(stem)
    stempoint = mw.ustring.len(stem)
    wordproduced = ""
    while stempoint > 0 do
       mychar = mw.ustring.sub(stem, stempoint, stempoint)
       newchar = m_data.perispomeni_to_oxeia[mychar]
       if newchar ~= nil then
          if stempoint > 1 then
              return mw.ustring.sub(stem, 1, stempoint-1 ) .. newchar .. wordproduced
          else
              return newchar .. wordproduced
          end
       else
           wordproduced = mychar .. wordproduced
           stempoint = stempoint -1
       end
    end
    return wordproduced
end

-- !!!!!!!!NOT CHECKED
-- ===================== Convert oxia (acute) to persipomeni (circumflex)
-- previously in [[Module:grc-nouns-decl]] by Flyax
-- Η συνάρτηση αλλάζει την οξεία του θέματος σε περισπωμένη. Αν δεν βρει οξεία δεν κάνει τίποτα.
 
function export.OxiaToPerispomeni(stem)
    stempoint = mw.ustring.len(stem)
    wordproduced = ""
    while stempoint > 0 do
       mychar = mw.ustring.sub(stem, stempoint, stempoint)
       newchar = m_data.oxeia_to_perispomeni[mychar]
       if newchar ~= nil then
          if stempoint > 1 then
              return mw.ustring.sub(stem, 1, stempoint-1 ) .. newchar .. wordproduced
          else
              return newchar .. wordproduced
          end
       else
           wordproduced = mychar .. wordproduced
           stempoint = stempoint -1
       end
    end
    return wordproduced
end






--------------------------------------------------------------------------
--                      b)     NOTES - TRIALS                             --
--------------------------------------------------------------------------

--------------------------------------------------------------------------
--                      a general function (?)                         --
--------------------------------------------------------------------------
-- how does this produce multiple stems used for inflections?
export.main = function(frame)
	myarg = frame:getParent().args
	wordproduced = ''
    PAGENAME = mw.title.getCurrentTitle().text
    lemma = PAGENAME:match( "^%s*(.-)%s*$" )
	lemma_target = myarg['lemma'] or myarg['λήμμα'] or ''
	if lemma_target ~= '' then lemma = lemma_target end
	stemL = myarg['stemL'] or ''
return link(args)	
end

link = function(args)
	myarg = args
    if lemma == '' then lemma = PAGENAME:match( "^%s*(.-)%s*$" ) end
    if lemma_target ~= '' then lemma = lemma_target end

	stemL = ''
	stemL = mw.ustring.sub(lemma,1,-3)
return accent_1(removeaccent(mw.ustring.sub(lemma,1,-2))) -- for ακρόαμα --> ακροάμ for FocalPoint
-- TRIALS OK:
-- return lemma .. ', ' .. stemL ..', ' .. removeaccent(lemma) ..', ' .. removeaccent(stemL)
-- return accent_1(removeaccent(lemma)) .. ' - ' .. accent_1(removeaccent(stemL))
-- FAILED
-- return accent_penultima(removeaccent(stemL))
	--The time allocated for running scripts has expired.
-- return removeaccent(lemma) .. '-' .. removeaccent(stemL) .. ' - ' .. accent_1(removeaccent(stemL)) .. ' - ' .. accent_penultima(lemma)
	-- The time allocated for running scripts has expired.
		-- for most of the tests: i reduce number.

end



-- ============= TEST Trial =============== Keep instructions  --
-- ?? Do i user (frame) or e.g. (word) or (something)?
function export.testT(frame)
--TRIALS for (frame)
	parent=frame:getParent() -- same as parent=frame.getParent(frame) --https://en.wikipedia.org/wiki/Help:Lua_for_beginners
--	myarg = frame:getParent().args --  for Template only
--	myarg = frame.args -- for invoke only
--	local myarg = frame.args or frame:getParent().args or '' -- invoke & Template. (only invoke functions)
		-- Note: placing « or '' » before frame...args, does not work
--	myarg = frame:getParent().args or frame.args or '' -- Template and invoke (nothing works)
--TRIALS for (stem1): needed: myarg
--	myarg = ''	-- does not work
--	local myarg -- attempt to index local 'myarg' (a nil value).
--	wordproduced = '' --not needed
    PAGENAME = mw.title.getCurrentTitle().text
    lemma = PAGENAME:match( "^%s*(.-)%s*$" ) -- ALSO WRITTEN: lemma = mw.text.trim(PAGENAME)
	lemma_target = myarg['lemma'] or myarg['λήμμα'] or ''
	if lemma_target ~= '' then lemma = lemma_target end
    if lemma == '' then lemma = PAGENAME:match( "^%s*(.-)%s*$" ) end
return accent_1(removeaccent(mw.ustring.sub(lemma,1,-3)))	
end



return export