呢個模組嘅解說可以喺模組:fr-pron/doc度開

--[=[
imported from https://en.wiktionary.org/wiki/Module:fr-pron, CC BY-SA 3.0

Author: Benwing, rewritten from original by Kc kennylau

Generates French IPA from spelling. Implements template {{fr-IPA}}; also
used in [[Module:fr-verb]] (particularly [[Module:fr-verb/pron]], the submodule
handling pronunciation of verbs).

--]=]

local export = {}

local str_gsub = string.gsub

local u = mw.ustring.char
local rfind = mw.ustring.find
local rsubn = mw.ustring.gsub
local rmatch = mw.ustring.match
local rsplit = mw.text.split
local ulower = mw.ustring.lower
local uupper = mw.ustring.upper
local usub = mw.ustring.sub
local ulen = mw.ustring.len

-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = rsubn(term, foo, bar)
	return retval
end

-- apply rsub() repeatedly until no change
local function rsub_repeatedly(term, foo, bar)
	while true do
		local new_term = rsub(term, foo, bar)
		if new_term == term then
			return term
		end
		term = new_term
	end
end

local function ine(x)
	if x == "" then return nil else return x end
end

local function track(page)
	local m_debug = require("Module:debug").track("fr-pron/" .. page)
	return true
end

-- pairs of consonants where a schwa between then can never be deleted;
-- primarily, consonants that are the same except possibly for voicing
local no_delete_schwa_between_list = {
	'kɡ', 'ɡk', 'kk', 'ɡɡ', -- WARNING: IPA ɡ used here
	'td', 'dt', 'tt', 'dd',
	'bp', 'pb', 'pp', 'bb',
	'ʃʒ', 'ʒʃ', 'ʃʃ', 'ʒʒ',
	'fv', 'vf', 'ff', 'vv',
	'sz', 'zs', 'ss', 'zz',
	'jj', 'ww', 'ʁʁ', 'll', 'nn', 'ɲɲ', 'mm'
	-- FIXME, should be others
}
-- generate set
local no_delete_schwa_between = {}
for _, x in ipairs(no_delete_schwa_between_list) do
	no_delete_schwa_between[x] = true
end

local remove_diaeresis_from_vowel =
	{['ä']='a', ['ë']='e', ['ï']='i', ['ö']='o', ['ü']='u', ['ÿ']='i'}

-- True if C1 and C2 form an allowable onset (in which case we always
-- attempt to place them after the syllable break)
local function allow_onset_2(c1, c2)
	-- WARNING: Both IPA and non-IPA g below, and both r and ʁ, because it is
	-- called both before and after the substitutions of these chars.
	return (c2 == "l" or c2 == "r" or c2 == "ʁ") and rmatch(c1, "[bkdfgɡpstv]") or
		c1 == "d" and c2 == "ʒ" or
		c1 ~= "j" and c2 == "j" or
		(c2 == "w" or c2 == "W" or c2 == "ɥ")
end

-- list of vowels, including both input Latin and output IPA; note that
-- IPA nasal vowels are two-character sequences with a combining tilde,
-- which we include as the last char
local oral_vowel_no_schwa = "aeiouyAEIOUYéàèùâêîôûŷäëïöüÿăŏŭɑɛɔæœø"
local oral_vowel = oral_vowel_no_schwa .. "əƏĕė"
local vowel_no_schwa = oral_vowel_no_schwa .. "̃"
local vowel = oral_vowel .. "̃"
local vowel_c = "[" .. vowel .. "]"
local vowel_no_schwa_c = "[" .. vowel_no_schwa .. "]"
local vowel_maybe_nasal_r = "[" .. oral_vowel .. "]̃?"
local non_vowel_c = "[^" .. vowel .. "]"
local oral_vowel_c = "[" .. oral_vowel .. "]"
local vowel_no_i = "aeouəAEOUƏéàèùâêôûäëöüăĕŏŭɛɔæœø"
local vowel_no_i_c = "[" .. vowel_no_i .. "]"
-- special characters that should be carried through but largely ignored when
-- syllabifying; single quote prevents interpretation of sequences,
-- ‿ indicates liaison, ⁀ is a word boundary marker, - is a literal hyphen
-- (we include word boundary markers because they mark word boundaries with
-- words joined by hyphens, but should be ignored for syllabification in
-- such a case), parens are used to explicitly indicate an optional sound, esp.
-- a schwa
local syljoiner_c = "[_'‿⁀%-()]" -- don't include syllable marker or space
local opt_syljoiners_c = syljoiner_c .. "*"
local schwajoiner_c = "[_'‿⁀%-. ]" -- also include . and space but not ()
local opt_schwajoiners_c = schwajoiner_c .. "*"
local cons_c = "[^" .. vowel .. ".⁀ %-]" -- includes underscore, quote and liaison marker
local cons_no_liaison_c = "[^" .. vowel .. ".⁀‿ %-]" -- includes underscore and quote but not liaison marker
local real_cons_c = "[^" .. vowel .. "_'‿.⁀ %-()]" -- excludes underscore, quote and liaison marker
local cons_or_joiner_c = "[^" .. vowel .. ". ]" -- includes all joiners
local front_vowel = "eiéèêĕėəɛæœy" -- should not include capital E, used in cœur etc.
local front_vowel_c = "[" .. front_vowel .. "]"

