模組:Swadesh

local export = {}

local m_links = require("Module:links")
local m_scripts = require("Module:scripts")
local m_ipa = require("Module:IPA")

local gsub = mw.ustring.gsub

local English = {"[[我]]", "[[你]]", "[[他]]", "[[我們]]", "[[你們]]", "[[他們]]", "[[這]]", "[[那]]", "[[這裡]]", "[[那裡]]", "[[誰]]", "[[什麼]]", "[[哪裡]]", "[[何時]]", "[[如何]]", "[[不]]", "[[所有]]", "[[多]]", "[[一些]]", "[[少]]", "[[其他]]", "[[一]]", "[[二]]", "[[三]]", "[[四]]", "[[五]]", "[[大]]", "[[長]]", "[[寬]]", "[[厚]]", "[[重]]", "[[小]]", "[[短]]", "[[窄]]", "[[薄]]", "[[女]]", "[[男]]", "[[人]]", "[[孩]]", "[[妻]]", "[[夫]]", "[[母]]", "[[父]]", "[[動物]]", "[[魚]]", "[[鳥]]", "[[狗]]", "[[虱]]", "[[蛇]]", "[[蟲]]", "[[樹]]", "[[林]]", "[[棍]]", "[[果]]", "[[種]]", "[[葉]]", "[[根]]", "[[樹皮]]", "[[花]]", "[[草]]", "[[繩]]", "[[膚]]", "[[肉]]", "[[血]]", "[[骨]]", "[[脂]]", "[[蛋]]", "[[角]]", "[[尾]]", "[[羽]]", "[[髮]]", "[[頭]]", "[[耳]]", "[[眼]]", "[[鼻]]", "[[口]]", "[[牙]]", "[[舌]]", "[[指甲]]", "[[腳]]", "[[腿]]", "[[膝]]", "[[手]]", "[[翅]]", "[[腹]]", "[[腸]]", "[[頸]]", "[[背]]", "[[乳]]", "[[心]]", "[[肝]]", "[[喝]]", "[[吃]]", "[[咬]]", "[[吸]]", "[[吐]]", "[[嘔]]", "[[吹]]", "[[呼吸]]", "[[笑]]", "[[看]]", "[[聽]]", "[[知]]", "[[想]]", "[[嗅]]", "[[怕]]", "[[睡]]", "[[活]]", "[[死]]", "[[殺]]", "[[鬥]]", "[[獵]]", "[[擊]]", "[[切]]", "[[分]]", "[[刺]]", "[[撓]]", "[[挖]]", "[[游]]", "[[飛]]", "[[走]]", "[[來]]", "[[躺]]", "[[坐]]", "[[站]]", "[[轉]]", "[[落]]", "[[給]]", "[[拿]]", "[[擠]]", "[[磨]]", "[[洗]]", "[[擦]]", "[[拉]]", "[[推]]", "[[扔]]", "[[系]]", "[[縫]]", "[[計]]", "[[說]]", "[[唱]]", "[[玩]]", "[[浮]]", "[[流]]", "[[凍]]", "[[腫]]", "[[日]]", "[[月]]", "[[星]]", "[[水]]", "[[雨]]", "[[河]]", "[[湖]]", "[[海]]", "[[鹽]]", "[[石]]", "[[沙]]", "[[塵]]", "[[地]]", "[[雲]]", "[[霧]]", "[[天]]", "[[風]]", "[[雪]]", "[[冰]]", "[[煙]]", "[[火]]", "[[灰]]", "[[燒]]", "[[路]]", "[[山]]", "[[紅]]", "[[綠]]", "[[黃]]", "[[白]]", "[[黑]]", "[[夜]]", "[[晝]]", "[[年]]", "[[暖]]", "[[冷]]", "[[滿]]", "[[新]]", "[[舊]]", "[[好]]", "[[壞]]", "[[腐]]", "[[髒]]", "[[直]]", "[[圓]]", "[[尖]]", "[[鈍]]", "[[滑]]", "[[濕]]", "[[乾]]", "[[對]]", "[[近]]", "[[遠]]", "[[右]]", "[[左]]", "[[在]]", "[[裡]]", "[[與]]", "[[和]]", "[[若]]", "[[因]]", "[[名]]"}
--array - list of 207 objects in this form {gloss=term}
local data = {}

