Active version Development version
  M:uz-inflection     {{uz-inflection}}     M:*/inflection     {{*/inflection}}  

Active version:

unit testcases main template table template
  M:uz-inflection/units/ru-noun     M:/.../testcases     {{ru-decl-noun-z}}     {{ru-decl-noun-table-z}}  
  M:uz-inflection/units/uz-noun     M:/.../testcases     {{uz-decl-noun}}     {{uz-decl-noun-table}}  

-- Inflection unit for Uzbek nouns
-- Version 2.0.3
-- Date: 2015-08-06

local dev_prefix = 'uz-'
-- dev_prefix = 'User:Vitalik/'  -- comment this on active version

local export = {}
local _ = require('Module:' .. dev_prefix .. 'inflection-tools')

local gen = 'ning'
local acc = 'ni'
local loc = 'da'
local abl = 'dan'
local sim = 'dek'
local dat = 'ga'

local possessive_first_sg = 'm'
local possessive_second_sg = 'ng'
local possessive_first_pl = 'miz'
local possessive_second_pl = 'ngiz'

function export.template()
	-- if base:match('^[A-Za-z]') == nil then  -- TODO: check alphabet from special parameter
	--   TODO: don't use this template in this case
	return 'uz-decl-noun-table'
end

function export.forms(base, args)
	local pl  = 'lar'
	local possessive_third = 'i'
	local interfix = 'i'

	-- Specific dative endings
	if _.endswith(base, 'k') then
		dat_specific = 'ka'
	elseif _.endswith(base, 'q') then
		dat_specific = 'qa'
	else
		dat_specific = dat
	end
	
	-- If two last letters are identical to first letter of declension ending — then we need to leave only two letters from three.
	-- For example, metall + lar = metallar
	if _.endswith(base, 'll') then
		pl = 'ar'
	end
	
	-- Start procissing possessive forms
	local base_possessive = base
	local possessive_type = args['possessive_type']
	local possessive_type_expected = ''
	
	-- When last letter is vowel
	if _.endswith(base, 'a') then
		interfix = ''
		possessive_third = 'si'
	end
	if _.endswith(base, {'[eiuo]', "o[ʻ`']"}) then
		if possessive_type == 'std' then
			interfix = ''
			possessive_third = 'si'
		elseif possessive_type == 'yisi' then
			interfix = 'yi'
			possessive_third = 'si'
		elseif possessive_type == 'yiyi' then
			interfix = 'yi'
			possessive_third = 'yi'
		else
			possessive_type_expected = '1'
		end
	end

	-- When last letters are "iy"
	if _.endswith(base, 'iy') then
		possessive_third = 'si'
	end
	
	-- When last letter is "k" or "q"
	if _.endswith(base, '[kq]') then
		if possessive_type == 'std' then
			if _.endswith(base, 'k') then
				base_possessive = _.replace(base, 'k$', 'g')
			elseif _.endswith(base, 'q') then
				base_possessive = _.replace(base, 'q$', 'gʻ')
			end
		elseif possessive_type == 'kq' then
			-- do nothing
		else
			possessive_type_expected = '1'
		end
	end

	-- When penultimate letter is vowel and last letter is consonant (except "y")
	if _.penultimate(base, {'[aeiuo]', "o[ʻ`']"}) and
			not _.endswith(base, {'[aeiuo]', "o[ʻ`']", 'y', 'k', 'q'}) then
		if possessive_type == 'reduced' then
			base_possessive = _.replace(base, '.(.)$', '%1')  -- but we don't process case with apostrophe at the end..
		end
	end

	-- Make decision to show/hide block "Possesive"
	if possessive_type_expected == '1' then
		show_possessive = ''
	else
		show_possessive = '1'
	end
	
	-- If there are no plural forms
	if st == '1' then
		-- singularia_tantum
		-- TODO
	end
	
	local forms = {
		nom_sg = base,
		gen_sg = base .. gen,
		acc_sg = base .. acc,
		loc_sg = base .. loc,
		abl_sg = base .. abl,
		sim_sg = base .. sim,
		dat_sg = base .. dat_specific,
		nom_pl = base .. pl,
		gen_pl = base .. pl .. gen,
		acc_pl = base .. pl .. acc,
		loc_pl = base .. pl .. loc,
		abl_pl = base .. pl .. abl,
		sim_pl = base .. pl .. sim,
		dat_pl = base .. pl .. dat,
		
		show_possessive = show_possessive,
		possessive_type_expected = possessive_type_expected,

		nom_sg_possessive_1sg = base_possessive .. interfix .. possessive_first_sg,
		nom_sg_possessive_2sg = base_possessive .. interfix .. possessive_second_sg,
		nom_sg_possessive_3sg = base_possessive .. possessive_third,
		nom_sg_possessive_1pl = base_possessive .. interfix .. possessive_first_pl,
		nom_sg_possessive_2pl = base_possessive .. interfix .. possessive_second_pl,
		nom_sg_possessive_3pl = base_possessive .. possessive_third,
		nom_pl_possessive_1sg = base .. pl .. 'i' .. possessive_first_sg,
		nom_pl_possessive_2sg = base .. pl .. 'i' .. possessive_second_sg,
		nom_pl_possessive_3sg = base .. pl .. 'i',
		nom_pl_possessive_1pl = base .. pl .. 'i' .. possessive_first_pl,
		nom_pl_possessive_2pl = base .. pl .. 'i' .. possessive_second_pl,
		nom_pl_possessive_3pl = base .. pl .. 'i',
	}
	
	-- Other possessive forms
	local plurals = {'sg', 'pl'}
	local persons = {'1sg', '2sg', '3sg', '1pl', '2pl', '3pl'}
	for i, plural in pairs(plurals) do
		for j, person in pairs(persons) do
			forms['gen_'..plural..'_possessive_'..person] = forms['nom_'..plural..'_possessive_'..person] .. gen
			forms['acc_'..plural..'_possessive_'..person] = forms['nom_'..plural..'_possessive_'..person] .. acc
			forms['loc_'..plural..'_possessive_'..person] = forms['nom_'..plural..'_possessive_'..person] .. loc
			forms['abl_'..plural..'_possessive_'..person] = forms['nom_'..plural..'_possessive_'..person] .. abl
			forms['sim_'..plural..'_possessive_'..person] = forms['nom_'..plural..'_possessive_'..person] .. sim
			forms['dat_'..plural..'_possessive_'..person] = forms['nom_'..plural..'_possessive_'..person] .. dat
		end
	end

	return forms
end

return export