localm_links=require("Module:links")localm_utilities=require("Module:utilities")localexport={}-- FIXME: should be script-based-- But we can't do that unless we do script detection before linking.localhyphens={["ar"]="ـ",["fa"]="ـ",["he"]="־",["ja"]="",["ko"]="",["yi"]="־",["zh"]="",}localfunctionpluralize(pos)ifpos~="nouns"andmw.ustring.sub(pos,-5)~="verbs"andmw.ustring.sub(pos,-4)~="ives"thenifpos:find("[sx]$")thenpos=pos.."es"elsepos=pos.."s"endendreturnposendlocalfunctionlink_term(terminfo,lang,sc,sort_key)localterminfo_new={}forkey,valinpairs(terminfo)doterminfo_new[key]=valendifterminfo_new.langthenreturnrequire("Module:etymology").format_borrowed(lang,terminfo,sort_key,false,true,"plain")elseterminfo_new.lang=langterminfo_new.sc=terminfo_new.scorscreturnm_links.full_link(terminfo_new,"term",false)endendlocalfunctionget_hyphen(lang,sc)--The script will be "Latn" for transliterations.ifscandsc:getCode()=="Latn"thenreturn"-"elsereturnhyphens[lang:getCode()]or"-"endendlocalfunctionget_affix_type(lang,sc,part)ifnotpartthenreturnnilendlocalhyphen=get_hyphen(lang,sc)localhyphen_space_hyphen=hyphen.." "..hyphenpart=part:gsub("^*","")ifmw.ustring.find(part,hyphen_space_hyphen,1,true)thenreturn"circumfix"elseifmw.ustring.sub(part,1,1)==hyphenandmw.ustring.sub(part,-1)==hyphenthenreturn"infix"elseifmw.ustring.sub(part,-1)==hyphenthenreturn"prefix"elseifmw.ustring.sub(part,1,1)==hyphenthenreturn"suffix"elsereturnnilendendfunctionexport.show_affixes(lang,sc,parts,pos,sort_key,nocat)pos=posor"word"pos=pluralize(pos)-- Process each partlocalparts_formatted={}localcategories_formatted={}localwhole_words=0fori,partinipairs(parts)do-- Make a link for the parttable.insert(parts_formatted,link_term(part,lang,sc,sort_key))-- Is it an affix, and if so, what type of affix?localaffix_type=get_affix_type(part.langorlang,part.scorsc,part.term)ifaffix_typethen-- Make a sort key-- For the first part, use the second part as the sort keylocalpart_sort_base=nillocalpart_sort=part.sortorsort_keyifi==1andparts[2]andparts[2].termthenpart_sort_base=lang:makeEntryName(parts[2].term)endifaffix_type=="infix"thenaffix_type="interfix"endifpart.posandmw.ustring.match(part.pos,"patronym")thentable.insert(categories_formatted,m_utilities.format_categories({lang:getCanonicalName().." patronymics"},lang,part_sort,part_sort_base))endifpos~="words"andpart.posandmw.ustring.match(part.pos,"diminutive")thentable.insert(categories_formatted,m_utilities.format_categories({lang:getCanonicalName().." diminutive "..pos},lang,part_sort,part_sort_base))endtable.insert(categories_formatted,m_utilities.format_categories({lang:getCanonicalName().." "..pos.." "..affix_type.."ed with "..lang:makeEntryName(part.term)..(part.idand" ("..part.id..")"or"")},lang,part_sort,part_sort_base))elsewhole_words=whole_words+1ifwhole_words==2thentable.insert(categories_formatted,m_utilities.format_categories({lang:getCanonicalName().." compound "..pos},lang,sort_key))endendend-- If there are no categories, then there were no actual affixes, only regular words.-- This function does not support compounds (yet?), so show an error.if#categories_formatted==0thenerror("The parameters did not include any affixes, and the word is not a compound. Please provide at least one affix.")endreturntable.concat(parts_formatted," +&lrm; ")..(nocatand""ortable.concat(categories_formatted))endfunctionexport.show_compound(lang,sc,parts,pos,sort_key,nocat)pos=posor"words"localparts_formatted={}localcategories_formatted={}table.insert(categories_formatted,m_utilities.format_categories({lang:getCanonicalName().." compound words"},lang,sort_key))-- Make links out of all the partslocalwhole_words=0fori,partinipairs(parts)dotable.insert(parts_formatted,link_term(part,lang,sc,sort_key))localaffix_type=get_affix_type(part.langorlang,part.scorsc,part.term)ifaffix_type=="infix"thentable.insert(categories_formatted,m_utilities.format_categories({lang:getCanonicalName().." "..pos.." interfixed with "..lang:makeEntryName(part.term)},lang,part.sortorsort_key))elseifaffix_typethenrequire("Module:debug").track{"compound","compound/"..affix_type,"compound/"..affix_type.."/lang/"..lang:getCode()}elsewhole_words=whole_words+1endendifwhole_words==1thenrequire("Module:debug").track("compound/one whole word")elseifwhole_words==0thenrequire("Module:debug").track("compound/looks like confix")endreturntable.concat(parts_formatted," +&lrm; ")..(nocatand""ortable.concat(categories_formatted))endfunctionexport.show_circumfix(lang,sc,prefix,base,suffix,pos,sort_key,nocat)localcategories={}pos=posor"word"pos=pluralize(pos)-- Hyphenate the affixesprefix.term=export.make_affix(prefix.term,prefix.langorlang,prefix.scorsc,"prefix")prefix.alt=export.make_affix(prefix.alt,prefix.langorlang,prefix.scorsc,"prefix")ifprefix.annotationsthenprefix.annotations.tr=export.make_affix(prefix.annotations.tr,prefix.langorlang,require("Module:scripts").getByCode("Latn"),"prefix")prefix.annotations.ts=export.make_affix(prefix.annotations.ts,prefix.langorlang,require("Module:scripts").getByCode("Latn"),"prefix")endsuffix.term=export.make_affix(suffix.term,suffix.langorlang,suffix.scorsc,"suffix")suffix.alt=export.make_affix(suffix.alt,suffix.langorlang,suffix.scorsc,"suffix")ifsuffix.annotationsthensuffix.annotations.tr=export.make_affix(suffix.annotations.tr,suffix.langorlang,require("Module:scripts").getByCode("Latn"),"suffix")suffix.annotations.ts=export.make_affix(suffix.annotations.ts,suffix.langorlang,require("Module:scripts").getByCode("Latn"),"suffix")endlocalprefix_affix_type=get_affix_type(prefix.langorlang,prefix.scorsc,prefix.term)ifprefix_affix_type~="prefix"thenrequire("Module:debug").track{"circumfix","circumfix/prefix","circumfix/prefix/"..(prefix_affix_typeor"none"),"circumfix/prefix/"..(prefix_affix_typeor"none").."/lang/"..lang:getCode()}endlocalbase_affix_type=get_affix_type(base.langorlang,base.scorsc,base.term)ifbase_affix_typethenrequire("Module:debug").track{"circumfix","circumfix/base","circumfix/base/"..base_affix_type,"circumfix/base/"..base_affix_type.."/lang/"..lang:getCode()}endlocalsuffix_affix_type=get_affix_type(suffix.langorlang,suffix.scorsc,suffix.term)ifsuffix_affix_type~="suffix"thenrequire("Module:debug").track{"circumfix","circumfix/suffix","circumfix/suffix/"..(suffix_affix_typeor"none"),"circumfix/suffix/"..(suffix_affix_typeor"none").."/lang/"..lang:getCode()}end-- Create circumfix termlocalcircumfix=nilifprefix.termandsuffix.termthencircumfix=prefix.term.." "..suffix.termprefix.alt=prefix.altorprefix.termsuffix.alt=suffix.altorsuffix.termprefix.term=circumfixsuffix.term=circumfixend-- Make links out of all the partslocalparts_formatted={}localsort_base=lang:makeEntryName(base.term)table.insert(parts_formatted,link_term(prefix,lang,sc,sort_key))table.insert(parts_formatted,link_term(base,lang,sc,sort_key))table.insert(parts_formatted,link_term(suffix,lang,sc,sort_key))-- Insert the categoriestable.insert(categories,lang:getCanonicalName().." "..pos.." circumfixed with "..lang:makeEntryName(circumfix,lang))returntable.concat(parts_formatted," +&lrm; ")..(nocatand""orm_utilities.format_categories(categories,lang,sort_key,sort_base))endfunctionexport.show_confix(lang,sc,prefix,base,suffix,pos,sort_key,nocat)pos=posor"word"pos=pluralize(pos)-- Hyphenate the affixesprefix.term=export.make_affix(prefix.term,prefix.langorlang,prefix.scorsc,"prefix")prefix.alt=export.make_affix(prefix.alt,prefix.langorlang,prefix.scorsc,"prefix")ifprefix.annotationsthenprefix.annotations.tr=export.make_affix(prefix.annotations.tr,prefix.langorlang,require("Module:scripts").getByCode("Latn"),"prefix")prefix.annotations.ts=export.make_affix(prefix.annotations.ts,prefix.langorlang,require("Module:scripts").getByCode("Latn"),"prefix")endsuffix.term=export.make_affix(suffix.term,suffix.langorlang,suffix.scorsc,"suffix")suffix.alt=export.make_affix(suffix.alt,suffix.langorlang,suffix.scorsc,"suffix")ifsuffix.annotationsthensuffix.annotations.tr=export.make_affix(suffix.annotations.tr,suffix.langorlang,require("Module:scripts").getByCode("Latn"),"suffix")suffix.annotations.ts=export.make_affix(suffix.annotations.ts,suffix.langorlang,require("Module:scripts").getByCode("Latn"),"suffix")endlocalprefix_affix_type=get_affix_type(prefix.langorlang,prefix.scorsc,prefix.term)ifprefix_affix_type~="prefix"thenrequire("Module:debug").track{"confix","confix/prefix","confix/prefix/"..(prefix_affix_typeor"none"),"confix/prefix/"..(prefix_affix_typeor"none").."/lang/"..lang:getCode()}endifbasethenlocalbase_affix_type=get_affix_type(base.langorlang,base.scorsc,base.term)ifbase_affix_typethenrequire("Module:debug").track{"confix","confix/base","confix/base/"..base_affix_type,"confix/base/"..base_affix_type.."/lang/"..lang:getCode()}endendlocalsuffix_affix_type=get_affix_type(suffix.langorlang,suffix.scorsc,suffix.term)ifsuffix_affix_type~="suffix"thenrequire("Module:debug").track{"confix","confix/suffix","confix/suffix/"..(suffix_affix_typeor"none"),"confix/suffix/"..(suffix_affix_typeor"none").."/lang/"..lang:getCode()}end-- Make links out of all the partslocalparts_formatted={}localprefix_sort_base=lang:makeEntryName(suffix.term)localprefix_categories={}localsuffix_categories={}table.insert(parts_formatted,link_term(prefix,lang,sc,sort_key))table.insert(prefix_categories,lang:getCanonicalName().." "..pos.." prefixed with "..lang:makeEntryName(prefix.term,lang))ifbasethenprefix_sort_base=lang:makeEntryName(base.term)table.insert(parts_formatted,link_term(base,lang,sc,sort_key))endtable.insert(parts_formatted,link_term(suffix,lang,sc,sort_key))table.insert(suffix_categories,lang:getCanonicalName().." "..pos.." suffixed with "..lang:makeEntryName(suffix.term,lang))returntable.concat(parts_formatted," +&lrm; ")..(nocatand""orm_utilities.format_categories(prefix_categories,lang,sort_key,prefix_sort_base)..m_utilities.format_categories(suffix_categories,lang,sort_key))endfunctionexport.show_infix(lang,sc,base,infix,pos,sort_key,nocat)localcategories={}pos=posor"word"pos=pluralize(pos)-- Hyphenate the affixesinfix.term=export.make_affix(infix.term,infix.langorlang,infix.scorsc,"infix")infix.alt=export.make_affix(infix.alt,infix.langorlang,infix.scorsc,"infix")ifinfix.annotationstheninfix.annotations.tr=export.make_affix(infix.annotations.tr,infix.langorlang,require("Module:scripts").getByCode("Latn"),"infix")infix.annotations.ts=export.make_affix(infix.annotations.ts,infix.langorlang,require("Module:scripts").getByCode("Latn"),"infix")endlocalbase_affix_type=get_affix_type(base.langorlang,base.scorsc,base.term)ifbase_affix_typethenrequire("Module:debug").track{"infix","infix/base"}require("Module:debug").track{"infix/base/"..base_affix_type,"infix/base/"..base_affix_type.."/lang/"..lang:getCode()}endlocalinfix_affix_type=get_affix_type(infix.langorlang,infix.scorsc,infix.term)ifinfix_affix_type~="infix"thenrequire("Module:debug").track{"infix","infix/infix","infix/infix/"..(infix_affix_typeor"none"),"infix/infix/"..(infix_affix_typeor"none").."/lang/"..lang:getCode()}end-- Make links out of all the partslocalparts_formatted={}table.insert(parts_formatted,link_term(base,lang,sc,sort_key))table.insert(parts_formatted,link_term(infix,lang,sc,sort_key))-- Insert the categoriestable.insert(categories,lang:getCanonicalName().." "..pos.." infixed with "..lang:makeEntryName(infix.term))returntable.concat(parts_formatted," +&lrm; ")..(nocatand""orm_utilities.format_categories(categories,lang,sort_key))endfunctionexport.show_prefixes(lang,sc,prefixes,base,pos,sort_key,nocat)pos=posor"word"pos=pluralize(pos)-- Hyphenate the affixesfori,prefixinipairs(prefixes)doprefixes[i].term=export.make_affix(prefix.term,prefix.langorlang,prefix.scorsc,"prefix")prefixes[i].alt=export.make_affix(prefix.alt,prefix.langorlang,prefix.scorsc,"prefix")ifprefix.annotationsthenprefixes[i].annotations.tr=export.make_affix(prefix.annotations.tr,prefix.langorlang,require("Module:scripts").getByCode("Latn"),"prefix")prefixes[i].annotations.ts=export.make_affix(prefix.annotations.ts,prefix.langorlang,require("Module:scripts").getByCode("Latn"),"prefix")endendfori,prefixinipairs(prefixes)dolocalprefix_affix_type=get_affix_type(prefix.langorlang,prefix.scorsc,prefix.term)ifprefix_affix_type~="prefix"thenrequire("Module:debug").track{"prefix","prefix/prefix","prefix/prefix/"..(prefix_affix_typeor"none"),"prefix/prefix/"..(prefix_affix_typeor"none").."/lang/"..lang:getCode()}endendifbasethenlocalbase_affix_type=get_affix_type(base.langorlang,base.scorsc,base.term)ifbase_affix_typethenrequire("Module:debug").track{"prefix","prefix/base","prefix/base/"..base_affix_type,"prefix/base/"..base_affix_type.."/lang/"..lang:getCode()}endend-- Make links out of all the partslocalparts_formatted={}localfirst_sort_base=nillocalcategories={}fori,prefixinipairs(prefixes)dotable.insert(parts_formatted,link_term(prefix,lang,sc,sort_key))table.insert(categories,lang:getCanonicalName().." "..pos.." prefixed with "..lang:makeEntryName(prefix.term,lang)..(prefix.idand" ("..prefix.id..")"or""))ifi>1andfirst_sort_base==nilthenfirst_sort_base=lang:makeEntryName(prefix.term)endendifbasetheniffirst_sort_base==nilthenfirst_sort_base=lang:makeEntryName(base.term)endtable.insert(parts_formatted,link_term(base,lang,sc,sort_key))elsetable.insert(parts_formatted,"")endlocalfirst_category=table.remove(categories,1)returntable.concat(parts_formatted," +&lrm; ")..(nocatand""orm_utilities.format_categories({first_category},lang,sort_key,first_sort_base)..m_utilities.format_categories(categories,lang,sort_key))endfunctionexport.show_suffixes(lang,sc,base,suffixes,pos,sort_key,nocat)localcategories={}pos=posor"word"pos=pluralize(pos)-- Hyphenate the affixesfori,suffixinipairs(suffixes)dosuffixes[i].term=export.make_affix(suffix.term,suffix.langorlang,suffix.scorsc,"suffix")suffixes[i].alt=export.make_affix(suffix.alt,suffix.langorlang,suffix.scorsc,"suffix")ifsuffix.annotationsthensuffixes[i].annotations.tr=export.make_affix(suffix.annotations.tr,suffix.langorlang,require("Module:scripts").getByCode("Latn"),"suffix")suffixes[i].annotations.ts=export.make_affix(suffix.annotations.ts,suffix.langorlang,require("Module:scripts").getByCode("Latn"),"suffix")endendifbasethenlocalbase_affix_type=get_affix_type(base.langorlang,base.scorsc,base.term)ifbase_affix_typethenrequire("Module:debug").track{"suffix","suffix/base","suffix/base/"..base_affix_type,"suffix/base/"..base_affix_type.."/lang/"..lang:getCode()}endendfori,suffixinipairs(suffixes)dolocalsuffix_affix_type=get_affix_type(suffix.langorlang,suffix.scorsc,suffix.term)ifsuffix_affix_type~="suffix"thenrequire("Module:debug").track{"suffix","suffix/suffix","suffix/suffix/"..(suffix_affix_typeor"none"),"suffix/suffix/"..(suffix_affix_typeor"none").."/lang/"..lang:getCode()}endend-- Make links out of all the partslocalparts_formatted={}ifbasethentable.insert(parts_formatted,link_term(base,lang,sc,sort_key))elsetable.insert(parts_formatted,"")endfori,suffixinipairs(suffixes)dotable.insert(parts_formatted,link_term(suffix,lang,sc,sort_key))end-- Insert the categoriesfori,suffixinipairs(suffixes)doifsuffix.termthentable.insert(categories,lang:getCanonicalName().." "..pos.." suffixed with "..lang:makeEntryName(suffix.term)..(suffix.idand" ("..suffix.id..")"or""))endifsuffix.posandmw.ustring.match(suffix.pos,"patronym")thentable.insert(categories,lang:getCanonicalName().." patronymics")endendreturntable.concat(parts_formatted," +&lrm; ")..(nocatand""orm_utilities.format_categories(categories,lang,sort_key))endfunctionexport.show_transfix(lang,sc,base,transfix,pos,sort_key,nocat)localcategories={}pos=posor"word"pos=pluralize(pos)-- Hyphenate the affixestransfix.term=export.make_affix(transfix.term,transfix.langorlang,transfix.scorsc,"transfix")transfix.alt=export.make_affix(transfix.alt,transfix.langorlang,transfix.scorsc,"transfix")transfix.tr=export.make_affix(transfix.tr,transfix.langorlang,require("Module:scripts").getByCode("Latn"),"transfix")transfix.ts=export.make_affix(transfix.ts,transfix.langorlang,require("Module:scripts").getByCode("Latn"),"transfix")-- Make links out of all the partslocalparts_formatted={}table.insert(parts_formatted,link_term(base,lang,sc,sort_key))table.insert(parts_formatted,link_term(transfix,lang,sc,sort_key))-- Insert the categoriestable.insert(categories,lang:getCanonicalName().." "..pos.." transfixed with "..lang:makeEntryName(transfix.term))returntable.concat(parts_formatted," +&lrm; ")..(nocatand""orm_utilities.format_categories(categories,lang,sort_key))end-- Adds a hyphen to a word in the appropriate placefunctionexport.make_affix(term,lang,sc,affixtype)ifnot(affixtype=="prefix"oraffixtype=="suffix"oraffixtype=="circumfix"oraffixtype=="infix"oraffixtype=="interfix"oraffixtype=="transfix")thenerror("Invalid affix type")endifnottermthenreturnnilelseifaffixtype=="circumfix"oraffixtype=="transfix"thenreturntermelseifaffixtype=="interfix"thenaffixtype="infix"endlocaldetected_type=get_affix_type(lang,sc,term)ifaffixtypeanddetected_type==affixtypeor(detected_type=="infix"andaffixtype=="prefix")or(detected_type=="infix"andaffixtype=="suffix")thenreturntermend--remove an asterisk if the morpheme is reconstructed and add it in the endlocalreconstructed=""ifterm:find("^*")thenreconstructed="*"term=term:gsub("^*","")endlocalhyphen=get_hyphen(lang,sc)ifaffixtype=="suffix"thenterm=hyphen..termelseifaffixtype=="prefix"thenterm=term..hyphenelseifaffixtype=="infix"thenterm=hyphen..term..hyphenelseerror("Invalid affix type")endreturnreconstructed..termendreturnexport