Module:compound: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 14: Line 14:
local debug_force_cat = false -- if set to true, always display categories even on userspace pages
local debug_force_cat = false -- if set to true, always display categories even on userspace pages


local default_pos = "term"


-- ABOUT TEMPLATE AND DISPLAY HYPHENS:
-- ABOUT TEMPLATE AND DISPLAY HYPHENS:
Line 146: Line 148:
["Laoo"] = no_display_hyphen,
["Laoo"] = no_display_hyphen,
["Nshu"] = no_display_hyphen,
["Nshu"] = no_display_hyphen,
["Thaa"] = no_display_hyphen,
["Thai"] = no_display_hyphen,
["Thai"] = no_display_hyphen,
}
}
Line 227: Line 230:
end
end


local function link_term(terminfo, display_term, lang, sc, sort_key, force_cat)
function export.link_term(terminfo, display_term, lang, sc, sort_key, force_cat, nocat)
local terminfo_new = require("Module:table").shallowcopy(terminfo)
local terminfo_new = require("Module:table").shallowcopy(terminfo)
local result
local result


terminfo_new.term = display_term
terminfo_new.term = display_term
terminfo_new.sc = terminfo_new.sc or sc
if terminfo_new.lang then
if terminfo_new.lang then
result = require("Module:etymology").format_derived(lang, terminfo_new, sort_key, nil,
result = require("Module:etymology").format_derived(lang, terminfo_new, sort_key, nocat,
force_cat or debug_force_cat)
force_cat or debug_force_cat)
else
else
terminfo_new.lang = lang
terminfo_new.lang = lang
terminfo_new.sc = terminfo_new.sc or sc
result = m_links.full_link(terminfo_new, "term_i", false)
result = m_links.full_link(terminfo_new, "term_i", false)
end
end
Line 359: Line 362:
return affix_type, part
return affix_type, part
end
end
export.get_affix_type = get_affix_type




Line 382: Line 386:
-- specifying the sort key and sort base to use when formatting the category. If NOCAT is given, no
-- specifying the sort key and sort base to use when formatting the category. If NOCAT is given, no
-- categories are added.
-- categories are added.
local function concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
function export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
local cattext
local cattext
if nocat then
if nocat then
Line 437: Line 441:


function export.show_affixes(lang, sc, parts, pos, sort_key, typ, nocap, notext, nocat, lit, force_cat)
function export.show_affixes(lang, sc, parts, pos, sort_key, typ, nocap, notext, nocat, lit, force_cat)
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 462: Line 466:
table.insert(parts_formatted, "")
table.insert(parts_formatted, "")
else
else
table.insert(parts_formatted, link_term(part, display_term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(part, display_term, lang, sc, sort_key, force_cat, nocat))
end
end


Line 484: Line 488:
end
end


if pos ~= "words" and part.pos and rfind(part.pos, "diminutive") then
if pos ~= "terms" and part.pos and rfind(part.pos, "diminutive") then
table.insert(categories, {cat="diminutive " .. pos, sort_key=part_sort, sort_base=part_sort_base})
table.insert(categories, {cat="diminutive " .. pos, sort_key=part_sort, sort_base=part_sort_base})
end
end
Line 498: Line 502:
end
end


-- If there are no categories, then there were no actual affixes, only a single regular word.
-- If there are no categories, then there were no actual affixes, only a single regular term.
if #categories == 0 then
if #categories == 0 then
error("The parameters did not include any affixes, and the word is not a compound. Please provide at least one affix.")
error("The parameters did not include any affixes, and the term is not a compound. Please provide at least one affix.")
end
end


table.insert(text_sections, concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat))
table.insert(text_sections, export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat))
return table.concat(text_sections)
return table.concat(text_sections)
end
end
Line 509: Line 513:


function export.show_compound(lang, sc, parts, pos, sort_key, typ, nocap, notext, nocat, lit, force_cat)
function export.show_compound(lang, sc, parts, pos, sort_key, typ, nocap, notext, nocat, lit, force_cat)
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 525: Line 529:
local affix_type, display_term = get_affix_type(part_lang, part_sc, part.term)
local affix_type, display_term = get_affix_type(part_lang, part_sc, part.term)


