Module:Sandbox/Smsingh34/Names

-- smsingh Lua Task 8, Names of People local p = {} p.formatname = function (frame) fullname = frame.args.fullname nameformat = frame.args.nameformat lastname = "" givenname = "" -- Dutch names dutchnames = {"van den ", "van der ", "van de ", "de la ", "van ", "de ", "ter ", "tot "} function dutchlastname(name) for i = 1, #dutchnames do			if string.match(name, dutchnames[i]) then lastnameindex = string.find(name, dutchnames[i]) lastname = string.sub(name, lastnameindex) name = string.gsub(name, dutchnames[i], "") break end end return lastname end -- Spanish names function spanishlastname(name) if string.match(name, " y ") then lastname = string.gsub(name, " y ", " ") else lastname = fullname end if string.match(string.reverse(string.match(string.reverse(lastname), "[%S]+ [%S]+")), "de ") then lastname = string.reverse(string.match(string.reverse(lastname), "[%S]+ [%S]+ [%S]+")) else lastname = string.reverse(string.match(string.reverse(lastname), "[%S]+ [%S]+")) end return lastname end -- Portugeuse names portuguesenames = {'das', 'dos', 'da', 'do', 'de'} function portugueselastname(name) for i = 1, #portuguesenames do			if string.match(name, portuguesenames[i]) then lastnameindex = string.find(name, portuguesenames[i]) lastname = string.sub(name, lastnameindex) name = string.gsub(name, portuguesenames[i], "") break end end return lastname end -- juniors, seniors, and roman numerals fullname = string.gsub(fullname, " Jr.", "") fullname = string.gsub(fullname, " Sr.", "") fullname = string.gsub(fullname, " Jr", "") fullname = string.gsub(fullname, " Sr", "") if not (string.match(fullname, " II") == nil) then local numerals = string.reverse(string.match(string.reverse(fullname), "(%a+)")) fullname = string.gsub(fullname, numerals, "") end -- most basic last name: if nameformat == "es" then spanishlastname(fullname) elseif nameformat == "zh" then if string.gsub(fullname, "(%a+)", "") == "" then lastname = "" else lastname = string.match(fullname, "(%a+)") end elseif (not (dutchlastname(fullname) == "")) then dutchlastname(fullname) elseif not (portugueselastname(fullname)=="") then portugueselastname(fullname) elseif fullname == nil or fullname == "" then return "No name given" else lastname = string.reverse(string.match(string.reverse(fullname), "[%S-]+")) end if lastname == "" then lastname = "none" end givenname = string.gsub(fullname, lastname, "") return "given name: " .. givenname .. " last name: " .. lastname end return p