Documentation for this module may be created at မဝ်ဂျူ:hy-pronunciation/doc

local export = {}

local m_str_utils = require("Module:string utilities")
local lang = require("Module:languages").getByCode("hy")
local m_IPA = require("Module:IPA")

local toNFC = mw.ustring.toNFC
local ufind = m_str_utils.find
local ugsub = m_str_utils.gsub
local ulower = m_str_utils.lower

-- single characters that map to IPA sounds
local phonemic_chars_map = {
	-- Eastern Armenian
	east = {
		["ա"] = "ɑ",
		["բ"] = "b",
		["գ"] = "ɡ",
		["դ"] = "d",
		["ե"] = "e",
		["զ"] = "z",
		["է"] = "e",
		["ը"] = "ə",
		["թ"] = "tʰ",
		["ժ"] = "ʒ",
		["ի"] = "i",
		["լ"] = "l",
		["խ"] = "χ",
		["ծ"] = "t͡s",
		["կ"] = "k",
		["հ"] = "h",
		["ձ"] = "d͡z",
		["ղ"] = "ʁ",
		["ճ"] = "t͡ʃ",
		["մ"] = "m",
		["յ"] = "j",
		["ն"] = "n",
		["շ"] = "ʃ",
		["ո"] = "o",
		["չ"] = "t͡ʃʰ",
		["պ"] = "p",
		["ջ"] = "d͡ʒ",
		["ռ"] = "r",
		["ս"] = "s",
		["վ"] = "v",
		["տ"] = "t",
		["ր"] = "ɾ",
		["ց"] = "t͡sʰ",
		["ւ"] = "v",
		["փ"] = "pʰ",
		["ք"] = "kʰ",
		["և"] = "ev",
		["օ"] = "o",
		["ֆ"] = "f",
		["-"] = " ",
		["’"] = "",
		["-"] = "",
	},
	-- Western Armenian
	west = {
		["ա"] = "ɑ",
		["բ"] = "p",
		["գ"] = "k",
		["դ"] = "t",
		["ե"] = "e",
		["զ"] = "z",
		["է"] = "e",
		["ը"] = "ə",
		["թ"] = "t",
		["ժ"] = "ʒ",
		["ի"] = "i",
		["լ"] = "l",
		["խ"] = "χ",
		["ծ"] = "d͡z",
		["կ"] = "ɡ",
		["հ"] = "h",
		["ձ"] = "t͡s",
		["ղ"] = "ʁ",
		["ճ"] = "d͡ʒ",
		["մ"] = "m",
		["յ"] = "j",
		["ն"] = "n",
		["շ"] = "ʃ",
		["ո"] = "o",
		["չ"] = "t͡ʃ",
		["պ"] = "b",
		["ջ"] = "t͡ʃ",
		["ռ"] = "r",
		["ս"] = "s",
		["վ"] = "v",
		["տ"] = "d",
		["ր"] = "ɾ",
		["ց"] = "t͡s",
		["ւ"] = "v",
		["փ"] = "p",
		["ք"] = "k",
		["և"] = "ev",
		["օ"] = "o",
		["ֆ"] = "f",
		["-"] = " ",
		["’"] = "",
		["-"] = "",
	},
}

-- character sequences of two that map to IPA sounds
local phonemic_2chars_map = {
	east = {
		{ "ու", "u" },
	},
	west = {
		-- if not in the initial position and if not preceded by [ɑeəoiu]
		{
			"(.?.?)յու", -- can be overridden by using "."
			function(before)
				if not (before == "" or ufind(before, "[%sաեէիոօ]$") or before == "ու") then
					return before .. "ʏ"
				end
			end,
		},
		{ "ու", "u" },
		{ "էօ", "œ" },
		-- Western Armenian inserts ə in the causative
		{ "ցնել", "t͡sənel" },
	},
}