-- If the word is an infix, recognize it as such (which means
-- If the term is an infix, recognize it as such (which means e.g. that we will display the term without hyphens for
-- e.g. that we will display the word without hyphens for
-- East Asian languages). Otherwise, ignore the fact that it looks like an affix and display as specified in the
-- East Asian languages). Otherwise, ignore the fact that it
-- template (but pay attention to the detected affix type for certain tracking purposes)
-- looks like an affix and display as specified in the template
-- (but pay attention to the detected affix type for certain
-- tracking purposes)
if affix_type == "infix" then
if affix_type == "infix" then
table.insert(categories, {cat=pos .. " interfixed with " .. make_entry_name_no_links(part_lang, display_term), sort_key=part.sort or sort_key})
table.insert(categories, {cat=pos .. " interfixed with " .. make_entry_name_no_links(part_lang, display_term), sort_key=part.sort or sort_key})
Line 539: Line 540:
end
end
end
end
table.insert(parts_formatted, link_term(part, display_term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(part, display_term, lang, sc, sort_key, force_cat, nocat))
end
end


table.insert(text_sections, concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat))
table.insert(text_sections, export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat))
return table.concat(text_sections)
return table.concat(text_sections)
end
end




function export.show_compound_like(lang, sc, parts, sort_key, text, oftext, cat, lit, force_cat)
function export.show_compound_like(lang, sc, parts, sort_key, text, oftext, cat, nocat, lit, force_cat)
local parts_formatted = {}
local parts_formatted = {}
local categories = {}
local categories = {}
Line 557: Line 558:
-- Make links out of all the parts
-- Make links out of all the parts
for i, part in ipairs(parts) do
for i, part in ipairs(parts) do
table.insert(parts_formatted, link_term(part, part.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(part, part.term, lang, sc, sort_key, force_cat, nocat))
end
end


Line 569: Line 570:
table.insert(text_sections, " ")
table.insert(text_sections, " ")
end
end
-- FIXME, should support nocat=
table.insert(text_sections, export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat))
table.insert(text_sections, concat_parts(lang, parts_formatted, categories, nil, sort_key, lit, force_cat))
return table.concat(text_sections)
return table.concat(text_sections)
end
end
Line 587: Line 587:
part.term = export.make_affix(part.term, part_lang, part_sc, affix_type)
part.term = export.make_affix(part.term, part_lang, part_sc, affix_type)
part.alt = export.make_affix(part.alt, part_lang, part_sc, affix_type)
part.alt = export.make_affix(part.alt, part_lang, part_sc, affix_type)
part.tr = export.make_affix(part.tr, part_lang, require("Module:scripts").getByCode("Latn"), affix_type)
local Latn = require("Module:scripts").getByCode("Latn")
part.ts = export.make_affix(part.ts, part_lang, require("Module:scripts").getByCode("Latn"), affix_type)
part.tr = export.make_affix(part.tr, part_lang, Latn, affix_type)
part.ts = export.make_affix(part.ts, part_lang, Latn, affix_type)
end
 
 
local function track_wrong_affix_type(template, part, lang, sc, expected_affix_type, part_name)
if part then
local affix_type = get_affix_type(part.lang or lang, part.sc or sc, part.term)
end
end
end


Line 607: Line 615:
function export.show_circumfix(lang, sc, prefix, base, suffix, pos, sort_key, nocat, lit, force_cat)
function export.show_circumfix(lang, sc, prefix, base, suffix, pos, sort_key, nocat, lit, force_cat)
local categories = {}
local categories = {}
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 614: Line 622:
make_part_affix(prefix, lang, sc, "prefix")
make_part_affix(prefix, lang, sc, "prefix")
make_part_affix(suffix, lang, sc, "suffix")
make_part_affix(suffix, lang, sc, "suffix")
track_wrong_affix_type("circumfix", prefix, lang, sc, "prefix", "prefix")
track_wrong_affix_type("circumfix", base, lang, sc, nil, "base")
track_wrong_affix_type("circumfix", suffix, lang, sc, "suffix", "suffix")


-- Create circumfix term
-- Create circumfix term
Line 633: Line 645:
end
end


