Module:User:Theknightwho/mn-common

local export = {} local lang = require("Module:languages").getByCode( "mn" ) local links = require( "Module:links" ) table["reverse"] = require( "Module:table" ).reverse table["removeDuplicates"] = require( "Module:table" ).removeDuplicates

local char = mw.ustring.char local find = mw.ustring.find local len = mw.ustring.len local match = mw.ustring.match local gmatch = mw.ustring.gmatch local sub = mw.ustring.sub local gsub = mw.ustring.gsub local lower = mw.ustring.lower local reverse = require( "Module:string" ).reverse

export.FVS1 = char( 0x180B ) export.FVS2 = char( 0x180C ) export.FVS3 = char( 0x180D ) export.FVS4 = char( 0x180F ) export.MVS = char( 0x180E ) export.NNBSP = char( 0x202F ) export.stem_barrier = char( 0xF000 )

local function format_Mongolian_text(text) return "" .. text .. " " end

function export.see(frame) local params = { [1] = {},	}	local args = require("Module:parameters").process(frame:getParent.args, params) local title = args[1] local curr_title = mw.title.getCurrentTitle.subpageText local content = mw.title.new(title):getContent local senses = {} local sense_id = 0 local j, pos, s, section if title == curr_title then return error("The soft-directed item is the same as the page title.") end if content then if not match(content, "==Mongolian==") then categories = categories .. ""		elseif not match(content, "mn%-IPA") and not match(content, "mn%-see") then require("Module:debug").track("mn-see/unidirectional reference to variant") elseif not match(content, curr_title) then require("Module:debug").track("mn-see/unidirectional reference variant→orthodox") end end while true do		_, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.-)\n==%f[^=]", pos) if j == nil then i, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.+)", pos) end if j == nil then break else pos = j - 1 end if language_name == "Mongolian" then section = s		end end if not section then return "" end section = section:gsub("\n===+Etymology.-(\n==)", "%1") local text = {} for sense in section:gmatch("\n# ([^\n]+)") do		if not sense:match("rfdef") and not sense:match("defn") then sense_id = sense_id + 1 table.insert(senses, sense) end end table.insert(text, table.concat(senses, "\n# ")) table.insert(text, " ") return frame:preprocess( table.concat(text) ) end

function export.ipasee(frame) local params = { [1] = {},	}	local args = require("Module:parameters").process(frame:getParent.args, params) local title = args[1] local curr_title = mw.title.getCurrentTitle.subpageText local content = mw.title.new(title):getContent local senses = {} local sense_id = 0 local j, pos, s, section if title == curr_title then return error("The soft-directed item is the same as the page title.") end if content then if not match(content, "==Mongolian==") then categories = categories .. ""		elseif not match(content, "mn%-IPA") and not match(content, "mn%-see") and not match(content, "mn%-IPA-see") then require("Module:debug").track("mn-IPA-see/unidirectional reference to variant") elseif not match(content, curr_title) then require("Module:debug").track("mn-IPA-see/unidirectional reference variant→orthodox") end end while true do		_, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.-)\n==%f[^=]", pos) if j == nil then i, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.+)", pos) end if j == nil then break else pos = j - 1 end if language_name == "Mongolian" then section = s		end end if not section then return "" end section = section:gsub("\n===+Etymology.-(\n==)", "%1") local text = {} if section:match("") then return frame:expandTemplate{ title = "mn-IPA", args = { section:match("") } } else return frame:expandTemplate{ title = "mn-IPA", args = { title } } end end

