This module is not to be directly used. It is used by Template:pt-IPA, see there for usage.
--[=[
This module implements the template {{pt-IPA}}.
Author: Benwing
-- FIXME:
1. Implement i^ not before vowel = epenthetic i or deleted epenthetic i in Brazil (in that order), and i^^ not before
vowel = opposite order. Epenthetic i should not affect stress but should otherwise be treated like a normal vowel.
Deleted epenthetic i should trigger palatalization of t/d but have no other effects.
2. Implement i^ before vowel = i.V or yV (in that order), and i^^ before vowel = opposite order.
3. Implement i* = mandatory epenthetic i in Brazil.
4. Implement u^ = u. or w (in that order), and u^^ = opposite order.
5. Implement e^ = e or i in Brazil (in that order), and e^^ = opposite order.
6. Implement o^ = o or u in Brazil (in that order), and o^^ = opposite order.
7. Implement ê* = ê in Brazil, é in Portugal (useful especially before nasal consonants).
8. Implement ô* = ô in Brazil, ó in Portugal (useful especially before nasal consonants).
9. Implement é* = é in Brazil, ê in Portugal (useful especially in -éi-).
10. Implement ó* = ó in Brazil, ô in Portugal (useful especially in -ói-).
11. Implement des^ at beginning of word = 'dis++' or 'des++' in Brazil (in that order), and des^^ = opposite order.
12. In Portugal, before , unstressed 'a' should be /a/; unstressed 'e' should be /ɛ/; and unstressed 'o' should be
either /o/ or /ɔ/ (in that order).
13. Support qualifiers using <q:...>.
14. Support references using <ref:...>. Syntax is the same as for IPA ref=.
15. In Portugal, unstressed o in hiatus should be /w/, and unstressed e in hiatus should be /j/.
16. Support - (hyphen) = left and right parts should be treated as distinct phonological words but written joined
together, and non-final primary stresses turn into secondary stresses. Word-initial and word-final behavior should
happen, e.g. Brazil epenthesis of (j) before word-final /s/ followed a stressed vowel, Brazil raising of esC- and
Portugal rendering of o- as ò-.
17. Support : (colon), similar to hyphen but in non-final parts, final vowels aren't rendered as closed.
18. Support + (plus sign), similar to colon but non-final primary stresses aren't displayed and syllable-division
ignores the plus sign.
19. In Brazil, word-initial enC-, emC- should display as (careful pronunciation) ẽ-, (natural pronunciation) ĩ-.
20. In Portugal, -sç- and -sc(e/i)- should show as (careful pronunciation) /ʃs/, (natural pronunciation) /ʃ/. Same for
-sz- (] ]). ] ].]
21. In Portugal, grave accent indicates unstressed open a/e/o and macron indicates unstressed closed a/e/o; both are
ignored in Brazil.
22. In Portugal, iCi where the first i is before the stress should (maybe) show as iCi, (traditional pronunciation) ɨCi.
In iCiCi, both of the first two i's show as ɨ in the traditional pronunciation (FIXME: verify this). C should be
only a single consonant, hence not in ] or ] (FIXME: verify this). Does not apply if the first
i is stressed (e.g. ], ], ], ]) or if the stressed i is word-final (],
], ], ]), or in certain other words (], ], ],
]). Possibly this means it doesn't apply when the stressed i is in a suffix (-íssimo, -inho, -ista). We
can always disable the eCi spelling by adding an h in 'ihCi' to make it look like a cluster between the i's. NOTE:
It appears that iCi -> eCi should apply in ], meaning if we apply it at the end, we have to distinguish
between glides from original i and glides from e or y.
23. In Portugal and Brazil, stressed o in hiatus should automatically be ô (voo, Samoa, Alagoas, perdoe, abençoe).
24. In Northern Portugal, stressed closed ô in hiatus (whether written explicitly as e.g. vôo, Côa or generated automatically)
should show as e.g. /ˈbo.(w)ɐ/.
25. Recognize -zinha like -zinho, -mente. Just use hyphen (-) to handle these. We don't recognize -zão, -zona, -zito,
-zita because of too many false positives; you can just write the hyphen explicitly before the suffix as needed.
Cf. among our current vocabulary we have 10 -zão augmentatives (animalzão, aviãozão, cipozão, cuzão, homenzão,
leãozão, paizão, pãozão, pezão, tatuzão), 2 -ão augmentatives after a word ending in -z (codornizão, felizão), and
7 non-augmentatives (alazão, coalizão, razão, rezão, sazão, sezão, vazão). Similarly for -zona: we have 5 -zona
augmentatives (boazona, cuzona, maçãzona, mãezona, mãozona) against 8 non-augmentatives (amazona, aminofenazona,
arilidrazona, Arizona, cronozona, ecozona, Eurozona, fenazona) and no -ona augmentatives after words ending in -z.
For -zito, we have 1 -ito diminutive after a word ending in -z (Queluzito), one non-diminutive (quartzito), and no
-zito diminutives. For -zita we have 1 -zita diminutive (maçãzita) and 4 non-diminutives (andaluzita, monazita,
pedzita, stolzita).
26. Final 'r' isn't optional before -zinho, -zinha, -mente.
27. Consider making secondary stress optional in cases like traduçãozinha where the stress is directly before the
primary stress.
28. In Brazil, unstressed final-syllable /a/ should be reduced before -r, cf. ].
29. Support + = pagename, and pagename= argument.
30. Deduplicate final pronunciations without distinct qualifiers.
31. Implement support for dot-under without accompanying quality diacritic. When attached to a/e/o, it defaults to acute
= open pronun, except in the following circumstances, where it defaults to circumflex: (1) in the diphthongs
ei/eu/oi/ou; (2) in a nasal vowel.
32. Portugal final -e should show as optional (ɨ) unless there is a vowel-initial word following, in which case it
should not be displayed at all.
33. Syllabification: "Improper" clusters of non-sibiliant-obstruent + obstruent (pt, bt, bd, dk, kt; ps, bs, bv, bʒ, tz,
dv, ks; ft), non-sibiliant-obstruent + nasal (pn, bn, tm, tn, dm, dn, gm, gn), nasal + nasal (mn) are syllabified in
Portugal as .pt, .bv, .mn, etc. Note ʃ.t, ʃ.p, ʃ.k, etc. But in Brazil, all of these divide between the consonants
(p.t, b.v, ʃ.t, s.p, etc.). Particular case: ] divides as a.brr in Portugal but ab.rr in Brazil.
34. -ão, -ãe, -õe should be recognized as nasal diphthongs with a circumflex added to force stress.
35. Recognize obsolete -aõ, -aẽ, -oẽ as equivalent to -ão, -ãe, -õe.
36. In CluV, CruV, CliV, CriV, the 'u' and 'i' are vowels not glides in both Portugal and Brazil.
37. Epenthesis of (j) before final stressed s in Brazil should not happen after i.
38. Dialect markers such as "Brazil", "Portugal" should go at the beginning.
39. Portugal exC, êxC should be rendered like eiʃC (FIXME: Does this apply to "Central Portugal" as well?). exs- needs
handling like eiʃs-/(i)ʃs- not like eiss-.
40. Unstressed word-initial exC- should maybe have two pronunciations, one with eiʃC- and the other with (i)ʃC-.
(FIXME: Verify.)
41. -sj- (e.g. ]) should reduce to a single /ʒ/.
42. ] should have /z/ (Brazil), /ʒ/ (Portugal) instead of /s/, /ʃ/.
43. Unstressed -ie- in hiatus should automatically be -iè- in Portugal or maybe -iè-/-ié-? (FIXME: Verify.)
44. Initial esC- in Brazil should be either isC- or esC-.
45. Initial sC- in Portugal and maybe Brazil should be /s/ not /ʃ/.
46. Deleted epenthetic /i/ should block conversion of syllable-final m/n into nasalization, cf. ] respelled
'ami^nési^a'.
47. Portugal 'o', 'os' should be unstressed with /u/, not have /ɔ/.
48. /s/ after nasal vowel before glide should not become voiced.
49. ] (which uses the + component divider) should have the IPA stress mark before the 'z' not after.
50. Portugal final -ɨ should be suppressed before a vowel (with a tie sign), and made optional word-finally.
51. Final -dor/-tor/-sor/-ssor + feminine and plural should have closed /o/.
52. Final -oso should have closed /o/, but feminine and plural should have open /ɔ/.
53. Hiatuses in Brazil involving 'i' should have two possibilities (full vowel or glide); likewise for 'u'.
54. In Brazil phonetic representation, hiatuses involving 'i' should be , and those involving 'u' should be .
55. ui^ should convert to 'ui' in Portugal = /wi/, but to 'u.i' or 'uy' in Brazil.
56. des^ should be 'des++' or 'dis++' (++ in both cases).
57. Word-boundary special handling (e.g. des^, x-, -x, etc.) should also respect component boundaries e.g. in
], ].
58. Convert apostrophe to tie, and make tie transparent to syllabification.
59. 'x' in -nx- should default to /ʃ/ (], ]).
60. Final i^ should not be stressed.
61. There should not be a comma between phonemic and phonetic representations.
62. Final stressed -io in Brazil should be either i.u or iw.
63. Unstressed final '-ax' has open /a/ including in Portugal.
64. Clean up handling of qualifiers and fix bugs.
65. Support i#, u# for i./y or u./w in both Brazil and Portugal.
66. In Portugal, final unstressed -ar/-or should be pronounced open, but in Brazil, closed.
67. Suppress initial e- in esC- in Portugal after /i/.
68. In C, should be either full vowel or glide in Portugal.
69. Support substitution notation.
70. ] by itself should be /ẽj̃/ or /ĩ/.
71. Suffixes beginning with a vowel should act as if a pseudo-consonant precedes.
72. Prefixes should change primary stress to secondary.
73. -ing should be like -im in Brazil but -ingh in Portugal.
74. Single 's' after colon should be /z/ not /s/, in keeping with normal spelling practices;
likewise for single 'r'.
75. Intertonic 'o' after stressed i/e/ɛ (], ]) in Brazil should be .u or w.
76. Don't display phonetic IPA if identical to phonemic IPA.
77. Add "South Brazil" pronunciation.
78. -êm should be like -ém in Brazil but -ẽiem in Portugal.
79. -õem should be like -õe in Brazil but -õeem in Portugal.
80. -le in Portugal should split into two pronuns, one with vowel for -e, one without.
81. Initial 'e' in Portugal after des^ should be /i/.
]=]
--[=[
Issues concerning South Brazil pronunciation:
(Rather than an unified South Brazil pronunciation, it's better to transform it in Rio Grande do Sul/Gaúcho pronunciation)
1. Should all 'ẽ' (not just word-final) be rendered as ? We have several existing examples, e.g.
/de.zẽ.ba.ˈla.do/|/de.zẽj̃.ba.ˈla.do/ for ], /ˌde.zẽj̃.has.ˈkɐ̃.so/ for ],
/ẽj̃.baw.sa.ˈma(ɻ)/ for ], for ], /ẽ.ʁus.tiɾ/| for
].
2. Coda 'r': Mostly /ɻ/ but sometimes given as both /ɾ/ and /ɻ/ or occasionally /r/.
3. Raising of unstressed 'e' and 'o', and 'des-': Mostly not present but sometimes yes, e.g. ]
/kla.ɾe.ˈa(ɾ)/|/kla.ɾe.ˈa(ɻ)/|/kla.ɾi.ˈa(ɾ)/|/kla.ɾi.ˈa(ɻ)/|/kla.ˈɾja(ɾ)/|/kla.ˈɾja(ɻ)/, ]
, ] /ko.ˈzi.dʊ/|/ku.ˈzi.dʊ/, ] /dez.blo.ke.ˈa(ɾ)/|/dez.blo.ˈkja(ɾ)/|/d͡ʒiz-/|/-(ɻ)/,
] /des.fe.ˈɾi(ɾ)/|/des.fe.ˈɾi(ɻ)/|/des.fi.ˈɾi(ɾ)/|/des.fi.ˈɾi(ɻ)/, ] /dez.hes.pej.ˈta(ɾ)/|/d͡ʒiz-/|/-(ɻ)/,
] /des.tɾu.ˈi.do/|/d͡ʒis.tɾu.ˈi.do/.
4. Epenthetic 'i': Less common? E.g. ] given as just /ˌd͡ʒiɡ.ni.ˈda.de/, but ] as /ˈd͡ʒiɡ.no/|/ˈd͡ʒi.ɡi.no/.
5. Secondary stress: Often given two syllables before the stress. I have left it out unless it seems stable and in a prefix, e.g.
] /e.ˌlej.to.ˈɾa.do/, ] /e.ˌle.tɾi.ˈzɐ̃.te/, ] /ˌe.le.ˈva.do/, ] /e.ˌle.va.ˈdoɻ/,
] /ˌẽ.ɡa.ˌha.fa.ˈmẽ.to/.
6. Initial em-: Mostly given as /ẽ-/ or /ẽj̃-/, but sometimes /ĩ-/ as in ] /ĩ.pa.ɾe.ˈda(ɾ)/|/ĩ.pa.ɾe.ˈda(ɻ)/
or both as in ] /ẽ.kaj.ˈʃa(ɾ)/|/ĩ.kaj.ˈʃa(ɻ)/|/ẽ.kaj.ˈʃa(ɾ)/|/ĩ.kaj.ˈʃa(ɻ)/.
7. -nh-: ] given as , but is /j̃/ actually characteristic of this accent or is it /ɲ/?
8. Other inconsistencies: e.g. ] /ẽ.ʁus.tiɾ/|, with coda /ʃ/ and strong (usually ).
] /ab.ʁoˈɡa(ɻ)/|/ab.hoˈɡa(ɻ)/|/ab.χoˈɡa(ɻ)/|/ab.ɦoˈɡa(ɻ)/ with all possible strong r's.
9. Nasal vowels: Almost always as elsewhere, but occasionally e.g. ] /ĩn.tẽnˈde(r)/, ] /a.ˈʒen.te/|/a.ˈʒẽ.te/.
10. Nasal diphthongs: -ão sometimes claims to be /ɐ̃õ/, sometimes /ɐ̃w̃/. ] listed both.
]=]
local export = {}
local m_IPA = require("Module:IPA")
local m_table = require("Module:table")
local m_strutils = require("Module:string utilities")
local m_qual = require("Module:qualifier")
local lang = require("Module:languages").getByCode("pt")
local u = require("Module:string/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
local AC = u(0x0301) -- acute = ́
local GR = u(0x0300) -- grave = ̀ = open vowel quality without stress in Portugal only
local MACRON = u(0x0304) -- macron = ̄ = closed vowel quality without stress in Portugal only
local CFLEX = u(0x0302) -- circumflex = ̂
local TILDE = u(0x0303) -- tilde = ̃
local DIA = u(0x0308) -- diaeresis = ̈
local CEDILLA = u(0x0327) -- cedilla = ̧
local DOTOVER = u(0x0307) -- dot over = ̇
-- DOTUNDER indicates an explicitly unstressed syllable; useful when accompanied by a quality marker (acute or
-- circumflex), or by itself with a/e/o, where it defaults to acute (except in the following circumstances, where it
-- defaults to circumflex: (1) in the diphthongs ei/eu/oi/ou; (2) in a nasal vowel).
local DOTUNDER = u(0x0323) -- dot under = ̣
-- LINEUNDER indicates an explicit secondary stress; normally not necessary as primary stress is converted to secondary
-- stress if another primary stress follows, but can be used e.g. after a primary stress; can be accompanied by a
-- quality marker (acute or circumflex) with a/e/o; if not, defaults to acute (except in the same circumstances where
-- dot under defaults to circumflex).
local LINEUNDER = u(0x0331) -- line under = ̱
-- Serves to temorarily mark where a syllable division should not happen; temporarily substitutes for comma+space;
-- temporarily substitutes for #.
local TEMP1 = u(0xFFF0)
local SYLDIV = u(0xFFF1) -- used to represent a user-specific syllable divider (.) so we won't change it
local PSEUDOCONS = u(0xFFF2) -- pseudo-consonant at the edge of prefixes ending in a vowel and suffixes beginning with a vowel
local PREFIX_MARKER = u(0xFFF3) -- marker indicating a prefix so we can convert primary to secondary accents
-- Since we convert all symbols at the beginning and decompose accented characters (except for ç and ü), we can later
-- use capital and/or accented letters to represent additional distinctions, typically in cases where we want to
-- remember the source of a given phoneme. By convention we use capital letters, optionally with accents.
-- Specifically:
-- * A/E/O represent written a/e/o where we don't yet know the vowel quality. Towards the beginning, we convert all
-- written a/e/o to A/E/O and later convert them to their final qualities (which might include /a/ /e/ /o/, so we
-- can't use those symbols directly for this purpose).
-- * Ẽ stands for a word-initial Brazilian sound that can be pronounced either /ẽ/ (in careful speech) or /ĩ/ (in
-- natural speech) and originates from en- or em- before a consonant. We distinguish this from written in-/im-,
-- which can be only /ĩ/, and written ehn-/ehm- (or similar), which can be only /ẽ/.
-- * I is used to represent epenthetic i in Brazilian variants (which should not affect stress assignment but is
-- otherwise treated as a normal sound), and Ɨ represents deleted epenthetic i (which still palatalizes /t/ and /d/).
-- I is also used to represent Brazil e or i from initial esC-, and Portugal (i) from initial esC-.
-- * Ì is used to represent either i. in hiatus or /j/ in Brazil; likewise for Ù representing u. in hiatus or /w/.
-- * Ɔ (capital version of ɔ) stands for a Portugal sound that can be pronounced either /o/ or /ɔ/ (depending on the
-- speaker), before syllable-final /l/.
-- * Ú is used word-finally after i to represent either .u in hiatus or /w/ in Brazil.
-- * L is used word-finally in Portugal to split words ending in -le into two pronuns due to the differing pronunciation
-- of /l/ in the two cases (coda or not).
local vowel = "aɐeɛiɨoɔuüAEẼIƗÌOƆÙÚ"
local V = ""
local NV_NOT_SPACING_CFLEX = ""
local high_front_vocalic = "iIƗÌy"
local front_vocalic = "eɛɨẼ" .. high_front_vocalic
local FRONTV = ""
local glide = "yw"
local W = "" -- glide
local ipa_stress = "ˈˌ"
local ipa_stress_c = ""
local primary_quality = AC .. CFLEX
local primary_quality_c = ""
local quality = AC .. CFLEX .. GR .. MACRON
local quality_c = ""
local stress = LINEUNDER .. DOTOVER .. DOTUNDER .. ipa_stress
local stress_c = ""
local non_primary_stress = LINEUNDER .. DOTUNDER .. "ˌ"
local non_primary_stress_c = ""
local accent = quality .. stress .. TILDE
local accent_c = ""
-- Any component separator that should be "transparent" (i.e. ignored) during syllabification processes. This should
-- include a subset of the component_sep characters, currently + and * (which ++ is converted into).
local syl_transp_component_sep = "+*"
local syl_transp_component_sep_c = ""
-- Any character that should be "transparent" (i.e. ignored) during syllabification processes. This includes the
-- syllable-transparent component separators + and ++ (converted into *) as well as the tie character, which originates
-- from an apostrophe (e.g. ]).
local syl_transp = syl_transp_component_sep .. "‿"
local syl_transp_c = ""
-- Zero or more syllable-transparent characters; used during syllabification.
local STC = syl_transp_c .. "*"
-- Component separators that are not transparent to syllabification. Includes colon (:), hyphen (-) and double hyphen
-- (--), which is converted internally to @.
local non_syl_transp_component_sep = ":@%-"
local non_syl_transp_component_sep_c = ""
-- "component_sep" means any symbol that may separate word components (not including #, which is added at a certain
-- point next to certain word components so that the adjacent characters are treated as if they are at word bounaries).
local component_sep = syl_transp_component_sep .. non_syl_transp_component_sep
local component_sep_c = ""
local word_or_component_sep_c = ""
-- Syllable divider (auto-inserted or user-specified).
local syldiv = "." .. SYLDIV
local syldiv_c = ""
-- "charsep" means any symbol that may separate the individual characters that make up a word, and which should be
-- ignored for e.g. consonant-consonant assimilation processes. This currently includes accents and syllable dividers.
local charsep = accent .. syldiv
local charsep_c = ""
-- Characters that may divide words, other than the tie (‿), which is transparent to syllabification.
local non_syl_transp_word_divider = " #"
-- All characters that may divide words.
local word_divider = non_syl_transp_word_divider .. "‿"
-- "wordsep_not_syl_transp" means the same as "wordsep" below but excludes syllable-transparent characters. It is used
-- in other collections of symbols (particularly when negated, so as to include syllable-transparent characters but
-- otherwise exclude word separators) rather than by itself.
local wordsep_not_syl_transp = charsep .. non_syl_transp_word_divider .. non_syl_transp_component_sep
-- "wordsep" means any symbol that may separate the individual characters that make up a word or may separate words or
-- components, and which should be ignored for e.g. consonant-consonant assimilation processes that operate across
-- words. This currently includes everything in "charsep" and "component_sep" plus symbols that may divide words.
local wordsep = wordsep_not_syl_transp .. syl_transp
local wordsep_c = ""
local C = "" -- consonant
-- consonant or syllable-transparent component separator
local C_OR_SYL_TRANSP = ""
local H_OR_SYL_TRANSP = ""
local H_GLIDE_OR_SYL_TRANSP = ""
local C_NOT_H_OR_GLIDE = "" -- consonant other than h, w or y
local C_OR_WORD_BOUNDARY = "" -- consonant or word boundary
local voiced_cons = "bdglʎmnɲŋrɾʁvzjʒʤ" -- voiced sound
-- Unstressed words with vowel reduction in Brazil and Portugal.
local unstressed_words = require("Module:table").listToSet({
"o", "os", -- definite articles
"me", "te", "se", "lhe", "lhes", "nos", "vos", -- unstressed object pronouns
-- See https://en.wikipedia.orghttps://dictious.com/en/Personal_pronouns_in_Portuguese#Contractions_between_clitic_pronouns
"mo", "mos", "to", "tos", "lho", "lhos", -- object pronouns combined with articles
-- Allomorphs of articles after certain consonants
"lo", "los", "no", -- ] above as object pronoun
-- Allomorphs of object pronouns before other pronouns
"vo", -- ] above as allomorph of article
"que", -- subordinating conjunctions
"e", -- coordinating conjunctions
"de", "do", "dos", "por", -- basic prepositions + combinations with articles; ], ] above already
-- FIXME: Portugal pronun for pelos, pela, pelas given as if spelled pêlos, etc. with stress, but differently for pelo.
-- I am assuming the Portugal pronuns with stress are wrong.
"pelo", "pelos", "pela", "pelas" -- preposition + article combinations
})
-- Unstressed words with vowel reduction in Portugal only.
local unstressed_full_vowel_words_brazil = require("Module:table").listToSet({
"a", "as", -- definite articles
-- See https://en.wikipedia.orghttps://dictious.com/en/Personal_pronouns_in_Portuguese#Contractions_between_clitic_pronouns
"ma", "mas", "ta", "tas", "lha", "lhas", -- object pronouns combined with articles
-- Allomorphs of articles after certain consonants
"la", "las", "na", "nas",
"da", "das", -- basic prepositions + combinations with articles; ], ] above already
-- coordinating conjunctions; ] above already
"para", "pra", -- preposition; for the verb form use "pára" instead
})
-- Unstressed words without vowel reduction.
local unstressed_full_vowel_words = require("Module:table").listToSet({
"um", "uns", -- single-syllable indefinite articles
"meu", "teu", "seu", "meus", "teus", "seus", -- single-syllable possessives
"ou", -- coordinating conjunctions
-- Note that in order to match à and às we have to write them as below because at the point we are trying to
-- match them, all text has been converted to canonical decomposed Unicode form. Writing "à" and "às" directly
-- won't work even if you type in the text using decomposed Unicode characters because all page contents are
-- automatically converted to canonical composed form when saved.
"ao", "aos", "a" .. GR, "a" .. GR .. "s", -- basic prepositions + combinations with articles
"em", "com", -- other prepositions
})
-- Special-case pronunciations for certain unstressed words with irregular pronunciations. The left side is the
-- original spelling after DOTUNDER or DOTOVER has been added; which diacritic gets added depends on whether the word
-- has vowel reduction (DOTOVER) or no vowel reduction (DOTUNDER). The right side is the respelling. See comment just
-- above for why we write "a" .. GR instead of "à".
local unstressed_pronunciation_substitution = {
= "a" .. DOTUNDER .. "u",
= "a" .. DOTUNDER .. "us",
= "a" .. DOTUNDER,
= "a" .. DOTUNDER .. "s",
= "pu" .. DOTOVER .. "r",
}
-- Dialects and subdialects:
export.all_styles = {"gbr", "rio", "sp", "sbr", "gpt", "cpt", "spt", "npt"}
export.all_style_groups = {
all = export.all_styles,
br = {"gbr", "rio", "sp", "sbr"},
pt = {"gpt", "cpt", "spt", "npt"},
}
local style_to_style_group = {}
for group, styles in pairs(export.all_style_groups) do
if group ~= "all" then
for _, style in ipairs(styles) do
style_to_style_group = group
end
end
end
export.all_style_descs = {
-- style groups
br = "]",
pt = "]",
-- styles
gbr = "]", -- "general" Brazil
rio = "]", -- Carioca accent
sp = "]", -- Paulistano accent
sbr = "Southern Brazil",
gpt = "]", -- "general" Portugal
-- lisbon = "Lisbon", -- (not added yet)
cpt = "]", -- Central Portugal outside of Lisbon
spt = "Southern Portugal", -- Dialects of Alentejo and Algarve (West Algarve is a distinct subdialect)
npt = "]" -- Northern Portugal dialects (Porto/Minho and Transmontano are subdialects)
}
-- 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
-- version of rsubn() that returns a 2nd argument boolean indicating whether
-- a substitution was made.
local function rsubb(term, foo, bar)
local retval, nsubs = rsubn(term, foo, bar)
return retval, nsubs > 0
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
-- Flat-map a function `fun` over `items`. This is like `map` over a sequence followed by `flatten`, i.e. the function
-- must itself return a sequence and all of the returned sequences are flattened into a single sequence.
local function flatmap(items, fun)
local new = {}
for _, item in ipairs(items) do
local results = fun(item)
for _, result in ipairs(results) do
table.insert(new, result)
end
end
return new
end
-- Combine two sets of qualifiers, either of which may be nil or a list of qualifiers. Remove duplicate qualifiers.
-- Return value is nil or a list of qualifiers.
local function combine_qualifiers(qual1, qual2)
if not qual1 then
return qual2
end
if not qual2 then
return qual1
end
local qualifiers = m_table.deepcopy(qual1)
for _, qual in ipairs(qual2) do
m_table.insertIfNot(qualifiers, qual)
end
return qualifiers
end
-- Reorder the diacritics (accent marks) in `text` according to a canonical order. Specifically, there can conceivably
-- be up to three accents on a vowel: a quality mark (acute/circumflex/grave/macron); a mark indicating secondary stress
-- (lineunder), tertiary stress (dotunder; i.e. no stress but no vowel reduction) or forced vowel reduction (dotover);
-- and a nasalization mark (tilde). Order them as follows: quality - stress - nasalization. `err` is a function of one
-- argument (an error string) and should throw an error if called.
local function reorder_accents(text, err)
local function reorder_accent_string(accentstr)
local accents = rsplit(accentstr, "")
local accent_order = {
= 1,
= 1,
= 1,
= 1,
= 2,
= 2,
= 2,
= 3,
}
table.sort(accents, function(ac1, ac2)
return accent_order < accent_order
end)
return table.concat(accents)
end
text = rsub(text, "(" .. accent_c .. "+)", reorder_accent_string)
-- Remove duplicate accents.
text = rsub_repeatedly(text, "(" .. accent_c .. ")%1", "%1")
-- Make sure we don't have more than one of a given class.
if rfind(text, quality_c .. quality_c) then
err("Two different quality diacritics cannot occur together")
end
if rfind(text, stress_c .. stress_c) then
err("Two different stress diacritics cannot occur together")
end
-- Only a/e/o can receive a circumflex, grave or macron.
if rfind(text, "") then
err("Only a/e/o can be followed by circumflex, grave or macron")
end
return text
end
-- Generate partial IPA for a single preprocessed term respelling `text` in the specified `style` ('gbr', 'rio', etc.;
-- see all_style_descs above). If `phonetic` is given, generate phonetic output, otherwise phonemic output. `err` is a
-- function of one argument (an error string) and should throw an error if called. This function is a subfunction of
-- `IPA` and cannot really be used by itself, because it generates output containing special symbols that need to be
-- postprocessed into multiple outputs (and in addition some other final postprocessing needs to happen, e.g. to get
-- stress marks in the right place). The function `IPA` is available be called externally.
local function one_term_ipa(text, style, phonetic, err)
local brazil = m_table.contains(export.all_style_groups.br, style)
local portugal = m_table.contains(export.all_style_groups.pt, style)
-- Initial x -> /ʃ/: ], ], ], etc.
text = rsub(text, "(" .. word_or_component_sep_c .. ")x", "%1ʃ")
-- Final x -> /ks/ (], ], ], ], ], ], etc.), but for now we map to
-- X because later on we open unstressed vowels before final x.
text = rsub(text, "x(" .. word_or_component_sep_c .. ")", "X%1")
-- x after certain dipthongs (ai, ei, oi, ou) and after -en- should be /ʃ/. Other diphthongs before x are rare
-- and mostly learned and we need to force explicit respelling.
text = rsub(text, "(()" .. charsep_c .. "*()" .. charsep_c .. "*)x",
function(all, a, b)
local ab = a .. b
-- ], ], ], ], ], etc.
if ab == "ai" or ab == "ei" or ab == "oi" or ab == "ou" or ab == "en" then
return all .. "ʃ"
else
return all .. "x"
end
end)
-- -exC- should be pronounced like -esC- in Brazil but -eisC- in Portugal. Cf. excelente, experiência, têxtil,
-- êxtase. Not with other vowels (cf. ], ], ]).
-- FIXME: Maybe this applies only to Lisbon and environs?
text = rsub(text, "(e" .. accent_c .. "*)x(" .. C .. ")", function(v, c)
if brazil then
return v .. "s" .. c
elseif c == "s" then
return v .. "isç"
else
return v .. "is" .. c
end
end)
if rfind(text, "x") then
err("x must be respelled z, sh, cs, ss or similar")
end
-- combinations with h; needs to precede handling of c and s, and needs to precede syllabification so that
-- the consonant isn't divided from the following h.
if style == "npt" then
-- In Northern Portugal the affricate tch is kept
text = rsub(text, "ch", "tʃ")
end
-- Else
text = rsub(text, "()h", {="ʃ", ="ʃ", ="ɲ", ="ʎ" })
-- remove initial <h>
text = rsub(text, "(" .. word_or_component_sep_c .. ")h()", "%1%2")
-- Betacism
if style == "npt" then
-- In Northern Portugal, <v> is realized as <b>
text = rsub(text, "v", "b")
end
-- c, g, q
-- This should precede syllabification especially so that the latter isn't confused by gu, qu, gü, qü
-- also, c -> ç before front vowel ensures that cc e.g. in ], ] isn't reduced to single c.
text = rsub(text, "c(" .. FRONTV .. ")", "ç%1")
text = rsub(text, "g(" .. FRONTV .. ")", "j%1")
text = rsub(text, "gu(" .. FRONTV .. ")", "g%1")
-- ], ], ], ], ], ]
text = rsub(text, "ng()", brazil and "n%1" or "ngh%1")
text = rsub(text, "qu(" .. FRONTV .. ")", "k%1")
text = rsub(text, "ü", "u") -- ], ], ], ], etc.
text = rsub(text, "()u(" .. V .. ")", "%1w%2") -- ], ], etc.
text = rsub(text, "", "k") -- ], ], ], etc.
-- y -> i between non-vowels, cf. ] /i.ta.ma.ɾa.ˈt(ʃ)i/, ] respelled 'Sýdjney' or similar
-- /ˈsid͡ʒ.nej/ (Brazilian). Most words with y need respelling in any case, but this may help.
text = rsub(text, "(" .. C_OR_WORD_BOUNDARY .. ")y(" .. accent_c .. "*" .. C_OR_WORD_BOUNDARY .. ")", "%1i%2")
-- Reduce double letters to single, except for rr, mm, nn and ss, which map to special single sounds. Do this
-- before syllabification so double letters don't get divided across syllables. The case of cci, cce is handled
-- above. nn always maps to /n/ and mm to /m/ and can be used to force a coda /n/ or /m/. As a result,
-- ] will need respelling 'comnôsco', 'cõnôsco' or 'con.nôsco', and ] will similarly
-- need respelling e.g. as 'comum.mente' or 'comũmente'. Examples of words with double letters (Brazilian
-- pronunciation):
-- * ] no respelling needed /ˈa.kɾɐ/;
-- * ] respelled 'Aléppo' /aˈlɛ.pu/;
-- * ] respelled 'bâfferh' /ˈbɐ.feʁ/;
-- * ] respelled 'chéddarh' /ˈʃɛ.daʁ/;
-- * ] respelled 'Ranna' /ˈʁɐ̃.nɐ/;
-- * ] respelled 'djézz' /ˈd͡ʒɛs/;
-- * ] respelled 'Minnessôta' /mi.neˈso.tɐ/;
-- * ] respelled 'nutélla' /nuˈtɛ.lɐ/;
-- * ] respeled 'shópping' /ˈʃɔ.pĩ/ or 'shóppem' /ˈʃɔ.pẽj̃/;
-- * ] respelled 'Sto̱wnn.rrendj' /ˌstownˈʁẽd͡ʒ/;
-- * ] no respelling needed /juˈnɐ̃/.
--
-- Note that further processing of r and s happens after syllabification and stress assignment, because we need
-- e.g. to know the distinction between final -s and -z to assign the stress properly.
text = rsub(text, "rr", "ʁ")
text = rsub(text, "nn", "N")
text = rsub(text, "mm", "M")
-- Deleted epenthetic /i/ should prevent preceding /m/, /n/ from being converted into nasalization.
text = rsub(text, "mƗ", "MƗ")
text = rsub(text, "nƗ", "NƗ")
-- Will map later to /s/; need to special case to support spellings like 'nóss' (= nós, plural of nó).
text = rsub(text, "ss", "S")
text = rsub(text, "(" .. C .. ")%1", "%1")
-- muit- is special and contains nasalization. Do before palatalization of t/d so ] works.
text = rsub(text, "(" .. word_or_component_sep_c .. "mu" .. stress_c .. "*)(it)", "%1" .. TILDE .. "%2")
-- Palatalize t/d + Ɨ -> affricates in Brazil. Use special unitary symbols, which we later convert to regular affricate
-- symbols, so we can distinguish palatalized d from written dj. We only do Ɨ now so we can delete it; we do another
-- palatalization round towards the end after raising e -> i.
local palatalize_td = { = "ʧ", = "ʤ"}
if brazil then
text = rsub(text, "()(" .. word_or_component_sep_c .. "*Ɨ)",
function(td, high_vocalic) return palatalize_td .. high_vocalic end)
-- Now delete the symbol for deleted epenthetic /i/; it still triggers palatalization of t and d.
text = rsub(text, "Ɨ", "")
end
-- Divide words into syllables.
-- First, change user-specified . into a special character so we won't move it around. We need to keep this
-- going forward until after we place the stress, so we can correctly handle initial i- + vowel, as in ],
-- ] and ]. We need to divide ] as 'i.a' but ] as 'ia.te' and ] as 'Ia.un.dé'.
-- In the former case, the stress goes on i but in the latter cases not; so we always divide <ia> as 'i.a',
-- and then after stress assignment remove the syllable divider if the <i> isn't stressed. The tricky thing is
-- that we want to allow the user to override this by explicitly adding a . between the <i> and <a>. So we need
-- to keep the distinction between user-specified . and auto-determined . until after stress assignment.
text = rsub(text, "%.", SYLDIV)
-- We have various characters indicating divisions between word components where we want to treat the components
-- more or less like separate words (e.g. -mente, -zinho/-zinha). Some such "characters" are digraphs, which we
-- convert internally to single characters to simplify the code. Here, -- separates off -mente/-zinho/-zinha and
-- ++ separates off prefixes. We want to ignore at least + and ++ (converted to *) for syllabification purposes.
text = rsub(text, "%-%-", "@")
text = rsub(text, "%+%+", "*")
-- Respell ] as 'homemzinho' so it is stressed correctly.
text = rsub(text, "n(" .. SYLDIV .. "?ziɲos?" .. word_or_component_sep_c .. ")", "m%1")
-- Divide before the last consonant (possibly followed by a glide). We then move the syllable division marker
-- leftwards over clusters that can form onsets. Note that syllable-transparent component separators will always
-- be (and will continue to be) to the left of syllable dividers rather than to the right, so we don't need to
-- check for the latter situation.
text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*" .. C_OR_SYL_TRANSP .. "-)(" .. C .. H_GLIDE_OR_SYL_TRANSP .. "*" .. V .. ")", "%1.%2")
text = rsub(text, "(" .. H_OR_SYL_TRANSP .. "*)%.()", ".%1%2")
if portugal then
-- "Improper" clusters of non-sibiliant-obstruent + obstruent (pt, bt, bd, dk, kt; ps, bs, bv, bʒ, tz, dv, ks;
-- ft), non-sibiliant-obstruent + nasal (pn, bn, tm, tn, dm, dn, gm, gn), nasal + nasal (mn) are syllabified in
-- Portugal as .pt, .bv, .mn, etc. Note ʃ.t, ʃ.p, ʃ.k, etc. But in Brazil, all of these divide between the
-- consonants (p.t, b.v, ʃ.t, s.p, etc.). Particular case: ] divides as a.brr in Portugal but ab.rr
-- in Brazil.
text = rsub(text, "(" .. H_OR_SYL_TRANSP .. "*)%.()", ".%1%2")
text = rsub(text, "(" .. H_OR_SYL_TRANSP .. "*)%.()", ".%1%2")
else
-- /tʃ/, /dʒ/ are normally single sounds, but adj- in ], ] etc. should be 'ad.j'
text = rsub(text, "(t" .. STC .. ")%.(ʃ)", ".%1%2")
text = rsub(text, "(d" .. STC .. ")%.(j)", ".%1%2")
text = rsub(text, "(" .. word_or_component_sep_c .. "a" .. STC .. ")%.(d" .. STC .. ")(j)", "%1%2.%3")
end
-- All vowels should be separated from adjacent vowels by a syllable division except
-- (1) aeo + unstressed i/u, (], ], ], ], ], ]), except when
-- followed by nh or m/n/r/l + (non-vowel or word end), e.g. Bom.ba.im, ra.i.nha, Co.im.bra, sa.ir, but Jai.me,
-- a.mai.nar, bai.le, ai.ro.so, quei.mar, bei.ra;
-- (2) iu(s), ui(s) at end of word, e.g. fui, Rui, a.zuis, pa.riu, viu, sa.iu;
-- (3) ão, ãe, õe.
--
-- The easiest way to handle this is to put a special symbol between vowels that should not have a syllable
-- division between them.
--
-- First, put a syllable divider between ., as in ], ], ], ],
-- ], ]. Note that in cases like ], ], ], ], ], ],
-- where a vowel follows the m/n/l/r, there will already be a syllable division between i.m, u.n, etc., which will
-- block the following substitution.
text = rsub(text, "(" .. accent_c .. "*" .. STC .. ")(" .. STC .. ")", "%1.%2")
-- Also put a syllable divider between ..ɲ coming from 'nh' (], ]).
text = rsub(text, "(" .. accent_c .. "*" .. STC .. ")(" .. STC .. "%.ɲ)", "%1.%2")
-- Prevent syllable division between final -ui(s), -iu(s). This should precede the following rule that prevents
-- syllable division between ai etc., so that ] "he left" gets divided as sa.iu.
-- It doesn't make sense to have STC in the middle of a diphthong here.
text = rsub(text, "(u" .. accent_c .. "*)(is?" .. word_or_component_sep_c .. ")", "%1" .. TEMP1 .. "%2")
text = rsub(text, "(i" .. accent_c .. "*)(us?" .. word_or_component_sep_c .. ")", "%1" .. TEMP1 .. "%2")
-- Prevent syllable division between ai, ou, etc. unless either the second vowel is accented ]) or there's
-- a TEMP1 marker already after the second vowel (which will occur e.g. in ] divided as 'sa.iu').
text = rsub_repeatedly(text, "(" .. accent_c .. "*)()", "%1" .. TEMP1 .. "%2")
text = rsub_repeatedly(text, "(u" .. accent_c .. "*)(i)", "%1" .. TEMP1 .. "%2")
text = rsub_repeatedly(text, "(i" .. accent_c .. "*)(u)", "%1" .. TEMP1 .. "%2")
-- Prevent syllable division between nasal diphthongs unless somehow the second vowel is accented.
text = rsub_repeatedly(text, "(a" .. accent_c .. "*" .. TILDE .. ")()", "%1" .. TEMP1 .. "%2")
text = rsub_repeatedly(text, "(o" .. accent_c .. "*" .. TILDE .. ")(e)", "%1" .. TEMP1 .. "%2")
text = rsub_repeatedly(text, "(u" .. accent_c .. "*" .. TILDE .. ")(i)", "%1" .. TEMP1 .. "%2")
-- All other sequences of vowels get divided.
text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*" .. STC .. ")(" .. V .. ")", "%1.%2")
-- Remove the marker preventing syllable division.
text = rsub(text, TEMP1, "")
-- An acute or circumflex not followed by a stress marker has primary stress, so indicate it.
text = rsub_repeatedly(text, "(" .. V .. quality_c .. ")()", "%1ˈ%2")
-- Line-under indicates secondary stress.
text = rsub(text, LINEUNDER, "ˌ")
-- Add primary stress to the word if not already present.
local function accent_word(word)
-- Check if stress already marked. We check first for primary stress before checking for tilde in case both
-- primary stress and tilde occur, e.g. ], ], ], ] respelled 'cõnôsco'.
if rfind(word, "ˈ") then
return word
end
-- Preserve the syllable divider, which may be auto-added or user-specified.
local syllables = m_strutils.capturing_split(word, "()")
-- Check for nasal vowel marked with tilde and without non-primary stress; assign stress to the last such
-- syllable in case there's more than one tilde, e.g. ]. Note, this can happen in the part before
-- -mente, cf. ], and before -zinho, cf. ].
for i = #syllables, 1, -2 do -- -2 because of the syllable dividers; see above.
local changed
syllables, changed = rsubb(syllables, "(" .. V .. quality_c .. "*)" .. TILDE, "%1ˈ" .. TILDE)
if changed then
return table.concat(syllables)
end
end
-- Apply the default stress rule.
local sylno
-- Prefixes ending in a vowel such as pseudo- have a PSEUDOCONS after the final vowel, but we don't want that to
-- interfere in the stress-assignment algorithm.
if #syllables > 1 and (rfind(word, "?$") or rfind(word, "m$") or rfind(word, "ns$")) then
-- Stress the last syllable but one. The -2 is because of the syllable dividers; see above.
sylno = #syllables - 2
else
sylno = #syllables
end
-- Don't put stress on epenthetic i; instead, we stress the preceding syllable, as if epenthetic i weren't
-- there.
while sylno > 1 and rfind(syllables, "I") do
sylno = sylno - 2
end
-- It is (vaguely) possible that we have a one-syllable word beginning with a complex cluster such as gn-
-- followed by a normally unstressed ending such as -em. In this case, we want the ending to be stressed.
while sylno < #syllables and rfind(syllables, "I") do
sylno = syno + 2
end
-- If we are on a syllable without a vowel (can happen if it's the last syllable in a non-final component of a
-- word, when using a component separator that is transparent to stress, such as in ] respelled
-- 'rapaz+inho'), stress the syllable to the left.
while sylno > 1 and not rfind(syllables, V) do
sylno = sylno - 2
end
if rfind(syllables, stress_c) then
-- Don't do anything if stress mark already present. (Since we check for primary stress above, this check
-- specifically affects non-primary stress.)
return word
end
-- Add stress mark after first vowel (and any quality mark).
syllables = rsub(syllables, "^(.-" .. V .. quality_c .. "*)", "%1ˈ")
return table.concat(syllables)
end
-- Split the text into words and the words into components so we can correctly add stress to components without it.
local words = rsplit(text, " ")
for j, word_with_boundary_markers in ipairs(words) do
-- Prefixes have a PREFIX_MARKER after the # at the end of the prefix; split it off.
local begin_marker, word, end_marker = rmatch(word_with_boundary_markers, "^(#*)(.-)(*)$")
-- Words ends in -mente, -zinho(s) or -zinha(s); add primary stress to the preceding portion as if stressed
-- (e.g. ] -> 'agitádamente') unless already stressed (e.g. ] respelled
-- 'rápidamente'). The primary stress will be converted to secondary stress further below. Essentially, we
-- rip the word apart into two words ('mente'/'zinho' and the preceding portion) and
-- stress each one independently. Note that the effect of adding a primary stress will also be to cause
-- an error if stressed 'e' or 'o' is not properly marked as é/ê or ó/ô; cf. ], which must
-- be respelled 'cértamente', and ], which must be respelled 'posteriôrmente', just as
-- with ] and ]. To prevent this happening, you can add an accent to -mente or
-- -zinho, e.g. ] respelled 'dormênte', ] respelled 'vizínho'.
if rfind(word, syldiv_c .. "men%.te$") then
word = rsub(word, syldiv_c .. "(men%.te)$", "@%1")
else
word = rsub(word, syldiv_c .. "(zi%.ɲs?)$", "@%1")
end
-- Split on components; preserve the component divider.
local components = m_strutils.capturing_split(word, "(" .. component_sep_c .. syldiv_c .. "*)")
for k = 1, #components, 2 do -- 2 because of the component dividers.
-- Don't add stress to components followed by ++ (converted to *).
if k == #components or not rfind(components, "%*") then
components = accent_word(components)
end
end
-- Reconstruct the word.
words = begin_marker .. table.concat(components, "") .. end_marker
end
-- Reconstruct the text from the words.
text = table.concat(words, " ")
-- Add word boundaries around component separators. We add them on both sides of - and -- (converted to @), which
-- behave mostly like a true word separator, but only on the right side of other component separators (which
-- corresponds to the beginning of the word following the separator). Note that some component separators (+ and ++
-- ) are transparent to syllable boundaries, meaning that there may be a syllable divider directly
-- to the right of the component separator. To simplify the code below, we put the word boundary marker on the outside
-- of the syllable boundary marker.
text = rsub(text, "(" .. syldiv_c .. "?)", "#%1#")
text = rsub(text, "(" .. syldiv_c .. "?)", "%1#")
-- I has served its purpose (not considered when accenting).
text = rsub(text, "I", "i")
-- Remove hiatus between initial <i> and following vowel (]) unless the <i> is stressed (]) or the
-- user explicitly added a . (converted to SYLDIV above).
text = rsub(text, "#i%.(" .. V .. ")", "#y%1")
if brazil then
-- In Brazil, hiatuses involving i. or u. have two possibilities (full vowel or glide); represent using Ì. and Ù.,
-- which we later convert appropriately. Do this before eliminating SYLDIV so the user can force a hiatus using a
-- period.
local hiatus_to_optional_glide = { = "Ì", = "Ù"}
text = rsub(text, "(" .. C_OR_WORD_BOUNDARY .. ")()(%." .. V .. ")",
function(before, hiatus, after) return before .. hiatus_to_optional_glide .. after end)
-- In Brazil, hiatuses of the form í.o (e.g. ] "river", ]; but not ] "I laugh") have two
-- possibilities (i.u or iw); represent using Ú, which we later convert appropriately. Do this before eliminating
-- SYLDIV so the user can force a hiatus using a period, as in ] "I laugh" respelled 'ri.o'.
text = rsub(text, "(i" .. ipa_stress_c .. "%.)o(s?#)", "%1Ú%2")
else
-- Outside of Brazil, e.i -> a.i, e.g. ], ], ] respelled 'prote.inúrio'. But seems
-- not to happen in rei- (], ], ], etc.). Note, it does occur in ],
-- which needs respelling.
text = rsub(text, "(#re" .. syldiv_c .. ")(i)", "%1" .. TEMP1 .. "%2")
text = rsub(text, "e(" .. syldiv_c .. "i)", "a%1")
text = rsub(text, TEMP1, "")
-- Outside of Brazil, hiatuses involving 'e./i.' or 'o./u.' after obstruent + l/r preceding a vowel have two
-- possibilities (full vowel or glide), as in ], ], etc. Represent using Ì. and Ù., which
-- we later convert appropriately. Do this before eliminating SYLDIV so the user can force a hiatus using a
-- period.
local hiatus_to_optional_glide = { = "Ì", = "Ì", = "Ù", = "Ù"}
text = rsub(text, "(" .. H_OR_SYL_TRANSP .. "*)()(%." .. V .. ")",
function(before, hiatus, after) return before .. hiatus_to_optional_glide .. after end)
-- Outside of Brazil, remove hiatus more generally whenever 'e./i.' or 'o./u.' precedes a vowel. Do this before
-- eliminating SYLDIV so the user can force hiatus using a period.
local hiatus_to_glide = { = "y", = "y", = "w", = "w"}
text = rsub(text, "(" .. C_OR_WORD_BOUNDARY .. ")(%.)(" .. V .. ")",
function(before, hiatus, after) return before .. hiatus_to_glide .. after end)
end
-- Convert user-specified syllable division back to period. See comment above when we add SYLDIV.
text = rsub(text, SYLDIV, ".")
-- Vowel quality handling. First convert all a -> A, e -> E, o -> O. We will then convert A -> a/ɐ, E -> e/ɛ/ɨ,
-- O -> o/ɔ/u depending on accent marks and context. Ultimately all vowels will be one of the nine qualities
-- aɐeɛiɨoɔu and following each vowel will either be nothing (no stress), an IPA primary stress mark (ˈ) or an
-- IPA secondary stress mark (ˌ), in turn possibly followed by a tilde (nasalization). After doing everything
-- that depends on the position of stress, we will move the IPA stress marks to the beginning of the syllable.
text = rsub(text, "", { = "A", = "E", = "O"})
text = rsub(text, DOTOVER, "") -- eliminate DOTOVER; it served its purpose of preventing stress
-- Nasal vowel handling.
-- Final unstressed -am (in third-person plural verbs) pronounced like unstressed -ão.
text = rsub(text, "Am#", "A" .. TILDE .. "O#")
if portugal then
-- In Portugal, final -n is really /n/, and preceding unstressed e/o are open (], ], ];
-- ], ], ]).
text = rsub(text, "n#", "N#")
text = rsub(text, "()(N#)", "%1" .. AC .. "%2")
end
if brazil then
-- In Brazil, ] is pronounced like 'põe'.
text = rsub(text, "(Oˈ" .. TILDE .. ")(Em#)", "%1E#")
else
-- In Portugal, circumflex accent on final -em (], ], etc.) indicates a special double nasal diphthong
-- pronunciation.
text = rsub(text, "E" .. CFLEX .. "ˈm#", "E" .. CFLEX .. "ˈ" .. TILDE .. "y" .. TILDE .. ".E" .. CFLEX .. "m#")
-- In Portugal, ] is pronounced like 'põeem'.
text = rsub(text, "(Oˈ" .. TILDE .. ")(Em#)", "%1E.%2")
end
-- Acute accent on final -em (], ]) and final -ens (]) does not indicate an open
-- pronunciation.
text = rsub(text, "E" .. AC .. "(ˈs?#)", "E" .. CFLEX .. "%1")
-- Vowel + m/n within a syllable gets converted to tilde.
text = rsub(text, "(" .. V .. quality_c .. "*" .. stress_c .. "*)", "%1" .. TILDE)
-- Non-high vowel without quality mark + tilde needs to get the circumflex (possibly fed by the previous change).
text = rsub(text, "()(" .. stress_c .. "*)" .. TILDE, "%1" .. CFLEX .. "%2" .. TILDE)
-- Primary-stressed vowel without quality mark + m/n/nh across syllable boundary gets a circumflex, cf. ],
-- ], ] (excluding Northern Portugal)
if style == "npt" then
-- Northern Portugal keeps the open "a" vowels
text = rsub(text, "(A)(ˈ%.)", "%1" .. AC .. "%2")
end
text = rsub(text, "(" .. V .. ")(ˈ%.)", "%1" .. CFLEX .. "%2")
if brazil then
if style ~= "sbr" then -- Seems this happens less or not at all in South Brazil.
-- Primary-stressed vowel + m/n across syllable boundary gets nasalized in Brazil, cf. ], ].
text = rsub(text, "(" .. V .. quality_c .. "*)(ˈ%.)", "%1" .. TILDE .. "%2")
end
-- All vowels before nh (always across syllable boundary) get circumflexed and nasalized in Brazil,
-- cf. ]. I *think* the circumflexing but not nasalizing happens in South Brazil.
text = rsub(text, "(" .. V .. stress_c .. "*)(%.ɲ)", "%1" .. CFLEX .. "%2")
if style ~= "sbr" then -- I *think* this doesn't apply to South Brazil; need to verify.
text = rsub(text, "(" .. V .. quality_c .. "*" .. stress_c .. "*)(%.ɲ)", "%1" .. TILDE .. "%2")
end
-- Convert initial unstressed em-/en- before consonant to special symbol /Ẽ/, which later on is converted
-- to /e/ (careful pronunciation) or /i/ (natural pronunciation).
text = rsub(text, "(#E" .. CFLEX .. TILDE ..")(%." .. C ..")", "#Ẽ" .. TILDE .. "%2")
-- Same in ] standing alone (which will have a DOTUNDER in it), and in ].
text = rsub(text, "(#E" .. CFLEX .. DOTUNDER .. "?" .. TILDE ..")(#)", "#Ẽ" .. TILDE .. "%2")
end
-- Nasal diphthongs.
local nasal_termination_to_glide = { = "y", = "w"}
-- In ãe, ão, the second letter represents a glide.
text = rsub(text, "(A" .. CFLEX .. stress_c .. "*" .. TILDE .. ")()",
function(v1, v2) return v1 .. nasal_termination_to_glide .. TILDE end)
-- Likewise for õe.
text = rsub(text, "(O" .. CFLEX .. stress_c .. "*" .. TILDE .. ")E", "%1y" .. TILDE)
-- Likewise for ũi (generated above from muit-).
text = rsub(text, "(u" .. stress_c .. "*" .. TILDE .. ")i", "%1y" .. TILDE)
-- Final -em and -ens (stressed or not) pronounced /ẽj̃(s)/. (Later converted to /ɐ̃j̃(s)/ in Portugal.)
text = rsub(text, "(E" .. CFLEX .. stress_c .. "*" .. TILDE .. ")(s?#)", "%1y" .. TILDE .. "%2")
-- Oral diphthongs.
-- ei, eu, oi, ou -> êi, êu, ôi, ôu
text = rsub(text, "()(" .. stress_c .. "*)", "%1" .. CFLEX .. "%2")
-- ai, au -> ái, áu
text = rsub(text, "(A)(" .. stress_c .. "*)", "%1" .. AC .. "%2")
-- Convert A/E/O as appropriate when followed by a secondary or tertiary stress marker. If a quality is given,
-- it takes precedence; otherwise, act as if an acute accent were given.
text = rsub(text, "()(" .. non_primary_stress_c .. ")", "%1" .. AC .. "%2")
-- Stressed o in -dor, -dor, -sor (], ], ], ], etc.) and feminines and plurals
-- is closed /o/.
text = rsub(text, "()O(ˈr#)", "%1o%2")
text = rsub(text, "()O(ˈ%.rs?#)", "%1o%2")
-- Stressed o in -oso is closed /o/.
text = rsub(text, "O(ˈ%.sO#)", "o%1")
-- Stressed o in -osa, -osos, -osas is open /ɔ/.
text = rsub(text, "O(ˈ%.ss?#)", "ɔ%1")
-- Unstressed syllables.
-- Before final <x>, unstressed a/e/o are open, e.g. ], ], ].
text = rsub(text, "()(X)", "%1" .. AC .. "%2")
-- Capital X has served its purpose, so replace it.
text = rsub(text, "X", "kç")
if brazil then
if style ~= "sbr" then
-- Final unstressed -e(s), -o(s) -> /i/ /u/ (including before -mente)
local brazil_final_vowel = { = "i", = "u"}
text = rsub(text, "()(s?#)", function(v, after) return brazil_final_vowel .. after end)
-- Word-final unstressed -a(s) -> /ɐ/ (not before -mente)
text = rsub(text, "A(s?#)", function(after) return "ɐ" .. after end)
-- Word-final unstressed -ar -> /ɐr/ (e.g. ])
text = rsub(text, "A(r#)", function(after) return "ɐ" .. after end)
end
-- Initial unmarked unstressed non-nasal e- + -sC- -> /i/ or /e/ (], ]). To defeat this,
-- explicitly mark the <e> e.g. as <ệ> or <eh>. We reuse the special symbol /I/ for this purpose, which later
-- on is converted to /i/ or /e/. In South Brazil, however, the raised /i/ variant doesn't seem to exist.
if not rfind(text, "#Es.ç") then
text = rsub(text, "#E(s" .. C .. "*%.)", style == "sbr" and "#e%1" or "#I%1")
end
-- Remaining unstressed a, e, o without quality mark -> /a/ /e/ /o/.
local brazil_unstressed_vowel = { = "a", = "e", = "o"}
text = rsub(text, "()()",
function(v, after) return brazil_unstressed_vowel .. after end)
end
if portugal then
-- In Portugal, final unstressed -r opens preceding a/e/o (], ], ], ]
-- respelled 'ínter:...').
text = rsub(text, "()(r" .. word_or_component_sep_c .. ")", "%1" .. AC .. "%2")
-- In Portugal, unstressed a/e/o before coda l takes on an open quality. Note that any /l/ directly after a
-- vowel must be a coda /l/ because otherwise there would be a syllable boundary marker.
text = rsub(text, "()l", function(v)
-- The symbol Ɔ is later converted to /o/ or /ɔ/.
local vowel_to_before_l = { = "a", = "ɛ", = "Ɔ"}
return vowel_to_before_l .. "l"
end)
-- Unstressed 'ie' -> /jɛ/
text = rsub(text, "yE()", "yɛ%1")
-- Initial unmarked unstressed non-nasal e- + -sC- (seemingly also after des-, see Infopédia ]) ->
-- temporary symbol I (later changed to /(i)/, except after a vowel, in which case it is deleted). Note that /s/
-- directly after a vowel must be a coda /s/ because otherwise there would be a syllable boundary marker.
text = rsub(text, "#Es", "#Is")
-- Initial unmarked unstressed non-nasal e- -> /i/, including after des-.
text = rsub(text, "#E()", "#i%1")
-- Initial unmarked unstressed non-nasal o- -> /ɔ/ if another vowel follows (not 'o', 'os' by themselves).
text = rsub(text, "(#O)(.-#)", function(o, rest)
if rfind(rest, "^") and rfind(rest, V) then
return "#ɔ" .. rest
else
return o .. rest
end
end)
-- All other unmarked unstressed non-nasal e, o, a -> /ɨ/ /u/ /ɐ/
local portugal_unstressed_vowel = { = "ɐ", = "ɨ", = "u"}
text = rsub(text, "()()",
function(v, after) return portugal_unstressed_vowel .. after end)
end
-- Remaining vowels.
-- All remaining a -> /a/ (should always be stressed).
text = rsub(text, "A()", "a%1")
-- Ignore quality markers on i, u; only one quality.
text = rsub(text, "()" .. quality_c, "%1")
-- Convert a/e/o + quality marker appropriately.
local vowel_quality = {
= "a", = "ɐ",
= "ɛ", = "e",
= "ɔ", = "o",
}
text = rsub(text, "(" .. quality_c .. ")", vowel_quality)
-- Stressed o in hiatus (], ], ], etc.) is closed /o/.
text = rsub(text, "O(ˈ%." .. V .. ")", "o%1")
-- Stressed closed /o/ in Northern Portugal has a following optional /w/.
if style=="npt" then
text = rsub(text, "o(ˈ%." .. V .. ")", "o(w)%1")
end
-- Any remaining E or O (always without quality marker) is an error.
if rfind(text, "") then
err("Stressed e or o not occurring nasalized or in a diphthong must be marked for quality using é/ê or ó/ô")
end
-- Finally, eliminate DOTUNDER, now that we have done all vowel reductions.
text = rsub(text, DOTUNDER, "")
if brazil then
-- Epenthesize /(j)/ in ], ], ], ], ], ], ], etc. Note, this only
-- triggers at actual word boundaries (not before -mente), and not on nasal vowels or diphthongs. To defeat this
-- (e.g. in plurals), respell using 'ss' or 'hs'.
text = rsub(text, "(" .. V .. "ˈ)(#)", "%1Y%2")
-- Also should happen at least before + (cf. ] respelled 'rapaz+inho', ] respelled
-- 'vóz+inha').
text = rsub(text, "(" .. V .. "ˈ)(%.?%+)", "%1Y%2")
-- But should not happen after /i/.
text = rsub(text, "iˈY", "iˈ")
end
-- 'S' here represents earlier ss. Word-finally it is used to prevent epenthesis of (j) and should behave
-- like 's'. Elsewhere (between vowels) it should behave like 'ç'.
text = rsub(text, "S#", "s#")
text = rsub(text, "S", "ç")
-- s, z
-- s in trans + V -> z: ], ]
text = rsub(text, "(trɐ" .. stress_c .. "*" .. TILDE .. ".)s(" .. V .. ")", "%1z%2")
-- word final z -> s
text = rsub(text, "z#", "s#")
-- s is voiced between vowels (not nasalized) or between vowel and voiced consonant, including across word
-- boundaries; may be fed by previous rule. We have to split this into two rules before /s/ should not be voiced
-- between nasal vowel and another vowel (]) but should be voiced between nasal vowel and a voiced
-- consonant (]). Note that almost all occurrences of nasal vowel + s + voiced consonant are in
-- trans- which potentially could be handled above, but there may be others, e.g. ].
text = rsub(text, "(" .. V .. stress_c .. "*Y?%.?)s(" .. wordsep_c .. "*h?)", "%1z%2")
text = rsub(text, "(" .. V .. accent_c .. "*Y?%.?)s(" .. wordsep_c .. "*h?)", "%1z%2")
-- z before voiceless consonant, e.g. ]; c and q already removed
text = rsub(text, "z(" .. wordsep_c .. "*)", "s%1")
-- Coda /s,z/
if portugal or style == "rio" then
-- In General Portugal and Rio de Janeiro; s/z before consonant (including across word boundaries) or end of utterance -> ʃ/ʒ;
-- but not word-initially (e.g. ]).
local sibilant = { = "ʃ", = "j"}
text = rsub(text, "()(##)", function(sz, after) return sibilant .. after end)
-- s/z are maintained word-initially. To implement this, insert TEMP1 directly before the s/z we want to preserve, then check for this
-- TEMP1 not being present when converting to shibilant, then remove TEMP1.
text = rsub(text, "(#)()", "%1" .. TEMP1 .. "%2")
text = rsub_repeatedly(text, "()()(" .. wordsep_c .. "*" .. C_NOT_H_OR_GLIDE .. ")",
function(before, sz, after) return before .. sibilant .. after end)
text = rsub(text, TEMP1, "")
end
text = rsub(text, "ç", "s")
text = rsub(text, "j", "ʒ")
-- Reduce identical sibilants, including across word boundaries.
text = rsub(text, "()(" .. wordsep_c .. "*)(%1)", "%2%1")
if style == "rio" then
-- Also reduce sibilant (], ] ]); not in Portugal, but in Portugal we later
-- generate two outputs in this case, either /ʃs/ and /ʒz/ (careful pronunciation) or /ʃ/ and /ʒ/ (natural
-- pronunciation). Note that the reduction of /ʃs/ to /ʃ/ in Portugal is different from the reduction of the
-- same to /s/ in Brazil (though it shouldn't be)
text = rsub(text, "ʃ(" .. wordsep_c .. "*s)", "%1")
text = rsub(text, "ʒ(" .. wordsep_c .. "*z)", "%1")
end
-- N/M from double n/m
text = rsub(text, "", { = "n", = "m"})
if portugal then
--Drop unneeded 'h' in 'rh' respellings for European Portuguese
text = rsub(text, "rh","r")
if style=="spt" then
--Paragoge of i in word-final r/l in Southern Portugal
text = rsub(text, "ˈr#", "ˈ.ri#")
text = rsub(text, "ˈl#", "ˈ.li#")
end
end
-- r
-- Double rr -> ʁ already handled above.
-- Initial r or l/n/s/z + r -> strong r (ʁ).
text = rsub(text, "(%.?)r", "%1ʁ")
if brazil then
-- Word-final r before vowel in verbs is /(ɾ)/.
text = rsub(text, "(ˈ)r(#" .. wordsep_c .. "*h?" .. V .. ")", "%1(ɾ)%2")
-- Coda r before vowel is /ɾ/.
text = rsub(text, "r(" .. wordsep_c .. "*h?" .. V .. ")", "ɾ%1")
end
-- Word-final r in Brazil in verbs (not ]) is usually dropped. Use a spelling like 'marh' for ]
-- to prevent this. Make sure not to do this before -mente/-zinho (], ]).
if brazil then
text = rsub(text, "(ˈ)r(#)",
"%1(" .. (style == "sp" and "ɾ" or style == "sbr" and "ɻ" or "ʁ") .. ")%2")
if style ~= "sp" then
-- Coda r in Southern Brazil is , otherwise outside of São Paulo is /ʁ/.
text = rsub(text, "r(" .. C .. "*)", (style == "sbr" and "ɻ" or "ʁ") .. "%1")
end
end
-- All other r -> /ɾ/.
text = rsub(text, "r", "ɾ")
if brazil and phonetic then
-- "Strong" ʁ before voiced consonant is in much of Brazil, in Rio. Use R as a temporary symbol.
text = rsub(text, "ʁ(" .. wordsep_c .. "*)", style == "rio" and "R%1" or "ɦ%1")
-- Other "strong" ʁ is in much of Brazil, in Rio. Use H because later we remove all <h>.
text = rsub(text, "ʁ", style == "rio" and "χ" or "H")
text = rsub(text, "R", "ʁ")
end
-- Nasal Diphthong <ẽi> and <ɛi>
if portugal then
-- In Portugal, always lower e -> ɐ before j when nasalized.
text = rsub(text, "e(" .. accent_c .. TILDE .. "*%.?y)", "ɐ%1")
-- In Portugal, lower ɛ -> e before i in <ɛi> (the open pronunciation is dated)
text = rsub(text, "ɛ(" .. accent_c .. "*i)", "e%1")
end
-- Diphthong <ei> (if unchanged, it shall yield /ej/; this should be the case for African pronunciations)
if brazil then
-- In Brazil, add optional /j/ in <eir>, <eij>, <eig> and <eix> (as in ], ], ] and
-- ]).
text = rsub(text, "(e" .. accent_c .. "*)i(%.)", "%1(j)%2")
-- ] and ]).] -- This was added by an IP, see
-- ]; this seems non-standard to me. If we are to include it, it should
-- not be done this way, but as two separate outputs with the one lacking the /j/ marked with a qualifier such
-- as "non-standard"; compare the way the initial enC- is handled (near the end of export.IPA()), where there
-- are two outputs, with /ẽC-/ marked as "careful pronunciation" and /ĩC-/ marked as "natural pronunciation".
-- (Benwing2)
-- text = rsub(text, "(a" .. accent_c .. "*)i(%.ʃ)", "%1(j)%2")
elseif style == "spt" then
-- In Southern Portugal, <ei> monophthongizes to <e>
text = rsub(text, "(e" .. accent_c .. "*)i", "%1")
elseif style == "gpt" then
-- In general Portugal, lower e -> ɐ before i in <ei>.
text = rsub(text, "e(" .. accent_c .. "*i)", "ɐ%1")
-- In general Portugal, lower e -> ɐ before j
text = rsub(text, "e(" .. accent_c .. "*%.?y)", "ɐ%1")
-- In general Portugal, lower e -> ɐ(j) before other palatals.
text = rsub(text, "e(" .. stress_c .. "*)(%.?(" .. V .. "))", "ɐ%1(j)%2")
end
-- Diphthong <ou> (if unchanged, it shall yield /o(w)/)
if style == "spt" then
-- In Southern Portugal, <ou> always monophthongizes to <o>
text = rsub(text, "(o" .. accent_c .. "*)u", "%1")
elseif style == "npt" then
-- In Northern Portugal (and Galicia), <ou> is kept
text = rsub(text, "(o" .. accent_c .. "*)u", "%1w")
end
-- Else optional /w/ in <ou>
text = rsub(text, "(o" .. accent_c .. "*)u", "%1(w)")
-- Stop consonants.
if brazil then
-- Palatalize t/d + i/y -> affricates in Brazil.
text = rsub(text, "()(" .. word_or_component_sep_c .. "*)",
function(td, high_vocalic) return palatalize_td .. high_vocalic end)
elseif portugal and phonetic then
-- Fricativize voiced stops in Portugal when not utterance-initial or after a nasal; also not in /ld/.
-- Easiest way to do this is to convert all voiced stops to fricative and then back to stop in the
-- appropriate contexts.
local fricativize_stop = { = "β", = "ð", = "ɣ" }
local occlude_fricative = { = "b", = "d", = "g" }
text = rsub(text, "", fricativize_stop)
text = rsub(text, "##()", function(bdg) return "##" .. occlude_fricative end)
text = rsub(text, "(" .. TILDE .. wordsep_c .. "*)()", function(before, bdg) return before .. occlude_fricative end)
text = rsub(text, "(l" .. wordsep_c .. "*)ð", "%1d")
end
-- Glides and l. ou -> o(w) must precede coda l -> w in Brazil, because <ol> /ow/ cannot be reduced to /o/.
-- ou -> o(w) before conversion of remaining diphthongs to vowel-glide combinations so <ow> can be used to
-- indicate a non-reducible glide.
-- Handle coda /l/.
if brazil then
-- Coda l -> /w/ in Brazil.
text = rsub(text, "l(" .. C .. "*)", "w%1")
elseif phonetic then
-- Coda l -> in Portugal (and Rio Grande do Sul, according to Cunha-Cintra)
text = rsub(text, "l(" .. C .. "*)", "ɫ%1")
end
text = rsub(text, "y", "j")
if brazil then
text = rsub(text, "Y", "(j)") -- epenthesized in ], ], etc.
else
-- 'I' in Portugal represents word-initial (i) before sC, except after /i/ (e.g. ]), in which
-- case it is elided. In the latter case, we need to elide the word/component separators, otherwise we end up
-- with an extra syllable divider: /ˌɐ̃.ti.ʃˈta.ti.ku/ instead of correct /ˌɐ̃.tiʃˈta.ti.ku/.
text = rsub(text, "(i" .. accent_c .. "*)" .. word_or_component_sep_c .. "*#I", "%1")
text = rsub(text, "I", "(i)")
end
local vowel_termination_to_glide = brazil and phonetic and
{ = "ɪ̯", = "ɪ̯", = "ʊ̯", = "ʊ̯"} or
{ = "j", = "j", = "w", = "w"}
-- i/u as second part of diphthong becomes glide.
text = rsub(text, "(" .. V .. accent_c .. "*" .. "%(?)()",
function(v1, v2) return v1 .. vowel_termination_to_glide end)
-- nh
if brazil and phonetic and style ~= "sbr" then
-- ] pronounced ; nasalization of previous vowel handled above. But initial nh- e.g. ],
-- ], ] is . I *think* this doesn't happen in South Brazil. We do have the phonetic
-- representation given for ], but this is the only such case and may be a mistake.
text = rsub(text, "()ɲ", "%1j" .. TILDE)
end
if portugal then
-- Suppress final -ɨ before a vowel
text = rsub(text, "ɨ##(" .. V .. ")", "‿%1")
--Make optional utterance-finally
-- text = rsub(text, "ɨ##", "(ɨ)##")
-- (ɨ) after l when suppressed should convert to coda ɫ, so split it later into two pronuns.
-- text = rsub(text, "l%(ɨ%)##", "L##")
-- (ɨ) after r when suppressed should also convert to coda
-- text = rsub(text, "ɾ%(ɨ%)##", "R##")
end
text = rsub(text, "g", "ɡ") -- U+0261 LATIN SMALL LETTER SCRIPT G
text = rsub(text, "", { = "t͡ʃ", = "d͡ʒ"})
text = rsub(text, "tʃ", "t͡ʃ")
text = rsub(text, "dʒ", "d͡ʒ")
text = rsub(text, "h", "")
text = rsub(text, "H", "h")
return text
end
-- Generate the IPA for a single term respelling `text` in the specified `style` ('gbr', 'rio', etc.; see
-- all_style_descs above). Return value is a list of objects of the following form:
-- { phonemic = STRING, phonetic = STRING, qualifiers = {STRING, ...} }
-- Note that the returned qualifiers are only those generated automatically as a result of certain characteristics of
-- the respelling, e.g. in Brazil initial em-/en- + consonant has two outputs, one labeled "careful pronunciation" and
-- the other "natural pronunciation". User-specified qualifiers are added at the end by the caller of IPA(), and
-- prepended to the auto-generated qualifiers.
function export.IPA(text, style)
local brazil = m_table.contains(export.all_style_groups.br, style)
local portugal = m_table.contains(export.all_style_groups.pt, style)
local origtext = text
local function err(msg)
error(msg .. ": " .. origtext)
end
text = ulower(text)
-- decompose everything but ç and ü
text = mw.ustring.toNFD(text)
text = rsub(text, ".", {
= "ç",
= "ü",
})
text = reorder_accents(text, err)
-- convert commas and en/en dashes to IPA foot boundaries
text = rsub(text, "%s*%s*", " | ")
-- question mark or exclamation point in the middle of a sentence -> IPA foot boundary
text = rsub(text, "()%s*%s*()", "%1 | %2")
-- canonicalize multiple spaces and remove leading and trailing spaces
local function canon_spaces(text)
text = rsub(text, "%s+", " ")
text = rsub(text, "^ ", "")
text = rsub(text, " $", "")
return text
end
text = canon_spaces(text)
local words = m_strutils.capturing_split(text, "(+)")
local function word_is_prefix(i)
-- Check for prefixes, either a final prefix (followed by "-" separator, then a blank word, then no more
-- words) or a non-final prefix (followed by "- " separator).
return i == #words - 2 and words == "-" and words == "" or i < #words and words == "- "
end
for i = 1, #words, 2 do
local word = words
-- Make prefixes unstressed with vowel reduction unless they have an explicit stress marker;
-- likewise for certain monosyllabic words (e.g. ], ], ], etc.; also ], ], etc.
-- in Portugal) without stress marks.
if word_is_prefix(i) and not rfind(words, accent_c) or unstressed_words or
portugal and unstressed_full_vowel_words_brazil then
-- add DOTOVER to the last vowel not the first one, or we will mess up 'que' by
-- adding the DOTOVER after the 'u'
word = rsub(word, "^(.*" .. V .. quality_c .. "*)", "%1" .. DOTOVER)
end
-- Make certain monosyllabic words (e.g. ], ]; also ], ], etc. in Brazil)
-- without stress marks be unstressed without vowel reduction.
if unstressed_full_vowel_words or brazil and unstressed_full_vowel_words_brazil then
-- add DOTUNDER to the first vowel not the last one, or we will mess up 'meu' by
-- adding the DOTUNDER after the 'u'; add after a quality marker for à, às
word = rsub(word, "^(.-" .. V .. quality_c .. "*)", "%1" .. DOTUNDER)
end
-- Some unstressed words need special pronunciation.
word = unstressed_pronunciation_substitution or word
words = word
end
text = table.concat(words)
-- Now eliminate word-final question mark and exclamation point (converted to foot boundary above when word-medial).
text = rsub(text, "", "")
-- Apostrophe becomes tie (e.g. in ]).
text = rsub(text, "'", "‿")
-- User-specified # as in i# (= i. or y) and u# (= u. or w) becomes TEMP1 so we can add # for word boundaries.
text = rsub(text, "#", TEMP1)
-- Put # at word beginning and end and double ## at text/foot boundary beginning/end.
text = rsub(text, " | ", "# | #")
text = "##" .. rsub(text, " ", "# #") .. "##"
-- Eliminate hyphens indicating prefixes/suffixes; but preserve a marker indicating prefixes, so we can later
-- convert primary to secondary stress.
text = rsub(text, "(" .. V .. charsep_c .. "*)(%-#)", "%1" .. PSEUDOCONS .. "%2")
text = rsub(text, "%-#", "#" .. PREFIX_MARKER)
text = rsub(text, "#%-(" .. V .. ")", "#" .. PSEUDOCONS .. "%1")
text = rsub(text, "#%-", "#")
local variants
-- Map over each element in `variants`. If `from` is found in the element, replace the element with two elements, one
-- obtained by replacing `from` with `to1` and the other by replacing `from` with `to2`. If `to2` is nil, only one
-- element replaces the original element.
local function flatmap_and_sub_pre(from, to1, qual1, to2, qual2)
variants = flatmap(variants, function(item)
if rfind(item.respelling, from) then
local retval = {
{
respelling = rsub(item.respelling, from, to1),
qualifiers = combine_qualifiers(item.qualifiers, qual1),
}
}
if to2 then
table.insert(retval,
{
respelling = rsub(item.respelling, from, to2),
qualifiers = combine_qualifiers(item.qualifiers, qual2),
}
)
end
return retval
else
return {item}
end
end)
end
if brazil then
-- Remove grave accents and macrons, which have special meaning only for Portugal. Do this before handling o^
-- and similar so we can write áutò^:... and have it correctly give 'autò-' in Portugal but 'áutu-,áuto-' in
-- Brazil.
text = rsub(text, "", "")
elseif portugal then
-- Convert grave accents and macrons to explicit dot-under + quality marker.
local grave_macron_to_quality = {
= AC,
= CFLEX,
}
text = rsub(text, "", function(acc) return grave_macron_to_quality .. DOTUNDER end)
-- ê*/ô* -> é/ó and é*/ó* -> ê/ô (reverse accents)
text = rsub(text, "()()%*", function(eo, acc)
return eo .. (acc == CFLEX and AC or CFLEX) end)
-- Treat vowel after des- as word-initial, as in Brazil.
text = rsub(text, "(" .. word_or_component_sep_c .. ")des%^+", "%1des++")
-- Remove i*, i^ and i^^ not followed by a vowel (i.e. Brazilian epenthetic i), but not i^ and i^^ followed or
-- preceded by a vowel (which has a totally different meaning, i.e. i or y in Brazil).
-- Also remove all remaining ^.
text = rsub(text, "i%^+(" .. V .. ")", "i%1")
text = rsub(text, "(" .. V .. ")i%^+", "%1i")
text = rsub(text, "i?+", "")
end
variants = {{respelling = text}}
if brazil then
-- Handle i^ and i^^ before a vowel = /i/ or /j/.
flatmap_and_sub_pre("i%^%^(" .. V .. ")", "y%1", nil, "i.%1", nil)
flatmap_and_sub_pre("i%^(" .. V .. ")", "i.%1", nil, "y%1", nil)
-- Handle i^ and i^^ after a vowel = /i/ or /j/; mostly useful for ui^
flatmap_and_sub_pre("(" .. V .. ")i%^%^", "%1y", nil, "%1.i", nil)
flatmap_and_sub_pre("(" .. V .. ")i%^", "%1.i", nil, "%1y", nil)
-- Handle i^ and i^^ not before a vowel = optional epenthetic /i/.
if style == "sbr" then
-- Epenthetic /i/ seems less common in South Brazil. Make i^^ not epenthesize (but still palatalize /t/ and /d/),
-- and i^ epenthesize but not as the first option.
flatmap_and_sub_pre("i%^%^(" .. NV_NOT_SPACING_CFLEX .. ")", "Ɨ%1", nil)
flatmap_and_sub_pre("i%^(" .. NV_NOT_SPACING_CFLEX .. ")", "Ɨ%1", nil, "I%1", nil)
else
flatmap_and_sub_pre("i%^%^(" .. NV_NOT_SPACING_CFLEX .. ")", "Ɨ%1", nil, "I%1", nil)
flatmap_and_sub_pre("i%^(" .. NV_NOT_SPACING_CFLEX .. ")", "I%1", nil, "Ɨ%1", nil)
end
-- Handle i* = epenthetic /i/.
flatmap_and_sub_pre("i%*", "I", nil)
-- Handle u^ and u^^ = /u/ or /w/.
flatmap_and_sub_pre("u%^%^", "w", nil, "u.", nil)
flatmap_and_sub_pre("u%^", "u.", nil, "w", nil)
if style == "sbr" then
-- The raised variant apparently does not occur in South Brazil.
flatmap_and_sub_pre("()%^+", "%1", nil)
else
-- Handle e^ and e^^ = /e/ or /i/; handle o^ and o^^ = /o/ or /u/.
-- Do e^ and o^ together so we get only two outputs, not four, if they cooccur.
-- Likewise for e^^ and o^^.
flatmap_and_sub_pre("()%^%^",
function(eo) return eo == "e" and "i" or "u" end, nil,
"%1", nil)
flatmap_and_sub_pre("()%^",
"%1", nil,
function(eo) return eo == "e" and "i" or "u" end, nil)
end
-- Handle ê*/ô*/é*/ó* = same as without asterisk.
flatmap_and_sub_pre("()%*", "%1", nil)
-- Handle des^ at beginning of word or component = des++ or dis++, and des^^ = opposite order. But apparently
-- not in South Brazil, where the raised variant doesn't occur.
if style == "sbr" then
flatmap_and_sub_pre("(" .. word_or_component_sep_c .. ")des%^+", "%1des++", nil)
else
flatmap_and_sub_pre("(" .. word_or_component_sep_c .. ")des%^%^", "%1dis++", nil, "%1des++", nil)
flatmap_and_sub_pre("(" .. word_or_component_sep_c .. ")des%^", "%1des++", nil, "%1dis++", nil)
end
for _, variant in ipairs(variants) do
if rfind(variant.respelling, "") then
err(("* or ^ remains after applying all known replacements involving these characters (result is '%s')")
:format(variant.respelling))
end
end
end
-- Replace i# and u# sequences (above we replaced # with TEMP1).
flatmap_and_sub_pre("i" .. TEMP1, "i.", nil, "y", {"faster pronunciation"})
flatmap_and_sub_pre("u" .. TEMP1, "u.", nil, "w", {"faster pronunciation"})
-- Given a single variant element representing a preprocessed respelling along with any qualifiers resulting from the
-- preprocessing, generate the phonemic and phonetic representations using one_term_ipa() and postprocess to get the
-- final IPA. The postprocessing is there in general to handle cases where a single respelling produces multiple
-- outputs, such as Brazil -io producing either /i.u/ or /iw/. Note that user-specified qualifiers are not yet present
-- at any stage of this IPA generation; they are added at the end by the caller of IPA().
local function call_one_term_ipa(variant)
local result = {{
phonemic = one_term_ipa(variant.respelling, style, false, err),
phonetic = one_term_ipa(variant.respelling, style, true, err),
qualifiers = variant.qualifiers,
}}
local function unpack_if_list(obj)
if type(obj) == "table" and #obj == 2 and obj then
return unpack(obj)
else
return obj, obj
end
end
-- Map over each element in `result`. If `from` is found in the element, replace the element with two elements, one
-- obtained by replacing `from` with `to1` in both the phonemic and phonetic representations of the existing element,
-- and the other similarly by replacing `from` with `to2` in both the phonemic and phonetic representations. If `to2`
-- is nil, only one element replaces the original element. `qual1`, if non-nil, is a list of qualifiers to be added to
-- the new element associated with `to1` (appended to any existing qualifiers). Similarly, `qual2` is a list of
-- qualifiers to be added to the new element associated with `to2`. Normally, `to1` and `to2` can be anything that can
-- be used as the replacement argument to the Lua gsub() function, i.e. a string, a function or a table. However, if
-- `to1` or `to2` is a two-element list, it is unpacked into two separate substitution objects, respectively for the
-- phonemic and phonetic representations of the element being substituted. This is used, for example, when handling
-- Ú resulting from stressed final '-io(s)', so different phonemic vs. phonetic replacements can be used (/w/ vs ).
local function flatmap_and_sub_post(from, to1, qual1, to2, qual2)
result = flatmap(result, function(item)
if rfind(item.phonemic, from) or rfind(item.phonetic, from) then
local to1_phonemic, to1_phonetic = unpack_if_list(to1)
local retval = {
{
phonemic = rsub(item.phonemic, from, to1_phonemic),
phonetic = rsub(item.phonetic, from, to1_phonetic),
qualifiers = combine_qualifiers(item.qualifiers, qual1),
}
}
if to2 then
local to2_phonemic, to2_phonetic = unpack_if_list(to2)
table.insert(retval,
{
phonemic = rsub(item.phonemic, from, to2_phonemic),
phonetic = rsub(item.phonetic, from, to2_phonetic),
qualifiers = combine_qualifiers(item.qualifiers, qual2),
}
)
end
return retval
else
return {item}
end
end)
end
if brazil then
-- Convert Ẽ from initial ] as a word by itself to either /ẽj̃/ and /ĩ/.
flatmap_and_sub_post("Ẽ" .. TILDE .. "#", "e" .. TILDE .. "j" .. TILDE .. "#", {"careful pronunciation"},
"i" .. TILDE .. "#", {"natural pronunciation"})
-- Convert Ẽ from initial em-/en- + consonant to either /ẽ/ and /ĩ/.
flatmap_and_sub_post("Ẽ", "e", {"careful pronunciation"}, "i", {"natural pronunciation"})
flatmap_and_sub_post("I", "i", nil, "e", nil)
-- Convert Ú resulting from stressed final '-io(s)'.
flatmap_and_sub_post("%.Ú", ".u", nil, {"w", "ʊ̯"}, nil)
elseif portugal then
flatmap_and_sub_post("ʃ(" .. wordsep_c .. "*)s",
"ʃ%1s", {"careful pronunciation"}, "%1ʃ", nil)
flatmap_and_sub_post("ʒ(" .. wordsep_c .. "*)z",
"ʒ%1z", {"careful pronunciation"}, "%1ʒ", nil)
flatmap_and_sub_post("Ɔ", "o", nil, "ɔ", nil)
-- Split (ɨ) after l or r into two pronuns, one with ɨ and the other without it (with one fewer syllables and coda).
--flatmap_and_sub_post("%.(-)L#", ".%1lɨ#", nil, {"%1l#", "%1ɫ#"}, nil)
--flatmap_and_sub_post("%.(-)R#", ".%1ɾɨ#", nil, {"%1ɾ#", "%1ɾ#"}, nil)
end
flatmap_and_sub_post("()%.",
function(iu) return iu == "Ì" and "i." or "u." end, nil,
function(iu) return iu == "Ì" and "j" or "w" end, {"faster pronunciation"})
return result
end
-- Final changes to the generated IPA to produce what's shown to the user. We used to do this at the end of
-- one_term_ipa() but the stuff below needs to happen after the expansion of Ì. and Ù. in Brazil to either i./u.
-- or j/w, because the latter transformation involves removing a syllable boundary, which will cause a stress mark
-- on the following syllable to retract to the beginning of the newly combined syllable. To avoid lots of hassle,
-- we postpone this stress mark movement till now.
local function finalize_ipa(text, phonetic)
-- Convert Brazil i/u in hiatus to ɪ/ʊ in the phonetic representation. This needs to happen after handling of
-- Ì. and Ù., which feeds this change.
if brazil and phonetic then
local phonetic_hiatus_iu_to_actual = { = "ɪ", = "ʊ"}
text = rsub(text, "()(%." .. V .. ")", function(iu, after) return phonetic_hiatus_iu_to_actual .. after end)
end
-- Stress marks and syllable dividers.
-- Component separators that aren't transparent to syllabification need to be made into syllable dividers.
text = rsub(text, non_syl_transp_component_sep_c, ".")
-- IPA stress marks in components followed by + should be removed.
text = rsub(text, ipa_stress_c .. "(*%+)", "%1")
-- Component separators that are transparent to syllabification need to be removed now, before moving IPA stress marks
-- to the beginning of the syllable, so they don't interfere in this process.
text = rsub(text, syl_transp_component_sep_c .. "#?", "")
-- Move IPA stress marks to the beginning of the syllable.
text = rsub_repeatedly(text, "()(*)(" .. ipa_stress_c .. ")", "%1%3%2")
-- Suppress syllable divider before IPA stress indicator.
text = rsub(text, "%.(#?" .. ipa_stress_c .. ")", "%1")
-- Make all primary stresses but the last one in a given word be secondary. May be fed by the first rule above.
text = rsub_repeatedly(text, "ˈ(+)ˈ", "ˌ%1ˈ")
-- Make primary stresses in prefixes become secondary.
text = rsub_repeatedly(text, "ˈ(*#" .. PREFIX_MARKER .. ")", "ˌ%1")
-- Remove # symbols at word/text boundaries, as well as _ (which forces separate interpretation), pseudo-consonant
-- markers (at edges of some prefixes/suffixes), and prefix markers, and recompose.
text = rsub(text, "", "")
text = mw.ustring.toNFC(text)
return text
end
variants = flatmap(variants, call_one_term_ipa)
for i, variant in ipairs(variants) do
variants.phonemic = finalize_ipa(variants.phonemic, false)
variants.phonetic = finalize_ipa(variants.phonetic, true)
end
return variants
end
-- For bot usage; {{#invoke:pt-pronunc|IPA_json|SPELLING|style=STYLE}}
-- where
--
-- 1. SPELLING is the word or respelling to generate pronunciation for;
-- 2. required parameter style= indicates the pronunciation style to generate
-- (e.g. "rio" for Rio/Carioca pronunciation, "lisbon" for Lisbon pronunciation;
-- see the comment above export.IPA() above for the full list);
-- 3. phonetic=1 specifies to generate the phonetic rather than phonemic pronunciation;
function export.IPA_json(frame)
local iparams = {
= {},
= {required = true},
}
local iargs = require("Module:parameters").process(frame.args, iparams)
local pronuns = export.IPA(iargs, iargs.style)
return require("Module:JSON").toJSON(pronuns)
end
-- "Canonicalize" a single respelling (after splitting multiple respellings on comma and parsing off inline
-- modifiers). This currently handles '+' and substitution notation.
function export.canonicalize_respelling(text, pagename)
if not text or text == "+" then
text = pagename
end
-- No such substitutions currently.
-- text = rsub(text, "%)%]", function(sound)
-- return explicit_sound_to_substitution
-- end)
-- Implement substitution notation.
if rfind(text, "^%$") then
local subs = rsplit(rmatch(text, "^%$"), ";")
text = pagename
for _, sub in ipairs(subs) do
local fromto = rsplit(sub, ":")
if #fromto < 2 then
error("Bad substitution spec " .. sub .. " in {{pt-IPA}}, should have a colon in it")
end
local from, to
if #fromto == 2 then
from, to = fromto, fromto
else
from = fromto
table.remove(fromto, 1)
to = table.concat(fromto, ":")
end
local newtext = text
if rfind(from, "^%^") then
-- whole-word match
from = rmatch(from, "^%^(.*)$")
newtext = rsub(text, "%f" .. require("Module:string utilities").pattern_escape(from) .. "%f", to)
else
newtext = rsub(text, require("Module:string utilities").pattern_escape(from), to)
end
if from ~= to and newtext == text then
error("Substitution spec " .. sub .. " didn't match respelling '" .. text .. "'")
end
text = newtext
end
end
return text
end
function export.express_styles(inputs, args_style, pagename)
local pronuns_by_style = {}
local expressed_styles = {}
local function dostyle(style)
pronuns_by_style = {}
for _, val in ipairs(inputs.terms) do
local respelling = val.term
respelling = export.canonicalize_respelling(respelling, pagename)
local refs
if #val.ref == 0 then
refs = nil
else
refs = {}
for _, refspec in ipairs(val.ref) do
local this_refs = require("Module:references").parse_references(refspec)
for _, this_ref in ipairs(this_refs) do
table.insert(refs, this_ref)
end
end
end
local pronuns = export.IPA(respelling, style)
for _, pronun in ipairs(pronuns) do
local qualifiers = m_table.deepcopy(val.q)
if pronun.qualifiers then
for _, qual in ipairs(pronun.qualifiers) do
m_table.insertIfNot(qualifiers, qual)
end
end
pronun.qualifiers = #qualifiers > 0 and qualifiers or nil
pronun.refs = refs
m_table.insertIfNot(pronuns_by_style, pronun)
end
end
end
local function all_available(styles)
local available_styles = {}
for _, style in ipairs(styles) do
if pronuns_by_style then
table.insert(available_styles, style)
end
end
return available_styles
end
local function express_style(styles, indent)
local hidden_tag, tag
indent = indent or 1
if type(styles) == "string" then
styles = {styles}
tag = export.all_style_descs]
hidden_tag = export.all_style_descs]]
else
tag = false
hidden_tag = false
end
styles = all_available(styles)
if #styles == 0 then
return
end
local style = styles
-- If style specified, make sure it matches the requested style.
local style_matches
if not args_style then
style_matches = true
else
local or_styles = rsplit(args_style, "%s*,%s*")
for _, or_style in ipairs(or_styles) do
local and_styles = rsplit(or_style, "%s*%+%s*")
local and_matches = true
for _, and_style in ipairs(and_styles) do
local negate
if and_style:find("^%-") then
and_style = and_style:gsub("^%-", "")
negate = true
end
local this_style_matches = false
for _, part in ipairs(styles) do
if part == and_style then
this_style_matches = true
break
end
end
if negate then
this_style_matches = not this_style_matches
end
if not this_style_matches then
and_matches = false
end
end
if and_matches then
style_matches = true
break
end
end
end
if not style_matches then
return
end
local new_style = {
tag = tag,
represented_styles = styles,
pronuns = pronuns_by_style,
indent = indent,
bullets = inputs.bullets,
pre = inputs.pre,
post = inputs.post,
}
for _, hidden_tag_style in ipairs(expressed_styles) do
if hidden_tag_style.tag == hidden_tag then
table.insert(hidden_tag_style.styles, new_style)
return
end
end
table.insert(expressed_styles, {
tag = hidden_tag,
styles = {new_style},
})
end
for style, _ in pairs(inputs) do
dostyle(style)
end
local function diff(style1, style2)
if not pronuns_by_style or not pronuns_by_style then
return true
end
return not m_table.deepEquals(pronuns_by_style, pronuns_by_style)
end
local gbr_sp_different = diff("gbr", "sp")
local gbr_rio_different = diff("gbr", "rio")
local gbr_sbr_different = diff("gbr", "sbr")
local gpt_cpt_different = diff("gpt", "cpt")
local gpt_spt_different = diff("gpt", "spt")
local gpt_npt_different = diff("gpt", "npt")
local gbr_gpt_different = diff("gbr", "gpt") -- general differences between BP and EP
if not gbr_sp_different and not gbr_rio_different and gbr_sbr_different and
not gpt_cpt_different and not gpt_spt_different and not gpt_npt_different and
not gbr_gpt_different then
-- All the same
express_style(export.all_styles)
else
-- Within Brazil
express_style("gbr")
if gbr_sp_different then
express_style("sp", 2)
end
if gbr_rio_different then
express_style("rio", 2)
end
if gbr_sbr_different then
express_style("sbr", 2)
end
-- Within Portugal
express_style("gpt")
if gpt_npt_different then
express_style("npt", 2)
end
if gpt_cpt_different then
express_style("cpt", 2)
end
if gpt_spt_different then
express_style("spt", 2)
end
end
return expressed_styles
end
function export.show(frame)
-- Create parameter specs
local params = {
= {}, -- this replaces style group 'all'
= {},
= {},
}
for group, _ in pairs(export.all_style_groups) do
if group ~= "all" then
params = {}
end
end
for _, style in ipairs(export.all_styles) do
params = {}
end
-- Parse arguments
local parargs = frame:getParent().args
local args = require("Module:parameters").process(parargs, params)
local pagename = args.pagename or mw.title.getCurrentTitle().subpageText
-- Set inputs
local inputs = {}
-- If 1= specified, do all styles.
if args then
for _, style in ipairs(export.all_styles) do
inputs = args
end
end
-- Then do remaining style groups other than 'all', overriding 1= if given.
for group, styles in pairs(export.all_style_groups) do
if group ~= "all" and args then
for _, style in ipairs(styles) do
inputs = args
end
end
end
-- Then do individual style settings.
for _, style in ipairs(export.all_styles) do
if args then
inputs = args
end
end
-- If no inputs given, set all styles based on current pagename.
if not next(inputs) then
for _, style in ipairs(export.all_styles) do
inputs = "+"
end
end
-- Parse the arguments.
local put
for style, input in pairs(inputs) do
if input:find("") then
local function parse_err(msg)
error(msg .. ": " .. style .. "=" .. input)
end
if not put then
put = require("Module:parse utilities")
end
-- We don't want to split off a comma followed by a space, as in ], so replace
-- comma+space with a special character that we later undo.
input = rsub(input, ", ", TEMP1)
-- Parse balanced segment runs involving either (substitution notation) or <...> (inline modifiers). We do this
-- because we don't want commas inside of square or angle brackets to count as respelling delimiters. However, we
-- need to rejoin square-bracketed segments with nearby ones after splitting alternating runs on comma. For example,
-- if we are given "aa<q:learned>,<q:nonstandard>", after calling
-- parse_multi_delimiter_balanced_segment_run() we get the following output:
--
-- {"a", "", "a", "<q:learned>", ",", "", "", "<q:nonstandard>", ""}
--
-- After calling split_alternating_runs(), we get the following:
--
-- {{"a", "", "a", "<q:learned>", ""}, {"", "", "", "<q:nonstandard>", ""}}
--
-- We need to rejoin stuff on either side of the square-bracketed portions.
local segments = put.parse_multi_delimiter_balanced_segment_run(input, {{"<", ">"}, {""}})
-- Not with spaces around the comma; see above for why we don't want to split off comma followed by space.
local comma_separated_groups = put.split_alternating_runs(segments, ",")
local parsed = {terms = {}}
for i, group in ipairs(comma_separated_groups) do
-- Rejoin bracketed segments with nearby ones, as described above.
local j = 2
while j <= #group do
if group:find("^%[") then
group = group .. group .. group
table.remove(group, j)
table.remove(group, j)
else
j = j + 2
end
end
for j, segment in ipairs(group) do
group = rsub(segment, TEMP1, ", ")
end
local term = {term = group, ref = {}, q = {}}
for j = 2, #group - 1, 2 do
if group ~= "" then
parse_err("Extraneous text '" .. group .. "' after modifier")
end
local modtext = group:match("^<(.*)>$")
if not modtext then
parse_err("Internal error: Modifier '" .. group .. "' isn't surrounded by angle brackets")
end
local prefix, arg = modtext:match("^(+):(.*)$")
if not prefix then
parse_err("Modifier " .. group .. " lacks a prefix, should begin with one of " ..
"'pre:', 'post:', 'ref:', 'bullets:' or 'q:'")
end
if prefix == "ref" or prefix == "q" then
table.insert(term, arg)
elseif prefix == "pre" or prefix == "post" or prefix == "bullets" then
if i < #comma_separated_groups then
parse_err("Modifier '" .. prefix .. "' should occur after the last comma-separated term")
end
if parsed then
parse_err("Modifier '" .. prefix .. "' occurs twice, second occurrence " .. group)
end
if prefix == "bullets" then
if not arg:find("^+$") then
parse_err("Modifier 'bullets' should have a number as argument")
end
parsed.bullets = tonumber(arg)
else
parsed = arg
end
else
parse_err("Unrecognized prefix '" .. prefix .. "' in modifier " .. group
.. ", should be one of 'pre', 'post', 'ref', 'bullets' or 'q'")
end
end
table.insert(parsed.terms, term)
end
if not parsed.bullets then
parsed.bullets = 1
end
inputs = parsed
else
local terms = {}
-- We don't want to split on comma+space, which should become a foot boundary as in
-- ].
local subbed_input = rsub(input, ", ", TEMP1)
for _, term in ipairs(rsplit(subbed_input, ",")) do
term = rsub(term, TEMP1, ", ")
table.insert(terms, {term = term, ref = {}, q = {}})
end
inputs = {
terms = terms,
bullets = 1,
}
end
end
local expressed_styles = export.express_styles(inputs, args.style, pagename)
local lines = {}
local function format_style(tag, expressed_style, is_first)
local pronunciations = {}
local formatted_pronuns = {}
-- Loop through each pronunciation. For each one, add the phonemic and (if different) phonetic versions to
-- `pronunciations`, for formatting by ], and also create an approximation of the formatted
-- version so that we can compute the appropriate width of the HTML switcher div box that holds the different
-- per-dialect variants.
for i, pronun in ipairs(expressed_style.pronuns) do
table.insert(pronunciations, {
pron = "/" .. pronun.phonemic .. "/",
qualifiers = pronun.qualifiers,
separator = i > 1 and ", " or nil,
})
local formatted_phonemic = "/" .. pronun.phonemic .. "/"
if pronun.qualifiers then
formatted_phonemic = "(" .. table.concat(pronun.qualifiers, ", ") .. ") " .. formatted_phonemic
end
if i > 1 then
formatted_phonemic = ", " .. formatted_phonemic
end
table.insert(formatted_pronuns, formatted_phonemic)
-- Check if phonetic and phonemic are the same. If so, we skip displaying the phonetic version; but in this
-- case, we need to attach any references to the phonemic version.
if pronun.phonetic == pronun.phonemic then
pronunciations.refs = pronun.refs
else
table.insert(pronunciations, {
pron = "",
refs = pronun.refs,
separator = " ",
})
local reftext = ""
if pronun.refs then
reftext = string.rep("", #pronun.refs)
end
table.insert(formatted_pronuns, " " .. reftext)
end
end
-- Number of bullets: When indent = 1, we want the number of bullets given by `expressed_style.bullets`,
-- and when indent = 2, we want `expressed_style.bullets + 1`, hence we subtract 1.
local bullet = string.rep("*", expressed_style.bullets + expressed_style.indent - 1) .. " "
-- Here we construct the formatted line in `formatted`, and also try to construct the equivalent without HTML
-- and wiki markup in `formatted_for_len`, so we can compute the approximate textual length for use in sizing
-- the toggle box with the "more" button on the right.
local pre = is_first and expressed_style.pre and expressed_style.pre .. " " or ""
local pre_for_len = pre .. (tag and "(" .. tag .. ") " or "")
pre = pre .. (tag and m_qual.format_qualifier(tag) .. " " or "")
local post = is_first and (expressed_style.post and " " .. expressed_style.post or "") or ""
local formatted = bullet .. pre .. m_IPA.format_IPA_full { lang = lang, items = pronunciations, separator = "" } .. post
local formatted_for_len = bullet .. pre .. "IPA(key): " .. table.concat(formatted_pronuns) .. post
return formatted, formatted_for_len
end
for i, style_group in ipairs(expressed_styles) do
if #style_group.styles == 1 then
style_group.formatted, style_group.formatted_for_len =
format_style(style_group.styles.tag, style_group.styles, i == 1)
else
style_group.formatted, style_group.formatted_for_len =
format_style(style_group.tag, style_group.styles, i == 1)
for j, style in ipairs(style_group.styles) do
style.formatted, style.formatted_for_len =
format_style(style.tag, style, i == 1 and j == 1)
end
end
end
-- Remove any HTML from the formatted text, since it doesn't contribute to the textual length, and return the
-- resulting length in characters.
local function textual_len(text)
text = rsub(text, "<.->", "")
return ulen(text)
end
local maxlen = 0
for i, style_group in ipairs(expressed_styles) do
local this_len = textual_len(style_group.formatted_for_len)
if #style_group.styles > 1 then
for _, style in ipairs(style_group.styles) do
this_len = math.max(this_len, textual_len(style.formatted_for_len))
end
end
maxlen = math.max(maxlen, this_len)
end
for i, style_group in ipairs(expressed_styles) do
if #style_group.styles == 1 then
table.insert(lines, "<div>\n" .. style_group.formatted .. "</div>")
else
local inline = '\n<div class="vsShow" style="display:none">\n' .. style_group.formatted .. "</div>"
local full_prons = {}
for _, style in ipairs(style_group.styles) do
table.insert(full_prons, style.formatted)
end
local full = '\n<div class="vsHide">\n' .. table.concat(full_prons, "\n") .. "</div>"
local em_length = math.floor(maxlen * 0.68) -- from ]
table.insert(lines, '<div class="vsSwitcher" data-toggle-category="pronunciations" style="width: ' .. em_length .. 'em; max-width:100%;"><span class="vsToggleElement" style="float: right;"> </span>' .. inline .. full .. "</div>")
end
end
-- major hack to get bullets working on the next line
return table.concat(lines, "\n") .. "\n<span></span>"
end
return export