table.insert(parts_formatted, link_term(prefix, prefix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(prefix, prefix.term, lang, sc, sort_key, force_cat, nocat))
table.insert(parts_formatted, link_term(base, base.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(base, base.term, lang, sc, sort_key, force_cat, nocat))
table.insert(parts_formatted, link_term(suffix, suffix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(suffix, suffix.term, lang, sc, sort_key, force_cat, nocat))


-- Insert the categories
-- Insert the categories
table.insert(categories, {cat=pos .. " circumfixed with " .. make_entry_name_no_links(prefix.lang or lang, circumfix), sort_key=sort_key, sort_base=sort_base})
table.insert(categories, {cat=pos .. " circumfixed with " .. make_entry_name_no_links(prefix.lang or lang, circumfix), sort_key=sort_key, sort_base=sort_base})


return concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
return export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
end
end




function export.show_confix(lang, sc, prefix, base, suffix, pos, sort_key, nocat, lit, force_cat)
function export.show_confix(lang, sc, prefix, base, suffix, pos, sort_key, nocat, lit, force_cat)
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 652: Line 664:
make_part_affix(prefix, lang, sc, "prefix")
make_part_affix(prefix, lang, sc, "prefix")
make_part_affix(suffix, lang, sc, "suffix")
make_part_affix(suffix, lang, sc, "suffix")
track_wrong_affix_type("confix", prefix, lang, sc, "prefix", "prefix")
track_wrong_affix_type("confix", base, lang, sc, nil, "base")
track_wrong_affix_type("confix", suffix, lang, sc, "suffix", "suffix")


-- Make links out of all the parts
-- Make links out of all the parts
Line 663: Line 679:
local categories = {}
local categories = {}


table.insert(parts_formatted, link_term(prefix, prefix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(prefix, prefix.term, lang, sc, sort_key, force_cat, nocat))
insert_affix_category(categories, lang, pos, "prefix", prefix, sort_key, prefix_sort_base)
insert_affix_category(categories, lang, pos, "prefix", prefix, sort_key, prefix_sort_base)


if base then
if base then
table.insert(parts_formatted, link_term(base, base.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(base, base.term, lang, sc, sort_key, force_cat, nocat))
end
end


table.insert(parts_formatted, link_term(suffix, suffix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(suffix, suffix.term, lang, sc, sort_key, force_cat, nocat))
insert_affix_category(categories, lang, pos, "suffix", suffix)
insert_affix_category(categories, lang, pos, "suffix", suffix)


return concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
return export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
end
end


Line 679: Line 695:
function export.show_infix(lang, sc, base, infix, pos, sort_key, nocat, lit, force_cat)
function export.show_infix(lang, sc, base, infix, pos, sort_key, nocat, lit, force_cat)
local categories = {}
local categories = {}
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 685: Line 701:
-- Hyphenate the affixes
-- Hyphenate the affixes
make_part_affix(infix, lang, sc, "infix")
make_part_affix(infix, lang, sc, "infix")
track_wrong_affix_type("infix", base, lang, sc, nil, "base")
track_wrong_affix_type("infix", infix, lang, sc, "infix", "infix")


-- Make links out of all the parts
-- Make links out of all the parts
local parts_formatted = {}
local parts_formatted = {}


table.insert(parts_formatted, link_term(base, base.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(base, base.term, lang, sc, sort_key, force_cat, nocat))
table.insert(parts_formatted, link_term(infix, infix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(infix, infix.term, lang, sc, sort_key, force_cat, nocat))


-- Insert the categories
-- Insert the categories
insert_affix_category(categories, lang, pos, "infix", infix)
insert_affix_category(categories, lang, pos, "infix", infix)


return concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
return export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
end
end




function export.show_prefixes(lang, sc, prefixes, base, pos, sort_key, nocat, lit, force_cat)
function export.show_prefixes(lang, sc, prefixes, base, pos, sort_key, nocat, lit, force_cat)
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 708: Line 727:
make_part_affix(prefix, lang, sc, "prefix")
make_part_affix(prefix, lang, sc, "prefix")
end
end
for i, prefix in ipairs(prefixes) do
track_wrong_affix_type("prefix", prefix, lang, sc, "prefix", "prefix")
end
track_wrong_affix_type("prefix", base, lang, sc, nil, "base")


-- Make links out of all the parts
-- Make links out of all the parts
Line 721: Line 746:


for i, prefix in ipairs(prefixes) do
for i, prefix in ipairs(prefixes) do
table.insert(parts_formatted, link_term(prefix, prefix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(prefix, prefix.term, lang, sc, sort_key, force_cat, nocat))
insert_affix_category(categories, lang, pos, "prefix", prefix, sort_key, i == 1 and first_sort_base or nil)
insert_affix_category(categories, lang, pos, "prefix", prefix, sort_key, i == 1 and first_sort_base or nil)
end
end


if base then
if base then
table.insert(parts_formatted, link_term(base, base.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(base, base.term, lang, sc, sort_key, force_cat, nocat))
else
else
table.insert(parts_formatted, "")
table.insert(parts_formatted, "")
end
end


return concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
return export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
end
end


Line 737: Line 762:
function export.show_suffixes(lang, sc, base, suffixes, pos, sort_key, nocat, lit, force_cat)
function export.show_suffixes(lang, sc, base, suffixes, pos, sort_key, nocat, lit, force_cat)
local categories = {}
local categories = {}
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 744: Line 769:
for i, suffix in ipairs(suffixes) do
for i, suffix in ipairs(suffixes) do
make_part_affix(suffix, lang, sc, "suffix")
make_part_affix(suffix, lang, sc, "suffix")
end
track_wrong_affix_type("suffix", base, lang, sc, nil, "base")
for i, suffix in ipairs(suffixes) do
track_wrong_affix_type("suffix", suffix, lang, sc, "suffix", "suffix")
end
end


Line 750: Line 781:


if base then
if base then
table.insert(parts_formatted, link_term(base, base.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(base, base.term, lang, sc, sort_key, force_cat, nocat))
else
else
table.insert(parts_formatted, "")
table.insert(parts_formatted, "")
Line 756: Line 787:


for i, suffix in ipairs(suffixes) do
for i, suffix in ipairs(suffixes) do
table.insert(parts_formatted, link_term(suffix, suffix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(suffix, suffix.term, lang, sc, sort_key, force_cat, nocat))
end
end


Line 770: Line 801:
end
end


return concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
return export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
end
end


Line 776: Line 807:
function export.show_transfix(lang, sc, base, transfix, pos, sort_key, nocat, lit, force_cat)
function export.show_transfix(lang, sc, base, transfix, pos, sort_key, nocat, lit, force_cat)
local categories = {}
local categories = {}
pos = pos or "word"
pos = pos or default_pos


pos = pluralize(pos)
pos = pluralize(pos)
Line 786: Line 817:
local parts_formatted = {}
local parts_formatted = {}


table.insert(parts_formatted, link_term(base, base.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(base, base.term, lang, sc, sort_key, force_cat, nocat))
table.insert(parts_formatted, link_term(transfix, transfix.term, lang, sc, sort_key, force_cat))
table.insert(parts_formatted, export.link_term(transfix, transfix.term, lang, sc, sort_key, force_cat, nocat))


-- Insert the categories
-- Insert the categories
insert_affix_category(categories, lang, pos, "transfix", transfix)
insert_affix_category(categories, lang, pos, "transfix", transfix)


return concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
return export.concat_parts(lang, parts_formatted, categories, nocat, sort_key, lit, force_cat)
end
end




-- Add a hyphen to a word in the appropriate place, based on the specified
-- Add a hyphen to a term in the appropriate place, based on the specified affix type. For example, if `affix_type` ==
-- affix type. For example, if `affix_type` == "prefix", we'll add a hyphen
-- "prefix", we'll add a hyphen onto the end if it's not already there. In general, if the template and display hyphens
-- onto the end if it's not already there. In general, if the template and
-- are the same and the appropriate hyphen is already present, we leave it, else we strip off the template hyphen if
-- display hyphens are the same and the appropriate hyphen is already
-- present and add the display hyphen.
-- present, we leave it, else we strip off the template hyphen if present
-- and add the display hyphen.
function export.make_affix(term, lang, sc, affix_type)
function export.make_affix(term, lang, sc, affix_type)
if not (affix_type == "prefix" or affix_type == "suffix" or
if not (affix_type == "prefix" or affix_type == "suffix" or