--Breaks down a string into vowel harmonic segments function export.vowelharmony( text, params ) if not params then params = {} end local vh = {} local breaks = { 1 } local switchers = { "ау", "оу", "уу", "иу", "яу", "ёу", "еу", "юу", "уй", "эү", "өү", "үү", "иү", "еү", "юү", "үй", "%s" } local switchers2 = { "аа", "оо", "өө", "ээ", "яа", "ёо", "еө", "еэ", "Аа", "Оо", "Өө", "Ээ", "Яа", "Ёо", "Еө", "Еэ" } for _, v in pairs( switchers ) do v = gsub( v, "(.)(.)", "%1" .. char( 0x301 ) .. "?" .. char( 0x300 ) .. "?%2" ) local c = 0 while c ~= nil do			c = find( lower( text ), v, c + 1 ) if c ~= nil and c ~= 1 then table.insert( breaks, c ) end end end if params.bor then for _,v in pairs( switchers2 ) do if match( text, v .. "$" ) then table.insert( breaks, len( text ) - 1 ) end end end table.sort( breaks ) for i, b in ipairs( breaks ) do		table.insert( vh, { Cyrl = {}, Mong = {} } ) if i == #breaks then vh[i].substring = sub( text, b, len( text ) ) else vh[i].substring = sub( text, b, breaks[i + 1] - 1 ) end end if params.bor then for i,s in ipairs( vh ) do			vh[i] = { Cyrl = {}, Mong = {} } local orig_text = s.substring s.substring = lower( s.substring ) if params.bor == "Russian" then s.substring = gsub( s.substring, "у", "ү" ) end local substring_nostress = gsub( s.substring, "[" .. char( 0x301 ) .. char( 0x300 ) .. "]", "" ) if match( s.substring, "кило" .. char( 0x301 ) .. "?$" ) then -- irregular vh[i].Cyrl.a = "э" vh[i].location = find( s.substring, "[эүею]" ) vh[i].position = "front" vh[i].quality = "unrounded" elseif match( substring_nostress, "[аеёиоөуүэюя]у" ) or match( substring_nostress, "уй" ) then vh[i].Cyrl.a = "а" vh[i].location = 1 vh[i].position = "back" vh[i].quality = "unrounded" elseif match( substring_nostress, "[аеёиоөуүэюя]ү" ) or match( substring_nostress, "үй" ) then vh[i].Cyrl.a = "э" vh[i].location = 1 vh[i].position = "front" vh[i].quality = "unrounded" elseif match( s.substring, "[ауяᠠᠣᠤ]" .. char( 0x301 ) ) then vh[i].Cyrl.a = "а" vh[i].location = find( s.substring, char( 0x301 ) ) - 1 vh[i].position = "back" vh[i].quality = "unrounded" elseif match( s.substring, "[оё]" .. char( 0x301 ) ) then vh[i].Cyrl.a = "о" vh[i].location = find( s.substring, char( 0x301 ) ) - 1 vh[i].position = "back" vh[i].quality = "rounded" elseif ( match( substring_nostress, "[ауяоё]" ) and ( find( substring_nostress, "[ауяоё]" ) == find( substring_nostress, "[ауя]" ) ) ) or match( substring_nostress, "[ᠠᠣᠤ]" ) then vh[i].Cyrl.a = "а" vh[i].location = find( substring_nostress, "[ауя]" ) vh[i].position = "back" vh[i].quality = "unrounded" elseif match( substring_nostress, "[ауяоё]" ) and find( substring_nostress, "[ауяоё]" ) == find( substring_nostress, "[оё]" ) then vh[i].Cyrl.a = "о" vh[i].location = find( substring_nostress, "[оё]" ) vh[i].position = "back" vh[i].quality = "rounded" elseif ( match( substring_nostress, "[эүеюө]" ) and find( substring_nostress, "[эүеюө]" ) == find( substring_nostress, "[эүею]" ) ) or match( substring_nostress, "[ᠡᠥᠦᠧ]" ) then vh[i].Cyrl.a = "э" vh[i].location = find( substring_nostress, "[эүею]" ) vh[i].position = "front" vh[i].quality = "unrounded" elseif match( substring_nostress, "[эүеюө]" ) and find( substring_nostress, "[эүеюө]" ) == find( substring_nostress, "ө" ) then vh[i].Cyrl.a = "ө" vh[i].location = find( substring_nostress, "ө" ) vh[i].position = "front" vh[i].quality = "rounded" else vh[i].Cyrl.a = "э" vh[i].location = find( substring_nostress, "и" ) or 1 vh[i].position = "front" vh[i].quality = "unrounded" end if match( vh[i].Cyrl.a, "[ао]" ) then vh[i].Cyrl.ii = "ы" vh[i].Cyrl.u = "у" vh[i].Mong.a = "ᠠ" vh[i].Mong.u = "ᠤ" else vh[i].Cyrl.ii = "ий" vh[i].Cyrl.u = "ү" vh[i].Mong.a = "ᠡ" vh[i].Mong.u = "ᠦ" end if match( vh[i].Cyrl.a, "ө" ) then -- ө takes the diphthong эй not өй vh[i].Cyrl.ai = "эй" else vh[i].Cyrl.ai = vh[i].Cyrl.a .. "й" end vh[i].Cyrl.aa = vh[i].Cyrl.a .. vh[i].Cyrl.a vh[i].Cyrl.uu = vh[i].Cyrl.u .. vh[i].Cyrl.u			vh[i].substring = orig_text end else local location local pattern for i,s in ipairs( vh ) do			local orig = s.substring s.substring = reverse( lower( s.substring ) ) local vowel = match( s.substring, "[аеёоөуүэюя]" ) location,_ = find( s.substring, "[аеёоөуүэюя]" ) if vh[i].Cyrl.a == nil then vh[i].Cyrl.a = "э" vh[i].position = "front" vh[i].quality = "unrounded" pattern = "и" end vh[i].Cyrl.a = lower( vh[i].Cyrl.a ) if vowel == "а" or vowel == "у" or vowel == "я" then vh[i].Cyrl.a = "а" vh[i].position = "back" vh[i].quality = "unrounded" pattern = "[ауюя]" if match( s.substring, "[еёоөүэ]" ) then vh[i].violation = true else vh[i].violation = false end elseif vowel == "о" or vowel == "ё" then vh[i].Cyrl.a = "о" vh[i].position = "back" vh[i].quality = "rounded" pattern = "[ёо]" if match( s.substring, "[аеөуүэюя]" ) then vh[i].violation = true else vh[i].violation = false end elseif vowel == "э" then vh[i].position = "front" if location and sub( s.substring, location-1, location-1 ) == "й" and match( s.substring, "[аеёоөуүэюя]", location+1 ) == "ө" then vh[i].Cyrl.a = "ө" vh[i].quality = "rounded" pattern = "[еө]" if match( s.substring, "[аёоуүэюя]" ) then vh[i].violation = true else vh[i].violation = false end else vh[i].Cyrl.a = "э" vh[i].quality = "unrounded" pattern = "[еүэю]" if match( s.substring, "[аёоөуя]" ) then vh[i].violation = true else vh[i].violation = false end end elseif vowel == "ү" then vh[i].Cyrl.a = "э" vh[i].position = "front" vh[i].quality = "unrounded" pattern = "[еүэю]" if match( s.substring, "[аёоөуя]" ) then vh[i].violation = true else vh[i].violation = false end elseif vowel == "ө" then vh[i].Cyrl.a = "ө" vh[i].position = "front" vh[i].quality = "rounded" pattern = "[еө]" if match( s.substring, "[аёоуүэюя]" ) then vh[i].violation = true else vh[i].violation = false end elseif vowel == "е" then vh[i].position = "front" if match( s.substring, "ө", location+1 ) then vh[i].Cyrl.a = "ө" vh[i].quality = "rounded" pattern = "[еө]" if match( s.substring, "[аёоуүэюя]" ) then vh[i].violation = true else vh[i].violation = false end else vh[i].Cyrl.a = "э" vh[i].quality = "unrounded" pattern = "[еүэю]" if match( s.substring, "[аёоөуя]" ) then vh[i].violation = true else vh[i].violation = false end end elseif vowel == "ю" then vh[i].quality = "unrounded" if match( s.substring, "[ауя]", location+1 ) then vh[i].Cyrl.a = "а" vh[i].position = "back" pattern = "[ауюя]" if match( s.substring, "[еёоөүэ]" ) then vh[i].violation = true else vh[i].violation = false end else vh[i].Cyrl.a = "э" vh[i].position = "front" pattern = "[еүэю]" if match( s.substring, "[аёоөуя]" ) then vh[i].violation = true else vh[i].violation = false end end end location = 0 local function prev_vowel( n ) return match( s.substring, "[аеёиоөуүэюя]", n+1 ) end local function prev_hvowel( n ) return match( s.substring, pattern, n+1 ) end while prev_vowel( location ) and ( prev_vowel( location ) == prev_hvowel( location ) or prev_vowel( location ) == "и" ) do				if prev_vowel( location ) == prev_hvowel( location ) then location = find( s.substring, pattern, location+1 ) else -- if и local icheck = location+1 while prev_vowel( icheck ) == "и" do						icheck = find( s.substring, "и", icheck+1 ) end if prev_vowel( icheck ) and prev_vowel( icheck ) == prev_hvowel( icheck ) then location = icheck elseif not prev_vowel( icheck ) and vowel == "э" then location = icheck elseif vowel == "э" and sub( s.substring, icheck-1, icheck-1 ) == "й" then location = icheck else break end end end if match( vh[i].Cyrl.a, "[ао]" ) then vh[i].Cyrl.ii = "ы" vh[i].Cyrl.u = "у" vh[i].Mong.a = "ᠠ" vh[i].Mong.u = "ᠤ" else vh[i].Cyrl.ii = "ий" vh[i].Cyrl.u = "ү" vh[i].Mong.a = "ᠡ" vh[i].Mong.u = "ᠦ" end if match( vh[i].Cyrl.a, "ө" ) then -- ө takes the diphthong эй not өй vh[i].Cyrl.ai = "эй" else vh[i].Cyrl.ai = vh[i].Cyrl.a .. "й" end vh[i].Cyrl.aa = vh[i].Cyrl.a .. vh[i].Cyrl.a vh[i].Cyrl.uu = vh[i].Cyrl.u .. vh[i].Cyrl.u			s.substring = orig vh[i].location = len( s.substring ) + breaks[i] - location end end return vh end