-- Actual implementation of [[Template:fr-IPA]], compatible in spirit with
-- [[Template:IPA]].
function export.fr_IPA(frame)
	local params = {
		[1] = {list = true, allow_holes = true},
		["n"] = {list = true, allow_holes = true},
		["qual"] = {list = true, allow_holes = true},
		["pos"] = {required = false},
		["debug"] = {required = false},
	}
	
	local err = nil
	local args = require("Module:parameters").process(frame:getParent().args, params)

	local items = {}

	for i = 1, math.max(args[1].maxindex, args["n"].maxindex, 1) do
		local pron = "/" .. export.show(args[1][i], args["pos"], args["debug"]) .. "/"
		local note = args["n"][i]
		local qual = args["qual"][i]
		
		if pron or note or qual then
			table.insert(items, {pron = pron, note = note, qualifiers = {qual}})
		end
	end

	local lang = require("Module:languages").getByCode("fr")
	return require("Module:IPA").format_IPA_full(lang, items, err)
end

function export.show(text, pos, do_debug)
	if type(text) == 'table' then
		text, pos, do_debug = ine(text.args[1]), ine(text.args.pos), ine(text.args.debug)
	end
	local pagename = mw.title.getCurrentTitle().text 
	text = text or pagename
	text = ulower(text)

	-- track quote-separator if different numbers of quote symbols
	if ulen(rsub(text, "[^']", "")) ~= ulen(rsub(pagename, "[^']", "")) then
		track("quote-separator")
	end
	
	local debug = {}

	-- To simplify checking for word boundaries and liaison markers, we
	-- add ⁀ at the beginning and end of all words, and remove it at the end.
	-- Note that the liaison marker is ‿.
	text = rsub(text, "%s*,%s*", '⁀⁀ | ⁀⁀')
	text = rsub(text, "%s+", '⁀ ⁀')
	text = rsub(text, "%-+", '⁀-⁀')
	text = '⁀⁀' .. text .. '⁀⁀'

	if pos == "v" then
		-- special-case for verbs
		text = rsub(text, 'ai⁀', 'é⁀')
		-- vient, tient, and compounds will have to be special-cased, no easy
		-- way to distinguish e.g. initient (silent) from retient (not silent).
		text = rsub(text, 'ent⁀', 'e⁀')
		text = rsub(text, 'ent‿', 'ət‿')
		-- portions, retiens as verbs should not have /s/
		text = rsub(text, 'ti([oe])ns([⁀‿])', "t_i%1ns%2")
	end
	-- various early substitutions
	text = str_gsub(text, 'ǝ', 'ə') -- replace wrong schwa with same-looking correct one
	text = str_gsub(text, 'œu', 'Eu') -- capital E so it doesn't trigger c -> s
	text = str_gsub(text, 'oeu', 'Eu')
	text = str_gsub(text, 'œil', 'Euil')
	text = str_gsub(text, 'œ', 'æ') -- keep as æ, mapping later to è or é
	-- pas, gaz
	text = rsub(text, '[aä]([sz][⁀‿])', 'â%1')
	text = str_gsub(text, 'à', 'a')
	text = str_gsub(text, 'ù', 'u')
	text = str_gsub(text, 'î', 'i')
	text = str_gsub(text, '[Ee]û', 'ø')
	text = str_gsub(text, 'û', 'u')
	text = str_gsub(text, 'bs', 'ps') -- absolute, obstacle, subsumer, etc.
	text = str_gsub(text, 'ph', 'f')
	text = str_gsub(text, 'gn', 'ɲ')
	text = str_gsub(text, '⁀désh', '⁀déz')
	text = rsub(text, '([⁀‿])et([⁀‿])', '%1é%2')
	text = rsub(text, "([⁀‿'])es([⁀‿])", "%1ès%2")
	text = rsub(text, "([⁀‿'])est([⁀‿])", "%1èt%2")
	text = str_gsub(text, '⁀ress', '⁀rəss') -- ressortir, etc. should have schwa
	text = rsub(text, '⁀intrans(' .. vowel_c .. ')', '⁀intranz%1')
	text = rsub(text, '⁀trans(' .. vowel_c .. ')', '⁀tranz%1')
	-- adverbial -emment is pronounced -amment
	text = rsub(text, 'emment([⁀‿])', 'amment%1') 
	text = rsub(text, 'ie(ds?[⁀‿])', 'ié%1') -- pied, assieds, etc.
	text = rsub(text, '[eæ]([dgpt]s?[⁀‿])', 'è%1') -- permet
	text = rsub(text, 'ez([⁀‿])', 'éz%1') -- assez, avez, etc.
	text = str_gsub(text, 'er‿', 'èr‿') -- premier étage
	text = rsub(text, '([⁀‿]' .. cons_c .. '*)er(s?[⁀‿])', '%1èr%2') -- cher, fer, vers
	text = rsub(text, 'er(s?[⁀‿])', 'ér%1') -- premier(s)
	text = rsub(text, '(⁀' .. cons_c .. '*)e(s[⁀‿])', '%1é%2') -- ses, tes, etc.
	text = str_gsub(text, 'oien', 'oyen') -- iroquoien

	--s, c, g, j, q (ç handled below after dropping silent -s; x handled below)
	text = str_gsub(text, 'cueil', 'keuil') -- accueil, etc.
	text = str_gsub(text, 'gueil', 'gueuil') -- orgueil
	text = rsub(text, '(' .. vowel_c .. ')s(' .. vowel_c .. ')', '%1z%2')
	text = rsub(text, "c('?" .. front_vowel_c .. ')', 's%1')
	text = str_gsub(text, "qu'", "k'") -- qu'on
	text = rsub(text, 'qu(' .. vowel_c .. ')', 'k%1')
	text = rsub(text, 'ge([aoAOàâôäöăŏɔ])', 'j%1')
	text = rsub(text, 'g(' .. front_vowel_c .. ')', 'j%1')
	-- gu+vowel -> g+vowel, but gu+vowel+diaeresis -> gu+vowel
	text = rsub(text, 'gu(' .. vowel_c .. ')', function(vowel)
		local undo_diaeresis = remove_diaeresis_from_vowel[vowel]
		return undo_diaeresis and 'gu' .. undo_diaeresis or 'g' .. vowel
		end)
	text = str_gsub(text, 'gü', 'gu') -- aiguë might be spelled aigüe
	-- parking, footing etc.; also -ing_ e.g. swinguer respelled swing_guer,
	-- Washington respelled Washing'tonne
	text = rsub(text, '(' .. cons_c .. ")ing([_'⁀‿])", "%1iŋ%2")
	text = str_gsub(text, 'ngt', 'nt') -- vingt, longtemps
	text = str_gsub(text, 'j', 'ʒ')
	text = str_gsub(text, 's?[cs]h', 'ʃ')
	text = str_gsub(text, '[cq]', 'k')
	-- following two must follow s -> z between vowels
	text = rsub(text, '([^sçx⁀])ti([oe])n', '%1si%2n') -- tion, tien
	text = rsub(text, '([^sçx⁀])tial', '%1sial')
	table.insert(debug, text)

	-- special hack for uï; must follow guï handling and precede ill handling
	text = str_gsub(text, 'uï', 'ui') -- ouir, etc.

	-- special hack for oel, oil, oêl; must follow intervocal s -> z and
	-- ge + o -> j, and precede -il- handling
	text = rsub(text, 'o[eê]l', 'wAl') -- moelle, poêle
	-- poil but don't affect -oill- (otherwise interpreted as /ɔj/)
	text = str_gsub(text, 'oil([^l])', 'wAl%1')

	-- ill, il; must follow j -> ʒ above
	-- NOTE: In all of the following, we purposely do not check for a vowel
	-- following -ill-, so that respellings can use it before a consonant
	-- (e.g. [[boycotter]] respelled 'boillcotter')
	-- (1) special-casing for C+uill (juillet, cuillère, aiguille respelled
	--     aiguïlle)
	text = rsub_repeatedly(text, '(' .. cons_c .. ')uill', '%1ɥij')
	-- (2) -ill- after a vowel; repeat if necessary in case of VillVill
	--     sequence (ailloille respelling of ayoye)
	text = rsub_repeatedly(text, '(' .. vowel_c .. ')ill', '%1j')
	-- (3) any other ill, except word-initially (illustrer etc.)
	text = rsub(text, '([^⁀])ill', '%1ij')
	-- (4) final -il after a vowel; we consider final -Cil to contain a
	--     pronounced /l/ (e.g. 'il', 'fil', 'avril', 'exil', 'volatil', 'profil')
	text = rsub(text, '(' .. vowel_c .. ')il([⁀‿])', '%1j%2')
	-- (5) -il- after a vowel, before a consonant (not totally necessary;
	--     unlikely to occur normally, respelling can use -ill-)
	text = rsub(text, '(' .. vowel_c .. ')il(' .. cons_c .. ')', '%1j%2')

	-- y; include before removing final -e so we can distinguish -ay from
	-- -aye
	text = rsub(text, 'ay([⁀‿])', 'ai%1') -- Gamay
	text = str_gsub(text, 'éy', 'éj') -- used in respellings, eqv. to 'éill'
	text = rsub(text, '(' .. vowel_no_i_c .. ')y', '%1iy')
	text = rsub(text, 'yi([' .. vowel .. '.])', 'y.y%1')
	text = str_gsub(text, "'y‿", "'j‿") -- il n'y‿a
	text = rsub_repeatedly(text, '(' .. cons_c .. ')y(' .. cons_c .. ')', '%1i%2')
	text = rsub(text, '(' .. cons_c .. ')ye?([⁀‿])', '%1i%2')
	text = rsub(text, '⁀y(' .. cons_c .. ')', '⁀i%1')
	text = str_gsub(text, '⁀y⁀', '⁀i⁀')
	text = str_gsub(text, 'y', 'j')

	-- nasal hacks
	 -- make 'n' before liaison in certain cases both nasal and pronounced
	text = str_gsub(text, '(⁀[mts]?on)‿', '%1N‿') --mon, son, ton, on
	text = str_gsub(text, "('on)‿", '%1N‿') --qu'on, l'on
	text = str_gsub(text, '([eu]n)‿', '%1N‿') --en, bien, un, chacun etc.
	-- in bon, certain etc. the preceding vowel isn't nasal
	text = str_gsub(text, 'n‿', "N‿")

	-- other liaison hacks
	text = str_gsub(text, 'd‿', 't‿') -- grand arbre, pied-à-terre
	text = str_gsub(text, '[sx]‿', 'z‿') -- vis-a-vis, beaux-arts, premiers enfants, etc.
	text = str_gsub(text, 'f‿', 'v‿') -- neuf ans, etc.
	-- treat liaison consonants that would be dropped as if they are extra-word,
	-- so that preceding "word-final" letters are still dropped and preceding
	-- vowels take on word-final qualities
	text = str_gsub(text, '([bdgkpstxz]‿)', '⁀%1')
	text = str_gsub(text, 'i‿', 'ij‿') -- y a-t-il, gentil enfant

	--silent letters
	-- do this first so we also drop preceding letters of needed
	text = str_gsub(text, '[sxz]⁀', '⁀')
	-- silence -c and -ct in nc(t), but not otherwise
	text = str_gsub(text, 'nkt?⁀', 'n⁀')
	text = str_gsub(text, '([ks])t⁀', '%1T⁀') -- final -kt, -st pronounced
	text = str_gsub(text, 'ér⁀', 'é⁀') -- premier, converted earlier to premiér
	-- p in -mp, b in -mb will be dropped, but temporarily convert to capital
	-- letter so a trace remains below when we handle nasals
	text = str_gsub(text, 'm([bp])⁀', function(bp)
		local capbp = {b='B', p='P'}
		return 'm' .. capbp[bp] .. '⁀'
	end) -- plomb
	-- do the following after dropping r so we don't affect -rt
	text = str_gsub(text, '[dgpt]⁀', '⁀')
	-- remove final -e in various circumstances; leave primarily when
	-- preceded by two or more distinct consonants; in V[mn]e and Vmme/Vnne,
	-- use [MN] so they're pronounced in full
	text = rsub(text, '(' .. vowel_c .. ')n+e([⁀‿])', '%1N%2')
	text = rsub(text, '(' .. vowel_c .. ')m+e([⁀‿])', '%1M%2')
	text = rsub(text, '(' .. cons_c .. ')%1e([⁀‿])', '%1%2')
	text = rsub(text, '([mn]' .. cons_c .. ')e([⁀‿])', '%1%2')
	text = rsub(text, '(' .. vowel_c .. cons_c .. '?)e([⁀‿])', '%1%2')
	table.insert(debug, text)

	-- ç; must follow s -> z between vowels (above); do after dropping final s
	-- so that ç can be used in respelling to force a pronounced s
	text = str_gsub(text, 'ç', 's')

	-- x
	text = rsub(text, '[eæ]x(' .. vowel_c .. ')', 'egz%1')
	text = str_gsub(text, '⁀x', '⁀gz')
	text = str_gsub(text, 'x', 'ks')
	table.insert(debug, text)

	-- double consonants: eCC treated specially, then CC -> C; do after
	-- x -> ks so we handle exciter correctly
	text = rsub(text, '⁀e([mn])%1(' .. vowel_c .. ')', "⁀en_%1%2") -- emmener, ennui
	text = rsub(text, '⁀(h?)[eæ](' .. cons_c .. ')%2', '⁀%1é%2') -- effacer, essui, errer, henné
	text = rsub(text, '[eæ](' .. cons_c .. ')%1', 'è%1') -- mett(r)ons, etc.
	text = rsub(text, '(' .. cons_c .. ')%1', '%1')

	--diphthongs
	--uppercase is used to avoid the output of one change becoming the input
	--to another; we later lowercase the vowels; î and û converted early;
	--we do this before i/u/ou before vowel -> glide (for e.g. bleuet),
	--and before nasal handling because e.g. ou before n is not converted
	--into a nasal vowel (Bouroundi, Cameroun); au probably too, but there
	--may not be any such words
	text = str_gsub(text, 'ou', 'U')
	text = str_gsub(text, 'e?au', 'O')
	text = str_gsub(text, '[Ee]uz', 'øz')
	text = rsub(text, '[Ee]u([⁀‿])', 'ø%1')
	text = rsub(text, '[Ee][uŭ]', 'œ')
	text = str_gsub(text, '[ae]i', 'ɛ')

	-- Nasalize vowel + n, m
	-- Do before syllabification so we syllabify quatre-vingt-un correctly.
	-- We affect (1) n before non-vowel, (2) m before b/p (including B/P, which
	-- indicate original b/p that are slated to be deleted in words like
	-- plomb, champs), (3) -om (nom, dom, pronom, condom, etc.) and
	-- (4) -aim/-eim (faim, Reims etc.), (4). We leave alone other m's,
	-- including most final m. We do this after diphthongization, which
	-- arguably simplifies things somewhat; but we need to handle the
	-- 'oi' diphthong down below so we don't run into problems with the 'noi'
	-- sequence (otherwise we'd map 'oi' to 'wa' and then nasalize the n
	-- because it no longer precedes a vowel).
	text = rsub_repeatedly(text, '(.)(' .. vowel_c .. ')([mn])(' .. non_vowel_c .. ')',
		function(v1, v2, mn, c)
			if mn == 'n' or rfind(c, '[bpBP]') or v2 == 'o' or v2 == 'ɛ' then
				local nasaltab = {['a']='ɑ̃', ['ä']='ɑ̃', ['e']='ɑ̃', ['ë']='ɑ̃',
					['ɛ']='ɛ̃', ['i']='ɛ̃', ['ï'] = 'ɛ̃', ['o']='ɔ̃', ['ö']='ɔ̃',
					['ø']='œ̃', ['œ']='œ̃', ['u']='œ̃', ['ü']='œ̃'} -- à jeun
				if rfind(v1, '[éiï]') and v2 == 'e' then
					return v1 .. '.ɛ̃' .. c -- bien, européen, païen
				elseif v1 == 'j' and v2 == 'e' then
					return 'jɛ̃' .. c -- moyen
				elseif v1 == 'o' and v2 == 'i' then
					return 'wɛ̃' .. c -- coin, point
				elseif nasaltab[v2] then
					return v1 .. nasaltab[v2] .. c
				end
			end
			return v1 .. v2 .. mn .. c
		end)
	-- special hack for maximum, aquarium, circumlunaire, etc.
	text = rsub(text, 'um(' .. non_vowel_c .. ')', 'ɔm%1')
	-- now remove BP that represent original b/p to be deleted, which we've
	-- preserved so far so that we know that preceding m can be nasalized in
	-- words like plomb, champs
	text = str_gsub(text, '[BP]', '')
	table.insert(debug, text)

	-- do after nasal handling so 'chinois' works correctly
	text = str_gsub(text, 'oi', 'wA')

	-- remove silent h
	-- do after diphthongs to keep vowels apart as in envahir, but do
	-- before syllabification so it is ignored in words like hémorrhagie
	text = str_gsub(text, 'h', '')

	--syllabify
	-- (1) break up VCV as V.CV, and VV as V.V; repeat to handle successive
	--     syllables
	text = rsub_repeatedly(text, "(" .. vowel_maybe_nasal_r .. opt_syljoiners_c .. ")(" .. real_cons_c .. "?" .. opt_syljoiners_c .. oral_vowel_c .. ')', '%1.%2')
	-- (2) break up other VCCCV as VC.CCV, and VCCV as VC.CV; repeat to handle successive syllables
	text = rsub_repeatedly(text, "(" .. vowel_maybe_nasal_r .. opt_syljoiners_c .. real_cons_c .. opt_syljoiners_c .. ")(" .. real_cons_c .. cons_or_joiner_c .. "*" .. oral_vowel_c .. ")", '%1.%2')
	
	local function resyllabify(text)
		-- (3) resyllabify C.C as .CC for various CC that can form an onset:
		--     resyllabify C.[lr] as .C[lr] for C = various obstruents;
		--     resyllabify d.ʒ, C.w, C.ɥ, C.j as .dʒ, .Cw, .Cɥ, .Cj (C.w comes from
		--     written Coi; C.ɥ comes from written Cuill; C.j comes e.g. from
		--     des‿yeux, although most post-consonantal j generated later);
		--     don't resyllabify j.j
		text = rsub(text, "(%(?)(" .. real_cons_c .. ")(" .. opt_syljoiners_c .. ")%.(" .. opt_syljoiners_c .. ")(" .. real_cons_c .. ")",
			function(lparen, c1, j1, j2, c2)
				if allow_onset_2(c1, c2) then
					return "." .. lparen .. c1 .. j1 .. j2 .. c2
				end
			end)
		-- (4) resyllabify .CC as C.C for CC that can't form an onset (opposite of
		--     the previous step); happens e.g. in ouest-quart
		text = rsub(text, "%.(" .. opt_syljoiners_c .. ")(" .. real_cons_c .. ")(%)?)(" .. opt_syljoiners_c .. ")(" .. real_cons_c .. ")",
			function(j1, c1, rparen, j2, c2)
				if not allow_onset_2(c1, c2) and not (c1 == "s" and rfind(c2, "^[ptk]$")) then
					return j1 .. c1 .. rparen .. "." .. j2 .. c2
				end
			end)
		-- (5) fix up dʒ and tʃ followed by another consonant (management respelled
		--     'manadjment' or similar)
		text = rsub(text, "%.([%(]?[dt]" .. opt_syljoiners_c .. "[ʒʃ])(" .. opt_syljoiners_c .. ")(" .. real_cons_c .. ")",
			"%1.%2%3")
		return text
	end
	
	text = resyllabify(text)
	
	-- (6) eliminate diaeresis (note, uï converted early)
	text = rsub(text, '[äëïöüÿ]', remove_diaeresis_from_vowel)
	table.insert(debug, text)

	--single vowels
	text = str_gsub(text, 'â', 'ɑ')
	--don't do this, too many exceptions
	--text = rsub(text, 'a(%.?)z', 'ɑ%1z')
	text = str_gsub(text, 'ă', 'a')
	text = str_gsub(text, 'e%.j', 'ɛ.j') -- réveiller
	text = rsub_repeatedly(text, 'e%.(' .. cons_no_liaison_c .. '*' .. vowel_c .. ')', 'ə.%1')
	text = rsub(text, 'e([⁀‿])', 'ə%1')
	text = str_gsub(text, 'æ%.', 'é.')
	text = rsub(text, 'æ([⁀‿])', 'é%1')
	text = rsub(text, '[eèêæ]', 'ɛ')
	text = str_gsub(text, 'é', 'e')
	text = rsub(text, 'o([⁀‿])', 'O%1')
	text = str_gsub(text, 'o(%.?)z', 'O%1z')
	text = rsub(text, '[oŏ]', 'ɔ')
	text = str_gsub(text, 'ô', 'o')
	text = str_gsub(text, 'u', 'y')

	--other consonants
	text = str_gsub(text, 'r', 'ʁ')
	text = str_gsub(text, 'g', 'ɡ') -- use IPA variant of g
	table.insert(debug, text)

	--(mostly) final schwa deletions (FIXME, combine with schwa deletions below)
	--1. delete all instances of ė
	text = rsub(text, '%.([^.⁀]+)ė', '%1')
	--2. delete final schwa, only in the last word, not in single-syllable word
	--   (⁀. can occur after a hyphen, e.g. in puis-je)
	text = rsub(text, '([^⁀])%.([^ə.⁀]+)ə⁀⁀', '%1%2⁀')
	--3. delete final schwa before vowel in the next word, not in a single-
	--   syllable word (croyez-le ou non); the out-of-position %4 looks weird
	--   but the effect is that we preserve the initial period when there's a
	--   hyphen and period after the schwa (con.tre-.a.tta.quer ->
	--   con.tra.tta.quer) but not across a space (con.tre a.tta.quer ->
	--   contr a.tta.quer)
	text = rsub(text, '([^⁀])%.([^ə.⁀]+)ə⁀([⁀ %-]*)(%.?)(' .. vowel_c .. ')', '%1%4%2⁀%3%5')
	--4. delete final schwa before vowel in liaison, not in a single-syllable
	--   word
	text = rsub(text, '([^⁀]%.[^ə.⁀]+)ə‿%.?(' .. vowel_c .. ')', '%1‿%2')
	--5. delete schwa after any vowel (agréerons, soierie)
	text = rsub(text, '(' .. vowel_c .. ').ə', '%1')
	--6. make final schwa optional after two consonants except obstruent + approximant
	--   and [lmn] + ʁ
	text = rsub(text, '(' .. cons_c .. ')(' .. '%.?' .. ')(' .. cons_c .. ')ə⁀',
		function(a, dot, b)
			return a .. dot .. b .. (
				rfind(a, '[bdfɡkpstvzʃʒ]') and rfind(b, '[mnlʁwj]') and 'ə'
				or rfind(a, '[lmn]') and b == 'ʁ' and 'ə' or '(ə)') .. '⁀'
		end)

	--i/u/ou -> glide before vowel
	-- -- do from right to left to handle continuions and étudiions
	--    correctly
	-- -- do repeatedly until no more subs (required due to right-to-left
	--    action)
	-- -- convert to capital J and W as	a signal that we can convert them
	--    back to /i/ and /u/ later on if they end up preceding a schwa or
	--    following two consonants in the same syllable, whereas we don't
	--    do this to j from other sources (y or ill) and w from other
	--    sources (w or oi); will be lowercased later; not necessary to do 
	--    something similar to ɥ, which can always be converted back to /y/
	--    because it always originates from /y/.
	while true do
		local new_text = rsub(text, '^(.*)i%.?(' .. vowel_c .. ')', '%1J%2')
		new_text = rsub(new_text, '^(.*)y%.?(' .. vowel_c .. ')', '%1ɥ%2')
		new_text = rsub(new_text, '^(.*)U%.?(' .. vowel_c .. ')', '%1W%2')
		if new_text == text then
			break
		end
		text = new_text
	end

	--hack for agréions, pronounced with /j.j/
	text = str_gsub(text, 'e%.J', 'ej%.J')

	--glides -> full vowels after two consonants in the same syllable
	--(e.g. fl, tr, etc.), but only glides from original i/u/ou (see above)
	--and not in the sequence 'ui' (e.g. bruit), and only when the second
	--consonant is l or r (not in abstiennent)
	text = rsub(text, '(' .. cons_c .. '[lʁ])J(' .. vowel_c .. ')', '%1i.j%2')
	text = rsub(text, '(' .. cons_c .. '[lʁ])W(' .. vowel_c .. ')', '%1u.%2')
	text = rsub(text, '(' .. cons_c .. '[lʁ])ɥ(' .. vowel_no_i_c .. ')', '%1y.%2')
	-- remove _ that prevents interpretation of letter sequences; do this
	-- before deleting internal schwas
	text = str_gsub(text, "_", "")
	
	-- internal schwa
	-- 1. delete schwa in VCəCV sequence word-internally when neither V is schwa,
	--    except in ʁəʁ sequence (déchirerez); use uppercase schwa when not
	--    deleting it, see below; FIXME, we might want to prevent schwa deletion
	--    with other consonant sequences
	text = rsub_repeatedly(text, '(' .. vowel_no_schwa_c .. ')%.(' .. real_cons_c ..
		')ə%.(' .. real_cons_c .. ')(' .. vowel_no_schwa_c .. ')',
		function(v1, c1, c2, v2)
			if c1 == 'ʁ' and c2 == 'ʁ' then
				return v1 .. '.' .. c1 .. 'Ə.' .. c2 .. v2
			else
				return v1 .. c1 .. '.' .. c2 .. v2
			end
		end)
	-- 2. make optional internal schwa in remaining VCəCV sequences, including
	-- across words, except between certain pairs of consonants (FIXME, needs
	-- to be smarter); needs to happen after /e/ -> /ɛ/ before schwa in next
	-- syllable and after removing ' and _ (or we need to take them into account);
	-- include .* so we go right-to-left, convert to uppercase schwa so
	-- we can handle sequences of schwas and not get stuck if we want to
	-- leave a schwa alone.
	text = rsub_repeatedly(text, '(.*' .. vowel_c .. opt_schwajoiners_c ..
		')(' .. real_cons_c .. ')(' .. opt_schwajoiners_c.. ')ə(' ..
		opt_schwajoiners_c .. ')(' .. real_cons_c .. ')(' ..
		opt_schwajoiners_c .. vowel_c .. ')',
		function(v1, c1, sep1, sep2, c2, v2)
			if no_delete_schwa_between[c1 .. c2] then
				return v1 .. c1 .. sep1 .. 'Ə' .. sep2 .. c2 .. v2
			else
				return v1 .. c1 .. sep1 .. '(Ə)' .. sep2 .. c2 .. v2
			end
		end)

	-- lowercase any uppercase letters (AOUMNJW etc.); they were there to
	-- prevent certain later rules from firing
	text = ulower(text)
	
	--ĕ forces a pronounced schwa
	text = str_gsub(text, 'ĕ', 'ə')

	-- need to resyllabify again in cases like 'saladerie', where deleting the
	-- schwa above caused a 'd.r' boundary that needs to become '.dr'.
	text = resyllabify(text)

	-- rewrite apostrophes as liaison markers
	text = str_gsub(text, "'", "‿")
	--remove hyphens and word-boundary markers
	text = rsub(text, '[⁀%-]', '')
	if do_debug then return table.concat(debug, ':') end
	return text
end

return export