function export.python_dictionary(frame)
	local args = frame:getParent().args
	local dataurl = args['lang']
	if args['var'] then dataurl = dataurl .. '/' .. args['var'] end
	local data = require("Module:Swadesh/data/" .. dataurl)
	local lang = require("Module:languages").getByCode(args['lang'])
	local res = "{'name': '" .. lang:getCanonicalName()
	if data['header'] ~= nil then res = res .. ' (' .. data['header'] .. ')' end
	res = res .. "',"
	
	for word = 1, #English do
		res = res .. "'" .. word .. "': ["
		if data[word] ~= nil then
			for _, termdata in ipairs(data[word]) do
				local best = m_scripts.findBestScript(termdata.alt or termdata.term, lang)
				local translit = lang:transliterate((termdata.alt or termdata.term), best)
				res = res .. '{'
				if termdata.term then res = res .. "'term': '" .. gsub(termdata.term, "'", "'") .. "', " end
				if termdata.alt then res = res .. "'alt': '" .. gsub(termdata.alt, "'", "'") .. "', " end
				if termdata.tr or translit then res = res .. "'translit': '" ..  gsub((termdata.tr or translit), "'", "'") .. "', " end
				if termdata.ts then res = res .. "'transcript': '" ..  gsub(termdata.ts, "'", "'") .. "', " end
				if termdata.ipa then res = res .. "'ipa': '" ..  gsub(termdata.ipa, "'", "'") .. "', " end
				if termdata.nolink then res = res .. "'nolink': '" .. gsub(termdata.nolink, "'", "'") .. "', " end
				if termdata.notes then res = res .. "'notes': '" ..  gsub(termdata.notes, "'", "'") .. "', " end
				res = res .. '},'
			end
		end
		res = res .. "],"
	end
	res = res .. "}"
	
	return "<pre>" .. mw.text.nowiki(res) .. "</pre>"
end


function export.show(frame)
	local args = frame:getParent().args
	local data = {}
	local langs = {}
	
	local res = mw.html.create("table"):addClass("wikitable sortable")
	
	local headers = res:tag("tr")
	for _, text in ipairs { "序列", "漢語" } do
		headers:tag("th"):node(text)
	end
	
	for i, arg in ipairs(args) do
		local lang = arg
		local header = arg
		langs[i] = require("Module:languages").getByCode(lang)
		if args["var" .. i] ~= nil then
			arg = arg .. '/' .. args["var" .. i]
		end
		data[i] = require("Module:Swadesh/data/" .. arg)
		local header = langs[i]:getCanonicalName()
		if data[i]['header'] ~= nil and args['translit'] == nil then
			header = header .. ' (' .. data[i]['header'] .. ')'
		end
		if data[i]['nativename'] ~= nil then
			header = header .. '<br><small>' .. m_links.full_link({lang = langs[i], alt = data[i]['nativename']}) .. "</small>"
		end
		local count = 0
		for k, v in pairs(data[i]) do
    		if (type(k) == 'number') then count = count + 1 end
    	end
		header = header .. "<br><small><sup>[[Module:Swadesh/data/" .. arg .. "|edit (" .. count .. ")]]</sup></small>"
		headers:tag("th"):node(header)
	end
	
	if args[2] == nil and args['translit'] == nil then
		headers:tag("th"):node("IPA")
	end
	
	for word = 1, #English do
		local row = mw.html.create("tr")
		row:tag("td"):node(word)
		row:tag("td"):node(English[word])
		
		for lang, arg in ipairs(args) do
			local res = ""
			local count = 0
			if data[lang][word] then
				for _, termdata in ipairs(data[lang][word]) do
					if count ~= 0 then res = res .. ", " end
					if args["translit"] then
						res = res .. '<span class="swadesh-translit">'
						local best = m_scripts.findBestScript(termdata.alt or termdata.term, langs[lang])
						local translit = langs[lang]:transliterate((termdata.alt or termdata.term), best)
						if termdata.nolink == nil and termdata.term ~= nil then
							res = res .. m_links.language_link({lang = langs[lang], term = termdata.term or '?',
								alt = termdata.ts or termdata.tr or translit or termdata.term or (termdata.ipa and '<span class="IPA">' .. termdata.ipa .. '</span>') or '?'})
						else
							res = res .. (termdata.ts or termdata.tr or translit or termdata.term or (termdata.ipa and '<span class="IPA">' .. termdata.ipa .. '</span>') or '?')
						end
					else
						res = res .. '<span class="swadesh-term">'
						if termdata.nolink == nil then
							res = res .. m_links.full_link({lang = langs[lang], term = termdata.term, alt = termdata.alt, tr = termdata.tr, ts = termdata.ts})
						else
							res = res .. termdata.term
						end
					end
					if termdata.notes then
						if args[2] == nil then res = res .. " (''<span class=\"swadesh-note\">" .. termdata.notes .. "</span>'')"
						else res = res .. '<abbr title = "' .. termdata.notes .. '>*</abbr>' end
					end
					res = res .. '</span>'
					count = count + 1
				end
			end
			row:tag("td"):node(res)
		end
		
		if args[2] == nil and args['translit'] == nil then
			local ipas = ""
			local count = 0
			if data[1][word] then
				for _, termdata in ipairs(data[1][word]) do
					if count ~= 0 then
						ipas = ipas .. ", "
					end
					if termdata.ipa then
						ipas = ipas .. '<span class="IPA">' .. termdata.ipa .. '</span>'
						count = count + 1
					end
				end
			end
			row:tag("td"):node(ipas)
		end
		res:node(row)
	end
	
	
	return res;
end

return export