-- single characters that map to IPA sounds
local phonetic_chars_map = {
	-- Eastern Armenian
	east = {
		["ա"] = "ɑ",
		["բ"] = "b",
		["գ"] = "ɡ",
		["դ"] = "d",
		["ե"] = "e",
		["զ"] = "z",
		["է"] = "e",
		["ը"] = "ə",
		["թ"] = "tʰ",
		["ժ"] = "ʒ",
		["ի"] = "i",
		["լ"] = "l",
		["խ"] = "χ",
		["ծ"] = "t͡s",
		["կ"] = "k",
		["հ"] = "h",
		["ձ"] = "d͡z",
		["ղ"] = "ʁ",
		["ճ"] = "t͡ʃ",
		["մ"] = "m",
		["յ"] = "j",
		["ն"] = "n",
		["շ"] = "ʃ",
		["ո"] = "o",
		["չ"] = "t͡ʃʰ",
		["պ"] = "p",
		["ջ"] = "d͡ʒ",
		["ռ"] = "r",
		["ս"] = "s",
		["վ"] = "v",
		["տ"] = "t",
		["ր"] = "ɾ",
		["ց"] = "t͡sʰ",
		["ւ"] = "v",
		["փ"] = "pʰ",
		["ք"] = "kʰ",
		["և"] = "ev",
		["օ"] = "o",
		["ֆ"] = "f",
		["-"] = " ",
		["’"] = "",
		["-"] = "",
	},
	-- note that the default pronunciation of ostensible /ɾ/ is [ɹ]
	-- Western Armenian
	west = {
		["ա"] = "ɑ",
		["բ"] = "pʰ",
		["գ"] = "kʰ",
		["դ"] = "tʰ",
		["ե"] = "e",
		["զ"] = "z",
		["է"] = "e",
		["ը"] = "ə",
		["թ"] = "tʰ",
		["ժ"] = "ʒ",
		["ի"] = "i",
		["լ"] = "l",
		["խ"] = "χ",
		["ծ"] = "d͡z",
		["կ"] = "ɡ",
		["հ"] = "h",
		["ձ"] = "t͡sʰ",
		["ղ"] = "ʁ",
		["ճ"] = "d͡ʒ",
		["մ"] = "m",
		["յ"] = "j",
		["ն"] = "n",
		["շ"] = "ʃ",
		["ո"] = "o",
		["չ"] = "t͡ʃʰ",
		["պ"] = "b",
		["ջ"] = "t͡ʃʰ",
		["ռ"] = "r",
		["ս"] = "s",
		["վ"] = "v",
		["տ"] = "d",
		["ր"] = "ɾ",
		["ց"] = "t͡sʰ",
		["ւ"] = "v",
		["փ"] = "pʰ",
		["ք"] = "kʰ",
		["և"] = "ev",
		["օ"] = "o",
		["ֆ"] = "f",
		["-"] = " ",
		["’"] = "",
		["-"] = "",
	},
}

-- character sequences of two that map to IPA sounds
local phonetic_2chars_map = {
	east = {
		{ "ու", "u" },
	},
	west = {
		-- if not in the initial position and if not preceded by [ɑeəoiu]
		{
			"(.?.?)յու", -- can be overridden by using "."
			function(before)
				if not (before == "" or ufind(before, "[%sաեէիոօ]$") or before == "ու") then
					return before .. "ʏ"
				end
			end,
		},
		{ "ու", "u" },
		{ "էօ", "œ" },
		-- պ, տ, կ are not voiced after ս and շ
		{ "սպ", "sp" },
		{ "ստ", "st" },
		{ "սկ", "sk" },
		{ "շպ", "ʃp" },
		{ "շտ", "ʃt" },
		{ "շկ", "ʃk" },
		-- Western Armenian inserts ə in the causative
		{ "ցնել", "t͡sʰənel" },
	},
}

