Module:th
Appearance
Documentation for this module may be created at Module:th/doc
local export = {}
local gsub = mw.ustring.gsub
local sub = mw.ustring.sub
local m_translit = require("Module:th-translit")
function export.new(frame)
local title = mw.title.getCurrentTitle().text
local args = frame:getParent().args
local phonSpell = args[1] or title
local pos = args[2] or ""
local def = args[3] or "{{rfdef|lang=th}}"
local pos2 = args[4] or (args[5] and "" or false)
local def2 = args[5] or "{{rfdef|lang=th}}"
local pos3 = args[6] or (args[7] and "" or false)
local def3 = args[7] or "{{rfdef|lang=th}}"
local etym = args["e"] or false
local head = args["head"] or false
if phonSpell ~= title and gsub(phonSpell, "[%- ]", "") == title and not head then
head = "[[" .. gsub(gsub(phonSpell, "%-", ""), " ", "]][[") .. "]]"
end
local result = ""
local function genTitle(text)
local pos_title = {
[""] = "Noun", ["n"] = "Noun", ["pn"] = "Proper noun", ["propn"] = "Proper noun", ["pron"] = "Pronoun",
["v"] = "Verb", ["a"] = "Adjective", ["adj"] = "Adjective", ["adv"] = "Adverb",
["prep"] = "Preposition", ["postp"] = "Postposition", ["conj"] = "Conjunction",
["part"] = "Particle", ["suf"] = "Suffix",
["prov"] = "Proverb", ["id"] = "Idiom", ["ph"] = "Phrase", ["intj"] = "Interjection", ["interj"] = "Interjection",
["cl"] = "Classifier", ["cls"] = "Classifier", ["num"] = "Numeral", ["abb"] = "Abbreviation", ["deter"] = "Determiner"
};
return pos_title[text] or mw.ustring.upper(sub(text, 1, 1)) .. sub(text, 2, -1)
end
local function genHead(text)
local pos_head = {
[""] = "noun", ["n"] = "noun", ["pn"] = "proper noun", ["propn"] = "proper noun", ["v"] = "verb", ["a"] = "adj",
["postp"] = "post", ["conj"] = "con", ["part"] = "particle", ["pron"] = "pronoun",
["prov"] = "proverb", ["id"] = "idiom", ["ph"] = "phrase", ["intj"] = "interj",
["abb"] = "abbr", ["cl"] = "classifier", ["deter"] = "det"
};
return pos_head[text] or text
end
local function other(class, title, args)
local code = ""
if args[class] then
code = code .. "\n\n===" .. title .. "===\n* {{th-l|" .. args[class] .. "}}"
if args[class .. "2"] then
code = code .. "\n* {{th-l|" .. args[class .. "2"] .. "}}"
if args[class .. "3"] then
code = code .. "\n* {{th-l|" .. args[class .. "3"] .. "}}"
if args[class .. "4"] then
code = code .. "\n* {{th-l|" .. args[class .. "4"] .. "}}"
end
end
end
end
return code
end
result = result .. "==Thai=="
if args["wp"] then result = result .. "\n{{wikipedia|lang=th}}" end
result = result .. other("alt", "Alternative forms", args)
if etym then result = result .. "\n\n===Etymology===\n" .. etym end
result = result .. "\n\n===Pronunciation===\n{{th-pron" .. ((phonSpell ~= title and phonSpell ~= "") and ("|" .. gsub(phonSpell, ",", "|")) or "") .. "}}"
result = result .. "\n\n===" .. genTitle(pos) .. "===\n{{th-" .. genHead(pos) .. (head and ("|head=" .. head) or "") .. "}}\n\n# " .. def
result = result .. other("syn", "=Synonyms=", args)
result = result .. other("ant", "=Antonyms=", args)
result = result .. other("der", "=Derived terms=", args)
result = result .. other("also", "=See also=", args)
if pos2 then
result = result .. "\n\n===" .. genTitle(pos2) .. "===\n{{th-" .. genHead(pos2) .. (head and ("|head=" .. head) or "") .. "}}\n\n# " .. def2
end
if pos3 then
result = result .. "\n\n===" .. genTitle(pos3) .. "===\n{{th-" .. genHead(pos3) .. (head and ("|head=" .. head) or "") .. "}}\n\n# " .. def3
end
return result
end
function export.getTranslit(lemmas, phonSpell)
if not phonSpell then
phonSpell = lemmas
for lemma in mw.ustring.gmatch(lemmas, "[ก-๛ ]+") do
if mw.title.new(lemma).exists then
content = mw.title.new(lemma):getContent()
template = mw.ustring.match(content, "{{th%-pron[^}]*}}")
if template ~= "" then
template = mw.ustring.match(content, "{{th%-pron|([^}]+)}}")
phonSpell = mw.ustring.gsub(phonSpell, lemma, template and mw.text.split(template, "|")[1] or lemma)
end
end
end
end
transcription = m_translit.tr(phonSpell, "th", "Thai")
transcription = transcription or nil
return transcription
end
function export.link(frame)
local args = frame:getParent().args
local lemma = args[1] or error("Page to be linked to has not been specified. Please pass parameter 1 to the module invocation.")
local phonSpell = args["p"] or false
local gloss = args[2] or args["gloss"] or ""
local transcription = export.getTranslit(lemma, phonSpell)
return frame:expandTemplate{ title = "l", args = {"th", lemma, nil, gloss = gloss, sc = "Thai", tr = transcription}}
end
function export.usex(frame)
local args = frame:getParent().args
local pagename = mw.title.getCurrentTitle().text
local text = {}
local example = args[1] or error("Example has not been specified. Please pass parameter 1 to the module invocation.")
local translation = args[2] or ""
local exSet, romSet = {}, {}
example = gsub(example, "%*", pagename) -- shorthand
example = gsub(example, "ฯ ", "ฯ ")
example = gsub(example, " ", "&")
example = gsub(example, "([^ก-๛{}%-]+)", " %1 ")
example = gsub(example, " +", " ")
example = gsub(example, "^ ", "")
example = gsub(example, " $", "")
example = gsub(example, "([^ ]+)([ &]*)ๆ", function(previous, space)
return previous .. (space == "" and " " or space) .. previous .. "ๆ" end)
local syllables = mw.text.split(example, " ", true)
for _, thaiWord in ipairs(syllables) do
local phonSpell, content, template = "", "", ""
if mw.ustring.match(thaiWord, "[ก-๛]") then
phonSpell = thaiWord
if mw.ustring.match(thaiWord, "ๆ$") then
phonSpell = sub(phonSpell, 1, -2)
thaiWord = "ๆ"
end
if mw.ustring.match(thaiWord, "[{}]") then
phonSpell = mw.ustring.match(phonSpell, "{([^}]+)}")
thaiWord = mw.ustring.match(thaiWord, "^[^{}]+")
else
if thaiWord ~= "ๆ" and mw.title.new(thaiWord).exists then
content = mw.title.new(thaiWord):getContent()
template = mw.ustring.match(content, "{{th%-pron[^}]*}}")
if template ~= "" then
template = mw.ustring.match(content, "{{th%-pron|([^}]+)}}")
phonSpell = template and mw.text.split(template, "|")[1] or thaiWord
else
phonSpell = thaiWord
end
end
end
table.insert(exSet, "[[" .. thaiWord .. "]]")
if thaiWord == pagename then
thaiWord = "<b>" .. thaiWord .. "</b>"
phonSpell = "<b>" .. phonSpell .. "</b>"
end
table.insert(romSet, m_translit.tr(phonSpell, "th", "Thai"))
else
table.insert(exSet, thaiWord)
table.insert(romSet, m_translit.tr(thaiWord, "th", "Thai"))
end
end
example = table.concat(exSet)
example = gsub(example, "[- ]", "")
example = gsub(example, "&", " ")
example = gsub(example, "ฯ", "ฯ ")
translit = table.concat(romSet, " ")
translit = gsub(translit, " & ", ", ")
translit = gsub(translit, "&", " ")
translit = gsub(translit, " :", ":")
table.insert(text, ('<span lang="th" class="Thai">%s</span>'):format(example))
if mw.ustring.match(example, "[\.\?!।]") or mw.ustring.len(example) > 50 then
table.insert(text, "<dl><dd>''" .. translit .. "''</dd><dd>" .. translation .. "</dd></dl>")
else
table.insert(text, " ― ''" .. translit .. "'' ― " .. translation)
end
return table.concat(text)
end
return export