सामग्री पर जाएँ

मॉड्यूल:mn

विक्षनरी से

इस मॉड्यूल हेतु प्रलेख मॉड्यूल:mn/doc पर बनाया जा सकता है

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

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

-- Checks if the final characters in a string match a pattern
local function endmatch( text, pattern, length )
	if match( sub( text, len( text ) - length ), pattern ) then
		return true
	else
		return false
	end
end

local function format_Mongolian_text(text) return '<span class="Mong" lang="mn">' .. text .. '</span>' 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 .. "[[Category:Mongolian redlinks/mn-see]]"
		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, "</div>")
	
	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 .. "[[Category:Mongolian redlinks/mn-IPA-see]]"
		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("{{mn-IPA\|([^\n]+)}}") then
		return frame:expandTemplate{ title = "mn-IPA", args = { section:match("{{mn-IPA\|([^\n]+)}}") } }
	else
		return frame:expandTemplate{ title = "mn-IPA", args = { title } }
	end
	
	
end

--Breaks down a string into vowel harmonic segments
function export.vowelharmony(text)
	
	local resets = { "уу", "үү", "юу" ,"юү", "яу", "ёу", "еү", "иу", "Уу", "Үү", "Юу" ,"Юү", "Яу", "Ёу", "Еү", "Иу", "%s" }
	local breaks = { 1 } -- "break" at position 1 by default
	
	for _, v in pairs( resets ) do
		local c = 0
		while c ~= nil do
			c = find( text, v, c + 1 )
			if c ~= nil and c ~= 1 then
				table.insert( breaks, c )
			end
		end
	end
	
	table.sort( breaks, function(a, b) return a > b end )
	
	local breaks_rev = {}
	for i, v in ipairs( breaks ) do
		if v > 2 then
			if v > breaks[i + 1] + 1 then
				table.insert( breaks_rev, v )
			end
		else
			table.insert( breaks_rev, v )
		end
	end
	
	table.sort( breaks_rev )
	
	for i, v in ipairs( breaks_rev ) do
		if i ~= 1 and sub( text, v, v ) ~= "%s" then
			breaks_rev[i] = v + 2
		end
	end
	
	local data = {}
	
	for i, b in ipairs( breaks_rev ) do
		table.insert( data, {} )
		if i == #breaks_rev then
			data[i].substring = sub( text, b, len( text ) )
		else
			data[i].substring = sub( text, b, breaks_rev[i + 1] - 1 )
		end
	end
	local loc = 0
	for i, s in ipairs( data ) do
		
		if match( s.substring, "[АОУЯЁаоуяё]" ) and match( s.substring, "[ЭӨҮЕэөүе]" ) then
			local findback = find( s.substring, "[АОУЯЁаоуяё]" )
			local findfront = find( s.substring, "[ЭӨҮЕэөүе]" )
			if findback < findfront then
				data[i].position = "back"
				data[i].location,_ = loc + findback
			else
				data[i].position = "front"
				data[i].location,_ = loc + findfront
			end
			data[i].violation = true
		elseif match( s.substring, "[АОУЯЁаоуяё]" ) then
			data[i].position = "back"
			data[i].location,_ = loc + find( s.substring, "[АОУЯЁаоуяё]" )
		elseif match( s.substring, "[ЭӨҮЕэөүе]" ) then
			data[i].position = "front"
			data[i].location,_ = loc + find( s.substring, "[ЭӨҮЕэөүе]" )
		elseif match( s.substring, "[Юю]" ) then
			data[i].position = "uncertain" -- require user input
			data[i].location,_ = loc + find( s.substring, "[Юю]" )
		else
			data[i].position = "neutral"
			if i == 1 then data[i].location = nil else data[i].location = data[i-1].location or nil end
		end
		
		if match( s.substring, "[АУЯЭҮауяэү]" ) and match( s.substring, "[ОЁӨоёө]" ) then
			local findunrounded = find( s.substring, "[АУЯЭҮауяэү]" )
			local findrounded = find( s.substring, "[ОЁӨоёө]" )
			if findunrounded <= findrounded then
				data[i].quality = "unrounded"
			else
				data[i].quality = "rounded"
			end
			data[i].violation = true
		elseif match( s.substring, "[АУЯЭҮИЮауяэүию]" ) then
			data[i].quality = "unrounded"
		elseif match( s.substring, "[ОЁӨоёө]" ) then
			data[i].quality = "rounded"
		else
			data[i].quality = nil
		end
		
		if data[i].violation ~= true then data[i].violation = false end
		loc = loc + len ( s.substring )
	end
	
	return data
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]" ) 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 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
			
		
	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
	
return export