function export.phonemic_IPA(phonemic, system)
	if not (phonemic_chars_map[system] and phonemic_2chars_map[system]) then
		error("Invalid system " .. tostring(system))
	end

	phonemic = ulower(phonemic)

	-- then long consonants that are orthographically geminated.

	for _, replacement in ipairs(phonemic_2chars_map[system]) do
		phonemic = ugsub(phonemic, unpack(replacement))
	end

	-- ոու is pronounced ou
	phonemic = ugsub(phonemic, "ոːւ", "օու")

	-- ե and ո are pronounced as je and vo word-initially.
	phonemic = ugsub(phonemic, "^ե", "յէ")
	phonemic = ugsub(phonemic, "^ո", "վօ")
	-- except when followed by another վ.
	phonemic = ugsub(phonemic, "^վօվ", "օվ")

	--final ք, from the ancient plural, is extrasyllabic and should be marked.
	phonemic = ugsub(phonemic, "([^ɑeiouəœʏ])ք$", "%1.ք")

	-- ոու is pronounced oov
	phonemic = ugsub(phonemic, "ոու", "օու")

	-- palatalization in the Eastern Armenian sequence -ությ-, especially in the suffix -ություն [considered non-standard by strict prescriptivists]
	if system == "east" then
		phonemic = ugsub(phonemic, "ությ", "ուցյ")

		phonemic = ugsub(phonemic, "([բդգձջզժց])([փթքցչ])", "%1%2")
	end

	phonemic = ugsub(phonemic, ".", phonemic_chars_map[system])

	--oov is actually ou
	phonemic = ugsub(phonemic, "oov", "ou")

	if system == "west" then
		phonemic = ugsub(phonemic, "b([ptk])", "p%1")
		phonemic = ugsub(phonemic, "d([ptk])", "t%1")
		phonemic = ugsub(phonemic, "ɡ([ptk])", "k%1")
		phonemic = ugsub(phonemic, "d͡z([ptk])", "t͡s%1")
		phonemic = ugsub(phonemic, "d͡ʒ([ptk])", "t͡ʃ%1")
		phonemic = ugsub(phonemic, "z([ptk])", "s%1")
		phonemic = ugsub(phonemic, "ʒ([ptk])", "ʃ%1")
	end

	phonemic = ugsub(phonemic, "ʁ([ptksʃ])", "χ%1")
	phonemic = ugsub(phonemic, "v([ptksʃ])", "f%1")

	-- generating the stress
	phonemic = ugsub(phonemic, "%S+", function(word)
		-- Do not add a stress mark for monosyllabic words. Check to see if the word contains only a single instance of [ɑeəoiuœʏ]+.
		local numberOfVowels = select(2, ugsub(word, "[ɑeəoiuœʏ]", "%0"))

		-- If polysyllabic, add IPA stress mark using the following rules. The stress is always on the last syllable not
		-- formed by schwa [ə]. In some rare cases the stress is not on the last syllable. In such cases the stressed vowel
		-- is marked by the Armenian stress character <՛>, e.g. մի՛թե. So:
		--      1) Find the vowel followed by <՛>․ If none, jump to step 2. Else check if it is the first vowel of the word.
		--         If true, put the IPA stress at the beginning, else do step 3.
		--      2) Find the last non-schwa vowel, i.e. [ɑeoiuœʏ],
		--      3) If the IPA symbol preceding it is [ɑeəoiuœʏ], i.e. a vowel, put the stress symbol between them,
		--         if it is NOT [ɑeoiuəœʏ], i.e. it is a consonant,
		--         put the stress before that consonant.
		if numberOfVowels > 1 then
			local rcount
			word, rcount = ugsub(word, "([^ɑeoiuœʏə]*[ɑeoiuœʏə])՛", "ˈ%1")
			if rcount == 0 then
				word = ugsub(word, "([^ɑeoiuœʏə]*[ɑeoiuœʏ][^ɑeoiuœʏə]*)$", "ˈ%1")
				word = ugsub(
					word,
					"([^ɑeoiuœʏə]*[ɑeəoiuœʏ]?[ɑeoiuœʏ][^ɑeoiuœʏə]*ə[^ɑeoiuœʏə]*)$",
					"ˈ%1"
				)
			end
			-- Including () in the second and third sets will only work
			-- if () never encloses a vowel.
			word = ugsub(word, "([ɑeəoiuœʏ])ˈ([^ɑeoiuœʏə()]+)([^ɑeoiuœʏəːˈʰ()j])", "%1%2ˈ%3")
			word = ugsub(word, "(.)͡ˈ", "ˈ%1͡")
			return word
		end
	end)

	-- move stress marker out of opening/closing parentheses
	if system == "east" or system == "west" then
		phonemic = ugsub(phonemic, "ˈ%)", ")ˈ")
		phonemic = ugsub(phonemic, "%(ˈ", "ˈ(")
	end

	-- "." prevents assimilation and can act as a dummy initial consonant, so must be removed.
	phonemic = phonemic:gsub("%.", "")

	return toNFC(phonemic)