--Breaks down a string into syllables and returns a table function export.syllables( text ) local consonant = "[БВГДЖЗКЛМНПРСТФХЦЧШЩбвгджзклмнпрстфхцчшщ]" local vowel = "[АОУЭӨҮИЙЫЯЕЁЮаоуэөүийыяеёю]" local sign = "[ЪЬъь]" local iotated = "[ЯЕЁЮяеёю]" local punctuation = "[%s%p]" local final_clusters = require( "Module:mn/data" ).syll_final_cons local chars = {} for v in gmatch( text, "[%w%s%p" .. char( 0x301 ) .. char( 0x300 ) .. export.stem_barrier .. "]" ) do -- strip diacritics table.insert( chars, v ) end text = table.concat( chars ) -- reform text without diacritics local breaks = {} for i,v in pairs( chars ) do		if i == 1 or match( chars[i-1], punctuation ) then -- first letter of word table.insert( breaks, i ) elseif match( chars[i-1], export.stem_barrier ) then -- stem barrier (used by inflection templates) table.insert( breaks, i ) elseif i ~= #chars and match( v, consonant ) and match( chars[i+1], vowel ) then -- if followed by vowel (i.e. if lone/cluster-final), count backwards until vowel, punctuation/space or start of string is found; if a vowel is found first, then letter must be medial and lone/cluster-final, so is the break; if punctuation/start of string found first, letter is part of word-initial cluster, so is not the break (occurs in loanwords, e.g. трол|лей|бус ("trolleybus")) local j = i			while j > 1 and ( match( chars[j], consonant ) or match( chars[j], sign ) ) do				j = j - 1 if match( chars[j], vowel ) then table.insert( breaks, i ) end end elseif ( match( v, consonant ) or match( v, sign ) ) and ( i == #chars or match( chars[i+1], punctuation ) ) then -- if word-final consonant, count backwards until vowel, checking if each cluster is allowed as a word-final cluster; if it is, increase "stable" (the number of stable consonants (and signs) at the end) by one; if a vowel is found before an unstable cluster, the loop ends with no change; if an unstable cluster is found, "stable" will not iterate which will trigger an additional unvoweled syllable break at that consonant (occurs in loanwords, e.g. буд|ди|зм ("Buddhism"), ал|ге|бр ("algebra")) local j = i			local check = { chars[j] } local stable = 1 while j > 1 and j > i - #final_clusters and stable > i - j and ( match( chars[j-1], consonant ) or match( chars[j-1], sign ) ) do				j = j - 1 table.insert( check, chars[j] ) for k,cluster in ipairs( final_clusters[#check] ) do					if match( table.concat( table.reverse( check ) ), cluster ) then stable = stable + 1 break end end if stable == i - j then table.insert( breaks, j ) end end elseif match( v, vowel ) and match( chars[i-1], sign ) then -- vowel after Ь/Ъ table.insert( breaks, i ) elseif match( v, iotated ) and match( chars[i-1], vowel ) then -- iotated ("ya"-type) vowel after a vowel table.insert( breaks, i ) end end breaks = table.removeDuplicates( breaks ) local syll = {} for i,v in ipairs( breaks ) do		if i == #breaks then table.insert( syll, sub( text, v ) ) else table.insert( syll, sub( text, v, breaks[i+1] - 1 ) ) end end return syll end