end

function export.phonetic_IPA(phonetic, system)
	if not (phonetic_chars_map[system] and phonetic_2chars_map[system]) then
		error("Invalid system " .. tostring(system))
	end

	phonetic = ulower(phonetic)

	-- then long consonants that are orthographically geminated.
	phonetic = ugsub(phonetic, "(.)%1", "%1ː")

	for _, replacement in ipairs(phonetic_2chars_map[system]) do
		phonetic = ugsub(phonetic, unpack(replacement))
	end

	-- ոու is pronounced ou
	phonetic = ugsub(phonetic, "ոːւ", "օու")

	-- ե and ո are pronounced as je and vo word-initially.
	phonetic = ugsub(phonetic, "^ե", "յէ")
	phonetic = ugsub(phonetic, "^ո", "վօ")
	-- except when followed by another վ.
	phonetic = ugsub(phonetic, "^վօվ", "օվ")

	-- ոու is pronounced oov
	phonetic = ugsub(phonetic, "ոու", "օու")

	phonetic = ugsub(phonetic, ".", phonetic_chars_map[system])

	--oov is actually ou
	phonetic = ugsub(phonetic, "oov", "ou")

	-- insertion of the optional glide
	phonetic = ugsub(phonetic, "iɑ", "i(j)ɑ")
	phonetic = ugsub(phonetic, "ie", "i(j)e")
	phonetic = ugsub(phonetic, "io", "i(j)o")
	phonetic = ugsub(phonetic, "iu", "i(j)u")
	phonetic = ugsub(phonetic, "ɑi", "ɑ(j)i")
	phonetic = ugsub(phonetic, "ei", "e(j)i")
	phonetic = ugsub(phonetic, "oi", "o(j)i")
	phonetic = ugsub(phonetic, "ui", "u(j)i")

	-- assimilation: ppʰ = pʰː; ttʰ = tʰː; ; kkʰ = kʰː
	phonetic = ugsub(phonetic, "ppʰ", "pʰː")
	phonetic = ugsub(phonetic, "ttʰ", "tʰː")
	phonetic = ugsub(phonetic, "kkʰ ", "kʰː")

	-- nasal assimilation
	phonetic = ugsub(phonetic, "n([ɡk]+)", "ŋ%1")

	-- pseudo-palatalization under the influence of Russian [COLLOQUIAL, NOT STANDARD]
	--phonetic = ugsub(phonetic, "tj", "t͡sj")
	--phonetic = ugsub(phonetic, "tʰj", "t͡sʰj")
	--phonetic = ugsub(phonetic, "dj", "d͡zj")

	-- palatalization in the Eastern Armenian sequence -ությ-, especially in the suffix -ություն [considered non-standard by strict prescriptivists]
	if system == "east" then
		phonetic = ugsub(phonetic, "utʰj", "ut͡sʰj")
	end

	-- trilling of ɾ in some positions [COLLOQUIAL, NOT STANDARD]
	--phonetic = ugsub(phonetic, "ɾt", "rt")

	-- devoicing of consonants in some positions
	phonetic = ugsub(phonetic, "bpʰ", "pʰː")
	phonetic = ugsub(phonetic, "dpʰ", "tʰpʰ")
	phonetic = ugsub(phonetic, "ɡpʰ", "kʰpʰ")
	phonetic = ugsub(phonetic, "d͡zpʰ", "t͡sʰpʰ")
	phonetic = ugsub(phonetic, "d͡ʒpʰ", "t͡ʃʰpʰ")
	phonetic = ugsub(phonetic, "vpʰ", "fpʰ")
	phonetic = ugsub(phonetic, "ʒpʰ", "ʃpʰ")

	phonetic = ugsub(phonetic, "btʰ", "pʰtʰ")
	phonetic = ugsub(phonetic, "dtʰ", "tʰː")
	phonetic = ugsub(phonetic, "ɡtʰ", "kʰtʰ")
	phonetic = ugsub(phonetic, "d͡ztʰ", "t͡sʰtʰ")
	phonetic = ugsub(phonetic, "d͡ʒtʰ", "t͡ʃʰtʰ")
	phonetic = ugsub(phonetic, "vtʰ", "ftʰ")
	phonetic = ugsub(phonetic, "ʒtʰ", "ʃtʰ")

	phonetic = ugsub(phonetic, "bkʰ", "pʰkʰ")
	phonetic = ugsub(phonetic, "dkʰ", "tkʰ")
	phonetic = ugsub(phonetic, "ɡkʰ", "kʰː")
	phonetic = ugsub(phonetic, "d͡zkʰ", "t͡sʰkʰ")
	phonetic = ugsub(phonetic, "d͡ʒkʰ", "t͡ʃʰkʰ")
	phonetic = ugsub(phonetic, "vkʰ", "fkʰ")
	phonetic = ugsub(phonetic, "ʒkʰ", "ʃkʰ")

	phonetic = ugsub(phonetic, "bt͡ʃʰ", "pʰt͡ʃʰ")
	phonetic = ugsub(phonetic, "dt͡ʃʰ", "tʰt͡ʃʰ")
	phonetic = ugsub(phonetic, "ɡt͡ʃʰ", "kʰt͡ʃʰ")
	phonetic = ugsub(phonetic, "d͡zt͡ʃʰ", "t͡sʰt͡ʃʰ")
	phonetic = ugsub(phonetic, "d͡ʒt͡ʃʰ", "t͡ʃʰː")
	phonetic = ugsub(phonetic, "vt͡ʃʰ", "ft͡ʃʰ")
	phonetic = ugsub(phonetic, "ʒt͡ʃʰ", "ʃt͡ʃʰ")

	phonetic = ugsub(phonetic, "bt͡sʰ", "pʰt͡sʰ")
	phonetic = ugsub(phonetic, "dt͡sʰ", "tʰt͡sʰ")
	phonetic = ugsub(phonetic, "ɡt͡sʰ", "kʰt͡sʰ")
	phonetic = ugsub(phonetic, "d͡zt͡sʰ", "t͡sʰː")
	phonetic = ugsub(phonetic, "d͡ʒt͡sʰ", "t͡ʃʰt͡sʰ")
	phonetic = ugsub(phonetic, "vt͡sʰ", "ft͡sʰ")
	phonetic = ugsub(phonetic, "ʒt͡sʰ", "ʃt͡sʰ")

	phonetic = ugsub(phonetic, "zpʰ", "spʰ")
	phonetic = ugsub(phonetic, "ztʰ", "stʰ")
	phonetic = ugsub(phonetic, "zkʰ", "skʰ")

	phonetic = ugsub(phonetic, "ʁt͡s", "χt͡s")
	phonetic = ugsub(phonetic, "ʁt͡ʃ", "χt͡ʃ")
	phonetic = ugsub(phonetic, "ʁp", "χp")
	phonetic = ugsub(phonetic, "ʁt", "χt")
	phonetic = ugsub(phonetic, "ʁk", "χk")
	phonetic = ugsub(phonetic, "ʁs", "χs")
	phonetic = ugsub(phonetic, "ʁʃ", "χʃ")

	phonetic = ugsub(phonetic, "vt͡s", "ft͡s")
	phonetic = ugsub(phonetic, "vt͡ʃ", "ft͡ʃ")
	phonetic = ugsub(phonetic, "vp", "fp")
	phonetic = ugsub(phonetic, "vt", "ft")
	phonetic = ugsub(phonetic, "vk", "fk")
	phonetic = ugsub(phonetic, "vs", "fs")
	phonetic = ugsub(phonetic, "vʃ", "fʃ")

	if system == "west" then
		phonetic = ugsub(phonetic, "χd͡z", "χt͡s")
		phonetic = ugsub(phonetic, "χd͡ʒ", "χt͡ʃ")
		phonetic = ugsub(phonetic, "χb", "χp")
		phonetic = ugsub(phonetic, "χd", "χt")
		phonetic = ugsub(phonetic, "χɡ", "χk")
	end

	if system == "west" then
		phonetic = ugsub(phonetic, "t͡ʃʰd͡z", "t͡ʃʰt͡s")
		phonetic = ugsub(phonetic, "t͡sʰd͡z", "t͡sʰt͡s")
		phonetic = ugsub(phonetic, "pʰd͡z", "pʰt͡s")
		phonetic = ugsub(phonetic, "tʰd͡z", "tʰt͡s")
		phonetic = ugsub(phonetic, "kʰd͡z", "kʰt͡s")

		phonetic = ugsub(phonetic, "t͡ʃʰd͡ʒ", "t͡ʃʰt͡ʃ")
		phonetic = ugsub(phonetic, "t͡sʰd͡ʒ", "t͡sʰt͡ʃ")
		phonetic = ugsub(phonetic, "pʰd͡ʒ", "pʰt͡ʃ")
		phonetic = ugsub(phonetic, "tʰd͡ʒ", "tʰt͡ʃ")
		phonetic = ugsub(phonetic, "kʰd͡ʒ", "kʰt͡ʃ")

		phonetic = ugsub(phonetic, "t͡ʃʰb", "t͡ʃʰp")
		phonetic = ugsub(phonetic, "t͡sʰb", "t͡sʰp")
		phonetic = ugsub(phonetic, "pʰb", "pʰp")
		phonetic = ugsub(phonetic, "tʰb", "tʰp")
		phonetic = ugsub(phonetic, "kʰb", "kʰp")

		phonetic = ugsub(phonetic, "t͡ʃʰd", "t͡ʃʰt")
		phonetic = ugsub(phonetic, "t͡sʰd", "t͡sʰt")
		phonetic = ugsub(phonetic, "pʰd", "pʰt")
		phonetic = ugsub(phonetic, "tʰd", "tʰt")
		phonetic = ugsub(phonetic, "kʰd", "kʰt")

		phonetic = ugsub(phonetic, "t͡ʃʰɡ", "t͡ʃʰk")
		phonetic = ugsub(phonetic, "t͡sʰɡ", "t͡sʰk")
		phonetic = ugsub(phonetic, "pʰɡ", "pʰk")
		phonetic = ugsub(phonetic, "tʰɡ", "tʰk")
		phonetic = ugsub(phonetic, "kʰɡ", "kʰk")
	end

	-- prothetic ə before {s/ʃ/z}{p/t/k/b/d/g} in Western Armenian; this rule is not the norm in Eastern Armenian anymore
	if system == "west" then
		phonetic = ugsub(phonetic, "^([sʃz][ptkbdɡ]+)", "ə%1")
	end

	-- generating the stress
	phonetic = ugsub(phonetic, "%S+", function(word)
		-- Do not add a stress mark for monosyllabic words. Check to see if the word contains only a single instance of [ɑeəoiuœʏ]+.
		local numberOfVowels = select(2, ugsub(word, "[ɑeəoiuœʏ]", "%0"))

		-- If polysyllabic, add an acute using the following rules. The stress is always on the last syllable not
		-- formed by schwa [ə]. In some rare cases the stress is not on the last syllable. In such cases the stressed vowel
		-- is marked by the Armenian stress character <՛>, e.g. մի՛թե. So:
		--      1) Find the vowel followed by <՛> and put the acute on it․ If none, go to step 2.
		--      2) Find the last non-schwa vowel, i.e. [ɑeoiuœʏ], and put the acute on it.
		if numberOfVowels > 1 then
			local rcount
			word, rcount = ugsub(word, "([ɑeoiuœʏə])՛", "%1́")
			if rcount == 0 then
				word = ugsub(word, "([ɑeoiuœʏ])([^ɑeoiuœʏə]*)(ə?[^ɑeoiuœʏə]?)$", "%1́%2%3")
			end
			return word
		end
	end)

	-- change phonetically-impossible ɾː to ɹː
	if system == "east" or system == "west" then
		phonetic = ugsub(phonetic, "ɾː", "ɹː")
	end

	if system == "east" or system == "west" then
		phonetic = ugsub(phonetic, "([td])%1͡([sʃzʒ])(ʰ?)", "%1̚%1͡%2%3")
		phonetic = ugsub(phonetic, "([td])͡([sʃzʒ])(ʰ?)ː", "%1̚%1͡%2%3")
	end
	
	-- "." prevents assimilation and can act as a dummy initial consonant, so must be removed.
	phonetic = phonetic:gsub("%.", "")

	return toNFC(phonetic)
end

function export.generic_IPA(frame) 
	local params = {
		[1] = {},
		["system"] = {},
	}
	
	local parent_args = frame:getParent().args
	local args = require("Module:parameters").process(parent_args, params, nil, "hy-pronunciation", "IPA")
	
	local pagename = mw.loadData("Module:headword/data").pagename

	return m_IPA.format_IPA_full({
			lang = lang,
			items = {
					{ pron = "/" .. export.phonemic_IPA(args[1] or pagename, args.system or "east") .. "/" },
					{ pron = "[" .. export.phonetic_IPA(args[1] or pagename, args.system or "east") .. "]" },
				},
		})
end

function export.IPA(frame)

	local params = {
		["E"] = { type = "boolean", default = "true" },
		["W"] = { type = "boolean", default = "true" },
		[1] = {},
		[2] = {},
		["e"] = {},
		["e2"] = {},
		["coll"] = {},
		["w"] = {},
		["w2"] = {},
		["collw"] = {},
		["pagename"] = {},
	}

	local parent_args = frame:getParent().args
	local args = require("Module:parameters").process(parent_args, params, nil, "hy-pronunciation", "IPA")
	local lines = {}
	local function ins(text)
		table.insert(lines, text)
	end
	local function get_pagename()
		return args.pagename or mw.loadData("Module:headword/data").pagename
	end

	local function insert_east_or_west(prons, system, standard_accent, coll_pron, coll_qualifier)
		local items = {}
		for _, pron in ipairs(prons) do
			if pron then
				table.insert(items, { pron = "/" .. export.phonemic_IPA(pron, system) .. "/" })
				table.insert(items, { pron = "[" .. export.phonetic_IPA(pron, system) .. "]" })
			end
		end
		ins("* " .. m_IPA.format_IPA_full({
			lang = lang,
			items = items,
			a = { standard_accent },
		}))
		if coll_pron then
			ins("** " .. m_IPA.format_IPA_full({
				lang = lang,
				items = {
					{ pron = "/" .. export.phonemic_IPA(coll_pron, system) .. "/" },
					{ pron = "[" .. export.phonetic_IPA(coll_pron, system) .. "]" },
				},
				q = { coll_qualifier, "colloquial" },
			}))
		end
	end
	if args.E then
		insert_east_or_west(
			{ args.e or args[1] or get_pagename(), args[2], args.e2 },
			"east",
			"hy-E",
			args.coll,
			"Eastern Armenian"
		)
	end
	if args.W then
		insert_east_or_west(
			{ args.w or args[1] or get_pagename(), args[2], args.w2 },
			"west",
			"hy-W",
			args.collw,
			"Western Armenian"
		)
	end

	return table.concat(lines, "\n")
end

return export