function export.remove_final_short_vowel( text, params ) if not params then params = {} end local vh = export.vowelharmony( text, params )[#export.vowelharmony( text, params )] local syllables = #export.syllables( text ) local reduced = text local no_fv = false if ( syllables > 1 and match( text, "[бвгджзклмнпрстфхцчшщ][аоөэиь]$" ) ) or match( text, "[ьъ]$" ) then local matches = { not params.bor, match( text, "[ьъ]$" ), match( text, "[ауя][влмн]ба$" ), match( text, "[оё][влмн]бо$" ), match( text, "ө[влмн]бө$" ), match( text, "[эүе][влмн]бэ$" ), match( text, "[бвглмнр]" .. vh.Cyrl.a .. char( 0x301 ) .. "?" .. char( 0x300 ) .. "?н" .. vh.Cyrl.a .. "$" ), match( text, "[ауя]нга$" ), match( text, "[оё]нго$" ), match( text, "өнгө$" ), match( text, "[эүе]нгэ$" ) }		for _,v in pairs( matches ) do			if v then reduced = sub( text, 1, len( text ) - 1 ) no_fv = true end end end return reduced, no_fv end

function export.remove_penultimate_short_vowel( text, params ) if not params then params = {} end local vh = export.vowelharmony( text, params )[#export.vowelharmony( text, params )] local syllables = table.reverse( export.syllables( text ) ) if not params.proper and ( vh.location ~= len( text ) - 1 or vh.violation == false ) and not params.bor then -- exclude proper nouns, loanwords and terms where the deleted vowel determines the vowel harmony local check local syllable_orig for i,syllable in ipairs( syllables ) do			if i ~=1 and i ~= #syllables then syllable_orig = syllable if match( syllable, export.stem_barrier ) then break end local prev_syllable = gsub( syllables[i+1], export.stem_barrier, "" ) check = ( match( prev_syllable, "[бвгджзклмнпрстфхцчшщь]*$" ) or "" ) .. syllable .. ( match( syllables[i-1], "^[бвгджзклмнпрстфхцчшщь]*" ) or "" ) local matches = { -- CVC match( check, "^[влмр]ь?[аиоөэ][вгклмнпр]$" ), -- not [бн]VC match( check, "^гь?[аиоөэ][влмнр]$" ) and vh.position == "front", -- гV[влмнр] if front vowel only -- CCVC match( check, "^[вглмнр]?ь?[джзкпстхцчш]ь?[аиоөэ][вгджзклмнпрстцчш]$" ), match( check, "^[вглмнр]?ь?[сх]ь?[тч]ь?[аиоөэ][вглмнр]$" ), }				local exclusions = { match( check, "[лм]ь?[аиоөэ]в$" ), -- not [лм]Vв match( check, "[вдзклмпрстхц]ь?и[вгджзлмнпрстцчш]$" ), -- only [кжчш]иC }				for _,v in pairs( matches ) do					if v then syllables[i] = sub( syllable, 1, len( syllable ) - 1 ) for _,e in pairs( exclusions ) do							if e then syllables[i] = syllable_orig end end end end if syllables[i] ~= syllable_orig then break end end end end return table.concat( table.reverse( syllables ) ) end

function export.concat_forms_in_slot( forms ) if forms then local new_vals = {} for _, v in ipairs( forms ) do			local val = gsub( v.form, "|", "<!>" ) table.insert( new_vals, val ) end return table.concat( new_vals, "," ) else return nil end end

function export.combine_stem_ending( stem, ending ) if stem == "?" then return "?" else return stem .. ending end end

function export.generate_form( form, footnotes ) if type( footnotes ) == "string" then footnotes = { footnotes } end if footnotes then return { form = form, footnotes = footnotes } else return form end end return export